Merge pull request #30 from status-im/broadcast
Add broadcast feature for DatagramTransport.
This commit is contained in:
commit
c84d847b19
|
@ -236,6 +236,13 @@ when defined(windows):
|
|||
closeSocket(localSock)
|
||||
raiseTransportOsError(err)
|
||||
|
||||
if ServerFlags.Broadcast in flags:
|
||||
if not setSockOpt(localSock, SOL_SOCKET, SO_BROADCAST, 1):
|
||||
let err = osLastError()
|
||||
if sock == asyncInvalidSocket:
|
||||
closeSocket(localSock)
|
||||
raiseTransportOsError(err)
|
||||
|
||||
## Fix for Q263823.
|
||||
var bytesRet: DWORD
|
||||
var bval = WINBOOL(0)
|
||||
|
@ -418,6 +425,13 @@ else:
|
|||
closeSocket(localSock)
|
||||
raiseTransportOsError(err)
|
||||
|
||||
if ServerFlags.Broadcast in flags:
|
||||
if not setSockOpt(localSock, SOL_SOCKET, SO_BROADCAST, 1):
|
||||
let err = osLastError()
|
||||
if sock == asyncInvalidSocket:
|
||||
closeSocket(localSock)
|
||||
raiseTransportOsError(err)
|
||||
|
||||
if local.port != Port(0):
|
||||
var saddr: Sockaddr_storage
|
||||
var slen: SockLen
|
||||
|
|
|
@ -463,6 +463,24 @@ suite "Datagram Transport test suite":
|
|||
except:
|
||||
discard
|
||||
|
||||
proc testBroadcast(): Future[int] {.async.} =
|
||||
const expectMessage = "BROADCAST MESSAGE"
|
||||
var ta1 = initTAddress("0.0.0.0:45010")
|
||||
var bta = initTAddress("255.255.255.255:45010")
|
||||
var res = 0
|
||||
proc clientMark(transp: DatagramTransport,
|
||||
raddr: TransportAddress): Future[void] {.async.} =
|
||||
var bmsg = transp.getMessage()
|
||||
var smsg = cast[string](bmsg)
|
||||
if smsg == expectMessage:
|
||||
inc(res)
|
||||
transp.close()
|
||||
var dgram1 = newDatagramTransport(clientMark, local = ta1,
|
||||
flags = {Broadcast})
|
||||
await dgram1.sendTo(bta, expectMessage)
|
||||
await wait(dgram1.join(), 5.seconds)
|
||||
result = res
|
||||
|
||||
test "close(transport) test":
|
||||
check waitFor(testTransportClose()) == true
|
||||
test m1:
|
||||
|
@ -483,3 +501,5 @@ suite "Datagram Transport test suite":
|
|||
check waitFor(test3(true)) == ClientsCount * MessagesCount
|
||||
test "Datagram connection reset test":
|
||||
check waitFor(testConnReset()) == true
|
||||
test "Broadcast test":
|
||||
check waitFor(testBroadcast()) == 1
|
||||
|
|
Loading…
Reference in New Issue