Esempio n. 1
0
        public Message Recv(int timeout)
        {
            this.ConnectIfNeeded();
            if (this.msgidMatch != null && this.resultTable.ContainsKey(this.msgidMatch))
            {
                Message msg = this.resultTable[this.msgidMatch];
                this.resultTable.Remove(this.msgidMatch);
                return(msg);
            }
            this.client.ReceiveTimeout = timeout;

            while (true)
            {
                byte[] buf = new byte[4096];
                int    n   = this.stream.Read(buf, 0, buf.Length);
                this.readBuf.Put(buf, 0, n);

                IoBuffer tempBuf = this.readBuf.Duplicate();
                tempBuf.Flip(); //to read mode
                Message msg = Message.Decode(tempBuf);
                if (msg == null)
                {
                    continue;
                }

                this.readBuf.Move(tempBuf.Position);

                if (this.msgidMatch != null && !this.msgidMatch.Equals(msg.MsgId))
                {
                    this.resultTable[this.msgidMatch] = msg;
                    continue;
                }

                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("Recv: {0}", msg);
                }
                return(msg);
            }
        }
Esempio n. 2
0
        public void Encode(IoBuffer buf)
        {
            buf.Put("{0}\r\n", this.meta);
            foreach (KeyValuePair <string, string> e in this.head)
            {
                buf.Put("{0}: {1}\r\n", e.Key, e.Value);
            }
            string lenKey = "content-length";

            if (!this.head.ContainsKey(lenKey))
            {
                int bodyLen = this.body == null ? 0 : this.body.Length;
                buf.Put("{0}: {1}\r\n", lenKey, bodyLen);
            }

            buf.Put("\r\n");

            if (this.body != null)
            {
                buf.Put(this.body);
            }
        }