예제 #1
0
        /// <summary>
        /// Tries to initialize the Registered I/O handle.
        /// </summary>
        /// <param name="socket">A descriptor that identifies a socket.</param>
        /// <param name="result">Contains valid object if operation was successful, <c>null</c> otherwise.</param>
        /// <returns><c>true</c> if operation was successful, <c>false</c> otherwise.</returns>
        public static unsafe Boolean TryCreate(SOCKET socket, out RIOHandle result)
        {
            // get function table id
            var functionTableId = WinsockInterop.WSAID_MULTIPLE_RIO;

            // initialize functions table
            var functionTable = new RIO_EXTENSION_FUNCTION_TABLE();

            // get table size
            var tableSize = (UInt32)sizeof(RIO_EXTENSION_FUNCTION_TABLE);

            // will contain actual table size
            UInt32 actualTableSize;

            // try get registered IO functions table
            var tryGetTableResult = WinsockInterop.WSAIoctl(socket, WinsockInterop.SIO_GET_MULTIPLE_EXTENSION_FUNCTION_POINTER, &functionTableId, 16, &functionTable, tableSize, out actualTableSize, IntPtr.Zero, IntPtr.Zero);

            // check if attempt was successful
            if (tryGetTableResult == WinsockInterop.SOCKET_ERROR)
            {
                result = null;

                // return fail
                return(false);
            }

            // create registered I/O handle
            result = new RIOHandle(ref functionTable);

            // return success
            return(true);
        }
예제 #2
0
        /// <summary>
        /// Deactivates server.
        /// </summary>
        public WinsockErrorCode Deactivate()
        {
            // try close socket
            var tryCloseResultCode = WinsockInterop.closesocket(serverSocket);

            if (tryCloseResultCode == WinsockInterop.SOCKET_ERROR)
            {
                goto FAIL;
            }

            return(WinsockErrorCode.None);

FAIL:

            return((WinsockErrorCode)WinsockInterop.WSAGetLastError());
        }
예제 #3
0
        public TryResult <TcpConnection> TryAccept()
        {
            SOCKADDR address;

            var length = SOCKADDR.Size;

            // permit an incoming connection attempt on a socket
            var acceptedSocket = WinsockInterop.accept(serverSocket, out address, ref length);

            // ReSharper disable once InvertIf
            if (acceptedSocket == WinsockInterop.INVALID_SOCKET)
            {
                // get last error
                var errorCode = (WinsockErrorCode)WinsockInterop.WSAGetLastError();

                // return result
                return(TryResult <TcpConnection> .CreateFail(errorCode));
            }

            // try create connection
            return(workers[0].TryCreateConnection(acceptedSocket, acceptedSocket.ToUInt32()));
        }
예제 #4
0
        internal TryResult <TcpConnection> TryCreateConnection(SOCKET socket, UInt64 id)
        {
            var maxOutstandingReceive = 10u;

            var maxOutstandingSend = 10u;

            // try create request queue
            var requestQueue = RioHandle.CreateRequestQueue(socket, maxOutstandingReceive, 1, maxOutstandingSend, 1, CompletionQueue, CompletionQueue, id);

            if (requestQueue == WinsockInterop.RIO_INVALID_RQ)
            {
                var errorCode = (WinsockErrorCode)WinsockInterop.WSAGetLastError();

                TryResult <TcpConnection> .CreateFail(errorCode);
            }

            var connection = new TcpConnection(socket, requestQueue);

            // add connection into the collection of the connections
            Connections.TryAdd(id, connection);

            return(TryResult <TcpConnection> .CreateSuccess(connection));
        }
