private static void Main() { ThreadPool.SetMinThreads(256, 256); // 0 initialize server settings var serverSettings = new TcpWorkerSettings { AcceptPoint = new IPEndPoint(IPAddress.Loopback, /*IPAddress.Parse("2001:0:5ef5:79fb:142a:29b9:4f88:b3a6")*/ 5001), AcceptQueueMaxEntriesCount = 1024, AcceptQueueWaitTime = TimeSpan.FromMilliseconds(2), ReceiveBufferLength = 512, SendBufferLength = 512, ConnectionsBacklogLength = 4096, UseFastLoopback = true, UseNagleAlgorithm = false }; // 1 initialize server TcpWorker server; try { server = new TcpWorker(serverSettings, ServeSocket); } catch (TcpServerException e) { Console.WriteLine($"Error activating server. Kernel error code: {e.KErrorCode}. Winsock error code: {e.WErrorCode}."); //Console.WriteLine($"Error activating server."); return; } Console.WriteLine($"server is activated"); /* // 2 try accept connection var tryAccept = server.TryAccept(); if (!tryAccept.Success) { Console.WriteLine($"error on accept. Kernel error code: {tryAccept.KernelErrorCode}. Winsock error code: {tryAccept.WinsockErrorCode}"); } Console.WriteLine($"accept success"); */ Console.WriteLine("any key to exit"); Console.ReadLine(); Console.ReadLine(); }
private static void Main() { ThreadPool.SetMinThreads(256, 256); // 0 initialize server settings var serverSettings = new TcpWorkerSettings { AcceptPoint = new IPEndPoint(IPAddress.Loopback, /*IPAddress.Parse("2001:0:5ef5:79fb:142a:29b9:4f88:b3a6")*/ 5001), AcceptQueueMaxEntriesCount = 1024, AcceptQueueWaitTime = TimeSpan.FromMilliseconds(2), ReceiveBufferLength = 512, SendBufferLength = 512, ConnectionsBacklogLength = 4096, UseFastLoopback = true, UseNagleAlgorithm = false }; // 1 initialize server TcpWorker server; try { server = new TcpWorker(serverSettings, ServeSocket); } catch (TcpServerException e) { Console.WriteLine($"Error activating server. Kernel error code: {e.KErrorCode}. Winsock error code: {e.WErrorCode}."); //Console.WriteLine($"Error activating server."); return; } Console.WriteLine($"server is activated"); /* * // 2 try accept connection * var tryAccept = server.TryAccept(); * * if (!tryAccept.Success) * { * Console.WriteLine($"error on accept. Kernel error code: {tryAccept.KernelErrorCode}. Winsock error code: {tryAccept.WinsockErrorCode}"); * } * * Console.WriteLine($"accept success"); */ Console.WriteLine("any key to exit"); Console.ReadLine(); Console.ReadLine(); }
/// <summary> /// Deactivates server. /// </summary> public Boolean Deactivate() { // check if is not active if (!IsActive) { return(false); } server.Deactivate(); server = null; return(true); }
/// <summary> /// Activates server. /// </summary> public Boolean Activate() { // 0 check if is active if (IsActive) { return(false); } var tryInitializeServer = TcpWorker.TryInitialize(Settings); if (!tryInitializeServer.Success) { return(false); } server = tryInitializeServer.Result; IsActive = true; return(true); }
/// <summary> /// Activates server. /// </summary> public Boolean Activate() { // 0 check if is active if (IsActive) { return false; } var tryInitializeServer = TcpWorker.TryInitialize(Settings); if (!tryInitializeServer.Success) { return false; } server = tryInitializeServer.Result; IsActive = true; return true; }
/// <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); }
/// <summary> /// Deactivates server. /// </summary> public Boolean Deactivate() { // check if is not active if (!IsActive) { return false; } server.Deactivate(); server = null; return true; }
/// <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)); }