/// <summary> /// Catches all exceptions thrown when action is executed and removes the master end point. /// The exception is ignored when it simply signals a master closing its connection. /// </summary> internal void CatchExceptionAndRemoveMasterEndPoint(Action action, string endPoint) { if (action == null) throw new ArgumentNullException("action"); if (endPoint == null) throw new ArgumentNullException("endPoint"); if (endPoint.IsNullOrEmpty()) throw new ArgumentException("Argument endPoint cannot be empty."); try { action.Invoke(); } catch (IOException ioe) { _log.DebugFormat("IOException encountered in ReadHeaderCompleted - {0}", ioe.Message); ModbusMasterTcpConnectionClosed.Raise(this, new TcpConnectionEventArgs(EndPoint)); SocketException socketException = ioe.InnerException as SocketException; if (socketException != null && socketException.ErrorCode == Modbus.ConnectionResetByPeer) { _log.Debug("Socket Exception ConnectionResetByPeer, Master closed connection."); return; } throw; } catch (Exception e) { _log.Error("Unexpected exception encountered", e); throw; } }
/// <summary> /// Catches all exceptions thrown when action is executed and removes the master end point. /// The exception is ignored when it simply signals a master closing its connection. /// </summary> internal void CatchExceptionAndRemoveMasterEndPoint(Action action, string endPoint) { try { if (action == null) { throw new ArgumentNullException("action"); } if (endPoint == null) { throw new ArgumentNullException("endPoint"); } if (endPoint.IsNullOrEmpty()) { throw new ArgumentException("Argument endPoint cannot be empty."); } try { action.Invoke(); } catch (IOException ioe) { _log.DebugFormat("IOException encountered in ReadHeaderCompleted - {0}", ioe.Message); ModbusMasterTcpConnectionClosed.Raise(this, new TcpConnectionEventArgs(EndPoint)); SocketException socketException = ioe.InnerException as SocketException; if (socketException != null && socketException.ErrorCode == Modbus.ConnectionResetByPeer) { _log.Debug("Socket Exception ConnectionResetByPeer, Master closed connection."); return; } Console.WriteLine("Caught some unknown exception but not throwing it: " + ioe.Message); throw; } catch (ArgumentException e) { //We don't have handle this but we don't want to crash if (e.Message.Contains("Unsupported function code")) { Console.WriteLine(e.Message); } } catch (Exception e) { _log.Error("Unexpected exception encountered", e); Console.WriteLine("Caught some unknown exception but not throwing it: " + e.Message); throw; } } catch (Exception err) { Console.WriteLine("Caught Some Exception not thowing " + err.Message); } }
internal void ReadHeaderCompleted(IAsyncResult ar) { Debug.WriteLine("Read header completed."); CatchExceptionAndRemoveMasterEndPoint(() => { // this is the normal way a master closes its connection if (Stream.EndRead(ar) == 0) { Debug.WriteLine("0 bytes read, Master has closed Socket connection."); ModbusMasterTcpConnectionClosed.Raise(this, new TcpConnectionEventArgs(EndPoint)); return; } Debug.WriteLine("MBAP header: {0}", string.Join(", ", _mbapHeader)); ushort frameLength = (ushort)IPAddress.HostToNetworkOrder(BitConverter.ToInt16(_mbapHeader, 4)); Debug.WriteLine("{0} bytes in PDU.", frameLength); _messageFrame = new byte[frameLength]; Stream.BeginRead(_messageFrame, 0, frameLength, ReadFrameCompleted, null); }, EndPoint); }
/// <summary> /// Catches all exceptions thrown when action is executed and removes the master end point. /// The exception is ignored when it simply signals a master closing its connection. /// </summary> internal void CatchExceptionAndRemoveMasterEndPoint(Action action, string endPoint) { if (action == null) { throw new ArgumentNullException("action"); } if (endPoint == null) { throw new ArgumentNullException("endPoint"); } if (endPoint == string.Empty) { throw new ArgumentException(Resources.EmptyEndPoint); } try { action.Invoke(); } catch (IOException ioe) { Debug.WriteLine("IOException encountered in ReadHeaderCompleted - {0}", ioe.Message); ModbusMasterTcpConnectionClosed.Raise(this, new TcpConnectionEventArgs(EndPoint)); SocketException socketException = ioe.InnerException as SocketException; if (socketException != null && socketException.ErrorCode == Modbus.ConnectionResetByPeer) { Debug.WriteLine("Socket Exception ConnectionResetByPeer, Master closed connection."); return; } throw; } catch (Exception e) { Debug.WriteLine("Unexpected exception encountered: [{0}] {1}", e.GetType().Name, e.Message); throw; } }
internal void ReadHeaderCompleted(IAsyncResult ar) { Log.Debug("XmlSettingsReadFile header completed."); CatchExceptionAndRemoveMasterEndPoint(() => { // this is the normal way a master closes its connection if (Stream.EndRead(ar) == 0) { Log.Debug("0 bytes read, Master has closed Socket connection."); ModbusMasterTcpConnectionClosed.Raise(this, new TcpConnectionEventArgs(EndPoint)); return; } Log.Debug($"MBAP header: {_mbapHeader.JoinByCommas()}"); ushort frameLength = (ushort)IPAddress.HostToNetworkOrder(BitConverter.ToInt16(_mbapHeader, 4)); Log.Debug($"{frameLength} bytes in PDU."); _messageFrame = new byte[frameLength]; Stream.BeginRead(_messageFrame, 0, frameLength, ReadFrameCompleted, null); }, EndPoint); }