示例#1
0
        public void Publish(string name, string shard, int arg, IData obj, int proj = 0x00ff)
        {
            JsonContent cont = new JsonContent(true).Put(null, obj, proj);

            Publish(name, shard, arg, cont);
            BufferUtility.Return(cont); // back to pool
        }
示例#2
0
文件: Client.cs 项目: geffzhang/core
        public async Task <int> PostAsync(ActionContext ac, string uri, IContent content)
        {
            try
            {
                HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, uri);
                if (peerid != null && ac != null)
                {
                    if (ac.Token != null)
                    {
                        req.Headers.Add("Authorization", "Token " + ac.Token);
                    }
                }
                req.Content = (HttpContent)content;
                req.Headers.TryAddWithoutValidation("Content-Type", content.Type);
                req.Headers.TryAddWithoutValidation("Content-Length", content.Size.ToString());

                HttpResponseMessage rsp = await SendAsync(req, HttpCompletionOption.ResponseContentRead);

                return((int)rsp.StatusCode);
            }
            catch
            {
                retryat = Environment.TickCount + AHEAD;
            }
            finally
            {
                if (content is DynamicContent)
                {
                    BufferUtility.Return((DynamicContent)content);
                }
            }
            return(0);
        }
示例#3
0
        public void Publish <D>(string name, string shard, int arg, D[] arr, int proj = 0x00ff) where D : IData
        {
            JsonContent cont = new JsonContent(true).Put(null, arr, proj);

            Publish(name, shard, arg, cont);
            BufferUtility.Return(cont); // back to pool
        }
示例#4
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));
        }
示例#5
0
        //
        // EVENTS
        //

        public void Publish(string name, string shard, int arg, IDataInput inp)
        {
            DynamicContent dcont = inp.Dump();

            Publish(name, shard, arg, dcont);
            BufferUtility.Return(dcont); // back to pool
        }
示例#6
0
        public static string ToString <D>(D v, int proj = 0x00ff) where D : IData
        {
            JsonContent cont = new JsonContent(false, 4 * 1024);

            cont.Put(null, v, proj);
            string str = cont.ToString();

            BufferUtility.Return(cont); // return buffer to pool
            return(str);
        }
示例#7
0
        public override string ToString()
        {
            JsonContent cont = new JsonContent(false, 4 * 1024);

            cont.Put(null, this);
            string str = cont.ToString();

            BufferUtility.Return(cont);
            return(str);
        }
示例#8
0
 protected DynamicContent(bool octet, int capacity)
 {
     if (octet)
     {
         bytebuf = BufferUtility.GetByteBuffer(capacity);
     }
     else
     {
         charbuf = BufferUtility.GetCharBuffer(capacity);
     }
     count = 0;
 }
示例#9
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));
 }
示例#10
0
文件: Client.cs 项目: geffzhang/core
        public async Task <Dual <int, M> > PostAsync <M>(ActionContext ctx, string uri, IContent content) where M : class, IDataInput
        {
            try
            {
                HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, uri);
                if (ctx != null)
                {
                    req.Headers.Add("Authorization", "Token " + ctx.Token);
                }
                req.Content = (HttpContent)content;
                req.Headers.TryAddWithoutValidation("Content-Type", content.Type);
                req.Headers.TryAddWithoutValidation("Content-Length", content.Size.ToString());

                HttpResponseMessage rsp = await SendAsync(req, HttpCompletionOption.ResponseContentRead);

                string ctyp = rsp.Content.Headers.GetValue("Content-Type");
                if (ctyp == null)
                {
                    return(new Dual <int, M>((int)rsp.StatusCode, null));
                }
                else
                {
                    byte[] bytes = await rsp.Content.ReadAsByteArrayAsync();

                    M inp = ParseContent(ctyp, bytes, bytes.Length, typeof(M)) as M;
                    return(new Dual <int, M>((int)rsp.StatusCode, inp));
                }
            }
            catch
            {
                retryat = Environment.TickCount + AHEAD;
            }
            finally
            {
                if (content is DynamicContent)
                {
                    BufferUtility.Return((DynamicContent)content);
                }
            }
            return(default(Dual <int, M>));
        }
示例#11
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);
 }
示例#12
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
        }
示例#13
0
 public void Add(char c)
 {
     if (Octet) // byte-oriented
     {
         // UTF-8 encoding but without surrogate support
         if (c < 0x80)
         {
             // have at most seven bits
             AddByte((byte)c);
         }
         else if (c < 0x800)
         {
             // 2 char, 11 bits
             AddByte((byte)(0xc0 | (c >> 6)));
             AddByte((byte)(0x80 | (c & 0x3f)));
         }
         else
         {
             // 3 char, 16 bits
             AddByte((byte)(0xe0 | ((c >> 12))));
             AddByte((byte)(0x80 | ((c >> 6) & 0x3f)));
             AddByte((byte)(0x80 | (c & 0x3f)));
         }
     }
     else // char-oriented
     {
         // ensure capacity
         int olen = charbuf.Length; // old length
         if (count >= olen)
         {
             int    nlen = olen * 4; // new length
             char[] obuf = charbuf;
             charbuf = BufferUtility.GetCharBuffer(nlen);
             Array.Copy(obuf, 0, charbuf, 0, olen);
             BufferUtility.Return(obuf);
         }
         charbuf[count++] = c;
     }
 }
示例#14
0
        public void Dispose()
        {
            if (!disposed)
            {
                // return to pool
                if (sql != null)
                {
                    BufferUtility.Return(sql);
                }

                // commit ongoing transaction
                if (transact != null && !transact.IsCompleted)
                {
                    transact.Commit();
                }

                reader?.Dispose();
                command.Dispose();
                connection.Dispose();
                // indicate that the instance has been disposed.
                disposed = true;
            }
        }