private void SocketListener() { Log.Debug("SocketListener started"); if (networkSettings == null) { Log.Error("Network settings has not been initialized"); throw new NullReferenceException(); } while (!cancellationTokenSource.IsCancellationRequested) { var endPoint = new IPEndPoint(IPAddress.Any, networkSettings.Port); using (var listener = new NetworkClientWrapper(networkSettings.Protocol, endPoint)) { while (!cancellationTokenSource.IsCancellationRequested) { var remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); listener.SetReceiveTimeout(1000); try { var bytes = listener.Receive(ref remoteEndPoint); Log.DebugFormat("Received {0} bytes from {1} ({2})", bytes.Length, remoteEndPoint.Address, networkSettings.Protocol); var message = Encoding.UTF8.GetString(bytes, 0, bytes.Length); lock (pendingQueue) { pendingQueue.Enqueue(message); } } catch (SocketException socketException) { if (socketException.SocketErrorCode != SocketError.TimedOut) { Log.Debug("SocketException", socketException); Log.DebugFormat( "SocketException.SocketErrorCode = {0}", socketException.SocketErrorCode); // Break out of the 'using socket' loop and try to establish a new socket. break; } } catch (Exception e) { Log.DebugFormat("Exception: {0}", e.Message); } } } } Log.Debug("SocketListener completed"); }
private void SocketListener() { Log.Debug("SocketListener started"); if (networkSettings == null) { Log.Error("Network settings has not been initialised"); throw new NullReferenceException(); } while (!cancellationTokenSource.IsCancellationRequested) { var endPoint = new IPEndPoint(IPAddress.Any, networkSettings.Port); using (var listener = new NetworkClientWrapper(networkSettings.Protocol, endPoint)) { while (!cancellationTokenSource.IsCancellationRequested) { var remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); listener.SetReceiveTimeout(1000); try { var bytes = listener.Receive(ref remoteEndPoint); Log.DebugFormat("Received {0} bytes from {1} ({2})", bytes.Length, remoteEndPoint.Address, networkSettings.Protocol); var message = Encoding.UTF8.GetString(bytes, 0, bytes.Length); lock (pendingQueue) { pendingQueue.Enqueue(message); } } catch (SocketException socketException) { if (socketException.SocketErrorCode != SocketError.TimedOut) { Log.Debug("SocketException", socketException); Log.DebugFormat( "SocketException.SocketErrorCode = {0}", socketException.SocketErrorCode); // Break out of the 'using socket' loop and try to establish a new socket. break; } } catch (Exception e) { Log.DebugFormat("Exception: {0}", e.Message); } } } } Log.Debug("SocketListener completed"); }