예제 #1
0
파일: test_udp.py 프로젝트: kinnay/anynet
async def test_socket():
    async with udp.bind(HOST, 12345) as sock1:
        async with udp.bind(HOST, 12346) as sock2:
            assert sock1.local_address() == (HOST, 12345)
            assert sock2.local_address() == (HOST, 12346)

            await sock1.send(b"hi", (HOST, 12346))
            await sock2.send(b"hello", (HOST, 12345))
            assert (await sock1.recv())[0] == b"hello"
            assert (await sock2.recv())[0] == b"hi"
예제 #2
0
async def serve_transport(settings, host="", port=0, context=None):
	logger.debug("Serving PRUDP transport at %s:%i", host, port)
	transport = settings["prudp.transport"]
	if transport == settings.TRANSPORT_UDP:
		async with udp.bind(host, port) as socket:
			async with util.create_task_group() as group:
				transport = PRUDPDatagramTransport(settings, socket, group)
				await transport.start()
				yield transport
	else:
		transport = PRUDPSocketTransport(settings, (host, port))
		async with serve_transport_socket(transport.handle, settings, host, port, context):
			yield transport
예제 #3
0
파일: test_udp.py 프로젝트: kinnay/anynet
async def test_client():
    async with udp.bind(HOST, 12345) as server:
        async with udp.connect(HOST, 12345) as client:
            assert server.local_address() == (HOST, 12345)
            assert client.remote_address() == (HOST, 12345)

            await client.send(b"hi")

            data, addr = await server.recv()
            assert data == b"hi"

            await server.send(b"hello", addr)
            assert await client.recv() == b"hello"
예제 #4
0
async def browse(settings, search_criteria, key=None, timeout=1, max=0):
    challenge_key = secrets.token_bytes(16)
    challenge_data = secrets.token_bytes(256)
    async with udp.bind(broadcast=True) as sock:
        request = make_browse_request(settings, search_criteria, key,
                                      challenge_key, challenge_data)
        await sock.send(request, (util.broadcast_address(), 30000))

        sessions = []
        ids = []
        with anyio.move_on_after(timeout):
            while max == 0 or len(sessions) < max:
                data, addr = await sock.recv()
                session = parse_browse_reply(settings, data, key,
                                             challenge_key, challenge_data)
                if session and session.session_id not in ids:
                    ids.append(session.session_id)
                    sessions.append(session)
    return sessions
예제 #5
0
async def serve(settings, handler, key=None):
    async with udp.bind("0.0.0.0", 30000) as sock:
        async with util.create_task_group() as group:
            server = LanServer(settings, handler, key, sock, group)
            server.start()
            yield server