public void ClearTest() { ReceiveBuffer target = new ReceiveBuffer(); // TODO: 初始化为适当的值 target.Write(new byte[] { 1, 2 }, 0, 2); target.Clear(); Assert.IsTrue(target.Length == 0); }
public void ClearTest1() { ReceiveBuffer target = new ReceiveBuffer(); // TODO: 初始化为适当的值 target.Write(new byte[] { 1, 2 }, 0, 2); int count = 1; // TODO: 初始化为适当的值 target.Clear(count); Assert.IsTrue(target.Length == 1 && target[0] == 2); }
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); }
/// <summary> /// 释放资源 /// </summary> /// <param name="disposing">是否也释放托管资源</param> protected virtual void Dispose(bool disposing) { this.Close(); this.recvArg.Dispose(); if (disposing) { this.recvBuffer = null; this.recvArg = null; this.socket = null; this.socketRoot = null; this.TagBag = null; this.TagData = null; } }
/// <summary> /// 释放资源 /// </summary> /// <param name="disposing">是否也释放托管资源</param> protected virtual void Dispose(bool disposing) { this.CloseInternal(false); this.sendArg.Dispose(); this.recvArg.Dispose(); if (disposing) { this.recvBuffer = null; this.recvArg = null; this.byteRangeQueue = null; this.sendArg = null; this.socket = null; this.socketRoot = null; this.TagBag = null; this.TagData = null; } }
public void IndexOfTest() { ReceiveBuffer target = new ReceiveBuffer(); // 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); }
public void ItemTest() { ReceiveBuffer target = new ReceiveBuffer(); // TODO: 初始化为适当的值 target.Write(new byte[] { 234 }, 0, 1); int index = 0; // TODO: 初始化为适当的值 byte actual; actual = target[index]; Assert.IsTrue(actual == 234); }
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.Write(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); }
public void CopyToTest2() { ReceiveBuffer target = new ReceiveBuffer(); // TODO: 初始化为适当的值 target.Write(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); }
/// <summary> /// 释放资源 /// </summary> /// <param name="disposing">是否也释放托管资源</param> protected virtual void Dispose(bool disposing) { this.Close(); this.sendArg.Dispose(); this.recvArg.Dispose(); if (disposing) { this.recvBuffer = null; this.recvArg = null; this.byteRangeQueue = null; this.sendArg = null; this.socket = null; this.socketRoot = null; this.TagBag = null; this.TagData = null; } }
/// <summary> /// 调试视图 /// </summary> /// <param name="view">查看的对象</param> public DebugView(ReceiveBuffer view) { this.view = view; }
/// <summary> /// 当接收到远程端的数据时,将触发此方法 /// </summary> /// <param name="buffer">接收到的历史数据</param> /// <returns></returns> protected abstract void OnReceive(ReceiveBuffer buffer);
/// <summary> /// 当接收到会话对象的数据时,将触发此方法 /// </summary> /// <param name="session">会话对象</param> /// <param name="buffer">接收到的历史数据</param> /// <returns></returns> protected abstract void OnReceive(T session, ReceiveBuffer buffer);
/// <summary> /// 解析连接请求信息 /// </summary> /// <param name="buffer">接收到的原始数量</param> /// <returns></returns> public static HttpRequest From(ReceiveBuffer buffer) { buffer.Position = 0; var bytes = buffer.ReadArray(); var request = HttpRequest.From(bytes, "http"); if (request == null) { return null; } if (string.Equals(request.Method, "POST", StringComparison.OrdinalIgnoreCase)) { var contentLength = int.Parse(request["Content-Length"]); if (request.Body.Length < contentLength) { return null; } } buffer.Clear(); return request; }