async void AcceptConnections( ITcpListener tcpListener, ITcpClientHandler eventHandler, ILogger logger) { if (tcpListener == null) { throw new ArgumentNullException(nameof(tcpListener)); } while (false == IsDisposed()) { try { eventHandler.Handle( await tcpListener.AcceptTcpClientAsync() ); } catch (ObjectDisposedException ex) { if (ex.ObjectName != "System.Net.Sockets.Socket") { logger.Error(ex); } } catch (Exception ex) { logger.Error(ex); } } }
/// <summary> /// ImageServiceSrv constructor. /// </summary> /// <param name="port">port num</param> /// <param name="logging">ILoggingService obj</param> /// <param name="ch">IClientHandler obj</param> public ServerTcp(int port, ILoggingService logging, ITcpClientHandler ch) { this.Port = port; this.Logging = logging; this.Ch = ch; ClientHandler.Mutex = m_mutex; }
public Server( ITcpListenerFactory tcpListenerFactory, ITcpClientHandler tcpClientConnectedEventHandler) { _tcpListenerFactory = tcpListenerFactory ?? throw new ArgumentNullException(nameof(tcpListenerFactory)); _tcpClientConnectedEventHandler = tcpClientConnectedEventHandler ?? throw new ArgumentNullException(nameof(tcpClientConnectedEventHandler)); }
/// <summary> /// Starts this instance. /// </summary> public void Start() { IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), _port); _listener = new TcpListener(ep); _listener.Start(); _loggingService.Log("Waiting for connections", EventLogEntryType.Information); Task.Run(() => { while (true) { try { // Listens for new clients. TcpClient client = _listener.AcceptTcpClient(); _loggingService.Log("Got new connection", EventLogEntryType.Information); ITcpClientHandler ch = _clientHandlerFactory.Create(client, _loggingService); NewClientConnected?.Invoke(this, new NewClientConnectedEventArgs { ClientHandler = ch }); _clientHandlersList.Add(ch); ch.GuiClientClosed += (sender, args) => _clientHandlersList.Remove((ITcpClientHandler)sender); ch.HandleClient(); } catch (SocketException) { break; } } _loggingService.Log("Server stopped", EventLogEntryType.Information); }); }