public void Stop() { lock (SYNC) { Logger.Info("ServerSocket stop"); OnNewStatus?.Invoke(Status.Closing); if (_socket != null) { try { _socket.Close(); } catch (SocketException) { } } if (_thread != null) { if (_thread.IsAlive) { _thread.Interrupt(); _thread.Join(); } _thread = null; } _socket = null; OnNewStatus?.Invoke(Status.Closed); } }
public void Stop() { lock (_sync) { Logger.Info("SerialPortReader stop"); OnNewStatus?.Invoke(PortStatus.Closing); _tokenWatchdog.OnWatchdogTimeout -= TokenRemoved; Logger.Debug("Closing serial port"); if (_serialPort != null && _serialPort.IsOpen) { _serialPort.DataReceived -= ReadData; try { _serialPort.Close(); } catch (IOException) { } } _serialPort = null; } }
private void Loop() { Logger.Debug("Starting loop"); while (_socket.IsBound) { Socket newSocket; OnNewStatus?.Invoke(Status.Listening); try { newSocket = _socket.Accept(); } catch (Exception) { break; } OnNewStatus?.Invoke(Status.Accepting); var remoteAddress = newSocket.RemoteEndPoint.ToString(); Logger.Info($"New socket connection from {remoteAddress} accepted"); Logger.Debug("Preparing ClientSocket instance"); OnConnectionAccepted?.Invoke(newSocket); } }
protected void DoStatus(bool force) { OnNewStatus?.Invoke(force); if (IsDone && !IsDoneExecuted) { IsDoneExecuted = true; OnComplete?.Invoke(); } }
public void Start() { lock (_sync) { Logger.Info("SerialPortReader start"); Logger.Debug("Preparing serial port"); OnNewStatus?.Invoke(PortStatus.Preparing); _serialPort = new SerialPort(_config.SerialPort) { BaudRate = 9600, DataBits = 8, Parity = Parity.None, StopBits = StopBits.One, Handshake = Handshake.None, ReadTimeout = SerialPort.InfiniteTimeout }; Logger.Debug("Opening serial port"); try { _serialPort.Open(); } catch (Exception e) { Logger.Error(e.Message); OnNewStatus?.Invoke(PortStatus.Closed); return; } OnNewStatus?.Invoke(PortStatus.Open); _serialPort.DataReceived += ReadData; _tokenWatchdog.OnWatchdogTimeout += TokenRemoved; } }
public void Start() { lock (SYNC) { Logger.Info("ServerSocket start"); if (_socket != null) { Logger.Error("Already running"); return; } Logger.Debug("Preparing socket"); OnNewStatus?.Invoke(Status.Preparing); _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _socket.Bind(new IPEndPoint(IPAddress.Any, SocketPort)); try { Logger.Debug("Socket Listen"); _socket.Listen(SocketBacklog); } catch (SocketException e) { Logger.Error(e.Message); return; } OnNewStatus?.Invoke(Status.Listening); Logger.Debug("Preparing main loop thread"); _thread = new Thread(Loop); _thread.Start(); } }
/// <summary> /// Display a status message to the user /// </summary> private void NewStatus(string statusMessage) { Console.WriteLine(statusMessage); OnNewStatus?.Invoke(statusMessage); }