Example #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);
 }
Example #2
0
 /// <summary>
 /// 发送数据
 /// </summary>
 /// <param name="model">数据模型</param>
 public void Write(TcpDataModel model)
 {
     if (this.Client != null && this.Client.Connected)
     {
         bool flag = TcpStreamHelper.Write(Client, model);
     }
 }
Example #3
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="model">数据模型</param>
        public bool Write(TcpDataModel model)
        {
            bool flag = false;

            if (this.Client != null && this.Client.Connected)
            {
                flag = TcpStreamHelper.Write(Client, model);
            }
            return(flag);
        }
Example #4
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="host">主机地址</param>
        /// <param name="model">数据模型</param>
        public void Write(string host, TcpDataModel model)
        {
            var dictionary = Clients_Get(host);

            if (dictionary != null && dictionary.Client != null)
            {
                if (dictionary.Client.Connected)
                {
                    bool flag = TcpStreamHelper.Write(dictionary.Client, model);
                }
            }
        }
Example #5
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);//委托:断开连接
            });
        }
Example #6
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="host">主机地址</param>
        /// <param name="model">数据模型</param>
        public void Write(string host, TcpDataModel model)
        {
            var dictionary = TcpClientManager.GetInfoByHost(host);

            if (dictionary != null && dictionary.Client != null)
            {
                if (dictionary.Client.Connected)
                {
                    TcpClientManager.UpdateUploadFlowCount(host, model.Data.Length);
                    bool flag = TcpStreamHelper.Write(dictionary.Client, model);
                }
            }
        }
Example #7
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);//委托:断开连接
            });
        }
Example #8
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);
        }
Example #9
0
        /// <summary>
        /// 发送数据(action禁止使用阻塞操作,必须新建task线程操作)
        /// </summary>
        /// <param name="model">数据模型</param>
        /// <param name="actionType">事件驱动处理类型</param>
        /// <param name="action">事件驱动处理方法</param>
        public bool Write(TcpDataModel model, int?actionType = 0, Action <TcpDataModel> action = null)
        {
            bool flag = false;

            if (Client != null && Client.Connected)
            {
                flag = TcpStreamHelper.Write(Client, model);
            }
            if (flag)
            {
                if (actionType != null && action != null)
                {
                    int type = actionType.GetValueOrDefault();
                    SyncFunction.Enqueue(new Tuple <int, Action <TcpDataModel> >(type, action));
                }
            }
            return(flag);
        }