Ejemplo n.º 1
0
    def AcceptEx(self, listensocket, acceptsocket):
        listensocket = _int2handle(listensocket)
        acceptsocket = _int2handle(acceptsocket)
        bytesreceived = _ffi.new("DWORD[1]")

        if self.type != OverlappedType.TYPE_NONE:
            _winapi.raise_WinError()

        size = _ffi.sizeof("struct sockaddr_in6") + 16
        buf = _ffi.new("CHAR[]", size * 2)
        if not buf:
            return None

        self.type = OverlappedType.TYPE_ACCEPT
        self.handle = listensocket
        self.read_buffer = buf

        res = _accept_ex[0](listensocket, acceptsocket, buf, \
            0, size, size, bytesreceived, self.overlapped)

        if res:
            self.error = _winapi.ERROR_SUCCESS
        else:
            self.error = _kernel32.GetLastError()

        if self.error == _winapi.ERROR_SUCCESS or self.error == _winapi.ERROR_IO_PENDING:
            return None
        else:
            self.type = OverlappedType.TYPE_NOT_STARTED
            RaiseFromWindowsErr(0)
Ejemplo n.º 2
0
    def ConnectEx(self, socket, addressobj):
        socket = _int2handle(socket)

        if self.type != OverlappedType.TYPE_NONE:
            _winapi.raise_WinError()

        address = _ffi.new("struct sockaddr_in6*")
        length = _ffi.sizeof("struct sockaddr_in6")

        address, length = parse_address(addressobj,
                                        _ffi.cast("SOCKADDR*", address),
                                        length)

        if length < 0:
            return None

        self.type = OverlappedType.TYPE_CONNECT
        self.handle = socket

        res = _connect_ex[0](socket, address, length, \
            _ffi.NULL, 0, _ffi.NULL, self.overlapped)

        if res:
            self.error = _winapi.ERROR_SUCCESS
        else:
            self.error = _kernel32.GetLastError()

        if self.error == _winapi.ERROR_SUCCESS or self.error == _winapi.ERROR_IO_PENDING:
            return None
        else:
            self.type = OverlappedType.TYPE_NOT_STARTED
            RaiseFromWindowsErr(0)
Ejemplo n.º 3
0
def initiailize_function_ptrs():
    ## importing socket ensures that WSAStartup() is called
    import _socket
    s = _winsock2.socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
    dwBytes = _ffi.new("DWORD[1]", [0])
    if s == INVALID_SOCKET:
        _winapi.raise_WinError()

    result = _winsock2.WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER,    \
                             WSAID_ACCEPTEX, _ffi.sizeof(WSAID_ACCEPTEX[0]), _accept_ex,       \
                             _ffi.sizeof(_accept_ex[0]), dwBytes, _ffi.NULL, _ffi.NULL)
    if result == INVALID_SOCKET:
        _winsock2.closesocket(s)
        _winapi.raise_WinError()

    result = _winsock2.WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER,    \
                             WSAID_CONNECTEX, _ffi.sizeof(WSAID_CONNECTEX[0]), _connect_ex,       \
                             _ffi.sizeof(_connect_ex[0]), dwBytes, _ffi.NULL, _ffi.NULL)
    if result == INVALID_SOCKET:
        _winsock2.closesocket(s)
        _winapi.raise_WinError()

    result = _winsock2.WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER,    \
                             WSAID_DISCONNECTEX, _ffi.sizeof(WSAID_DISCONNECTEX[0]), _disconnect_ex,       \
                             _ffi.sizeof(_disconnect_ex[0]), dwBytes, _ffi.NULL, _ffi.NULL)

    _winsock2.closesocket(s)
    if result == INVALID_SOCKET:
        _winapi.raise_WinError()
Ejemplo n.º 4
0
def BindLocal(socket, family):
    socket = _int2handle(socket)
    if family == AF_INET:
        addr = _ffi.new("struct sockaddr_in*")
        addr[0].sin_family = AF_INET
        addr[0].sin_port = 0
        addr[0].sin_addr.S_un.S_addr = INADDR_ANY
        paddr = _ffi.cast("PSOCKADDR", addr)
        result = _winsock2.bind(socket, paddr,
                                _ffi.sizeof("struct sockaddr_in"))
    elif family == AF_INET6:
        addr = _ffi.new("struct sockaddr_in6*")
        addr.sin6_family = AF_INET6
        addr.sin6_port = 0
        addr.sin6_addr = in6addr_any[0]
        result = _winsock2.bind(socket, _ffi.cast("PSOCKADDR", addr),
                                _ffi.sizeof("struct sockaddr_in"))
    else:
        raise ValueError()

    if result == SOCKET_ERROR:
        RaiseFromWindowsErr(0)
Ejemplo n.º 5
0
def RegisterWaitWithQueue(object, completionport, ovaddress, miliseconds):
    data = _ffi.cast('PostCallbackData*',
                     _winapi.malloc(_ffi.sizeof("PostCallbackData")))
    newwaitobject = _ffi.new("HANDLE*")
    data[0].hCompletionPort = _int2handle(completionport)
    data[0].Overlapped = _int2overlappedptr(ovaddress)
    ret = _kernel32.RegisterWaitForSingleObject(
        newwaitobject, _int2handle(object),
        _ffi.cast("WAITORTIMERCALLBACK",
                  post_to_queue_callback), data, miliseconds,
        _kernel32.WT_EXECUTEINWAITTHREAD | _kernel32.WT_EXECUTEONLYONCE)
    if not ret:
        RaiseFromWindowsErr(0)

    return _handle2int(newwaitobject[0])