示例#1
0
 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)
示例#2
0
 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)
示例#3
0
 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)
示例#4
0
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