Commit 94ebd602 authored by Kirill Smelkov's avatar Kirill Smelkov Committed by Léo-Paul Géneau

.

parent 5053ea86
...@@ -9,7 +9,7 @@ from socket import socket, AF_INET, SOCK_DGRAM, IPPROTO_UDP, SOL_SOCKET, \ ...@@ -9,7 +9,7 @@ from socket import socket, AF_INET, SOCK_DGRAM, IPPROTO_UDP, SOL_SOCKET, \
SO_REUSEADDR, IPPROTO_IP, IP_ADD_MEMBERSHIP, IP_MULTICAST_TTL, IP_MULTICAST_LOOP, \ SO_REUSEADDR, IPPROTO_IP, IP_ADD_MEMBERSHIP, IP_MULTICAST_TTL, IP_MULTICAST_LOOP, \
inet_aton, INADDR_ANY inet_aton, INADDR_ANY
from golang import b from golang import b, u
from golang import sync, context from golang import sync, context
...@@ -17,68 +17,64 @@ group4 = '224.1.1.1' ...@@ -17,68 +17,64 @@ group4 = '224.1.1.1'
group6 = 'XXX' group6 = 'XXX'
port = 5678 port = 5678
# mjoin returns socket prepated to send/receive to/from multicast group:port. # mjoin_tx returns socket prepated to send/receive to/from multicast group:port.
def mjoin(group, port, ttl=2): def mjoin_tx(group, port, ttl=2):
# XXX autodetect ip4/ip6 based on group addr # XXX autodetect ip4/ip6 based on group addr
sk = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP) sk = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
sk.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
sk.bind((group, port))
# join group (for rx)
mreq = struct.pack("4sl", inet_aton(group), INADDR_ANY)
sk.setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP, mreq)
# set ttl and default destination address (for tx) # set ttl and default destination address (for tx)
sk.setsockopt(IPPROTO_IP, IP_MULTICAST_TTL, ttl) sk.setsockopt(IPPROTO_IP, IP_MULTICAST_TTL, ttl)
sk.connect((group, port)) sk.connect((group, port))
# receive what we send # so that what we send is received locally as well
sk.setsockopt(IPPROTO_IP, IP_MULTICAST_LOOP, 1) sk.setsockopt(IPPROTO_IP, IP_MULTICAST_LOOP, 1)
return sk return sk
""" # mjoin_rx returns socket prepated to send/receive to/from multicast group:port.
# open_tx returns function to send to group:port. def mjoin_rx(group, port):
def open_tx(group, port, ttl=2): # XXX autodetect ip4/ip6 based on group addr
sk = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP) sk = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
sk.setsockopt(IPPROTO_IP, IP_MULTICAST_TTL, ttl)
# XXX IP_MULTICAST_LOOP sk.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
def _(data): sk.bind((group, port))
return sk.sendto(data, (group, port))
return _ # join the group (for rx)
""" mreq = struct.pack("4sl", inet_aton(group), INADDR_ANY)
sk.setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP, mreq)
return sk
def txloop(ctx, sk, pkt): def txloop(ctx, sk, pkt):
print("txloop ...")
i = 0 i = 0
while 1: while 1:
if ctx.err(): if ctx.err():
raise ctx.err() raise ctx.err()
i += 1 i += 1
pkt_ = pkt + (b'%d' % i) pkt_ = pkt + (b'.%d' % i)
print("tx: %r ..." % pkt_) print("tx: %s ..." % u(pkt_))
sk.send(pkt + (b'%d' % i)) sk.send(pkt_)
time.sleep(1) time.sleep(1)
def rxloop(ctx, sk): def rxloop(ctx, sk):
print("rxloop ...")
while 1: while 1:
if ctx.err(): if ctx.err():
raise ctx.err() raise ctx.err()
pkt = sk.recv(4096) pkt = sk.recv(4096)
print("rx: %r" % pkt) print("rx: %s" % u(pkt))
def main(): def main():
sk = mjoin(group4, port) G = (group4, port)
action = sys.argv[1] action = sys.argv[1]
wg = sync.WorkGroup(context.background()) wg = sync.WorkGroup(context.background())
if "tx" in action: if "tx" in action:
wg.go(txloop, sk, b(sys.argv[2])) sktx = mjoin_tx(*G)
wg.go(txloop, sktx, b(sys.argv[2]))
if "rx" in action: if "rx" in action:
wg.go(rxloop, sk) skrx = mjoin_rx(*G)
wg.go(rxloop, skrx)
wg.wait() wg.wait()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment