示例#1
0
    def __init__(self, sock, client_side=False):
        self._socket = sock
        sock.setblocking(0)
        self._setsockopt(sock)
        self._socket = sock
        self.client_side = client_side

        self.buf = BytesIO()
        self.transmission_id, self.transmissions = 1, {}
        self.is_closed, self.close_callbacks = False, []

        self.connid = Connection.CONNID
        Connection.CONNID += 1
        self.peername = sock.getpeername()
        self.sockname = sock.getsockname()
        fd = self.fd = sock.fileno()

        self._send_queue = []
        self.r_io, self.w_io = io(fd, READ), io(fd, WRITE)
        self.r_gr, self.fed_write = None, False
示例#2
0
 def listen(self, address, backlog=1024, type_=SOCK_STREAM):
     # not support ipv6 yet
     if isinstance(address, string_types):
         family = AF_UNIX
         if os.path.exists(address):
             os.unlink(address)
     else:
         family = AF_INET
     self.logger.info("[listening|%s][type|%s][backlog|%d]", address, type_, backlog)
     sock = realsocket(family, type_)
     sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
     sock.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1)
     sock.bind(address)
     sock.listen(backlog)
     sock.setblocking(0)
     self.accept_watchers.append((io(sock.fileno(), READ), sock))
示例#3
0
 def connect(cls, address, client_side=False, timeout=None, family=AF_INET,
             type_=SOCK_STREAM, proto=0):
     assert getcurrent() != hub, 'could not call block func in main loop'
     sock = realsocket(family, type_, proto)
     errno = sock.connect_ex(address)
     if errno in connecting_error:
         rw_io = io(sock.fileno(), READ | WRITE)
         rw_gr = getcurrent()
         rw_io.start(rw_gr.switch)
         if timeout:
             rw_timer = timer(timeout)
             rw_timer.start(rw_gr.throw, Timeout)
         try:
             rw_gr.parent.switch()
         finally:
             if timeout:
                 rw_timer.stop()
             rw_io.stop()
             rw_timer = rw_io = None
         errno = sock.connect_ex(address)
     if errno != 0 and errno != EISCONN:
         raise socket_error(errno, strerror(errno))
     return cls(sock, client_side)