示例#1
0
        public static void WriteMessage(Stream input, Message message)
        {
            var buffer = new BinaryWriter(input);
            buffer.Write((byte)message.ProtocolVersion);
            buffer.Write((byte)message.MessageType);

            if (message.StatusCode > 0)
            {
                buffer.Write(MessageType.HeaderType.StatusCode);
                buffer.Write(message.StatusCode);
            }
            if (message.StatusPhase != null && message.StatusPhase != "")
            {
                buffer.Write(MessageType.HeaderType.StatusPhrase);
                WriteCountedString(buffer, message.StatusPhase);
            }
            if (message.Flag > 0)
            {
                buffer.Write(MessageType.HeaderType.Flag);
                buffer.Write(message.Flag);
            }
            if (message.Token != null && message.Token != "")
            {
                buffer.Write(MessageType.HeaderType.Token);
                WriteCountedString(buffer, message.Token);
            }
            if (message.Content != null)
            {
                foreach (var i in message.Content)
                    WriteCustomHeader(buffer, i.Key, i.Value);
            }
            buffer.Write(MessageType.HeaderType.EndOfHeaders);
        }
示例#2
0
        public void ReadWriteTest()
        {
            var s = new MemoryStream();
            var msg = new Message();
            msg.MessageType = MessageType.SEND;
            msg.StatusPhase = "error";
            msg.Content = new Dictionary<string, object>();
            msg.Content.Add("byte", (byte)1);
            msg.Content.Add("string", "string");
            msg.Content.Add("int16", (short)123);
            msg.Content.Add("int32", 156);
            msg.Content.Add("int64", 178L);
            msg.Content.Add("date", DateTime.Now);
            MessageIO.WriteMessage(s, msg);

            s.Position = 0;

            var msg2 = MessageIO.ReadMessage(s);
            Assert.AreEqual(msg.MessageType, msg2.MessageType);
            Assert.AreEqual(msg.StatusPhase, msg2.StatusPhase);
            foreach (var i in msg.Content)
            {
                Assert.AreEqual(msg.Content[i.Key], msg2.Content[i.Key]);
                Assert.AreEqual(msg.Content[i.Key].GetType(), msg2.Content[i.Key].GetType());
                Console.WriteLine(i.Key + "=" + msg2.Content[i.Key]);
            }
        }
示例#3
0
 public static Message ReadMessage(Stream input)
 {
     var buffer = new BinaryReader(input);
     Message msg = new Message();
     msg.ProtocolVersion = buffer.ReadByte();
     msg.MessageType = buffer.ReadByte();
     // read kv
     IDictionary<string, object> dict = new Dictionary<string, object>();
     short headerType = buffer.ReadInt16();
     while (headerType != MessageType.HeaderType.EndOfHeaders)
     {
         if (headerType == MessageType.HeaderType.Custom)
             dict.Add(ReadCountedString(buffer), ReadCustomValue(buffer));
         else if (headerType == MessageType.HeaderType.StatusCode)
             msg.StatusCode = buffer.ReadInt32();
         else if (headerType == MessageType.HeaderType.StatusPhrase)
             msg.StatusPhase = ReadCountedString(buffer);
         else if (headerType == MessageType.HeaderType.Flag)
             msg.Flag = buffer.ReadInt32();
         else if (headerType == MessageType.HeaderType.Token)
             msg.Token = ReadCountedString(buffer);
         headerType = buffer.ReadInt16();
     }
     msg.Content = dict;
     return msg;
 }
示例#4
0
 private Message CreateMessage(IDictionary<string, object> message)
 {
     Message msg = new Message();
     msg.MessageType = MessageType.SENDACK;
     msg.Content = message;
     msg.Flag = this._flag;
     msg.Token = this._token;
     return msg;
 }
示例#5
0
 internal IDictionary<string, object> SendAndWait(EndpointProxy e
     , IChannelSender sender
     , Message message
     , int timeout)
 {
     SendCallback callback = new SendCallback(e);
     this.Send(message, sender, callback);
     callback.WaitReturn(timeout);
     if (callback.Error != null)
         throw callback.Error;
     return callback.Return;
 }
示例#6
0
 public void Send(Message message, IChannelSender sender, SendCallback callback)
 {
     if (callback != null)
     {
         message.Flag = System.Threading.Interlocked.Increment(ref this._flag);
         this._callbacks.Add(message.Flag, callback);
     }
     using (var s = new MemoryStream())
     {
         MessageIO.WriteMessage(s, message);
         this.GetChannel(sender).Send(s.ToArray());
     }
 }
示例#7
0
文件: Endpoint.cs 项目: wsky/top-link
 public EndpointProxy GetEndpoint(Identity target, string uri, IDictionary<string, object> extras)
 {
     EndpointProxy e = this.GetEndpoint(target) ?? this.CreateProxy();
     e.Identity = target;
     // always clear, cached proxy will have broken channel
     e.Remove(uri);
     // always reget channel, make sure it's valid
     IClientChannel channel = this.ChannelSelector.GetChannel(new Uri(uri));
     // connect message
     Message msg = new Message();
     msg.MessageType = MessageType.CONNECT;
     IDictionary<string, object> content = new Dictionary<string, object>();
     this.Identity.Render(content);
     // pass extra data
     if (extras != null)
         foreach (var p in extras)
             content.Add(p);
     msg.Content = content;
     this._handler.SendAndWait(e, channel, msg, TIMOUT);
     e.Add(channel);
     return e;
 }
示例#8
0
 private void HandleCallback(SendCallback callback, Message msg, Identity msgFrom)
 {
     if (!callback.Target.Identity.Equals(msgFrom))
     {
         this._log.WarnFormat(Text.E_IDENTITY_NOT_MATCH_WITH_CALLBACK, msgFrom, callback.Target.Identity);
         return;
     }
     if (this.IsError(msg))
         callback.Error = new LinkException(msg.StatusCode, msg.StatusPhase);
     else
         callback.Return = msg.Content;
 }
示例#9
0
 public void Send(Message message, IChannelSender sender)
 {
     this.Send(message, sender, null);
 }
示例#10
0
 private bool IsError(Message msg)
 {
     return msg.StatusCode > 0 || !string.IsNullOrEmpty(msg.StatusPhase);
 }
示例#11
0
        private void HandleConnectAck(SendCallback callback, Message msg)
        {
            if (callback == null)
                throw new LinkException(Text.E_NO_CALLBACK);
            if (this.IsError(msg))
                callback.Error = new LinkException(msg.StatusCode, msg.StatusPhase);
            else
            {
                callback.Return = null;
                // set token for proxy for sending message next time
                callback.Target.Token = msg.Token;
                // store token from target endpoint for receiving it's message
                // next time
                if (this._idByToken.ContainsKey(msg.Token))
                    this._idByToken[msg.Token] = callback.Target.Identity;
                else
                    this._idByToken.Add(msg.Token, callback.Target.Identity);

                this._log.InfoFormat(Text.E_CONNECT_SUCCESS, callback.Target.Identity, msg.Token);
            }
        }