Example #1
0
        void ServiceConnection(SslStream Stream, TcpClient Client, NetworkStream BaseStream)
        {
            char[] buffer = new char[4096];
            //StringBuilder sb = new StringBuilder();

            while (!Stopping && IsStillConnected(Client) && BaseStream != null)
            {
                // PollConnection() checks and assembles incoming messages from the Stream (in this case, receiving from the master PC) and
                // calls OnMessage() when one is completed.
                if (BaseStream.DataAvailable)
                {
                    List <byte[]> Messages = PollConnection(NetworkStream);
                    foreach (byte[] Message in Messages)
                    {
                        OnMessageReceived(Message);
                    }
                }

                Thread.Sleep(50);

                // If a process is connected, service it now.
                lock (ProcessLock)
                {
                    if (ConnectedProcess != null)
                    {
                        if (CT != null)
                        {
                            // There is a 64KB message size limit imposed in SendMessage().  Unicode characters I think are 16-bits, but
                            // I think certain characters can be up to 4-bytes?  Well, maybe not.  Either way, there's not much cost here
                            // so I'll assume 4-bytes per character.  Base-64 expands the size of the data by 4/3rds (plus a handful).
                            // So if we max at 12000, that allows 48000 4-byte characters that expand to 64000, and our actual message
                            // limit is at 65536 allowing some margin plus the wrapping.
                            const int MaxSize = 12000;

                            bool   Stale         = false;
                            bool   MaxedMessage  = false;
                            int    ScrolledLines = 0;
                            string NewText       = "";
                            ScrolledLines = CT.CheckForLineScroll(out Stale);
                            if (!Stale)
                            {
                                NewText = CT.ReadNew(out Stale, out MaxedMessage, true, MaxSize);
                            }
                            if (!Stale)
                            {
                                CT.SpotCheckState(MaxedMessage, out Stale);
                            }
                            if (Stale)
                            {
                                NewText = CT.ReadWholeConsole(MaxSize);
                                string EncodedText = System.Convert.ToBase64String(Encoding.Unicode.GetBytes(NewText));
                                string XmlMsg      = "<Whole-Console>" + EncodedText + "</Whole-Console>";
                                byte[] RawMsg      = Encoding.Unicode.GetBytes(XmlMsg);
                                SendMessage(Stream, RawMsg);
                            }
                            else if ((NewText.Length > 0 && NewText.Contains("\n")) || ScrolledLines != 0)
                            {
                                string EncodedText = System.Convert.ToBase64String(Encoding.Unicode.GetBytes(NewText));
                                //string XmlMsg = "<Console-New Last-X=\"" + CT.LastCursorPosition.X + "\" Last-Y=\"" + CT.LastCursorPosition.Y + "\">" + EncodedText + "</Console-New>";
                                string XmlMsg;
                                if (NewText.Length > 0 && NewText.Contains("\n"))
                                {
                                    XmlMsg = "<Console-New Scrolled=\"" + ScrolledLines.ToString() + "\">" + EncodedText + "</Console-New>";
                                }
                                else
                                {
                                    XmlMsg = "<Console-New Scrolled=\"" + ScrolledLines.ToString() + "\" />";
                                }
                                byte[] RawMsg = Encoding.Unicode.GetBytes(XmlMsg);
                                SendMessage(Stream, RawMsg);
                            }
                            else
                            {
                                string CurrentLine = CT.PeekCurrentLine();
                                if (LastCurrentLine != CurrentLine)
                                {
                                    LastCurrentLine = CurrentLine;
                                    string EncodedText = System.Convert.ToBase64String(Encoding.Unicode.GetBytes(CurrentLine));
                                    string XmlMsg      = "<Current-Console-Line>" + EncodedText + "</Current-Console-Line>";
                                    byte[] RawMsg      = Encoding.Unicode.GetBytes(XmlMsg);
                                    SendMessage(Stream, RawMsg);
                                }
                            }
                        }
                    }
                }
            }
        }