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); }
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; }
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; }
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; }
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()); } }
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, TIMEOUT); e.Add(channel); return e; }
public void Send(Message message, IChannelSender sender) { this.Send(message, sender, null); }
private bool IsError(Message msg) { return msg.StatusCode > 0 || !string.IsNullOrEmpty(msg.StatusPhase); }
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; }
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); } }