예제 #5
0
        /// <summary>
        /// Tries to create a new instance of the <see cref="IocpWorker" />.
        /// </summary>
        /// <param name="rioHandle">The object that provides work with Winsock registered I/O extensions.</param>
        /// <param name="processorIndex">The index of the processor.</param>
        /// <param name="segmentLength">The length of the segment.</param>
        /// <param name="segmentsCount">The count of the segments.</param>
        /// <returns>
        /// An instance of <see cref="TryResult{T}" /> which encapsulates result of the operation.
        /// <see cref="TryResult{T}.Success" /> contains <c>true</c> if operation was successful, <c>false</c> otherwise.
        /// <see cref="TryResult{T}.Result" /> contains valid object if operation was successful, <c>null</c> otherwise.
        /// </returns>
        public static unsafe TryResult <IocpWorker> TryCreate(RIOHandle rioHandle, Int32 processorIndex, UInt32 segmentLength, UInt32 segmentsCount)
        {
            #region 0 try create completion port

            var completionPort = KernelInterop.CreateIoCompletionPort(KernelInterop.INVALID_HANDLE_VALUE, IntPtr.Zero, UIntPtr.Zero, 0);

            // check if operation has succeed
            if (completionPort == IntPtr.Zero)
            {
                // get error code
                var kernelErrorCode = (KernelErrorCode)KernelInterop.GetLastError();

                // return fail result
                TryResult <IocpWorker> .CreateFail(kernelErrorCode);
            }

            #endregion

            #region 1 try create completion queue

            // compose completion method structure
            var completionMethod = new RIO_NOTIFICATION_COMPLETION
            {
                // set type to IOCP
                Type  = RIO_NOTIFICATION_COMPLETION_TYPE.RIO_IOCP_COMPLETION,
                union =
                {
                    // set IOCP parameters
                    Iocp              = new RIO_NOTIFICATION_COMPLETION.UNION.IOCP
                    {
                        // set completion port
                        IocpHandle    = completionPort,
                        // set completion key
                        CompletionKey = (UInt64)processorIndex,
                        Overlapped    = (NativeOverlapped *)-1
                    }
                }
            };

            // try create completion queue
            var completionQueue = rioHandle.CreateCompletionQueue(maxOutsandingCompletions, completionMethod);

            if (completionQueue == WinsockInterop.RIO_INVALID_CQ)
            {
                // get error code
                var winsockErrorCode = (WinsockErrorCode)WinsockInterop.WSAGetLastError();

                // return fail result
                TryResult <IocpWorker> .CreateFail(winsockErrorCode);
            }

            #endregion

            #region 2 try create buffer pool

            var tryCreateBufferPool = RIOBufferPool.TryCreate(rioHandle, segmentLength, segmentsCount);

            if (tryCreateBufferPool.Success == false)
            {
                // return result
                return(TryResult <IocpWorker> .CreateFail(tryCreateBufferPool.KernelErrorCode, tryCreateBufferPool.WinsockErrorCode));
            }

            #endregion

            // success
            var result = new IocpWorker(rioHandle, processorIndex, completionPort, completionQueue, tryCreateBufferPool.Result);

            // return success result
            return(TryResult <IocpWorker> .CreateSuccess(result));
        }
예제 #6
0
        /// <summary>
        /// Tries to create a new instance of the <see cref="RIOBufferPool" />.
        /// </summary>
        /// <param name="rioHandle">The object that provides work with Winsock registered I/O extensions.</param>
        /// <param name="segmentLength">The length of the segment.</param>
        /// <param name="segmentsCount">The count of the segments.</param>
        /// <returns>
        /// An instance of <see cref="TryResult{T}" /> which encapsulates result of the operation.
        /// <see cref="TryResult{T}.Success" /> contains <c>true</c> if operation was successful, <c>false</c> otherwise.
        /// <see cref="TryResult{T}.Result" /> contains valid object if operation was successful, <c>null</c> otherwise.
        /// </returns>
        /// <remarks>
        /// The multiplication of <paramref name="segmentLength" /> and <paramref name="segmentsCount" /> must produce value that is aligned to Memory Allocation Granularity.
        /// </remarks>
        public static unsafe TryResult <RIOBufferPool> TryCreate(RIOHandle rioHandle, UInt32 segmentLength, UInt32 segmentsCount)
        {
            // calculate size of the memory buffer to allocate
            var bufferLength = segmentLength * segmentsCount;

            void *memoryPointer;

            IntPtr buffer;

            IntPtr bufferId;

            // 1 try allocate memory block
            {
                // try reserve and commit memory block
                memoryPointer = KernelInterop.VirtualAlloc(null, bufferLength, KernelInterop.MEM_COMMIT | KernelInterop.MEM_RESERVE, KernelInterop.PAGE_READWRITE);

                // check if allocation has failed
                if (memoryPointer == null)
                {
                    // get kernel error code
                    var kernelErrorCode = (KernelErrorCode)KernelInterop.GetLastError();

                    // return result
                    return(TryResult <RIOBufferPool> .CreateFail(kernelErrorCode));
                }

                // set buffer
                buffer = (IntPtr)memoryPointer;
            }

            // 2 try register buffer with Registered I/O extensions
            {
                bufferId = rioHandle.RegisterBuffer(buffer, bufferLength);

                if (bufferId == WinsockInterop.RIO_INVALID_BUFFERID)
                {
                    // get winsock error code
                    var winsockErrorCode = (WinsockErrorCode)WinsockInterop.WSAGetLastError();

                    // free allocated memory
                    var freeResult = KernelInterop.VirtualFree(memoryPointer, 0, KernelInterop.MEM_RELEASE);

                    // set kernel error code
                    if (freeResult)
                    {
                        // return result
                        return(TryResult <RIOBufferPool> .CreateFail(winsockErrorCode));
                    }

                    // get kernel error code
                    var kernelErrorCode = (KernelErrorCode)KernelInterop.GetLastError();

                    // return result
                    return(TryResult <RIOBufferPool> .CreateFail(kernelErrorCode, winsockErrorCode));
                }
            }

            // 3 success
            var result = new RIOBufferPool(buffer, bufferLength, bufferId, segmentLength, segmentsCount);

            // return result
            return(TryResult <RIOBufferPool> .CreateSuccess(result));
        }
