コード例 #1
0
        public void Listen(string topic)
        {
            List <NamedPipeServerStream> addServer(string p)
            {
                var pipepool = new List <NamedPipeServerStream>();
                var pipe     = PipelineStreamFactory.GetPipelineStream <NamedPipeServerStream>(topic, true);

                pipepool.Add(pipe);
                var thread = new Thread(new ParameterizedThreadStart(tpc =>
                {
                    while (true)
                    {
                        //try get  server connections
                        if (_servers.TryGetValue((string)tpc, out var pipepoolInstance))
                        {
                            bool noNewConnection = true;
                            foreach (var connectedPipe in usedPipeline)
                            {
                                if (!connectedPipe.IsConnected)
                                {
                                    pipepoolInstance.Remove(connectedPipe);
                                }
                            }
                            foreach (var namedPipeServerStream in pipepoolInstance)
                            {
                                if (!usedPipeline.Contains(namedPipeServerStream) && !namedPipeServerStream.IsConnected)
                                {
                                    noNewConnection = false;
                                    namedPipeServerStream.WaitForConnection();
                                    usedPipeline.Add(namedPipeServerStream);
                                }
                            }
                            if (noNewConnection)
                            {
                                pipepoolInstance.Add(PipelineStreamFactory.GetPipelineStream <NamedPipeServerStream>(topic, true));
                                Thread.Sleep(10);
                            }
                        }
                        else
                        {
                            Thread.Sleep(10);
                        }
                    }
                }));

                _connectionThreads.Add(p, thread);
                thread.Start(p);
                return(pipepool);
            }

            List <NamedPipeServerStream> updateServer(string p, List <NamedPipeServerStream> s)
            {
                return(s);
            }

            _servers.AddOrUpdate(topic, addServer, updateServer);
        }
コード例 #2
0
        /// <summary>
        /// 为每个话题单开一个线程 和一个 管道来传送数据
        /// </summary>
        /// <param name="topicObj"></param>
        private void runForTopic(object topicObj)
        {
            string topic = topicObj.ToString();
            NamedPipeClientStream client = PipelineStreamFactory.GetPipelineStream <NamedPipeClientStream>(topic, false);

            Console.WriteLine("connecting the server " + topic);
            client.Connect();
            Console.WriteLine("connected the server " + topic);
            while (true)
            {
                if (!client.IsConnected)
                {
                    client = PipelineStreamFactory.GetPipelineStream <NamedPipeClientStream>(topic, false);
                    client.Connect();
                }
                StreamReader sr      = new StreamReader(client);
                string       message = "";
                try
                {
                    message = sr.ReadLine();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Console.WriteLine("recieve message from " + topic + " " + message);
                if (message != null)
                {
                    var msg = JsonConvert.DeserializeObject <dynamic>(message);
                    if (_topicHandlers.TryGetValue(topic, out var value))
                    {
                        foreach (var notificationHandler in value)
                        {
                            notificationHandler.NotifiedBy(msg, topic);
                        }
                    }
                    else
                    {
                        Thread.Sleep(1);
                    }
                }
                else
                {
                    Thread.Sleep(1);
                }
            }
        }