Exemplo n.º 1
0
        /// <summary>
        /// write buffer contents to log file.
        /// </summary>
        /// <param name="Direction"></param>
        /// <param name="Buffer"></param>
        /// <param name="Length"></param>
        public void Write(string Direction, byte[] Buffer, int Length)
        {
            var lines = new List <string>();

            this.Counter += 1;

            var inputArray = new InputByteArray(Buffer, Length);

            while (inputArray.IsEof() == false)
            {
                // peek to see if the current byte is a telnet command.
                var escapeCmd = inputArray.PeekNextByte().ToTelnetCommandCode();

                // peek to see if the current bytes are an IBM5250 datastream header.
                DataStreamHeader dsh    = null;
                string           errmsg = null;
                {
                    var rv = DataStreamHeader.Factory(inputArray);
                    dsh    = rv.Item1;
                    errmsg = rv.Item2;
                }

                // current bytes are an ibm5250 datastream header. ( page 2-4 of rfc 1205 )
                // store this header as the "current datastream header". When there is a
                // current data stream header the code will match the current bytes against
                // ibm5250 data stream commands.
                if (errmsg == null)
                {
                    lines.Add(
                        Direction + " " + this.Counter.ToString() + " "
                        + dsh.ToString());

                    this.CurrentDataStreamHeader = dsh;

                    continue;
                }

                // currently in a 5250 data stream header.
                if (this.CurrentDataStreamHeader != null)
                {
                    var dataStreamCommand = WorkstationCommandBase.ParseFactory(inputArray);
                    if (dataStreamCommand != null)
                    {
                        lines.Add(
                            Direction + " " + this.Counter.ToString() + " "
                            + dataStreamCommand.ToString());

                        // all the data stream command ToString methods provide enough info.
                        // But for the WriteToDisplayCommand, list out the ToString contents of
                        // the WtdOrders of that command.
                        if (dataStreamCommand is WriteToDisplayCommand)
                        {
                            var wtdCommand = dataStreamCommand as WriteToDisplayCommand;
                            foreach (var order in wtdCommand.OrderList)
                            {
                                lines.Add(order.ToString());
                            }
                        }

                        continue;
                    }
                }

                {
                    var stmt = TelnetCommand.Factory(inputArray);
                    if (stmt != null)
                    {
                        lines.Add(
                            Direction + " " + this.Counter.ToString() + " " + stmt.ToString());
                        continue;
                    }
                }

                if ((escapeCmd != null) && (escapeCmd.Value == CommandCode.ESCAPE))
                {
                    var rv     = TerminalVt100Statement.Factory(inputArray);
                    var vtStmt = rv.Item1;
                    var otStmt = rv.Item2;

                    lines.Add(
                        Direction + " " + this.Counter.ToString() + " " + vtStmt.ToString());

                    if (otStmt != null)
                    {
                        lines.Add(
                            Direction + " " + this.Counter.ToString() + " " + otStmt.ToString());
                    }

                    continue;
                }

                {
                    var remBytes = inputArray.GetBytesToEnd();
                    lines.Add(Direction + " " + this.Counter.ToString()
                              + " Raw bytes follow:");
                    LogFile_WriteRawBytes(remBytes, lines);
                }
            }
            WriteTextLines(lines);
        }
        public static Tuple <int, int, List <ShowItemBase>, TelnetLogList> GetAndProcessStream(
            TcpClient Client, NetworkStream NetStream,
            NetworkStreamBackedInputByteArray InputArray,
            NegotiateSettings NegotiateSettings,
            bool ForceRead = false)
        {
            int  sendStmtCx   = 0;
            int  getStmtCx    = 0;
            var  logList      = new TelnetLogList();
            var  showItemList = new ShowItemList();
            bool exitLoop     = false;

            while (exitLoop == false)
            {
                // read available bytes from input stream.
                if (InputArray.IsEof())
                {
                    var log = InputArray.ReadFromNetworkStream(5, 5, ForceRead);
                    logList.AddItems(log);
                }

                // no input data to process.
                if (InputArray.IsEof())
                {
                    break;
                }

                DataStreamHeader currentDataStreamHeader = null;
                while (true)
                {
                    if (InputArray.IsEof() == true)
                    {
                        break;
                    }

                    // process the input as 5250 data stream commands.
                    if (currentDataStreamHeader != null)
                    {
                        var rv = ParseAndProcessWorkstationCommand(InputArray, currentDataStreamHeader);
                        var workstationCommand = rv.Item1;
                        showItemList            = rv.Item2;
                        currentDataStreamHeader = rv.Item3;
                        logList.AddItems(rv.Item4);
                        continue;
                    }

                    // peek and process input bytes as a telnet stmt. ( starts with IAC )
                    var telCmd = InputArray.NextTelnetCommand();
                    if (telCmd != null)
                    {
                        getStmtCx += 1;

                        var rv         = TelnetConnection.ProcessTelnetCommand(telCmd, NegotiateSettings);
                        var cx         = rv.Item1;
                        var writeBytes = rv.Item2;
                        logList.AddItems(rv.Item3);
                        sendStmtCx += cx;
                        WriteToHost(logList, writeBytes, NetStream);

                        continue;
                    }

                    var dsh = new DataStreamHeader(InputArray);
                    if ((dsh != null) && (dsh.Errmsg == null))
                    {
                        logList.AddItem(Direction.Read, dsh.ToString());
                        currentDataStreamHeader = dsh;
                        continue;
                    }
                    exitLoop = true;
                    break;
                }
            }
            return(new Tuple <int, int, List <ShowItemBase>, TelnetLogList>(
                       getStmtCx, sendStmtCx, showItemList, logList));
        }