Ejemplo n.º 1
0
 def handle_request_from_client(self, conn, remote_addr, data, err):
     if err.errcode != connection.E_OK or not data:
         return
     sess = self.handler.new_session()
     sess.client_addr = remote_addr
     sess.send_ts = time.time()
     sess.req_data = data
     is_continue, resp = self.handler.on_client_request(sess)
     if resp:
         self.send_response(sess.client_addr, resp)
         return
     if not is_continue:
         self.logger.error("[sid=%d] invalid request from client", sess.sid)
         return
     for addr in self.upstreams:
         remote_addr = addr[1], addr[2]
         if addr.protocol == 'udp':
             conn = connection.UDPConnection(io_engine=self.io_engine)
             conn.asend(remote_addr, sess.req_data, self.handle_udp_request)
         elif addr.protocol == 'tcp':
             conn = connection.TCPConnection(io_engine=self.io_engine)
             conn.aconnect(remote_addr, self.handle_tcp_connected)
         else:
             self.logger.error("[sid=%d] invalid protocol %s", sess.sid,
                               addr.protocol)
             continue
         if conn:
             self.sessions[conn] = sess
Ejemplo n.º 2
0
def test_udp_client_sent_err(udp_server_process):
    server_addr = udp_server_process
    io_engine = ioloop.get_ioloop("select")
    client = connection.UDPConnection(io_engine=io_engine)
    client.set_recv_buffer_size(2048)

    def on_sent_err(sock, _, err):
        client.close()
        client.stop()
        assert err.errcode == connection.E_FAIL

    length = 64 * 1024 + 1
    client.asend(server_addr, b'x' * length, on_sent_err)
    client.run()
Ejemplo n.º 3
0
 def __init__(self, io_engine, upstreams, listen, timeout, handler):
     self.logger = logging.getLogger()
     self.io_engine = io_engine
     self.handler = handler
     self.upstreams = upstreams
     self.timeout = timeout
     # Connection -> Session, multi conn using the same Session object
     self.sessions = {}
     self.server = connection.UDPConnection(io_engine=self.io_engine)
     try:
         ip, port = listen.split(':')
         self.server.bind((ip, int(port)))
         self.listen_addr = self.server.bind_addr
     except connection.BindException:
         print("failed to bind to %s" % listen, file=sys.stderr)
         sys.exit(1)
Ejemplo n.º 4
0
def test_udp_client(udp_server_process):
    server_addr = udp_server_process
    io_engine = ioloop.get_ioloop("select")
    client = connection.UDPConnection(io_engine=io_engine)
    client.set_recv_buffer_size(2048)

    def on_recved(sock, _, data, err):
        client.close()
        client.stop()
        assert data == b'x' * 2048
        assert err.errcode == connection.E_OK

    def on_sent(sock, _, err):
        client.arecv(on_recved)
        assert err.errcode == connection.E_OK

    client.asend(server_addr, b'x' * 2048, on_sent)
    client.run()
Ejemplo n.º 5
0
def my_udp_server():
    io_engine = ioloop.get_ioloop("select")
    server = connection.UDPConnection(io_engine=io_engine)
    server.bind(("127.0.0.1", 0))
    server.set_recv_buffer_size(2048)

    def on_sent(sock, _, err):
        server.close()
        server.stop()
        assert err.errcode == connection.E_OK

    def on_recved(sock, remote_addr, data, err):
        assert err.errcode == connection.E_OK
        server.asend(remote_addr, data, on_sent)

    server.arecv(on_recved)
    p = Process(target=server.run)
    p.start()
    yield server.bind_addr
    os.kill(p.pid, signal.SIGINT)
    p.join()
Ejemplo n.º 6
0
def test_udp_bind(my_udp_server):
    server_addr = my_udp_server
    io_engine = ioloop.get_ioloop("select")
    server = connection.UDPConnection(io_engine=io_engine)
    with pytest.raises(connection.BindException):
        server.bind(server_addr)