class W_SocketResource(W_FileResource): def __init__(self, space, hostname, port, type='tcp', fd=0, addr=None, read_filters=[], write_filters=[], errno=0, errstr=None, timeout=None): W_Resource.__init__(self, space) # assert hostname is not None self.hostname = hostname self.port = port self.type = type self.mode = 'r+' self.fd = fd self.eof = False self.errno = errno self.errstr = errstr self.space = space self.addr = addr self.remote_addr = None self.state = CLOSE self.timed_out = False self.write_filters = write_filters self.write_filters_params = [space.w_Null] * len(write_filters) self.read_filters = read_filters self.read_filters_params = [space.w_Null] * len(read_filters) self.initialize() def initialize(self): type = SOCK_STREAM if self.type == 'udp': type = SOCK_DGRAM if self.fd: self.socket = RSocket(fd=self.fd) self.state = OPEN else: self.socket = RSocket(type=type) self.addr = INETAddress(self.hostname, self.port) self.space.ec.interpreter.register_fd(self) def settimeout(self, timeout): self.socket.settimeout(timeout) def accept(self): return self.socket.accept() def open(self): try: self.socket.connect(self.addr) self.state = OPEN except (SocketTimeout, CSocketError), e: self.errno = e.errno self.errstr = e.get_msg()
def initialize(self): type = SOCK_STREAM if self.type == 'udp': type = SOCK_DGRAM if self.fd: self.socket = RSocket(fd=self.fd) self.state = OPEN else: self.socket = RSocket(type=type) self.addr = INETAddress(self.hostname, self.port)
def initialize(self): type = SOCK_STREAM if self.type == 'udp': type = SOCK_DGRAM if self.fd: self.socket = RSocket(fd=self.fd) self.state = OPEN else: self.socket = RSocket(type=type) self.addr = INETAddress(self.hostname, self.port) self.space.ec.interpreter.register_fd(self)
def descr_init(self, space, family=AF_INET, type=SOCK_STREAM, proto=0, w_fileno=None): try: if not space.is_w(w_fileno, space.w_None): sock = RSocket(family, type, proto, fd=space.c_filedescriptor_w(w_fileno)) else: sock = RSocket(family, type, proto) W_Socket.__init__(self, space, sock) except SocketError, e: raise converted_error(space, e)
def descr_init(self, space, family=-1, type=-1, proto=-1, w_fileno=None): from rpython.rlib.rsocket import _c if space.is_w(w_fileno, space.w_None): if family == -1: family = AF_INET if type == -1: type = SOCK_STREAM if proto == -1: proto = 0 try: if not space.is_w(w_fileno, space.w_None): if _WIN32 and space.isinstance_w(w_fileno, space.w_bytes): # it is possible to pass some bytes representing a socket # in the file descriptor object on winodws fdobj = space.bytes_w(w_fileno) if len(fdobj) != rffi.sizeof(_c.WSAPROTOCOL_INFOW): raise oefmt( space.w_ValueError, "socket descriptor string has wrong size, should be %d bytes", rffi.sizeof(_c.WSAPROTOCOL_INFOW)) info_charptr = rffi.str2charp(fdobj) try: info_ptr = rffi.cast(lltype.Ptr(_c.WSAPROTOCOL_INFOW), info_charptr) type = info_ptr.c_iSocketType fd = _c.WSASocketW(_c.FROM_PROTOCOL_INFO, _c.FROM_PROTOCOL_INFO, _c.FROM_PROTOCOL_INFO, info_ptr, 0, _c.WSA_FLAG_OVERLAPPED) if fd == rsocket.INVALID_SOCKET: raise converted_error(space, rsocket.last_error()) sock = RSocket(info_ptr.c_iAddressFamily, info_ptr.c_iSocketType, info_ptr.c_iProtocol, fd) finally: lltype.free(info_charptr, flavor='raw') else: fd = space.c_filedescriptor_w(w_fileno) if family == -1: family = rsocket.get_socket_family(fd) if type == -1: type = rsocket.getsockopt_int(fd, _c.SOL_SOCKET, _c.SO_TYPE) if proto == -1: proto = get_so_protocol(fd) sock = RSocket(family, type, proto, fd=fd) else: sock = RSocket(family, type, proto, inheritable=False) W_Socket.__init__(self, space, sock) except SocketError as e: raise converted_error(space, e)
def newsocket(space, w_subtype, family=AF_INET, type=SOCK_STREAM, proto=0): self = space.allocate_instance(W_Socket, w_subtype) try: sock = RSocket(family, type, proto) except SocketError as e: raise converted_error(space, e) W_Socket.__init__(self, space, sock) return self
def __init__(self, space, sock=None): self.space = space if sock is None: self.sock = RSocket.empty_rsocket() else: register_socket(space, sock) self.sock = sock self.register_finalizer(space)
def __del__(self): self.clear_all_weakrefs() RSocket.__del__(self)
def writer(): sock = RSocket() sock.connect(addr) writer_part(sock) sock.close() q.get()