public int Read(ref byte[] buffer, int offset, int count, int sequence) { var currentConnectAttempts = 0; try { var masterStopWatch = Stopwatch.StartNew(); var startTime = DateTime.Now; var startTicks = startTime.Ticks; var bytesRead = new List <byte>(); var networkStream = TcpClient.GetStream(); var timeoutStopWatch = Stopwatch.StartNew(); while (ReadTimeout > timeoutStopWatch.ElapsedMilliseconds) { var byteValue = -1; try { byteValue = networkStream.ReadByte(); if (byteValue >= 0) { bytesRead.Add((byte)byteValue); } if (count == bytesRead.Count) { goto Finish; } } catch (System.IO.IOException toex) { masterStopWatch.Stop(); if (bytesRead.Count > 0) { Buffer.BlockCopy(bytesRead.ToArray(), 0, buffer, offset, bytesRead.Count); } if (TracingEnabled) { Trace.WriteLine(string.Format("Channel,{0},{1},{2},ChannelRead,{3},{4},{5},TCPChannel", Name, startTime.ToString("O"), (1000.0 * (((double)masterStopWatch.ElapsedTicks) * (1.0 / ((double)Stopwatch.Frequency)))), sequence, bytesRead.Count, HexConverter.ToHexString(bytesRead.ToArray()))); } throw new TimeoutException("Read Timeout", toex); } Thread.Sleep(0); } Finish: timeoutStopWatch.Stop(); masterStopWatch.Stop(); if (bytesRead.Count > 0) { Buffer.BlockCopy(bytesRead.ToArray(), 0, buffer, offset, bytesRead.Count); } if (TracingEnabled) { Trace.WriteLine(string.Format("Channel,{0},{1},{2},ChannelRead,{3},{4},{5},TCPChannel", Name, startTime.ToString("O"), (1000.0 * (((double)masterStopWatch.ElapsedTicks) * (1.0 / ((double)Stopwatch.Frequency)))), sequence, bytesRead.Count, HexConverter.ToHexString(bytesRead.ToArray()))); } if (count != bytesRead.Count) { throw new TimeoutException("Read Timeout"); } else { return(count); } } catch (Exception exception) { if (exception.InnerException is SocketException) { goto Connect; } else if (exception is InvalidOperationException) { goto Connect; } else { throw exception; } Connect: currentConnectAttempts++; try { TcpClient.Dispose(); TcpClient = new TcpClient(); TcpClient.Connect(HostName, HostPort); ReadTimeout = TcpClientReadTimeout; WriteTimeout = TcpClientWriteTimeout; throw new IOException("Recconected to host"); } catch (SocketException) { if (currentConnectAttempts > ConnectionAttempts) { throw exception; } else { System.Threading.Thread.Sleep(ConnectionRetryDelay); goto Connect; } } } }
public int Read(ref byte[] buffer, int offset, int count, int sequence) { var masterStopWatch = Stopwatch.StartNew(); var startTime = DateTime.Now; var bytesRead = new List <byte>(); var numberOfActualBitsPerByte = (1 + (SerialPort.Parity == Parity.None ? 0 : 1) + SerialPort.DataBits + (SerialPort.StopBits == StopBits.None ? 0 : SerialPort.StopBits == StopBits.One ? 1 : SerialPort.StopBits == StopBits.OnePointFive ? 1.5 : 2)); var receiveTimeMilliseconds = ((count * numberOfActualBitsPerByte) / SerialPort.BaudRate) * 1000; TimedThreadBlocker.Wait((int)receiveTimeMilliseconds); var timeoutStopWatch = Stopwatch.StartNew(); while (ReadTimeout > timeoutStopWatch.ElapsedMilliseconds) { var byteValue = -1; try { byteValue = SerialPort.ReadByte(); if (byteValue >= 0) { bytesRead.Add((byte)byteValue); } if (count == bytesRead.Count) { goto Finish; } } catch (TimeoutException toex) { masterStopWatch.Stop(); if (bytesRead.Count > 0) { Buffer.BlockCopy(bytesRead.ToArray(), 0, buffer, offset, bytesRead.Count); } if (TracingEnabled) { Trace.WriteLine(string.Format("Channel,{0},{1},{2},ChannelRead,{3},{4},{5},SerialPortChannel", Name, startTime.ToString("O"), (1000.0 * (((double)masterStopWatch.ElapsedTicks) * (1.0 / ((double)Stopwatch.Frequency)))), sequence, bytesRead.Count, HexConverter.ToHexString(bytesRead.ToArray()))); } throw toex; } Thread.Sleep(0); } Finish: timeoutStopWatch.Stop(); masterStopWatch.Stop(); if (bytesRead.Count > 0) { Buffer.BlockCopy(bytesRead.ToArray(), 0, buffer, offset, bytesRead.Count); } if (TracingEnabled) { Trace.WriteLine(string.Format("Channel,{0},{1},{2},ChannelRead,{3},{4},{5},SerialPortChannel", Name, startTime.ToString("O"), (1000.0 * (((double)masterStopWatch.ElapsedTicks) * (1.0 / ((double)Stopwatch.Frequency)))), sequence, bytesRead.Count, HexConverter.ToHexString(bytesRead.ToArray()))); } if (count != bytesRead.Count) { throw new TimeoutException("Read Timeout"); } else { return(count); } }
public void Write(ref byte[] buffer, int offset, int count) { var currentConnectAttempts = 0; try { var masterStopWatch = Stopwatch.StartNew(); var startTime = DateTime.Now; var bufferLength = buffer.Length; var writeBuffer = new byte[bufferLength]; Buffer.BlockCopy(buffer, 0, writeBuffer, 0, bufferLength); TcpClient.GetStream().Write(writeBuffer, offset, count); masterStopWatch.Stop(); if (TracingEnabled) { Trace.WriteLine(string.Format("Channel,{0},{1},{2},ChannelWrite,{3},{4},{5},TCPChannel", Name, startTime.ToString("O"), (1000.0 * (((double)masterStopWatch.ElapsedTicks) * (1.0 / ((double)Stopwatch.Frequency)))), 0, count, HexConverter.ToHexString(buffer))); } } catch (Exception exception) { if (exception.InnerException is SocketException) { goto Connect; } else if (exception is InvalidOperationException) { goto Connect; } else { throw exception; } Connect: currentConnectAttempts++; try { TcpClient.Dispose(); TcpClient = new TcpClient(); TcpClient.Connect(HostName, HostPort); ReadTimeout = TcpClientReadTimeout; WriteTimeout = TcpClientWriteTimeout; throw new IOException("Recconected to host"); } catch (SocketException) { if (currentConnectAttempts > ConnectionAttempts) { throw exception; } else { System.Threading.Thread.Sleep(ConnectionRetryDelay); goto Connect; } } } }
public void Write(ref byte[] buffer, int offset, int count) { var masterStopWatch = Stopwatch.StartNew(); var startTime = DateTime.Now; var numberOfActualBitsPerByte = (1 + (SerialPort.Parity == Parity.None ? 0 : 1) + SerialPort.DataBits + (SerialPort.StopBits == StopBits.None ? 0 : SerialPort.StopBits == StopBits.One ? 1 : SerialPort.StopBits == StopBits.OnePointFive ? 1.5 : 2)); var transmitTimeMilliseconds = ((count * numberOfActualBitsPerByte) / SerialPort.BaudRate) * 1000; var bufferLength = buffer.Length; var writeBuffer = new byte[bufferLength]; Buffer.BlockCopy(buffer, 0, writeBuffer, 0, bufferLength); SerialPort.BaseStream.Write(writeBuffer, offset, count); SerialPort.BaseStream.Flush(); while (SerialPort.BytesToWrite > 0) { Thread.Sleep(0); } TimedThreadBlocker.Wait((int)transmitTimeMilliseconds); masterStopWatch.Stop(); if (TracingEnabled) { Trace.WriteLine(string.Format("Channel,{0},{1},{2},ChannelWrite,{3},{4},{5},SerialPortChannel", Name, startTime.ToString("O"), (1000.0 * (((double)masterStopWatch.ElapsedTicks) * (1.0 / ((double)Stopwatch.Frequency)))), 0, count, HexConverter.ToHexString(buffer))); } }