def __init__(self,multicast_addr,localip): # get local socket name sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.connect(("8.8.8.8",80)) self.__m_localip = sock.getsockname()[0] print("local ip ",self.__m_localip) # init recv_socket self.loop = pyuv.Loop.default_loop() self.__m_multicast_addr = multicast_addr self.__m_recv_socket = pyuv.UDP(self.loop) self.__m_recv_socket.bind((g_listenip,g_multicast_port),pyuv.UV_UDP_REUSEADDR) self.__m_recv_socket.set_membership(multicast_addr,pyuv.UV_JOIN_GROUP) self.__m_recv_socket.set_broadcast(True) self.__m_recv_socket.start_recv(self.handlerRecv) # recieve handler # init send_socket self.__m_send_socket = pyuv.UDP(self.loop) self.__m_send_socket.bind((g_listenip,g_multicast_port),pyuv.UV_UDP_REUSEADDR) # self.__m_send_socket.set_membership(multicast_addr,pyuv.UV_JOIN_GROUP) # self.__m_localEndpoint = "" self.__m_localrpcEndpoint = "" self.__m_jsnCbMap = {} self.__m_msgCbMap = {} self.__m_serviceCallMap = {} self.__m_topicMap = {} # std::unordered_map<std::string, std::unordered_set<std::string>> self.__m_serviceMap = {} # std::unordered_map<std::string, std::unordered_set<std::string>> self.__m_signal_h = pyuv.Signal(self.loop) self.__m_signal_h.start(self.signal_handler, signal.SIGINT)
def test_udp_bind_reuse(self): handle = pyuv.UDP(self.loop) handle.bind(("", TEST_PORT), pyuv.UV_UDP_REUSEADDR) handle2 = pyuv.UDP(self.loop) handle2.bind(("", TEST_PORT), pyuv.UV_UDP_REUSEADDR) handle.close() handle2.close() self.loop.run()
def test_udp_empty_datagram(self): self.server = pyuv.UDP(self.loop) self.client = pyuv.UDP(self.loop) self.client.bind(("0.0.0.0", TEST_PORT2)) self.client.start_recv(self.on_client_recv) self.server.send(("127.0.0.1", TEST_PORT2), b"") self.loop.run() self.assertEqual(self.on_close_called, 2)
def test_udp_bind_noreuse(self): handle = pyuv.UDP(self.loop) handle.bind(("", TEST_PORT)) handle2 = pyuv.UDP(self.loop) self.assertRaises(pyuv.error.UDPError, handle2.bind, ("", TEST_PORT)) handle.close() handle2.close() self.loop.run()
def test_udp_invalid_data(self): self.on_close_called = 0 self.server = pyuv.UDP(self.loop) self.server.bind(("0.0.0.0", TEST_PORT)) self.server.start_recv(self.on_server_recv) self.client = pyuv.UDP(self.loop) self.client.bind(("0.0.0.0", TEST_PORT2)) timer = pyuv.Timer(self.loop) timer.start(self.timer_cb, 0.1, 0) self.loop.run() self.assertEqual(self.on_close_called, 3)
def test_udp_multicast(self): self.on_close_called = 0 self.server = pyuv.UDP(self.loop) self.client = pyuv.UDP(self.loop) self.client.bind((MULTICAST_ADDRESS, TEST_PORT)) self.client.set_membership(MULTICAST_ADDRESS, pyuv.UV_JOIN_GROUP) self.client.start_recv(self.on_client_recv) self.server.send(b"PING", (MULTICAST_ADDRESS, TEST_PORT), self.on_server_send) self.loop.run() self.assertEqual(self.on_close_called, 2) self.assertEquals(self.received_data, b"PING")
def test_broadcast_pingpong(self): self.on_close_called = 0 self.server = pyuv.UDP(self.loop) self.server.bind(("0.0.0.0", TEST_PORT)) self.server.set_broadcast(True) self.server.start_recv(self.on_server_recv) self.client = pyuv.UDP(self.loop) self.client.bind(("0.0.0.0", TEST_PORT2)) self.client.set_broadcast(True) self.client.start_recv(self.on_client_recv) timer = pyuv.Timer(self.loop) timer.start(self.timer_cb, 0.1, 0) self.loop.run() self.assertEqual(self.on_close_called, 3)
def __init__(self, ip_port): self.loop = pyuv.Loop.default_loop() self.udp = pyuv.UDP(self.loop) self.udp.start_recv(cudp_on_read) self.ip_port = ip_port global CLIENT CLIENT.update({self.udp: ip_port})
def __init__(self, loop, ip_iface, iface_index, alpacaport, discoport=32227, mcgroup="ff12::00a1:9aca"): self.server = pyuv.UDP(loop) self.__alpacaport = alpacaport self.__sockv6 = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) self.__sockv6.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) if os.name != "nt": self.__sockv6.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEPORT, 1 ) #needed on Linux and OSX to share port with net core. Remove on windows. self.server.open(self.__sockv6.fileno()) self.server.bind((ip_iface, discoport)) addrinfo = socket.getaddrinfo(mcgroup, None)[0] group_bin = socket.inet_pton(addrinfo[0], addrinfo[4][0]) # Join group mreq = group_bin + struct.pack('@I', iface_index) if os.name == "nt": self.__sockv6.setsockopt( 41, socket.IPV6_JOIN_GROUP, mreq ) #some versions of python on Windows do not have this option else: self.__sockv6.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, mreq) self.server.start_recv(self.__on_read) self.signal_h = pyuv.Signal(loop) self.signal_h.start(self.__signal_cb, signal.SIGINT)
def test_udp_big_datagram(self): self.errorno = None self.handle = pyuv.UDP(self.loop) data = b"X" * 65536 self.handle.send(("127.0.0.1", TEST_PORT), data, self.send_cb) self.loop.run() self.assertEqual(self.errorno, pyuv.errno.UV_EMSGSIZE)
def test_family_unspec2(self): client = pyuv.UDP(self.loop, socket.AF_UNSPEC) with self.assertRaises(pyuv.error.UDPError): client.family client.bind(('::1', TEST_PORT)) self.assertEqual(client.family, socket.AF_INET6) client.close() self.loop.run()
def test_fromfd_inet6(self): client = pyuv.UDP(self.loop) client.bind(('::1', TEST_PORT)) sock = socket.fromfd(client.fileno(), client.family, socket.SOCK_DGRAM) self.assertEqual(client.getsockname(), sock.getsockname()) client.close() sock.close() self.loop.run()
def __init__(self): loop = evergreen.current.loop self._handle = pyuv.UDP(loop._loop) self._closed = False self._receive_result = Result() self._pending_writes = 0 self._flush_event = Event() self._flush_event.set() self._sockname = None
def create_udp_endpoint(loop, protocol_factory, local_addr=None, remote_addr=None): if not (local_addr or remote_addr): raise ValueError('local or remote address must be specified') handle = pyuv.UDP(loop._loop) handle.bind(local_addr or ('', 0)) protocol = protocol_factory() addr = handle.getsockname() transport = UDPTransport(loop, protocol, handle, remote_addr, extra={'sockname': addr}) return transport, protocol
def test_udp_pingpong(self): self.on_close_called = 0 self.server = pyuv.UDP(self.loop) self.server.bind(("0.0.0.0", TEST_PORT)) self.server.set_broadcast(True) # for coverage try: self.server.set_ttl(10) # for coverage except pyuv.error.UDPError: # This function is not implemented on Windows pass self.server.start_recv(self.on_server_recv) self.client = pyuv.UDP(self.loop) self.client.bind(("0.0.0.0", TEST_PORT2)) self.client.start_recv(self.on_client_recv) timer = pyuv.Timer(self.loop) timer.start(self.timer_cb, 0.1, 0) self.loop.run() self.assertEqual(self.on_close_called, 3)
def test_udp_multicast(self): self.on_close_called = 0 self.server = pyuv.UDP(self.loop) self._create_clients() self.server.send((MULTICAST_ADDRESS, TEST_PORT), b"PING", self.on_server_send) self.loop.run() self.assertEqual(self.on_close_called, 1 + len(self.clients)) self.assertEqual(self.received_data, b"PING")
def __init__(self, addr=('0.0.0.0', 0)): # listeners are all couroutines waiting for a connections self.listeners = deque() self.conns = {} self.uv = uv_server() self.task = getcurrent() self.listening = False self.handler = pyuv.UDP(self.uv.loop) self.handler.bind(addr)
def test_udp_open(self): sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) loop = pyuv.Loop.default_loop() handle = pyuv.UDP(loop) handle.open(sock.fileno()) try: handle.bind(("1.2.3.4", TEST_PORT)) except pyuv.error.UDPError as e: self.assertEqual(e.args[0], pyuv.errno.UV_EADDRNOTAVAIL) loop.run()
def __init__(self, family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0, _sock=None, _hub=None): """ Initialize the UV socket :param family_or_realsock: a socket descriptor or a socket family """ self.uv_fd = None self.uv_handle = None self.uv_hub = None self.uv_recv_string = '' # buffer for receiving data... if isinstance(family, (int, long)): self.uv_fd = _original_socket(family, type, proto, _sock) elif isinstance(family, GreenSocket): _sock = family self.uv_fd = _sock.uv_fd if hasattr(_sock, 'uv_hub') and _sock.uv_hub: _hub = _sock.uv_hub else: _sock = family self.uv_fd = _sock if not self.uv_hub: if _hub: self.uv_hub = _hub else: self.uv_hub = weakref.proxy(get_hub()) ## check if the socket type is supported by pyUV and we can create a pyUV socket... if not self.uv_handle: if self.type == socket.SOCK_STREAM: self.uv_handle = pyuv.TCP(self.uv_hub.uv_loop) self.uv_handle.open(self.fileno()) elif self.type == socket.SOCK_DGRAM: self.uv_handle = pyuv.UDP(self.uv_hub.uv_loop) self.uv_handle.open(self.fileno()) # import timeout from other socket, if it was there try: self._timeout = self.uv_fd.gettimeout( ) or socket.getdefaulttimeout() except AttributeError: self._timeout = socket.getdefaulttimeout() assert self.uv_fd, 'the socket descriptor must be not null' set_nonblocking(self.uv_fd) # when client calls setblocking(0) or settimeout(0) the socket must act non-blocking self.act_non_blocking = False
def _create_clients(self, loop=False): self.clients = list() for addr in interface_addresses(): client = pyuv.UDP(self.loop) client.bind((addr, TEST_PORT)) client.set_membership(MULTICAST_ADDRESS, pyuv.UV_JOIN_GROUP) if loop: client.set_multicast_loop(True) else: client.set_multicast_ttl(10) client.start_recv(self.on_client_recv) self.clients.append(client)
def on_channel_read(self, expected_type, handle, data, pending, error): self.assertEqual(pending, expected_type) if pending == pyuv.UV_NAMED_PIPE: recv_handle = pyuv.Pipe(self.loop) elif pending == pyuv.UV_TCP: recv_handle = pyuv.TCP(self.loop) elif pending == pyuv.UV_UDP: recv_handle = pyuv.UDP(self.loop) self.channel.accept(recv_handle) self.channel.close() self.send_handle.close() recv_handle.close()
def __init__(self, addr, raddr, client=None): self.uv = uv_server() if client is None: self.client = pyuv.UDP(self.uv.loop) self.client.bind(raddr) else: self.client = client self.reading = True self.queue = deque() self.cr = channel self._raddr = raddr self.addr = addr
def on_channel_read(handle, data, pending, error): global channel, loop, recv_handle assert pending in (pyuv.UV_NAMED_PIPE, pyuv.UV_UDP, pyuv.UV_TCP), "wrong handle type" if pending == pyuv.UV_NAMED_PIPE: recv_handle = pyuv.Pipe(loop) elif pending == pyuv.UV_TCP: recv_handle = pyuv.TCP(loop) elif pending == pyuv.UV_UDP: recv_handle = pyuv.UDP(loop) channel.accept(recv_handle) channel.write2(b".", recv_handle, on_write2)
def on_channel_read(self, expected_type, handle, data, error): pending = self.channel.pending_handle_type() if pending == expected_type: if pending == pyuv.UV_NAMED_PIPE: recv_handle = pyuv.Pipe(self.loop) elif pending == pyuv.UV_TCP: recv_handle = pyuv.TCP(self.loop) elif pending == pyuv.UV_UDP: recv_handle = pyuv.UDP(self.loop) self.channel.accept(recv_handle) self.channel.close() self.send_handle.close() recv_handle.close()
def __init__(self, loop, ip_iface, iface_index, discoport=32227, mcgroup="ff12::00a1:9aca"): self.server = pyuv.UDP(loop) self.server.bind((ip_iface, 0)) self.server.start_recv(self.__on_read) self.signal_h = pyuv.Signal(loop) self.signal_h.start(self.__signal_cb, signal.SIGINT) self.server.try_send((mcgroup, discoport), "alpacadiscovery1".encode())
def __init__(self, alpacaport, discport, loop): self.server = pyuv.UDP(loop) self.__alpacaport = alpacaport self.__sockv6 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.__sockv6.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) if os.name != "nt": self.__sockv6.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEPORT, 1 ) #needed on Linux and OSX to share port with net core. Remove on windows. self.server.open(self.__sockv6.fileno()) self.server.bind(('0.0.0.0', discport)) self.server.start_recv(self.__on_read) self.signal_h = pyuv.Signal(loop) self.signal_h.start(self.__signal_cb, signal.SIGINT)
def listenUDP(self, port, protocol, interface='0.0.0.0', maxPacketSize=8192): """ """ # p = protocol() # udp = pyuv.UDP(self._loop) # AF_UNSPEC udp.bind((interface, port)) # p.transport = udp # p.doStart() # return udp
def main(self, m): self.monitor = m self.loop = pyuv.Loop.default_loop() self.server = pyuv.UDP(self.loop) self.server.bind(("0.0.0.0", 6901)) self.server.start_recv(self.on_read) self.signal_h = pyuv.Signal(self.loop) self.signal_h.start(self.signal_cb, signal.SIGINT) print("PyUV version %s" % pyuv.__version__) #self.setup_midi() while not self.stop and self.loop.run(pyuv.UV_RUN_NOWAIT): for k, v in self.midi_devs.items(): for msg in v.iter_pending(): self.process_midi(k, msg) print("Stopped!")
def __init__(self, discoport, loop): self.server = pyuv.UDP(loop) self.server.bind(('0.0.0.0', 0)) self.server.set_broadcast(True) self.server.start_recv(self.__on_read) self.signal_h = pyuv.Signal(loop) self.signal_h.start(self.__signal_cb, signal.SIGINT) for interface in netifaces.interfaces(): for interfacedata in netifaces.ifaddresses(interface): if netifaces.AF_INET == interfacedata: for ip in netifaces.ifaddresses(interface)[ netifaces.AF_INET]: if ('broadcast' in ip): try: self.server.try_send( (ip['broadcast'], discoport), "alpacadiscovery1".encode()) except Exception as e: print(e)
def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0): global _next_fileno global _pyuv_loop global _socket_map # Make underlying pyuv "socket" object. if type == SOCK_STREAM: self._socket = self._tcp_socket = pyuv.TCP(_pyuv_loop) elif type == SOCK_DGRAM: self._socket = self._udp_socket = pyuv.UDP(_pyuv_loop) else: raise RuntimeError("Unsupported socket family: %s" % type) # Property variables. self._family = family self._type = type self._proto = proto # Internal support. self._fileno = _next_fileno _next_fileno += 1 if can_timeout(): self._timeout = stdsocket.getdefaulttimeout() _socket_map[id(self)] = self start_pumping()