Exemple #1
0
        internal RioSocketBase(RioFixedBufferPool sendBufferPool, RioFixedBufferPool receiveBufferPool,
                               uint maxOutstandingReceive, uint maxOutstandingSend, IntPtr SendCompletionQueue, IntPtr ReceiveCompletionQueue,
                               ADDRESS_FAMILIES adressFam, SOCKET_TYPE sockType, PROTOCOL protocol)
        {
            if ((Socket = WinSock.WSASocket(adressFam, sockType, protocol, IntPtr.Zero, 0, SOCKET_FLAGS.REGISTERED_IO | SOCKET_FLAGS.WSA_FLAG_OVERLAPPED)) == IntPtr.Zero)
            {
                WinSock.ThrowLastWSAError();
            }

            SendBufferPool    = sendBufferPool;
            ReceiveBufferPool = receiveBufferPool;

            _requestQueue = RioStatic.CreateRequestQueue(Socket, maxOutstandingReceive, 1, maxOutstandingSend, 1, ReceiveCompletionQueue, SendCompletionQueue, GetHashCode());
            WinSock.ThrowLastWSAError();
        }
        public unsafe RioTcpListener(RioFixedBufferPool sendPool, RioFixedBufferPool revicePool, uint socketCount, uint maxOutstandingReceive = 1024, uint maxOutstandingSend = 1024)
            : base(sendPool, revicePool, socketCount, maxOutstandingReceive, maxOutstandingSend, (maxOutstandingReceive + maxOutstandingSend) * socketCount)
        {
            if ((_listenerSocket = WinSock.WSASocket(ADDRESS_FAMILIES.AF_INET, SOCKET_TYPE.SOCK_STREAM, PROTOCOL.IPPROTO_TCP, IntPtr.Zero, 0, SOCKET_FLAGS.REGISTERED_IO | SOCKET_FLAGS.WSA_FLAG_OVERLAPPED)) == IntPtr.Zero)
            {
                WinSock.ThrowLastWSAError();
            }

            int    True    = -1;
            UInt32 dwBytes = 0;

            WinSock.setsockopt(_listenerSocket, WinSock.IPPROTO_TCP, WinSock.TCP_NODELAY, (char *)&True, 4);
            WinSock.WSAIoctlGeneral(_listenerSocket, WinSock.SIO_LOOPBACK_FAST_PATH, &True, 4, null, 0, out dwBytes, IntPtr.Zero, IntPtr.Zero);

            if ((_listenIocp = Kernel32.CreateIoCompletionPort(_listenerSocket, _listenIocp, 0, 1)) == IntPtr.Zero)
            {
                Kernel32.ThrowLastError();
            }

            Thread ListenIocpThread = new Thread(ListenIocpComplete);

            ListenIocpThread.IsBackground = true;
            ListenIocpThread.Start();
        }
