Example #1
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);
            }
        }
Example #2
0
 public TcpClient()
 {
     state     = NetState.DISCONNECTED;
     remainder = ByteCache.Alloc(1024);
 }
Example #3
0
 public bool CanAppend(ByteBuf other, int offset = 0)
 {
     return(maxSize >= len + other.len - offset);
 }
Example #4
0
 public void Copy(ByteBuf src, int offset, int length)
 {
     System.Buffer.BlockCopy(src.buf, offset, buf, 0, length);
     len = length;
 }
Example #5
0
        public override void Deserialize(ByteBuf buf, int start, int count)
        {
            string str = Encoding.UTF8.GetString(buf.buf, start, count);

            Load(str);
        }
Example #6
0
        public static T DecodeFromByte <T>(ByteBuf buf, int start, int count) where T : JsonProtocol
        {
            string str = Encoding.UTF8.GetString(buf.buf, start, count);

            return(JsonUtility.FromJson <T>(str));
        }
Example #7
0
 public abstract void Deserialize(ByteBuf buf, int start, int count);