Exemple #1
0
        public async Task <D> ReadObjectAsync <D>(int proj = 0x00ff) where D : IData, new()
        {
            if (entity == null && count == -1) // if not yet parse and read
            {
                // read
                count = 0;
                int?clen = HeaderInt("Content-Length");
                if (clen > 0)
                {
                    int len = (int)clen;
                    buffer = BufferUtility.GetByteBuffer(len); // borrow from the pool
                    while ((count += await Request.Body.ReadAsync(buffer, count, (len - count))) < len)
                    {
                    }
                }
                // parse
                string ctyp = Header("Content-Type");
                entity = ParseContent(ctyp, buffer, count);
            }
            IDataInput src = entity as IDataInput;

            if (src == null)
            {
                return(default(D));
            }
            return(src.ToObject <D>(proj));
        }
Exemple #2
0
 protected DynamicContent(bool octet, int capacity)
 {
     if (octet)
     {
         bytebuf = BufferUtility.GetByteBuffer(capacity);
     }
     else
     {
         charbuf = BufferUtility.GetCharBuffer(capacity);
     }
     count = 0;
 }
Exemple #3
0
 public async Task <ArraySegment <byte> > ReadAsync()
 {
     if (count == -1) // if not yet read
     {
         count = 0;
         int?clen = HeaderInt("Content-Length");
         if (clen > 0)
         {
             // reading
             int len = (int)clen;
             buffer = BufferUtility.GetByteBuffer(len); // borrow from the pool
             while ((count += await Request.Body.ReadAsync(buffer, count, (len - count))) < len)
             {
             }
         }
     }
     return(new ArraySegment <byte>(buffer, 0, count));
 }
Exemple #4
0
 public async Task <M> ReadAsync <M>() where M : class, IDataInput
 {
     if (entity == null && count == -1) // if not yet parse and read
     {
         // read
         count = 0;
         int?clen = HeaderInt("Content-Length");
         if (clen > 0)
         {
             int len = (int)clen;
             buffer = BufferUtility.GetByteBuffer(len); // borrow from the pool
             while ((count += await Request.Body.ReadAsync(buffer, count, (len - count))) < len)
             {
             }
         }
         // parse
         string ctyp = Header("Content-Type");
         entity = ParseContent(ctyp, buffer, count, typeof(M));
     }
     return(entity as M);
 }
Exemple #5
0
        void AddByte(byte b)
        {
            // ensure capacity
            int olen = bytebuf.Length; // old length

            if (count >= olen)
            {
                int    nlen = olen * 4; // new length
                byte[] obuf = bytebuf;
                bytebuf = BufferUtility.GetByteBuffer(nlen);
                Array.Copy(obuf, 0, bytebuf, 0, olen);
                BufferUtility.Return(obuf);
            }
            bytebuf[count++] = b;

            // calculate checksum
            ulong cs = checksum;

            cs      ^= b;                  // XOR
            checksum = cs >> 57 | cs << 7; // circular left shift 7 bit
        }