private void ListenAndRunAcceptLoop(ManualResetEventSlim listeningSignal) { Thread.CurrentThread.Name = "Server Listening Worker"; if (!Bind(_listeningSocket, _configuration.ListeningPort)) { return; } ListeningPort = GetSocketPort(); Listen(); listeningSignal.Set(); while (_isListening) { var acceptSocket = WinSock.accept(_listeningSocket, IntPtr.Zero, IntPtr.Zero); if (acceptSocket == (IntPtr)WinSock.Consts.INVALID_SOCKET) { var lastErrorCode = WinSock.WSAGetLastError(); if (lastErrorCode == WinSock.Consts.WSAEINTR) { break; } WinSock.ThrowLastWsaError(); continue; } var clientSession = _sessionManager.Acquire(); clientSession.Closed += OnClientSessionClosed; clientSession.MessageReceived += OnClientSessionMessageReceived; clientSession.Open(acceptSocket); clientSession.InitiateReceiving(); ClientConnected(clientSession.Id); } }
private void Listen() { Thread.CurrentThread.Name = "Server Listening Worker"; if (!Bind(_listeningSocket, _configuration.ListeningPort)) { return; } if (WinSock.listen(_listeningSocket, WinSock.Consts.SOMAXCONN) == WinSock.Consts.SOCKET_ERROR) { return; } while (_isListening) { var acceptSocket = WinSock.accept(_listeningSocket, IntPtr.Zero, IntPtr.Zero); if (acceptSocket == (IntPtr)WinSock.Consts.INVALID_SOCKET) { var lastErrorCode = WinSock.WSAGetLastError(); if (lastErrorCode == WinSock.Consts.WSAEINTR) { break; } WinSock.ThrowLastWsaError(); continue; } var clientSession = _sessionManager.Acquire(); clientSession.Closed += OnClientSessionClosed; clientSession.MessageReceived += OnClientSessionMessageReceived; clientSession.Open(acceptSocket); clientSession.InitiateReceiving(); ClientConnected(clientSession.Id); } }