예제 #1
0
        TelnetConnectAndNegotiate(
            string Host, NegotiateSettings NegotiateSettings,
            ConcurrentMessageQueue TelnetQueue, ToThread ToThread)
        {
            var           sessionSettings = new SessionSettings();
            TelnetLogList logList         = null;
            bool          breakLoop       = false;

            // loop reading from NetworkStream and processing the telnet command.
            // loop until break flag is set.
            while (breakLoop == false)
            {
                var item = TelnetQueue.WaitAndPeek();
                if ((item is TelnetCommand) == false)
                {
                    break;
                }
                var telCmd = TelnetQueue.WaitAndDequeue() as TelnetCommand;

                byte[] responseBytes = null;
                {
                    var rv = ProcessTelnetCommand(telCmd, NegotiateSettings);
                    var cx = rv.Item1;
                    responseBytes = rv.Item2;
                }

                if ((responseBytes != null) && (responseBytes.Length > 0))
                {
                    var dataMessage = new SendDataMessage(responseBytes);
                    ToThread.PostInputMessage(dataMessage);
                }
            }
            return(new Tuple <SessionSettings, TelnetLogList>(
                       sessionSettings, logList));
        }
예제 #2
0
        public static Tuple <int, byte[], TelnetLogList> ProcessTelnetCommand(
            TelnetCommand TelnetCmd, NegotiateSettings NegotiateSettings,
            bool doLog = false)
        {
            TelnetLogList logList = null;

            if (doLog == true)
            {
                logList = new TelnetLogList();
            }
            int cx          = 0;
            var writeStream = new ByteArrayBuilder();

            // write to the run log.
            if (logList != null)
            {
                logList.AddItem(Direction.Read, TelnetCmd.ToString());
            }

            if (TelnetCmd.CmdCode == Enums.CommandCode.DO)
            {
                var replyStmt = TelnetCmd.BuildReply(NegotiateSettings);
                writeStream.Append(replyStmt.ToBytes());
            }

            else if (TelnetCmd is NewEnvironCommand)
            {
                var envStmt = TelnetCmd as NewEnvironCommand;
                cx = ProcessNewEnvironStatement(envStmt, writeStream, NegotiateSettings);
            }

            else if (TelnetCmd is TerminalTypeCommand)
            {
                var ttStmt = TelnetCmd as TerminalTypeCommand;
                cx = ProcessTerminalTypeStatement(ttStmt, writeStream, NegotiateSettings);
            }

            else if ((TelnetCmd is EndOfRecordStatement) || (TelnetCmd is TransmitBinaryCommand))
            {
                if (TelnetCmd.CmdCode == Enums.CommandCode.DO)
                {
                    var replyStmt = TelnetCmd.BuildReply(NegotiateSettings);
                    writeStream.Append(replyStmt.ToBytes());

                    if ((replyStmt.CmdCode == Enums.CommandCode.DO) &&
                        (replyStmt.Subject.IsEqual(TelnetSubject.TRANSMIT_BINARY)))
                    {
                    }
                }
                else if (TelnetCmd.CmdCode == Enums.CommandCode.WILL)
                {
                    var replyStmt = TelnetCmd.BuildDoReply();
                    writeStream.Append(replyStmt.ToBytes());
                }
            }
            return(new Tuple <int, byte[], TelnetLogList>(cx, writeStream.ToByteArray(), logList));
        }
예제 #3
0
 public void AddItems(TelnetLogList LogList)
 {
     if (LogList != null)
     {
         foreach (var item in LogList)
         {
             this.Add(item);
         }
     }
 }
예제 #4
0
        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);
        }
예제 #5
0
        TelnetPrinterConnectAndNegotiate(
            string Host, NegotiateSettings NegotiateSettings,
            ConcurrentMessageQueue TelnetQueue,
            ServerConnectPack ConnectPack, bool doLog = false)
        {
            var           sessionSettings = new SessionSettings();
            TelnetLogList logList         = null;

            // BgnTemp
            doLog = true;
            // EndTemp

            if (doLog == true)
            {
                logList = new TelnetLogList();
            }
            bool breakLoop = false;

            // loop reading from NetworkStream and processing the telnet command.
            // loop until break flag is set.
            while (breakLoop == false)
            {
                var item = TelnetQueue.WaitAndPeek();
                if ((item is TelnetCommand) == false)
                {
                    break;
                }
                var telCmd = TelnetQueue.WaitAndDequeue() as TelnetCommand;

                byte[] responseBytes = null;
                {
                    var rv = ProcessTelnetCommand(telCmd, NegotiateSettings, doLog);
                    var cx = rv.Item1;
                    responseBytes = rv.Item2;
                    if (doLog == true)
                    {
                        logList.AddItems(rv.Item3);
                    }
                }

                if ((responseBytes != null) && (responseBytes.Length > 0))
                {
                    WriteToHost(logList, responseBytes, ConnectPack.TcpClient.GetStream());
                }
            }
            return(new Tuple <SessionSettings, TelnetLogList>(
                       sessionSettings, logList));
        }
