Exemple #1
0
 private void ConnectTask(string host, TcpClient client)
 {
     Task.Factory.StartNew(() =>
     {
         OnConnect?.Invoke(host);//委托:已连接
         while (client.Connected)
         {
             try
             {
                 TcpDataModel model = TcpStreamHelper.Read(client);
                 if (model != null)
                 {
                     if (model.Type == int.MaxValue)
                     {
                         //返回心跳
                         Write(new TcpDataModel()
                         {
                             Type = int.MaxValue
                         });
                     }
                     else
                     {
                         ReceiveMessage(host, model);//委托:接收消息
                     }
                 }
             }
             catch { }
             //Sleep.S(1);
         }
         client.Close();
         OnDisconnect?.Invoke(host);//委托:断开连接
     });
     //lstn.BeginAcceptTcpClient(new AsyncCallback(acceptCallback), lstn);
 }
Exemple #2
0
        private void ConnectTask(string host, TcpClient client)
        {
            TcpClientInfo clientInfo    = TcpClientManager.GetInfoByHost(host);
            DateTime      HeartbeatTime = DateTime.Now;

            //发送心跳
            Task.Factory.StartNew(() =>
            {
                while (client.Connected)
                {
                    TcpDataModel model = new TcpDataModel()
                    {
                        Type = int.MaxValue
                    };
                    TcpStreamHelper.Write(client, model);

                    Sleep.S(5);
                    //if (DateTime.Now.AddSeconds(-10) > HeartbeatTime)
                    //    client.Close();
                    Sleep.S(5);
                }
            });
            //接收消息
            Task.Factory.StartNew(() =>
            {
                OnConnectAction?.Invoke(clientInfo);//委托:已连接
                while (client.Connected)
                {
                    try
                    {
                        TcpDataModel model = TcpStreamHelper.Read(client);
                        if (model != null)
                        {
                            if (model.Type == int.MaxValue)
                            {
                                //过滤心跳
                                HeartbeatTime = DateTime.Now;
                            }
                            else
                            {
                                TcpClientManager.UpdateDownloadFlowCount(host, model.Data.Length);
                                OnReceiveAction(clientInfo, model);//委托:接收消息
                            }
                        }
                    }
                    catch { }
                    //Sleep.S(1);
                }
                client.Close();
                TcpClientManager.RemoveByHost(host);
                OnDisconnectAction?.Invoke(clientInfo);//委托:断开连接
            });
        }
Exemple #3
0
        private void ConnectTask(string host, TcpClient client)
        {
            DateTime HeartbeatTime = DateTime.Now;

            //发送心跳
            Task.Factory.StartNew(() =>
            {
                while (client.Connected)
                {
                    TcpDataModel model = new TcpDataModel()
                    {
                        Type = int.MaxValue
                    };
                    TcpStreamHelper.Write(client, model);

                    Sleep.S(5);
                    //if (DateTime.Now.AddSeconds(-10) > HeartbeatTime)
                    //    client.Close();
                    Sleep.S(5);
                }
            });
            //接收消息
            Task.Factory.StartNew(() =>
            {
                OnConnect?.Invoke(host);//委托:已连接
                while (client.Connected)
                {
                    try
                    {
                        TcpDataModel model = TcpStreamHelper.Read(client);
                        if (model != null)
                        {
                            if (model.Type == int.MaxValue)
                            {
                                //过滤心跳
                                HeartbeatTime = DateTime.Now;
                            }
                            else
                            {
                                ReceiveMessage(host, model);//委托:接收消息
                            }
                        }
                    }
                    catch { }
                    //Sleep.S(1);
                }
                client.Close();
                Clients_Del(host);
                OnDisconnect?.Invoke(host);//委托:断开连接
            });
        }
Exemple #4
0
        private void ConnectTask(TcpClient client)
        {
            Task.Factory.StartNew(() =>
            {
                OnConnectAction?.Invoke();//委托:已连接
                while (client.Connected)
                {
                    try
                    {
                        TcpDataModel model = TcpStreamHelper.Read(client);
                        if (model != null)
                        {
                            if (model.Type == int.MaxValue)
                            {
                                //返回心跳
                                Write(new TcpDataModel(int.MaxValue));
                            }
                            else
                            {
                                //优先调用默认接收消息方法Action
                                OnReceiveAction?.Invoke(model);

                                //调用同步处理委托方法
                                if (Ls.Ok(SyncFunction))
                                {
                                    for (var i = 0; i < SyncFunction.Count; i++)
                                    {
                                        bool flag = SyncFunction.TryDequeue(out Tuple <int, Action <TcpDataModel> > fun);
                                        if (flag)
                                        {
                                            Task.Factory.StartNew(() => { fun.Item2?.Invoke(model); });
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch { }
                    //Sleep.S(1);
                }
                client.Close();
                OnDisconnectAction?.Invoke();//委托:断开连接
            });
            //lstn.BeginAcceptTcpClient(new AsyncCallback(acceptCallback), lstn);
        }