public TelnetLogList ReadFromNetworkStream( int SleepTime = 0, int MaxTry = 0, string LogText = null, bool ForceRead = false) { this.EmptyArray(); var logList = new TelnetLogList(); logList.AddImportantItem(Direction.Read, "ReadFromNetworkStream. Enter method"); int tryCx = 0; if (this.NetStream != null) { while (true) { if ((NetStream.CanRead == true) && (NetStream.DataAvailable == true)) { var readBuffer = new byte[Client.ReceiveBufferSize]; int readLx = NetStream.Read(readBuffer, 0, Client.ReceiveBufferSize); this.LoadArray(readBuffer, readLx); { var bufText = readBuffer.ToHex(0, readLx, ' '); logList.AddChunk(Direction.Read, readBuffer.SubArray(0, readLx), LogText); } } else { this.EmptyArray(); } // got something. if (this.DataLgth > 0) { break; } // max number of read tries reached. tryCx += 1; if (tryCx >= MaxTry) { break; } // sleep before another read attempt. for (int cx = 0; cx < SleepTime; cx++) { Thread.Sleep(1); } logList.AddImportantItem(Direction.Read, "ReadFromNetworkStream. Sleep " + SleepTime); } } logList.AddImportantItem(Direction.Read, "ReadFromNetworkStream. Exit method. " + this.DataLgth); return(logList); }
public static void WriteToHost( TelnetLogList LogList, byte[] Buffer, NetworkStream NetStream) { if (NetStream.CanWrite == true) { if (LogList != null) { LogList.AddChunk(Direction.Write, Buffer); } NetStream.Write(Buffer, 0, Buffer.Length); TrafficLogFile.Write(Direction.Write, Buffer); } else { throw new Exception("cannot write to network stream"); } }