Exemple #1
0
 public void ProcessingMsg(Message msg, NetworkStream stream)
 {
     if (msg.TypeMsg != "die" && msg.TypeMsg != "willdie")
     {
         if (msg.IsSender)
         {
             queueDictionary.AddMessage(msg);
         }
         else
         {
             queueDictionary.SendMessageSub(msg, stream);
         }
     }
     else if (msg.TypeMsg == "willdie")
     {
         queueDictionary.SendWillDieMessage(msg);
     }
     else if (msg.TypeMsg == "die")
     {
         MessageDictionary.SendDieMessage(msg);
     }
 }
Exemple #2
0
        //public static Dictionary<Client, DateTime> Publishers { get; set; }
        private Broker()
        {
            queueDictionary = new MessageDictionary();
            Subscribers     = new List <Client>();
            Publishers      = new List <Client>();

            Task checkPublishers = new Task(() =>
            {
                while (true)
                {
                    if (Publishers.Count > 0)
                    {
                        for (int i = 0; i < Publishers.Count; i++)
                        {
                            try
                            {
                                var retMsg = Encoding.Unicode.GetBytes("Check");
                                //stream.Write(retMsg, 0, retMsg.Length);
                                Publishers[i].client.GetStream().Write(retMsg, 0, retMsg.Length);
                            }
                            catch
                            {
                                MessageDictionary.SendDieMessage(new Message()
                                {
                                    Name = Publishers[i].ClientName
                                });
                                Publishers.Remove(Publishers[i--]);
                            }
                        }
                    }
                    Thread.Sleep(15000);
                }
            });

            checkPublishers.Start();
            //Publishers = new Dictionary<Client, DateTime>();
        }