예제 #1
0
 /// <summary>
 /// 添加或更新到客户端列表
 /// </summary>
 public int AddOrUpdate(string host, TcpClient client)
 {
     try
     {
         HostNumber++;
         var item = TcpClientList.FirstOrDefault(x => x.Host == host);
         if (item == null)
         {
             var model = new TcpClientInfo()
             {
                 Number      = HostNumber,
                 Host        = host,
                 Client      = client,
                 IsConnect   = true,
                 ConnectTime = DateTime.Now
             };
             TcpClientList.Add(model);
             return(model.Number);
         }
         else
         {
             item.Client = client;
         }
     }
     catch { }
     return(0);
 }
예제 #2
0
        /// <summary>
        /// 添加或更新到客户端列表
        /// </summary>
        public int AddOrUpdate(string host, TcpClient client)
        {
            try
            {
                HostNumber++;
                var item = TcpClientList.FirstOrDefault(x => x.Host == host);
                if (item == null)
                {
                    string ip          = "";
                    int    ipFlagIndex = host.IndexOf(":");
                    if (ipFlagIndex > 0)
                    {
                        ip = host.Substring(0, ipFlagIndex);
                    }

                    var model = new TcpClientInfo()
                    {
                        Number      = HostNumber,
                        IP          = ip,
                        Host        = host,
                        Client      = client,
                        IsConnect   = true,
                        ConnectTime = DateTime.Now
                    };
                    TcpClientList.Add(model);
                    return(model.Number);
                }
                else
                {
                    item.Client = client;
                }
            }
            catch { }
            return(0);
        }
예제 #3
0
파일: TcppServer.cs 프로젝트: znw333/Fork
        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);//委托:断开连接
            });
        }
예제 #4
0
        public TcpClientInfo GetInfoByHost(string host)
        {
            TcpClientInfo client = null;

            try
            {
                if (IsExistByHost(host))
                {
                    client = TcpClientList.FirstOrDefault(x => x.Host == host);
                }
            }
            catch { }
            return(client);
        }