예제 #1
0
 public static ByteBuf Unzip(ByteBuf data)
 {
     try
     {
         SimpleZlib ds  = new SimpleZlib(new MemoryStream(data.buf, 0, data.len), CompressionMode.Decompress);
         ByteBuf    ret = null;
         ByteBuf    buf = ByteCache.Alloc(1024);
         int        len = ds.Read(buf.buf, 0, 1024);
         while (len > 0)
         {
             buf.len = len;
             if (ret == null)
             {
                 ret = ByteCache.Alloc(len);
             }
             else if (ret.maxSize < len + ret.len)
             {
                 ret = ByteCache.Reserve(ret, len + ret.len);
             }
             ret.Append(buf);
             len = ds.Read(buf.buf, 0, 1024);
         }
         ds.Close();
         return(ret);
     }
     catch (Exception e)
     {
         Debugger.LogError(e);
     }
     return(null);
 }
예제 #2
0
파일: ByteBuf.cs 프로젝트: luffyGame/source
        public ByteBuf Clone()
        {
            ByteBuf clone = ByteCache.Alloc(len);

            System.Buffer.BlockCopy(buf, 0, clone.buf, 0, len);
            clone.len = len;
            return(clone);
        }
예제 #3
0
        public override ByteBuf Serialize()
        {
            string  str   = JsonStr();
            int     count = Encoding.UTF8.GetByteCount(str);
            ByteBuf buf   = ByteCache.Alloc(count + 4);

            buf.Write(type(), 0);
            Encoding.UTF8.GetBytes(str, 0, str.Length, buf.buf, 4);
            buf.len = count + 4;
            return(buf);
        }
예제 #4
0
        public static ByteBuf ZipUnsafe(ByteBuf toCompress)
        {
            Zip(toCompress.buf, 0, toCompress.len);
            int     count = (int)zipOut.Length;
            ByteBuf buf   = ByteCache.Alloc(count);

            zipOut.Seek(0, SeekOrigin.Begin);
            zipOut.Read(buf.buf, 0, count);
            buf.len = count;
            zipOut.SetLength(0);
            return(buf);
        }
예제 #5
0
        public static ByteBuf UnzipUnsafe(ByteBuf data)
        {
            Unzip(data.buf, 0, data.len);
            int     count = (int)unzipOut.Length;
            ByteBuf buf   = ByteCache.Alloc(count);

            unzipOut.Seek(0, SeekOrigin.Begin);
            unzipOut.Read(buf.buf, 0, count);
            buf.len = count;
            unzipOut.SetLength(0);
            return(buf);
        }
예제 #6
0
파일: ByteBuf.cs 프로젝트: luffyGame/source
 public void Dispose()
 {
     if (disposed)
     {
         return;
     }
     disposed = true;
     if (null != ms)
     {
         ms = null;
     }
     ByteCache.Free(this);
 }
예제 #7
0
        public static ByteBuf Zip(ByteBuf toCompress)
        {
            ByteBuf    buf = ByteCache.Alloc(1024);
            SimpleZlib ds  = new SimpleZlib(new MemoryStream(buf.buf), CompressionMode.Compress, true);

            ds.Write(toCompress.buf, 0, toCompress.len);
            MemoryStream ms = (MemoryStream)ds.BaseStream;

            ds.Close();
            buf.len = (int)ms.Position;
            ms.Close();
            return(buf);
        }
예제 #8
0
        protected ByteBuf EncodeAndPackage(Protocol p)
        {
            ByteBuf buf = p.Serialize();
            ByteBuf zip = SimpleZlibExt.Zip(buf);
            //ByteBuf zip = DotNetZlibExt.ZipUnsafe(buf);
            ByteBuf ret = ByteCache.Alloc(zip.len + 4);

            ret.Write(zip.len, 0);
            ret.len = 4;
            ret.Append(zip);
            buf.Dispose();
            zip.Dispose();
            return(ret);
        }
예제 #9
0
        protected void HandleRawData(ByteBuf raw, int offset, int length)
        {
            if (state != NetState.CONNECTED)
            {
                return;
            }
            ByteBuf buf = ByteCache.Alloc(length);

            buf.Copy(raw, offset, length);
            Protocol p = Decode(buf);

            if (null != p)
            {
                HandleMessage(p);
            }
        }
예제 #10
0
        private void SendAndReceive()
        {
            ByteBuf buffer = ByteCache.Alloc(1024);

            while (state == NetState.CONNECTED)
            {
                if (!isSending)
                {
                    Send();
                }
                if (!isReceiving)
                {
                    Receive(buffer);
                }
                Thread.Sleep(1);
            }
        }
예제 #11
0
        private void HandleReceive(ByteBuf data)
        {
            if (!remainder.IsEmpty)
            {
                if (!remainder.CanAppend(data))
                {
                    remainder = ByteCache.Reserve(remainder, remainder.len + data.len);
                }
                remainder.Append(data);
                data      = remainder;
                remainder = ByteCache.Alloc(1024);
            }
            int cursor = 0;

            while (data.len - cursor > 4)
            {
                int length = data.GetInt(cursor);
                if (data.len - cursor >= 4 + length)
                {
                    cursor += 4;
                    HandleRawData(data, cursor, length);
                    cursor += length;
                }
                else
                {
                    break;
                }
            }
            if (data.len > cursor)
            {
                if (!remainder.CanAppend(data, cursor))
                {
                    remainder = ByteCache.Reserve(remainder, remainder.len + data.len - cursor);
                }
                remainder.Append(data, cursor);
            }
        }
예제 #12
0
 public TcpClient()
 {
     state     = NetState.DISCONNECTED;
     remainder = ByteCache.Alloc(1024);
 }