Merge pull request #30 from status-im/broadcast

Add broadcast feature for DatagramTransport.
This commit is contained in:
Eugene Kabanov 2019-03-31 12:52:16 +03:00 committed by GitHub
commit c84d847b19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 0 deletions

View File

@ -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

View File

@ -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