Esempio n. 1
0
        /// <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 { }
        }
Esempio n. 2
0
        /// <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 { }
        }
Esempio n. 3
0
        /// <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 { }
        }
Esempio n. 4
0
        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);
                    }
                }
            }
        }
Esempio n. 5
0
        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");
        }
Esempio n. 6
0
 /// <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);
     }
 }
Esempio n. 7
0
 /// <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 { }
 }
Esempio n. 8
0
        /// <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;
        }