Ejemplo n.º 1
0
 public bool SendData(byte[] bs)
 {
     if (m_userQueue.Available > bs.Length)
     {
         m_userQueue.PushValues(bs);
         return(true);
     }
     return(false);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 处理Telnet命令并返回数据
        /// </summary>
        /// <param name="recv"></param>
        /// <param name="send"></param>
        /// <param name="data"></param>
        void ParseTelnetCommand(RingQueue <byte> recv, RingQueue <byte> send, RingQueue <byte> data)
        {
            while (recv.Count > 0 && data.Available > 0)
            {
                var b = recv.Shift();

                if (b == IAC)
                {
                    //命令处理
                    if (recv.Count < 2)
                    {
                        recv.Unshift(b);
                        break;
                    }

                    var action = recv.Shift();
                    var option = recv.Shift();

                    switch (option)
                    {
                    case ECHO:
                    case NOGA:
                    case TERMTYPE:
                    case NAWS:
                        switch (action)
                        {
                        case WILL:
                            send.PushValues(new byte[] { IAC, DO, option });
                            break;

                        case DO:
                            send.PushValues(new byte[] { IAC, WILL, option });
                            break;

                        case SB:
                            switch (option)
                            {
                            case TERMTYPE:
                                if (recv.Count < 3)
                                {
                                    recv.UnshiftValues(new byte[] { option, action, b });
                                    break;
                                }
                                if (recv.Shift() == 1)
                                {
                                    recv.Shift(); recv.Shift();
                                    //发送客户端型号
                                    send.PushValues(new byte[] { IAC, SB, TERMTYPE, 0 });
                                    send.PushValues(Encoding.Default.GetBytes(TermType));
                                    send.PushValues(new byte[] { IAC, SE });
                                }
                                break;
                            }
                            break;
                        }
                        if (action == WILL && option == NAWS)
                        {
                            //发送窗体大小
                            send.PushValues(new byte[] { IAC, SB, NAWS,
                                                         (byte)((ScreenColumns >> 8) & 0xFF), (byte)(ScreenColumns & 0xFF),
                                                         (byte)((ScreenRows >> 8) & 0xFF), (byte)(ScreenRows & 0xFF), IAC, SE });
                        }
                        break;

                    default:
                        //拒绝其他一切选项
                        switch (action)
                        {
                        case WILL:
                        case WONT:
                            send.PushValues(new byte[] { IAC, DONT, option });
                            break;

                        case DO:
                        case DONT:
                            send.PushValues(new byte[] { IAC, WONT, option });
                            break;
                        }
                        break;
                    }
                }
                else
                {
                    //丢给VT220解析器
                    data.Push(b);
                }
            }
        }