예제 #1
0
파일: Telnet.cs 프로젝트: whble/crterm
        public override void Parse(byte b)
        {
            switch (ReadState)
            {
            case States.ReadingData:
                if (b == IAC)
                {
                    ReadState = States.ReadingVerb;
                }
                else
                {
                    ReceiveBuffer.Add(b);
                    if (b < 32 || b > 127)
                    {
                        System.Diagnostics.Debug.Write("[" + (int)b + "]");
                    }
                }
                break;

            case States.ReadingVerb:
                LastVerbRec = (Verbs)b;
                ReadState   = States.ReadingOption;
                break;

            case States.ReadingOption:
                HandleNVTCmd(LastVerbRec, (Options)b);
                break;
            }
        }
예제 #2
0
 /// <summary>
 /// 初始化新的TCP客户端接收器实例
 /// </summary>
 /// <param name="clientSocket">客户端Socket</param>
 public TcpClientManager(Socket clientSocket)
 {
     ClientGuid    = new Guid();
     _clientSocket = clientSocket;
     ReceiveBuffer.Add(new ArraySegment <byte>(new byte[AppConfig.TcpBufferSize]));
     IsConnected = true;
 }
예제 #3
0
        public void ClearTest()
        {
            ReceiveBuffer target = new ReceiveBuffer(); // TODO: 初始化为适当的值

            target.Add(new byte[] { 1, 2 }, 0, 2);
            target.Clear();
            Assert.IsTrue(target.Length == 0);
        }
예제 #4
0
        public void ClearTest1()
        {
            ReceiveBuffer target = new ReceiveBuffer(); // TODO: 初始化为适当的值

            target.Add(new byte[] { 1, 2 }, 0, 2);
            int count = 1; // TODO: 初始化为适当的值

            target.Clear(count);
            Assert.IsTrue(target.Length == 1 && target[0] == 2);
        }
예제 #5
0
 public SocketActiveClient(Socket clientSocket, TcpServiceHost host)
 {
     ClientGuid      = Guid.NewGuid();
     _tcpServiceHost = host;
     _clientSocket   = clientSocket;
     ClientAddress   = _clientSocket.RemoteEndPoint.ToString();
     ReceiveBuffer.Add(new ArraySegment <byte>(new byte[host.ClientReceiveBufferSize]));
     IsConnected = true;
     _clientSocket.BeginReceive(ReceiveBuffer, SocketFlags.None, Received, _clientSocket);
 }
예제 #6
0
        public void CopyToTest()
        {
            ReceiveBuffer target = new ReceiveBuffer(); // TODO: 初始化为适当的值

            target.Add(new byte[] { 1, 2, 3, 4 }, 0, 4);
            byte[] dstArray = new byte[2]; // TODO: 初始化为适当的值
            int    count    = 2;           // TODO: 初始化为适当的值

            target.CopyTo(dstArray, count);
            Assert.IsTrue(dstArray[0] == 1 && dstArray[1] == 2);
        }
예제 #7
0
        public void ItemTest()
        {
            ReceiveBuffer target = new ReceiveBuffer(); // TODO: 初始化为适当的值

            target.Add(new byte[] { 234 }, 0, 1);
            int  index = 0; // TODO: 初始化为适当的值
            byte actual;

            actual = target[index];
            Assert.IsTrue(actual == 234);
        }
예제 #8
0
        /* The serial port receives the data event callback function */
        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;

            byte[] readBuffer = new byte[sp.ReadBufferSize];
            while (sp.BytesToRead > 0)
            {
                int size = sp.Read(readBuffer, 0, readBuffer.Length);
                receiveBuffer.Add(readBuffer, 0, size);
            }

            dataUpdate = true;
        }
예제 #9
0
        public void CopyToTest2()
        {
            ReceiveBuffer target = new ReceiveBuffer(); // TODO: 初始化为适当的值

            target.Add(new byte[] { 1, 2, 3, 4 }, 0, 4);
            int srcOffset = 2;               // TODO: 初始化为适当的值

            byte[] dstArray  = new byte[3];; // TODO: 初始化为适当的值
            int    dstOffset = 1;            // TODO: 初始化为适当的值
            int    count     = 2;            // TODO: 初始化为适当的值

            target.CopyTo(srcOffset, dstArray, dstOffset, count);
            Assert.IsTrue(dstArray[1] == 3 && dstArray[2] == 4);
        }
예제 #10
0
        public void ReadAndPositionTest()
        {
            ReceiveBuffer target = new ReceiveBuffer(); // TODO: 初始化为适当的值
            var           bytes  = new byte[] {
                1,
                3,
                0, 1,
                0, 0, 0, 1,
                0, 0, 0, 0, 0, 0, 0, 5,
                255,
                255, 255
            };

            target.Add(bytes, 0, bytes.Length);

            target.Position = 0;
            Assert.IsTrue(target.ReadBoolean() && target.Position == 1);
            Assert.IsTrue(target.ReadByte() == 3 && target.Position == 1 + 1);
            Assert.IsTrue(target.ReadInt16() == 1 && target.Position == 1 + 1 + 2);
            Assert.IsTrue(target.ReadInt32() == 1 && target.Position == 1 + 1 + 2 + 4);
            Assert.IsTrue(target.ReadInt64() == 5 && target.Position == 1 + 1 + 2 + 4 + 8);
            Assert.IsTrue(target.ReadArray(1)[0] == 255 && target.Position == 1 + 1 + 2 + 4 + 8 + 1);
            Assert.IsTrue(target.ReadArray().Length == 2 && target.Position == 1 + 1 + 2 + 4 + 8 + 1 + 2);
        }