Пример #1
0
 public int SubPubService()
 {
     if (ModSubPub.Count > 0)
     {
         foreach (SubPub sp in ModSubPub)
         {
             SubPubRun spr = new SubPubRun();
             spr.name         = sp.pub.name;
             spr.Rthread      = new Thread(new ParameterizedThreadStart(SingleSubPubEntry));
             spr.Running      = true;
             spr.Working      = false;
             spr.Rthread.Name = spr.name;
             spr.Rthread.Start(spr.name);
             spr.pub  = sp.pub;
             spr.subs = sp.subs;
             SubPubRM.Add(spr.name, spr);
         }
     }
     return(0);
 }
Пример #2
0
        private void SingleSubPubEntry(object pubname)
        {
            string pn = (string)pubname;


            if (SubPubRM.ContainsKey(pn))
            {
                SubPubRun spr = SubPubRM[pn];
                using (PublisherSocket publisher = new PublisherSocket())
                    using (SubscriberSocket subsciber = new SubscriberSocket())
                    {
                        //List<string> EndPointl = new List<string>();
                        string e = string.Empty;
                        foreach (Point p in spr.subs)
                        {
                            e = "tcp://" + p.ip + ":" + p.port;
                            subsciber.Connect(e);
                            subsciber.Subscribe("");
                        }

                        e = "tcp://*:" + spr.pub.port;
                        try
                        {
                            publisher.Bind(e);
                        }
                        catch (Exception err)
                        {
                            Logging.logger.Error(ModName + " bind socket failed " + " " + err.Message);
                            throw (err);
                        }
                        spr.pubsock = publisher;
                        spr.subsock = subsciber;
                        string received;
                        string pubdata;
                        bool   result;
                        spr.Working = true;
                        while (spr.Running)
                        {
                            received = string.Empty;
                            result   = subsciber.TryReceiveFrameString(out received);

                            if (result == true)
                            {
                                pubdata = string.Empty;
                                try
                                {
                                    pubdata = this.Entry4SubPubData(spr.pub.name, received);
                                }
                                catch (Exception err)
                                {
                                    Logging.logger.Error(err.Message);
                                    pubdata = string.Empty;
                                    throw (err);
                                }
                                if (pubdata != null)
                                {
                                    publisher.SendFrame(pubdata);
                                }
                            }

                            DelayTime();
                        }
                        spr.Working = false;
                        publisher.Close();
                        subsciber.Close();
                    }
            }
            else
            {
                Logging.logger.Error("SingleSubPubEntry can not get pubname " + pubname);
                return;
            }
        }