/// <summary> /// 从收到的数据流中解析请求体 /// </summary> /// <param name="stream"></param> /// <returns></returns> public static TelnetRequest Parse(ReceiveStream stream) { stream.Position = 0; var index = stream.IndexOf(Crlf); // 以CRLF为结束标识 if (index < 0) { return null; } var input = stream.ReadString(index, Encoding.ASCII); stream.Clear(index + Crlf.Length); if (string.IsNullOrWhiteSpace(input) == true) // 空内容 { return TelnetRequest.Parse(stream); } var items = input.Split(new string[] { " " }, 2, StringSplitOptions.RemoveEmptyEntries); var cmd = items.First(); var arg = items.Length > 1 ? items.Last() : null; return new TelnetRequest(cmd, arg); }
public void IndexOfTest() { ReceiveStream target = new ReceiveStream(); // TODO: 初始化为适当的值 var bytes = new byte[] { 2, 3, 4, 4 }; target.Write(bytes, 0, bytes.Length); target.Position = 0; var actual = target.IndexOf(bytes); Assert.AreEqual(0, actual); var byte2 = new byte[] { 3, 4 }; actual = target.IndexOf(byte2); Assert.AreEqual(1, actual); var byte3 = new byte[] { 3, 4, 5 }; actual = target.IndexOf(byte3); Assert.AreEqual(-1, actual); var byte4 = new byte[] { 4 }; actual = target.IndexOf(byte4); Assert.AreEqual(2, actual); var byte5 = new byte[] { 2, 3, 4, 4 }; actual = target.IndexOf(byte5); Assert.AreEqual(0, actual); var byte6 = new byte[] { 2, 3, 4, 4,5 }; actual = target.IndexOf(byte6); Assert.AreEqual(-1, actual); }