Пример #1
0
 def __init__(self):
     self.engine = TCPSocketEngine(
         io_poll.get_poll(), is_blocking=False, debug=True)
     self.client_conn = dict()
     self.head_len = proto.head_len()
     self.is_running = False
     self.engine.set_timeout(rw_timeout=120, idle_timeout=600)
def start_echo_server():
    global server_addr
    poll = io_poll.get_poll()
    print str(poll)
    server = CoroSocketEngine(poll, is_blocking=False)
    server.set_logger(getLogger("server"))

    def _on_readable(conn):
        buf, eof = conn.read_avail(4096)
        if buf:
            #print "write", len(buf)
            yield conn.write(buf)
        if eof:
            conn.close()
        else:
            conn.watch()
        return
    server.listen_addr(server_addr, readable_cb=_on_readable)

    def _run(_server):
        while True:
            try:
                _server.poll()
            except Exception, e:
                traceback.print_exc()
                os._exit(1)
        return
Пример #3
0
 def __init__ (self, logger, _id, addr):
     self.logger = logger
     self._bind_addr = addr
     self.server_id = _id
     self.is_running = False
     self.server = TCPSocketEngine (get_poll(), debug=True, is_blocking=False)
     self.server.set_timeout (10, 1800)
     self.server.set_logger (self.logger)
     self._timers = TimerEvents (time.time, logger)
     self.passive_sock = None
     self._handles = {
             packet.StatusReq.__name__: self._recv_status_req
             }
     self._empty_head_ack = NetHead ().pack ()
     print "init"
def start_coro_server(poll=None):
    global server_addr
    poll = iopoll.get_poll()
    server = CoroSocketEngine(poll, is_blocking=False, debug=False)
    server.set_logger(getLogger("server"))
#    server.get_time = tc.time
    print "starting unblock server with", str(poll)
    def _handler(conn):
        try:
            buf = yield conn.read(len(data))
            yield conn.write(buf)
            server.watch_conn(conn)
        except PeerCloseError:
            pass
        except Exception, e:
            getLogger("server").exception(e)
            print e
        return
            conn.close()
            global_lock.acquire()
            g_done_client += 1
            print "client done", g_done_client
            if g_done_client == g_client_num:
                print "time:", time.time() - start_time
                global_lock.release()
                os._exit(0)
            global_lock.release()
        except socket.error, e:
            engine.logger.exception(e)
            print e
            os._exit(1)
        return

    engine = CoroSocketEngine(io_poll.get_poll())
    engine.set_logger(getLogger("client"))
    for i in xrange(g_client_num):
        engine.run_coro(client(engine, i, server_addr, g_round))
    while True:
        engine.poll()


def main():
    Log("client", config=conf)
    Log("server", config=conf)
    server = start_echo_server()
    time.sleep(1)
    test_client()

if __name__ == '__main__':