Exemple #3
0
        internal unsafe static void Initalize()
        {
            IntPtr tempSocket;

            tempSocket = WinSock.WSASocket(ADDRESS_FAMILIES.AF_INET, SOCKET_TYPE.SOCK_STREAM, PROTOCOL.IPPROTO_TCP, IntPtr.Zero, 0, SOCKET_FLAGS.REGISTERED_IO | SOCKET_FLAGS.WSA_FLAG_OVERLAPPED);
            WinSock.ThrowLastWSAError();

            uint dwBytes = 0;

            Guid AcceptExId  = new Guid("B5367DF1-CBAC-11CF-95CA-00805F48A192");
            var  acceptExptr = IntPtr.Zero;

            if (WinSock.WSAIoctl2(tempSocket,
                                  WinSock.SIO_GET_EXTENSION_FUNCTION_POINTER,
                                  ref AcceptExId,
                                  16,
                                  ref acceptExptr,
                                  IntPtr.Size,
                                  out dwBytes,
                                  IntPtr.Zero,
                                  IntPtr.Zero) != 0)
            {
                WinSock.ThrowLastWSAError();
            }
            else
            {
                AcceptEx = Marshal.GetDelegateForFunctionPointer <WinSock.AcceptEx>(acceptExptr);
            }


            Guid ConnectExId  = new Guid("25A207B9-DDF3-4660-8EE9-76E58C74063E");
            var  ConnectExptr = IntPtr.Zero;

            if (WinSock.WSAIoctl2(tempSocket,
                                  WinSock.SIO_GET_EXTENSION_FUNCTION_POINTER,
                                  ref ConnectExId,
                                  16,
                                  ref ConnectExptr,
                                  IntPtr.Size,
                                  out dwBytes,
                                  IntPtr.Zero,
                                  IntPtr.Zero) != 0)
            {
                WinSock.ThrowLastWSAError();
            }
            else
            {
                ConnectEx = Marshal.GetDelegateForFunctionPointer <WinSock.ConnectEx>(ConnectExptr);
            }


            Guid DisconnectExId  = new Guid("7FDA2E11-8630-436F-A031-F536A6EEC157");
            var  DisconnectExptr = IntPtr.Zero;

            if (WinSock.WSAIoctl2(tempSocket,
                                  WinSock.SIO_GET_EXTENSION_FUNCTION_POINTER,
                                  ref DisconnectExId,
                                  16,
                                  ref DisconnectExptr,
                                  IntPtr.Size,
                                  out dwBytes,
                                  IntPtr.Zero,
                                  IntPtr.Zero) != 0)
            {
                WinSock.ThrowLastWSAError();
            }
            else
            {
                DisconnectEx = Marshal.GetDelegateForFunctionPointer <WinSock.DisconnectEx>(DisconnectExptr);
            }

            var  rio = new RIO_EXTENSION_FUNCTION_TABLE();
            Guid RioFunctionsTableId = new Guid("8509e081-96dd-4005-b165-9e2ee8c79e3f");

            if (WinSock.WSAIoctl(tempSocket, WinSock.SIO_GET_MULTIPLE_EXTENSION_FUNCTION_POINTER,
                                 ref RioFunctionsTableId, 16, ref rio,
                                 sizeof(RIO_EXTENSION_FUNCTION_TABLE),
                                 out dwBytes, IntPtr.Zero, IntPtr.Zero) != 0)
            {
                WinSock.ThrowLastWSAError();
            }
            else
            {
                RegisterBuffer        = Marshal.GetDelegateForFunctionPointer <WinSock.RIORegisterBuffer>(rio.RIORegisterBuffer);
                CreateCompletionQueue = Marshal.GetDelegateForFunctionPointer <WinSock.RIOCreateCompletionQueue>(rio.RIOCreateCompletionQueue);
                CreateRequestQueue    = Marshal.GetDelegateForFunctionPointer <WinSock.RIOCreateRequestQueue>(rio.RIOCreateRequestQueue);
                Notify            = Marshal.GetDelegateForFunctionPointer <WinSock.RIONotify>(rio.RIONotify);
                DequeueCompletion = Marshal.GetDelegateForFunctionPointer <WinSock.RIODequeueCompletion>(rio.RIODequeueCompletion);
                Receive           = Marshal.GetDelegateForFunctionPointer <WinSock.RIOReceive>(rio.RIOReceive);
                Send = Marshal.GetDelegateForFunctionPointer <WinSock.RIOSend>(rio.RIOSend);
                CloseCompletionQueue  = Marshal.GetDelegateForFunctionPointer <WinSock.RIOCloseCompletionQueue>(rio.RIOCloseCompletionQueue);
                DeregisterBuffer      = Marshal.GetDelegateForFunctionPointer <WinSock.RIODeregisterBuffer>(rio.RIODeregisterBuffer);
                ResizeCompletionQueue = Marshal.GetDelegateForFunctionPointer <WinSock.RIOResizeCompletionQueue>(rio.RIOResizeCompletionQueue);
                ResizeRequestQueue    = Marshal.GetDelegateForFunctionPointer <WinSock.RIOResizeRequestQueue>(rio.RIOResizeRequestQueue);
            }

            WinSock.closesocket(tempSocket);
            WinSock.ThrowLastWSAError();
        }