예제 #6
0
        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");
            }
        }
예제 #7
0
 private static void WriteToHost(
     TelnetLogList LogList, TelnetCommand Stmt, NetworkStream NetStream)
 {
     WriteToHost(LogList, Stmt.ToBytes(), NetStream);
 }
        ProcessWorkstationDataStream(this ConnectedSocketPack SocketPack)
        {
            WorkstationCommandList       workstationCmdList = null;
            List <WriteToDisplayCommand> wtdCmdList         = null;
            HowReadScreen?howRead = null;
            var           logList = new TelnetLogList();

            while (true)
            {
                // input array is eof. Exit loop if have read a READ workstn command.
                // Otherwise, read from server.
                if (SocketPack.InputArray.IsEof( ) == true)
                {
                    if (howRead != null)
                    {
                        break;
                    }
                    {
                        var log = SocketPack.InputArray.ReadFromNetworkStream(20, 30);
                        logList.AddItems(log);
                        if (SocketPack.InputArray.IsEof() == true)
                        {
                            break;
                        }
                    }
                }

                // peek at the input stream from server. Classify the data that is next
                // to receive.
                var typeData = ServerDataStream.PeekServerCommand(SocketPack.InputArray);

                // input data not recogizied. Not a 5250 data strem header.
                if (typeData == null)
                {
                    logList.AddItem(Direction.Read, "Unknown data stream data");
                    logList.AddItems(
                        Direction.Read, SocketPack.InputArray.PeekBytes().ToHexReport(16));
                    break;
                }

                if (typeData.Value == TypeServerData.workstationHeader)
                {
                    {
                        var rv = Process5250.GetAndParseWorkstationCommandList(
                            SocketPack.InputArray, SocketPack.Settings);

                        workstationCmdList = rv.Item1;
                        logList.AddItems(rv.Item2);
                    }

                    foreach (var workstationCmd in workstationCmdList)
                    {
                        if (workstationCmd is ClearUnitCommand)
                        {
                        }

                        else if (workstationCmd is WriteToDisplayCommand)
                        {
                            var wtdCommand = workstationCmd as WriteToDisplayCommand;
                            if (wtdCmdList == null)
                            {
                                wtdCmdList = new List <WriteToDisplayCommand>();
                            }
                            wtdCmdList.Add(wtdCommand);
                        }

                        else if (workstationCmd is ReadMdtFieldsCommand)
                        {
                            howRead = HowReadScreen.ReadMdt;
                        }

                        // save screen command. Build response, send back to server.
                        else if (workstationCmd is SaveScreenCommand)
                        {
                            var ra = SaveScreenCommand.BuildSaveScreenResponse();

                            Debug.WriteLine("Response: " + ra.ToHex(' '));

                            // send response stream back to server.
                            {
                                TelnetConnection.WriteToHost(logList, ra, SocketPack.TcpStream);
                            }
                            // BgnTemp
                            logList.AddSpecialItem(LogItemSpecial.NewGeneration);
                            // EndTemp
                        }

                        else if (workstationCmd is WriteStructuredFieldCommand)
                        {
                            var wsfCmd = workstationCmd as WriteStructuredFieldCommand;
                            if (wsfCmd.RequestCode == WSF_RequestCode.Query5250)
                            {
                                var ra = Query5250Response.BuildQuery5250Response();

                                Debug.WriteLine("Response: " + ra.ToHex(' '));

                                // send response stream back to server.
                                {
                                    TelnetConnection.WriteToHost(logList, ra, SocketPack.TcpStream);
                                }
                            }
                        }
                    }
                }
            }
            return(new Tuple <HowReadScreen?, List <WriteToDisplayCommand>, TelnetLogList>(
                       howRead, wtdCmdList, logList));
        }