protected override string InnerHandleRead(byte[] buffer, int startIdx, int maxCount, out int didCount, ref ActionResultEnum readResult) { didCount = 0; if (dataSP == null) { return("InnerHandleRead failed: socket is null"); } try { SocketError sockError; bool pollRead = dataSP.Poll(0, SelectMode.SelectRead); didCount = dataSP.Receive(buffer, startIdx, maxCount, SocketFlags.None, out sockError); if (didCount == 0 && maxCount > 0 && sockError == SocketError.Success && pollRead) { // the remote socket port has been shutdown - report this to the caller and flag it on the port readResult = ActionResultEnum.ReadRemoteEndHasBeenClosed; } if (lastReadResult != readResult) { lastReadResult = readResult; } if (sockError != SocketError.Success && sockError != SocketError.WouldBlock) { string faultCode = "Socket.Receive failed with error:" + sockError.ToString(); if (!dataSP.Connected) { DisposeDataSocket(false); SetBaseState(ConnState.ConnectionFailed, faultCode + " [Error closed socket]", true); } else if (IsPerminantSocketFailure(sockError)) { DisposeDataSocket(false); SetBaseState(ConnState.ConnectionFailed, faultCode + " [Perminant Error occured on open socket]", true); } // else we assume that the connection might still work. return(faultCode); } return(string.Empty); } catch (System.Exception ex) { return(ex.ToString(ExceptionFormat.TypeAndMessage)); } }
protected override string InnerHandleWrite(byte[] buffer, int startIdx, int count, out int didCount, ref ActionResultEnum writeResult) { didCount = 0; if (!InnerIsConnected) return "InnerHandleWrite failed: serial port is null or is not open"; try { int maxCount = Math.Min(count, InnerWriteSpaceAvailable); sp.Write(buffer, startIdx, maxCount); didCount = maxCount; return string.Empty; } catch (System.Exception ex) { return "Exception:" + ex.Message; } }
protected override string InnerHandleRead(byte[] buffer, int startIdx, int maxCount, out int didCount, ref ActionResultEnum readResult) { didCount = 0; if (!InnerIsConnected) return "InnerHandleRead failed: serial port is null or is not open"; try { didCount = sp.Read(buffer, startIdx, maxCount); return string.Empty; } catch (System.TimeoutException e) { Log.Trace.Emit("TimeoutException:{0}", e); return string.Empty; } catch (System.Exception ex) { return "Exception:" + ex.Message; } }
protected override string InnerHandleWrite(byte[] buffer, int startIdx, int count, out int didCount, ref ActionResultEnum writeResult) { didCount = 0; if (!InnerIsConnected) { return("InnerHandleWrite failed: serial port is null or is not open"); } try { int maxCount = Math.Min(count, InnerWriteSpaceAvailable); sp.Write(buffer, startIdx, maxCount); didCount = maxCount; return(string.Empty); } catch (System.Exception ex) { return(ex.ToString(ExceptionFormat.TypeAndMessage)); } }
protected override string InnerHandleRead(byte[] buffer, int startIdx, int maxCount, out int didCount, ref ActionResultEnum readResult) { didCount = 0; if (!InnerIsConnected) { return("InnerHandleRead failed: serial port is null or is not open"); } try { didCount = sp.Read(buffer, startIdx, maxCount); return(string.Empty); } catch (System.TimeoutException e) { Log.Trace.Emit("TimeoutException:{0}", e); return(string.Empty); } catch (System.Exception ex) { return(ex.ToString(ExceptionFormat.TypeAndMessage)); } }
protected override string InnerHandleWrite(byte[] buffer, int startIdx, int count, out int didCount, ref ActionResultEnum writeResult) { return(InnerHandleWrite(buffer, startIdx, count, out didCount, ref writeResult, targetEPConfig.IPEndPoint)); }
protected override string InnerHandleWrite(byte[] buffer, int startIdx, int count, out int didCount, ref ActionResultEnum writeResult) { didCount = 0; if (dataSP == null) return "InnerHandleWrite failed: socket is null"; try { SocketError sockError; didCount = dataSP.Send(buffer, startIdx, count, SocketFlags.None, out sockError); if (sockError != SocketError.Success && sockError != SocketError.WouldBlock) { string faultCode = "Socket.Send failed with error:" + sockError.ToString(); if (!dataSP.Connected) { DisposeDataSocket(false); SetBaseState(ConnState.ConnectionFailed, faultCode + " [Error closed socket]", true); } else if (IsPerminantSocketFailure(sockError)) { DisposeDataSocket(false); SetBaseState(ConnState.ConnectionFailed, faultCode + " [Perminant Error occured on open socket]", true); } // else we assume that the connection might still work. return faultCode; } return string.Empty; } catch (System.Exception ex) { return "Exception:" + ex.Message; } }
protected void DisposeDataSocket(bool clearLastReadResult) { if (dataSP != null) SelectSocketMonitor.Instance.RemoveSocketFromList(dataSP); MosaicLib.Utils.Fcns.DisposeOfObject(ref dataSP); if (clearLastReadResult) lastReadResult = ActionResultEnum.None; }
protected override string InnerHandleWrite(byte[] buffer, int startIdx, int count, out int didCount, ref ActionResultEnum writeResult) { return InnerHandleWrite(buffer, startIdx, count, out didCount, ref writeResult, targetEPConfig.IPEndPoint); }
protected string InnerHandleRead(byte[] buffer, int startIdx, int maxCount, out int didCount, ref ActionResultEnum readResult, ref EndPoint remoteEP) { didCount = 0; if (dataSP == null) return "InnerHandleRead failed: socket is null"; try { didCount = dataSP.ReceiveFrom(buffer, startIdx, maxCount, SocketFlags.None, ref remoteEP); return string.Empty; } catch (System.Exception ex) { return "Exception:" + ex.Message; } }
protected override string InnerHandleRead(byte[] buffer, int startIdx, int maxCount, out int didCount, ref ActionResultEnum readResult) { EndPoint fromEP; string ec = string.Empty; for (;;) { fromEP = targetEPConfig.IPEndPoint; ec = InnerHandleRead(buffer, startIdx, maxCount, out didCount, ref readResult, ref fromEP); if (fromEP != targetEPConfig.IPEndPoint && didCount != 0) continue; return ec; } }
protected override string InnerHandleWrite(byte[] buffer, int startIdx, int count, out int didCount, ref ActionResultEnum writeResult) { if (connectedEP != epNone) { return(InnerHandleWrite(buffer, startIdx, count, out didCount, ref writeResult, connectedEP)); } else { didCount = 0; return("InnerHandleWrite failed: there is no target endpoint to send this data to"); } }
protected override string InnerHandleRead(byte [] buffer, int startIdx, int maxCount, out int didCount, ref ActionResultEnum readResult) { EndPoint entryEP = connectedEP; string ec = string.Empty; ec = InnerHandleRead(buffer, startIdx, maxCount, out didCount, ref readResult, ref connectedEP); if (entryEP != connectedEP && connectedEP != epNone) { string reason = Utils.Fcns.CheckedFormat("Received data from new EndPoint:{0}, disconnected from old EndPoint:{1}", connectedEP.ToString(), entryEP.ToString()); SetBaseState(ConnState.Connected, reason, true); } return(ec); }
protected string InnerHandleWrite(byte[] buffer, int startIdx, int count, out int didCount, ref ActionResultEnum writeResult, EndPoint remoteEP) { didCount = 0; if (dataSP == null) { return("InnerHandleWrite failed: socket is null"); } try { didCount = dataSP.SendTo(buffer, startIdx, count, SocketFlags.None, remoteEP); return(string.Empty); } catch (System.Exception ex) { return(ex.ToString(ExceptionFormat.TypeAndMessage)); } }
protected string InnerHandleRead(byte[] buffer, int startIdx, int maxCount, out int didCount, ref ActionResultEnum readResult, ref EndPoint remoteEP) { didCount = 0; if (dataSP == null) { return("InnerHandleRead failed: socket is null"); } try { didCount = dataSP.ReceiveFrom(buffer, startIdx, maxCount, SocketFlags.None, ref remoteEP); return(string.Empty); } catch (System.Exception ex) { return(ex.ToString(ExceptionFormat.TypeAndMessage)); } }
protected string InnerHandleWrite(byte[] buffer, int startIdx, int count, out int didCount, ref ActionResultEnum writeResult, EndPoint remoteEP) { didCount = 0; if (dataSP == null) return "InnerHandleWrite failed: socket is null"; try { didCount = dataSP.SendTo(buffer, startIdx, count, SocketFlags.None, remoteEP); return string.Empty; } catch (System.Exception ex) { return "Exception:" + ex.Message; } }
protected override string InnerHandleWrite(byte[] buffer, int startIdx, int count, out int didCount, ref ActionResultEnum writeResult) { didCount = 0; if (dataSP == null) { return("InnerHandleWrite failed: socket is null"); } try { SocketError sockError; didCount = dataSP.Send(buffer, startIdx, count, SocketFlags.None, out sockError); if (sockError != SocketError.Success && sockError != SocketError.WouldBlock) { string faultCode = "Socket.Send failed with error:" + sockError.ToString(); if (!dataSP.Connected) { DisposeDataSocket(false); SetBaseState(ConnState.ConnectionFailed, faultCode + " [Error closed socket]", true); } else if (IsPerminantSocketFailure(sockError)) { DisposeDataSocket(false); SetBaseState(ConnState.ConnectionFailed, faultCode + " [Perminant Error occured on open socket]", true); } // else we assume that the connection might still work. return(faultCode); } return(string.Empty); } catch (System.Exception ex) { return(ex.ToString(ExceptionFormat.TypeAndMessage)); } }
protected override string InnerHandleRead(byte [] buffer, int startIdx, int maxCount, out int didCount, ref ActionResultEnum readResult) { EndPoint entryEP = connectedEP; string ec = string.Empty; ec = InnerHandleRead(buffer, startIdx, maxCount, out didCount, ref readResult, ref connectedEP); if (entryEP != connectedEP && connectedEP != epNone) { string reason = Utils.Fcns.CheckedFormat("Received data from new EndPoint:{0}, disconnected from old EndPoint:{1}", connectedEP.ToString(), entryEP.ToString()); SetBaseState(ConnState.Connected, reason, true); } return ec; }
protected override string InnerHandleRead(byte[] buffer, int startIdx, int maxCount, out int didCount, ref ActionResultEnum readResult) { didCount = 0; if (dataSP == null) return "InnerHandleRead failed: socket is null"; try { SocketError sockError; bool pollRead = dataSP.Poll(0, SelectMode.SelectRead); didCount = dataSP.Receive(buffer, startIdx, maxCount, SocketFlags.None, out sockError); if (didCount == 0 && maxCount > 0 && sockError == SocketError.Success && pollRead) { // the remote socket port has been shutdown - report this to the caller and flag it on the port readResult = ActionResultEnum.ReadRemoteEndHasBeenClosed; } if (lastReadResult != readResult) lastReadResult = readResult; if (sockError != SocketError.Success && sockError != SocketError.WouldBlock) { string faultCode = "Socket.Receive failed with error:" + sockError.ToString(); if (!dataSP.Connected) { DisposeDataSocket(false); SetBaseState(ConnState.ConnectionFailed, faultCode + " [Error closed socket]", true); } else if (IsPerminantSocketFailure(sockError)) { DisposeDataSocket(false); SetBaseState(ConnState.ConnectionFailed, faultCode + " [Perminant Error occured on open socket]", true); } // else we assume that the connection might still work. return faultCode; } return string.Empty; } catch (System.Exception ex) { return "Exception:" + ex.Message; } }
protected override string InnerHandleWrite(byte[] buffer, int startIdx, int count, out int didCount, ref ActionResultEnum writeResult) { if (connectedEP != epNone) return InnerHandleWrite(buffer, startIdx, count, out didCount, ref writeResult, connectedEP); else { didCount = 0; return "InnerHandleWrite failed: there is no target endpoint to send this data to"; } }
protected void UseDataSocket(Socket s) { dataSP = s; dataSP.LingerState = new LingerOption(false, 0); dataSP.NoDelay = true; dataSP.Blocking = false; if (PortConfig.RxBufferSize != 0) dataSP.ReceiveBufferSize = (int) PortConfig.RxBufferSize; if (PortConfig.TxBufferSize != 0) dataSP.SendBufferSize = (int) PortConfig.TxBufferSize; dataSP.ReceiveTimeout = 0; // read operations are non-blocking at this level. dataSP.SendTimeout = 0; // write operations are non-blocking at this level. lastReadResult = ActionResultEnum.None; SelectSocketMonitor.Instance.AddSocketToList(dataSP, true, false, true, threadWakeupNotifier); }
protected override string InnerHandleRead(byte[] buffer, int startIdx, int maxCount, out int didCount, ref ActionResultEnum readResult) { EndPoint fromEP; string ec = string.Empty; for (;;) { fromEP = targetEPConfig.IPEndPoint; ec = InnerHandleRead(buffer, startIdx, maxCount, out didCount, ref readResult, ref fromEP); if (fromEP != targetEPConfig.IPEndPoint && didCount != 0) { continue; } return(ec); } }