Attempt to fix #64.

This commit is contained in:
cheatfate 2020-01-28 12:47:38 +02:00
parent 74700fdcab
commit e34857364e
No known key found for this signature in database
GPG Key ID: 46ADD633A7201F95
2 changed files with 26 additions and 3 deletions

View File

@ -65,6 +65,28 @@ type
const const
DgramTransportTrackerName = "datagram.transport" DgramTransportTrackerName = "datagram.transport"
proc remoteAddress*(transp: DatagramTransport): TransportAddress =
## Returns ``transp`` remote socket address.
if transp.remote.family == AddressFamily.None:
var saddr: Sockaddr_storage
var slen = SockLen(sizeof(saddr))
if getpeername(SocketHandle(transp.fd), cast[ptr SockAddr](addr saddr),
addr slen) != 0:
raiseTransportOsError(osLastError())
fromSAddr(addr saddr, slen, transp.remote)
result = transp.remote
proc localAddress*(transp: DatagramTransport): TransportAddress =
## Returns ``transp`` local socket address.
if transp.local.family == AddressFamily.None:
var saddr: Sockaddr_storage
var slen = SockLen(sizeof(saddr))
if getsockname(SocketHandle(transp.fd), cast[ptr SockAddr](addr saddr),
addr slen) != 0:
raiseTransportOsError(osLastError())
fromSAddr(addr saddr, slen, transp.local)
result = transp.local
template setReadError(t, e: untyped) = template setReadError(t, e: untyped) =
(t).state.incl(ReadError) (t).state.incl(ReadError)
(t).error = getTransportOsError(e) (t).error = getTransportOsError(e)
@ -318,13 +340,13 @@ when defined(windows):
var saddr: Sockaddr_storage var saddr: Sockaddr_storage
var slen: SockLen var slen: SockLen
toSAddr(local, saddr, slen) toSAddr(local, saddr, slen)
if bindAddr(SocketHandle(localSock), cast[ptr SockAddr](addr saddr), if bindAddr(SocketHandle(localSock), cast[ptr SockAddr](addr saddr),
slen) != 0: slen) != 0:
let err = osLastError() let err = osLastError()
if sock == asyncInvalidSocket: if sock == asyncInvalidSocket:
closeSocket(localSock) closeSocket(localSock)
raiseTransportOsError(err) raiseTransportOsError(err)
result.local = local
else: else:
var saddr: Sockaddr_storage var saddr: Sockaddr_storage
var slen: SockLen var slen: SockLen

View File

@ -434,18 +434,19 @@ suite "Datagram Transport test suite":
result += counters[i] result += counters[i]
proc testConnReset(): Future[bool] {.async.} = proc testConnReset(): Future[bool] {.async.} =
var ta = initTAddress("127.0.0.1:65000") var ta = initTAddress("127.0.0.1:0")
var counter = 0 var counter = 0
proc clientMark(transp: DatagramTransport, proc clientMark(transp: DatagramTransport,
raddr: TransportAddress): Future[void] {.async.} = raddr: TransportAddress): Future[void] {.async.} =
counter = 1 counter = 1
transp.close() transp.close()
var dgram1 = newDatagramTransport(client1, local = ta) var dgram1 = newDatagramTransport(client1, local = ta)
var localta = dgram1.localAddress()
dgram1.close() dgram1.close()
await dgram1.join() await dgram1.join()
var dgram2 = newDatagramTransport(clientMark) var dgram2 = newDatagramTransport(clientMark)
var data = "MESSAGE" var data = "MESSAGE"
asyncCheck dgram2.sendTo(ta, data) asyncCheck dgram2.sendTo(localta, data)
await sleepAsync(2000.milliseconds) await sleepAsync(2000.milliseconds)
result = (counter == 0) result = (counter == 0)
dgram2.close() dgram2.close()