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))
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)
def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, _sock=None): self._sock = sock = _sock or realsocket(family, type, proto) file.__init__(self, sock.fileno()) self._recv = sock.recv self._send = sock.send self._close = sock.close