public Test_SweetStream() { SweetStream sw = new SweetStream(); while (true) { MemoryStream memoryStream = new MemoryStream(); for (int i = 1; i <= 1000; i++) { byte[] b = sw.Encode(Encoding.UTF8.GetBytes(RandomCode.Generate(new Random().Next(1, 5000)))); // byte[] b = sw.Encode(Encoding.UTF8.GetBytes("a")); int size = 128 - memoryStream.ToArray().Length; if (size > 0) { int sss = size - b.Length; if (sss >= 0) { memoryStream.Write(b); } else { memoryStream.Write(b, 0, size); sw.Decode(memoryStream.ToArray()); memoryStream = new MemoryStream(); memoryStream.Write(b, size, -sss); } } else { sw.Decode(memoryStream.ToArray()); memoryStream = new MemoryStream(); memoryStream.Write(b); } } if (memoryStream.ToArray().Length > 0) { sw.Decode(memoryStream.ToArray()); } int count = 0; byte[] r = sw.TakeStreamBuffer(); do { count++; Console.WriteLine(Encoding.UTF8.GetString(r)); r = sw.TakeStreamBuffer(); } while (r != null); Console.WriteLine("all:" + count); Thread.Sleep(100); Console.ReadKey(); } }
/// <summary> /// 构造函数 /// </summary> /// <param name="esfSocket">ESFSocket对象</param> /// <param name="numMaxBufferSize">接受数据最大容量</param> public BaseClientSocket(Socket esfSocket, int numMaxBufferSize) { clientSocket = esfSocket; this.numMaxBufferSize = numMaxBufferSize + SweetStream.OUTSOURCING_SIZE; RBuffer = new SweetStream(); sendEventArgs = new SocketAsyncEventArgsEx(clientSocket, clientSocket.endPoint, IO_Completed); readWriteEventArg = new SocketAsyncEventArgs() { UserToken = clientSocket, RemoteEndPoint = clientSocket.endPoint }; readWriteEventArg.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed); }
/// <summary> /// 初始化 /// </summary> internal void Init(Socket socket, EventHandler <System.Net.Sockets.SocketAsyncEventArgs> eventHandler) { receiveEventArgs = new System.Net.Sockets.SocketAsyncEventArgs(); receiveEventArgs.UserToken = this; connectDateTime = DateTime.Now; rBuffer = new SweetStream(); isAlive = true; timeoutCount = 0; this.socket = socket; if (this.socket != null) { sendEventArgs = new SocketAsyncEventArgsEx(this, socket, eventHandler); receiveEventArgs.AcceptSocket = this.socket.GetSocket(); receiveEventArgs.RemoteEndPoint = this.socket.endPoint; } }