예제 #7
0
        private static Boolean TryConfigureBindAndStartListen(SOCKET serverSocket, TcpServerSettings settings)
        {
            // try disable use of the Nagle algorithm if requested
            if (settings.UseNagleAlgorithm == false)
            {
                var optionValue = -1;

                unsafe
                {
                    var tryDisableNagle = WinsockInterop.setsockopt(serverSocket, WinsockInterop.IPPROTO_TCP, WinsockInterop.TCP_NODELAY, (Byte *)&optionValue, sizeof(Int32));

                    // check if attempt has succeed
                    if (tryDisableNagle == WinsockInterop.SOCKET_ERROR)
                    {
                        return(false);
                    }
                }
            }

            // try enable faster operations on the loopback if requested
            if (settings.UseFastLoopback)
            {
                unsafe
                {
                    UInt32 optionValue = 1;

                    UInt32 dwBytes;

                    var tryEnableFastLoopbackResult = WinsockInterop.WSAIoctl(serverSocket, WinsockInterop.SIO_LOOPBACK_FAST_PATH, &optionValue, sizeof(UInt32), null, 0, out dwBytes, IntPtr.Zero, IntPtr.Zero);

                    // check if attempt has succeed
                    if (tryEnableFastLoopbackResult == WinsockInterop.SOCKET_ERROR)
                    {
                        return(false);
                    }
                }
            }

            // try bind
            {
                // compose address
                var address = new IN_ADDR
                {
                    s_addr = 0
                };

                // compose socket address
                var socketAddress = new SOCKADDR_IN
                {
                    sin_family = WinsockInterop.AF_INET,
                    sin_port   = WinsockInterop.htons(settings.Port),
                    sin_addr   = address
                };

                // try associate address with socket
                var tryBindResult = WinsockInterop.bind(serverSocket, ref socketAddress, SOCKADDR_IN.Size);

                if (tryBindResult == WinsockInterop.SOCKET_ERROR)
                {
                    return(false);
                }
            }

            // try start listen
            {
                var tryStartListen = WinsockInterop.listen(serverSocket, settings.AcceptBacklogLength);

                if (tryStartListen == WinsockInterop.SOCKET_ERROR)
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #8
0
        /// <summary>
        /// Activates server.
        /// </summary>
        public static TryResult <TcpWorker> TryInitialize(TcpServerSettings settings)
        {
            // 0 try initiates use of the Winsock DLL by a process
            {
                WSADATA data;

                var startupResultCode = WinsockInterop.WSAStartup(WinsockInterop.Version, out data);

                // check if startup was successful
                if (startupResultCode != WinsockErrorCode.None)
                {
                    return(TryResult <TcpWorker> .CreateFail(startupResultCode));
                }
            }

            // 1 try create server socket
            SOCKET serverSocket;

            {
                serverSocket = WinsockInterop.WSASocket(WinsockInterop.AF_INET, (Int32)SocketType.Stream, (Int32)ProtocolType.Tcp, IntPtr.Zero, 0, WinsockInterop.WSA_FLAG_REGISTERED_IO);

                // check if socket created
                if (WinsockInterop.INVALID_SOCKET == serverSocket)
                {
                    goto FAIL;
                }
            }

            // 2 try initialize Registered I/O extension
            RIOHandle rioHandle;

            if (!RIOHandle.TryCreate(serverSocket, out rioHandle))
            {
                goto FAIL;
            }

            // 3 get count of processors
            var processorsCount = Environment.ProcessorCount;

            // 4 create collection of the IOCP workers
            var workers = new IocpWorker[processorsCount];

            // initialize workers
            for (var processorIndex = 0; processorIndex < processorsCount; processorIndex++)
            {
                // try create worker
                var tryCreateWorker = IocpWorker.TryCreate(rioHandle, processorIndex, 4096, 1024);

                // check if operation has succeed
                if (!tryCreateWorker.Success)
                {
                    goto FAIL;
                }

                // add to collection
                workers[processorIndex] = tryCreateWorker.Result;
            }

            // try configure server socket and start listen
            if (!TryConfigureBindAndStartListen(serverSocket, settings))
            {
                goto FAIL;
            }

            // success
            var server = new TcpWorker(serverSocket, rioHandle, workers);

            foreach (var worker in workers)
            {
                worker.thread.Start();
            }

            return(TryResult <TcpWorker> .CreateSuccess(server));

FAIL:

            // get last error
            var errorCode = (WinsockErrorCode)WinsockInterop.WSAGetLastError();

            // terminate use of the Winsock DLL
            WinsockInterop.WSACleanup();

            // return fail
            return(TryResult <TcpWorker> .CreateFail(errorCode));
        }