/// <summary> /// Send a notification /// </summary> /// <param name="target"></param> /// <param name="data"></param> public void Notify(string target, byte[] data) { try { var message = new Message(); message.Type = MessageType.Notify; message.Source = this.Name; message.Target = target; message.Data = data; message.WriteTo(m_Stream); } catch { } }
/// <summary> /// Response for the request /// </summary> /// <param name="data"></param> public void Reply(byte[] data) { try { var reply = new Message(); reply.Type = MessageType.Reply; reply.Source = this.Message.Target; reply.Target = this.Message.Source; reply.SessionId = this.Message.SessionId; reply.Data = data; reply.WriteTo(m_Stream); } catch { } }
/// <summary> /// Connect to Broker /// </summary> /// <param name="host"></param> /// <param name="port"></param> public void Connect(string host, int port) { try { m_Client.Connect(host, port); m_Stream = m_Client.GetStream(); var message = new Message(); message.Type = MessageType.ClientLogin; message.Source = this.Name; message.Target = string.Empty; message.Path = string.Empty; message.WriteTo(m_Stream); } catch { } }
/// <summary> /// Connect to Broker /// </summary> /// <param name="host"></param> /// <param name="port"></param> public void Connect(string host, int port) { try { m_Client.Connect(host, port); m_Stream = m_Client.GetStream(); //Login var message = new Message(); message.Type = MessageType.ClientLogin; message.Source = this.Name; message.Target = string.Empty; message.WriteTo(m_Stream); //Begin reading var lengthBytes = new byte[4]; m_Stream.BeginRead(lengthBytes, 0, lengthBytes.Length, new AsyncCallback(ReadCallback), lengthBytes); } catch { } }
public static Message Read(NetworkStream stream, byte[] lengthBytes, int alreadyRead) { Message message = null; try { //Read length while(alreadyRead < lengthBytes.Length) { alreadyRead += stream.Read(lengthBytes, alreadyRead, lengthBytes.Length - alreadyRead); } int messageLength = BitConverter.ToInt32(lengthBytes, 0); //Read full packet alreadyRead = 0; var messageBytes = new byte[messageLength]; do { alreadyRead += stream.Read(messageBytes, alreadyRead, messageBytes.Length - alreadyRead); } while(alreadyRead < messageBytes.Length); //Deserialize the message message = new Message(messageBytes); } catch { } return message; }
private void OnEndpointReceiveMessage(string name, Message message) { try { //Put message in queue and notify dispatch thread to start work Console.WriteLine("{0} =={3}==> {1}: {2}", message.Source, message.Target, Encoding.UTF8.GetString(message.Data), message.Type); m_Messages.Enqueue(message); m_MessageArrived.Set(); } catch { } }
private void DeliverMessage(Message message) { //If target exists, deliver it if(m_Endpoints.ContainsKey(message.Target)) { m_Endpoints[message.Target].WriteMessage(message); } //Deliver it to other Brokers else { //Avoid loop if(!message.Path.Contains("/" + m_Name)) { //Add route path message.Path += "/" + this.m_Name; //To parent if(m_Client.Connected) { lock(m_Client) { message.WriteTo(m_ClientStream); } } //To children foreach(var broker in m_Brokers.Values) { broker.WriteMessage(message); } } } }
private void ConnectToBroker(object obj) { while(!m_Client.Connected && m_Running) { try { //Connect to another Broker m_Client = new TcpClient(); m_Client.Connect(m_ParentAddress, m_ParentPort); m_ClientStream = m_Client.GetStream(); //Login var message = new Message(); message.Type = MessageType.BrokerLogin; message.Source = m_Name; message.Target = string.Empty; message.WriteTo(m_ClientStream); //Begin to read message from parent Broker var lengthBytes = new byte[4]; m_ClientStream.BeginRead(lengthBytes, 0, lengthBytes.Length, new AsyncCallback(this.ReadCallback), lengthBytes); } catch { } Thread.Sleep(2000); } Console.WriteLine("Connect succeed"); }
/// <summary> /// Thread for auto reply overdue requests /// </summary> private void CheckOverdue() { while(m_Running) { foreach(string item in m_PendingRequests.Keys) { if(!m_Running) break; //超时的自动发回复消息 if(m_PendingRequests[item] < DateTime.Now && m_Endpoints.ContainsKey(item)) { Console.WriteLine("Request from {0} overdue", item); var autoReply = new Message(); autoReply.Type = MessageType.Error; autoReply.Source = "[BROKER]"; autoReply.Target = item; autoReply.SessionId = m_Endpoints[item].CurrentSessionId; autoReply.Data = Encoding.UTF8.GetBytes("OVERTIME"); DateTime value = DateTime.Now; m_PendingRequests.TryRemove(item, out value); lock(m_Endpoints[item]) { m_Endpoints[item].WriteMessage(autoReply); } } } Thread.Sleep(500); } }
/// <summary> /// Serialize the message and write to stream /// </summary> /// <param name="message"></param> public void WriteMessage(Message message) { lock(m_Client) { message.WriteTo(m_Stream); } }
/// <summary> /// Send a request and wait for reply /// </summary> /// <param name="target"></param> /// <param name="data"></param> /// <param name="timeout"></param> /// <returns></returns> public byte[] Request(string target, byte[] data, short timeout = 30) { byte[] response = null; try { //Send request var message = new Message(); message.Type = MessageType.Request; message.Source = this.Name; message.Target = target; message.Data = data; message.Timeout = timeout; message.WriteTo(m_Stream); //Wait for reply var reply = Message.Read(m_Stream); if(reply.Type == MessageType.Reply) { response = reply.Data; } else if(reply.Type == MessageType.Error) { string reason = Encoding.UTF8.GetString(reply.Data); throw new Exception(reason); } } catch { } return response; }
public MessageReceivedEventArgs(NetworkStream stream, Message message) { m_Stream = stream; Message = message; }