Пример #1
0
    def _thread_func(self):
        # Note: create event loop in the new thread
        # instead in the constructor 
        event_loop = ZEventLoop()

        if self._thr_callback:
            self._thr_callback(event_loop)

        with self._guard:
            self._event_loop = event_loop
            self._guard.notify()

        event_loop.loop()
Пример #2
0
 def __init__(self, server_address):
     # FIXME Hack for Windows
     if system() != 'Windows':
         import signal
         signal.signal(signal.SIGPIPE, signal.SIG_IGN)
     self._event_loop = ZEventLoop() 
     self._acceptor = ZTcpAcceptor(self._event_loop, server_address)
     self._thread_pool = ZEventLoopThreadPool(self._event_loop)
     self._connection_callback = self._default_connection_callback
     self._message_callback = self._default_message_callback
     self._started = False
     self._next_conn_id = 1
     self._thread_init_callback = None
     self._connections = {}
     self._hostport = server_address[0] + '_' + str(server_address[1])
     self._acceptor.set_new_connection_callback(self._new_connection)
Пример #3
0
class ZTcpServer(object):
    def __init__(self, server_address):
        # FIXME Hack for Windows
        if system() != 'Windows':
            import signal
            signal.signal(signal.SIGPIPE, signal.SIG_IGN)
        self._event_loop = ZEventLoop() 
        self._acceptor = ZTcpAcceptor(self._event_loop, server_address)
        self._thread_pool = ZEventLoopThreadPool(self._event_loop)
        self._connection_callback = self._default_connection_callback
        self._message_callback = self._default_message_callback
        self._started = False
        self._next_conn_id = 1
        self._thread_init_callback = None
        self._connections = {}
        self._hostport = server_address[0] + '_' + str(server_address[1])
        self._acceptor.set_new_connection_callback(self._new_connection)

    def start(self):
        if not self._started:
            self._started = True
            self._thread_pool.start(self._thread_init_callback)

        if not self._acceptor.listening():
            self._event_loop.run_in_loop(self._acceptor.listen)

    def serve_forever(self):
        self._event_loop.loop()

    def quit(self):
        self._event_loop.quit()

    def set_thread_num(self, num_thrds):
        self._thread_pool.set_thread_num(num_thrds)

    def set_thread_init_callback(self, cb):
        self._thread_init_callback = cb

    def set_connection_callback(self, cb):
        self._connection_callback = cb

    def set_message_callback(self, cb):
        self._message_callback = cb

    def set_write_complete_callback(self, cb):
        self._write_complete_callback = cb

    def _new_connection(self, sock, peeraddr):
        self._event_loop.assert_in_loop_thread()
        io_loop = self._thread_pool.get_next_loop()
        self._next_conn_id += 1
        conn_name = self._hostport + ':' + peeraddr[0] + '_' + str(peeraddr[1])
        logging.info('ZTcpServer.new_connection %s' %conn_name)
        tcp_conn = ZTcpConnection(io_loop, conn_name, sock, peeraddr)
        self._connections[conn_name] = tcp_conn
        tcp_conn.set_connection_callback(self._connection_callback)
        tcp_conn.set_message_callback(self._message_callback)
        tcp_conn.set_close_callback(self._remove_connection)
        io_loop.run_in_loop(tcp_conn.connection_established)

    def _remove_connection_in_loop(self, conn):
        self._event_loop.assert_in_loop_thread()
        logging.info('ZTcpServer._remove_connection_in_loop connection %s' %conn.name())
        c = self._connections.pop(conn.name(), None)
        assert c is not None
        io_loop = conn.get_event_loop()
        io_loop.queue_in_loop(conn.connection_destroyed)

    def _remove_connection(self, conn):
        def _remove_connection_in_loop_wrapper():
            self._remove_connection_in_loop(conn)
        self._event_loop.run_in_loop(_remove_connection_in_loop_wrapper)

    def _default_connection_callback(self, tcp_conn):
        logging.info('ZTcpServer %s is %s' %(tcp_conn.name(), (tcp_conn.connected() and 'UP' or 'DOWN')))

    def _default_message_callback(self, tcp_conn, buf, receive_time):
        import time
        data = buf.retrieve_all_as_string()
        logging.info('ZTcpServer receive data: %s at %s' %(data, time.ctime(receive_time)))
        tcp_conn.send('you sent: %s' %data)