public void stdinLoop()
 {
     try
     {
         if (stdin == null)
         {
             return;
         }
         if (stdin.CanSeek)
         {
             stdin.Seek(0, SeekOrigin.Begin);
         }
         var buffer = new byte[8 * 1024];
         int l;
         while (connection.IsOnline)
         {
             l = stdin.Read(buffer, 0, buffer.Length);
             if (l > 0)
             {
                 connection.writeUsb(ClovershellConnection.ClovershellCommand.CMD_EXEC_STDIN, (byte)id, buffer, 0, l);
             }
             else
             {
                 break;
             }
             LastDataTime = DateTime.Now;
             if (stdinQueue > 32 * 1024 && connection.IsOnline)
             {
                 Debug.WriteLine(string.Format("queue: {0} / {1}, {2}MB / {3}MB ({4}%)",
                                               stdinQueue, stdinPipeSize, stdin.Position / 1024 / 1024, stdin.Length / 1024 / 1024, stdin.Length == 0 ? 100 : (100 * stdin.Position / stdin.Length)));
                 while (stdinQueue > 16 * 1024)
                 {
                     Thread.Sleep(50);
                     connection.writeUsb(ClovershellConnection.ClovershellCommand.CMD_EXEC_STDIN_FLOW_STAT_REQ, (byte)id);
                 }
             }
         }
         connection.writeUsb(ClovershellConnection.ClovershellCommand.CMD_EXEC_STDIN, (byte)id); // eof
         if (stdinQueue > 0 && connection.IsOnline)
         {
             Thread.Sleep(50);
             connection.writeUsb(ClovershellConnection.ClovershellCommand.CMD_EXEC_STDIN_FLOW_STAT_REQ, (byte)id);
         }
         stdinFinished = true;
     }
     catch (ThreadAbortException) { }
     catch (ClovershellException ex)
     {
         Debug.WriteLine("stdin error: " + ex.Message + ex.StackTrace);
     }
     finally
     {
         stdinThread = null;
     }
 }
 internal void shellConnectionLoop()
 {
     try
     {
         var buff = new byte[1024];
         while (socket.Connected)
         {
             var l = socket.Receive(buff);
             if (l > 0)
             {
                 connection.writeUsb(ClovershellConnection.ClovershellCommand.CMD_SHELL_IN, (byte)id, buff, l);
             }
             else
             {
                 break;
             }
         }
     }
     catch (ThreadAbortException)
     {
     }
     catch (ClovershellException)
     {
     }
     finally
     {
         shellConnectionThread = null;
     }
     Debug.WriteLine(string.Format("Shell client {0} disconnected", id));
     socket.Close();
     connection.shellConnections[id] = null;
 }
Beispiel #3
0
        internal void shellConnectionLoop()
        {
            try
            {
                var buff = new byte[1024];
                while (socket.Connected)
                {
                    var l = socket.Receive(buff);
                    if (l > 0)
                    {
                        int start = 0;
                        int pos   = 0;
                        do
                        {
                            if ((pos + 1 < l) && (buff[pos] == '\r') && (buff[pos + 1] == '\n')) // New line?
                            {
                                // Hey, dot not send \r\n! I'll cut it to \n
                                buff[pos] = (byte)'\n';
                                connection.writeUsb(ClovershellConnection.ClovershellCommand.CMD_SHELL_IN, (byte)id, buff, start, pos - start + 1);
                                pos  += 2;
                                start = pos;
                            }
                            else if ((pos + 1 < l) && (buff[pos] == 0xFF)) // Telnet command?
                            {
                                if (buff[pos + 1] == 0xFF)                 // Or just 0xFF...
                                {
                                    connection.writeUsb(ClovershellConnection.ClovershellCommand.CMD_SHELL_IN, (byte)id, buff, start, pos - start + 1);
                                    pos  += 2;
                                    start = pos;
                                }
                                else if (pos + 2 < l)
                                {
                                    if (pos - start > 0)
                                    {
                                        connection.writeUsb(ClovershellConnection.ClovershellCommand.CMD_SHELL_IN, (byte)id, buff, start, pos - start);
                                    }
                                    var cmd = buff[pos + 1]; // Telnet command code
                                    var opt = buff[pos + 2]; // Telnet option code
#if VERY_DEBUG
                                    Debug.WriteLine(string.Format("Telnet command: CMD={0:X2} ARG={1:X2}", cmd, opt));
#endif
                                    pos  += 3;
                                    start = pos;
                                }
                            }
                            else
                            {
                                pos++;                         // No, moving to next character
                            }
                            if ((pos == l) && (l - start > 0)) // End of packet
                            {
                                connection.writeUsb(ClovershellConnection.ClovershellCommand.CMD_SHELL_IN, (byte)id, buff, start, l - start);
                            }
                        } while (pos < l);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            catch (ThreadAbortException)
            {
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message + ex.StackTrace);
                if (socket.Connected)
                {
                    socket.Send(Encoding.ASCII.GetBytes("Error: " + ex.Message));
                }
            }
            finally
            {
                shellConnectionThread = null;
                Debug.WriteLine(string.Format("Shell client {0} disconnected", id));
                if (socket != null)
                {
                    socket.Close();
                }
                connection.shellConnections[id] = null;
            }
        }