private void CheckBufferPoll(int willwritecount) { if (!_needcheckbuffuer) { return; } if (_ms.Position + willwritecount > _bufferPollManager.BlockSize) { var pos = _ms.Position; var newms = new System.IO.MemoryStream(); newms.Write(_bufferPollManager.Buffer, (int)_bufferoffset, (int)pos); _ms.Close(); _ms.Dispose(); _bufferPollManager.RealseBuffer(Bufferindex); Bufferindex = -1; _bufferPollManager = null; _ms = newms; _needcheckbuffuer = false; } }
public bool StartServer() { try { if (_bufferpoll == null) { _bufferpoll = new BufferPollManager(2000, 10240); } if (socketServer == null) { socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socketServer.ReceiveBufferSize = 64000; socketServer.SendBufferSize = 64000; socketServer.NoDelay = true; if (bindIpArray == null) { socketServer.Bind(new IPEndPoint(IPAddress.Any, ipPort)); } else { //foreach(var ip in bindIpArray) //{ // socketServer.Bind(new IPEndPoint(IPAddress.Parse(ip), ipPort)); //} socketServer.Bind(new IPEndPoint(IPAddress.Any, ipPort)); } } socketServer.Listen(int.MaxValue); if (!isStartServer) { Listening(); } if (_worktimer == null) { _worktimer = LJC.FrameWork.Comm.TaskHelper.SetInterval(5000, () => { CheckConnectedClient(); return(false); }); } isStartServer = true; return(true); } catch (Exception e) { OnError(e); return(false); } }
public static void Serialize(object o, BufferPollManager poolmanager, ref int bufferindex, ref long size, ref byte[] serbyte) { MemoryStreamWriter2 writer = new MemoryStreamWriter2(poolmanager); Serialize(o, writer); bufferindex = writer.Bufferindex; size = writer.GetDataLen(); if (bufferindex == -1) { serbyte = writer.GetBytes(); } }
public MemoryStreamWriter(BufferPollManager bufferpollmanger) { var bufferindex = bufferpollmanger.GetBuffer(); if (bufferindex == -1) { _ms = new MemoryStream(); } else { _bufferPollManager = bufferpollmanger; Bufferindex = bufferindex; _bufferoffset = bufferpollmanger.GetOffset(bufferindex); _ms = new MemoryStream(bufferpollmanger.Buffer, (int)_bufferoffset, bufferpollmanger.BlockSize); } //_ms.Position = 4; _ms.Position = 8; }
public static void Serialize(object o, BufferPollManager poolmanager, ref int bufferindex, ref long size, ref byte[] serbyte, bool compress = true) { MemoryStreamWriter writer = new MemoryStreamWriter(poolmanager); Serialize(o, writer); bufferindex = writer.Bufferindex; size = writer.GetDataLen(); if (bufferindex == -1) { serbyte = writer.GetBytes(); if (compress && size - 8 > minGZIPCompressLenth) { var compressBytes = GZip.Compress(serbyte.Skip(8).Take((int)size - 8).ToArray()); size = 8 + compressBytes.Length; serbyte = new byte[size]; for (int i = 0; i < compressBytes.Length; i++) { serbyte[i + 8] = compressBytes[i]; } } } else { if (compress && size - 8 > minGZIPCompressLenth) { var offset = poolmanager.GetOffset(bufferindex); var compressBytes = GZip.Compress(poolmanager.Buffer.Skip(offset + 8).Take((int)size - 8).ToArray()); size = 8 + compressBytes.Length; for (int i = 0; i < compressBytes.Length; i++) { poolmanager.Buffer[offset + 8 + i] = compressBytes[i]; } } } }
public static void TestGZIP() { BufferPollManager poll = new BufferPollManager(100, 1024 * 1000); string str = "我是中国人的,我常常爱着我的祖asfdgefaserfaTimeout 时间已到。"; RunConfig rc = new RunConfig(); rc.CmdPhoneNumber = str; rc.DefaultEmailAccount = str; rc.DefaultEmailPwd = str; rc.DefaultReciveEmailAccount = str; rc.ID = 12456; rc.StopEarn = 12.0; rc.CmdPhoneNumber = str; byte[] bytes = null; var utf8bytes = Encoding.UTF8.GetBytes(str); var cputf8bytes = GZip.Compress(utf8bytes); System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); bytes = LJC.FrameWork.EntityBuf.EntityBufCore.Serialize(rc); sw.Restart(); int bufferindex = -1; long size = 0; for (int i = 0; i < 100000; i++) { //string str1 = JsonUtil<object>.Serialize(rc); //var obj = JsonUtil<RunConfig>.Deserialize(str1); LJC.FrameWork.EntityBuf.EntityBufCore.Serialize(rc, poll, ref bufferindex, ref size, ref bytes);// Encoding.UTF8.GetBytes(str); //LJC.FrameWork.EntityBuf.EntityBufCore.Serialize(rc); if (bytes == null) { } else { poll.RealseBuffer(bufferindex); } } sw.Stop(); Console.WriteLine("序列化用时" + sw.ElapsedMilliseconds); Console.WriteLine("原始长度:" + bytes.Length); return; byte[] commpressbytes = null, decompressbytes = null; sw.Restart(); for (int i = 0; i < 10000; i++) { commpressbytes = GZip.Compress(bytes); } sw.Stop(); Console.WriteLine("压缩用时" + sw.ElapsedMilliseconds + ",压缩后长度:" + commpressbytes.Length); sw.Restart(); for (int i = 0; i < 10000; i++) { decompressbytes = GZip.Decompress(commpressbytes); } sw.Stop(); Console.WriteLine("解压缩用时" + sw.ElapsedMilliseconds + ",解压缩后长度" + decompressbytes.Length); Console.Read(); }