| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- using Azylee.Core.DataUtils.CollectionUtils;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Sockets;
- using System.Text;
- namespace Azylee.YeahWeb.SocketUtils.TcpUtils
- {
- /// <summary>
- /// 客户端信息管理器
- /// </summary>
- public class TcpClientManager
- {
- private int HostNumber { get; set; }
- private List<TcpClientInfo> TcpClientList { get; set; }
- /// <summary>
- /// 构造方法(初始化标号起始标记6位,初始化客户端列表)
- /// </summary>
- public TcpClientManager()
- {
- HostNumber = 100000;
- TcpClientList = new List<TcpClientInfo>();
- }
- #region 统计项
- /// <summary>
- /// 当前连接客户端总数
- /// </summary>
- /// <returns></returns>
- public int CountClient()
- {
- return TcpClientList.Count();
- }
- #endregion
- #region 判定项
- public bool IsExistByNumber(int number)
- {
- if (Ls.Ok(TcpClientList))
- {
- return TcpClientList.Any(x => x.Number == number);
- }
- return false;
- }
- public bool IsExistByHost(string host)
- {
- if (Ls.Ok(TcpClientList))
- {
- return TcpClientList.Any(x => x.Host == host);
- }
- return false;
- }
- public bool IsConnectKey(string host, string key)
- {
- var item = GetInfoByHost(host);
- if (item != null) return item.ConnectKey == key;
- return false;
- }
- #endregion
- #region 查询项
- public List<TcpClientInfo> GetAll()
- {
- return TcpClientList;
- }
- public TcpClientInfo GetInfoByNumber(int number)
- {
- if (IsExistByNumber(number))
- {
- return TcpClientList.FirstOrDefault(x => x.Number == number);
- }
- return null;
- }
- public TcpClientInfo GetInfoByHost(string host)
- {
- TcpClientInfo client = null;
- try
- {
- if (IsExistByHost(host))
- {
- client = TcpClientList.FirstOrDefault(x => x.Host == host);
- }
- }
- catch { }
- return client;
- }
- #endregion
- #region 添加项
- /// <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;
- }
- #endregion
- #region 更新项
- /// <summary>
- /// 更新 ConnectKey 连接秘钥
- /// </summary>
- /// <param name="host"></param>
- /// <param name="s"></param>
- /// <returns></returns>
- public bool UpdateConnectKey(string host, string s)
- {
- if (IsExistByHost(host))
- {
- for (var i = 0; i < TcpClientList.Count; i++)
- {
- if (TcpClientList[i].Host == host)
- {
- TcpClientList[i].ConnectKey = s;
- return true;
- }
- }
- }
- return false;
- }
- /// <summary>
- /// 更新 UserEmail 用户邮箱
- /// </summary>
- /// <param name="host"></param>
- /// <param name="s"></param>
- /// <returns></returns>
- public bool UpdateUserEmail(string host, string s)
- {
- if (IsExistByHost(host))
- {
- for (var i = 0; i < TcpClientList.Count; i++)
- {
- if (TcpClientList[i].Host == host)
- {
- TcpClientList[i].UserEmail = s;
- return true;
- }
- }
- }
- return false;
- }
- /// <summary>
- /// 更新 AccessCode 权限编码
- /// </summary>
- /// <param name="host"></param>
- /// <param name="s"></param>
- /// <returns></returns>
- public bool UpdateAccessCode(string host, string s)
- {
- if (IsExistByHost(host))
- {
- for (var i = 0; i < TcpClientList.Count; i++)
- {
- if (TcpClientList[i].Host == host)
- {
- TcpClientList[i].AccessCode = s;
- return true;
- }
- }
- }
- return false;
- }
- /// <summary>
- /// 更新 MachineID 主机ID
- /// </summary>
- /// <param name="host"></param>
- /// <param name="s"></param>
- /// <returns></returns>
- public bool UpdateMachineID(string host, string s)
- {
- if (IsExistByHost(host))
- {
- for (var i = 0; i < TcpClientList.Count; i++)
- {
- if (TcpClientList[i].Host == host)
- {
- TcpClientList[i].MachineID = s;
- return true;
- }
- }
- }
- return false;
- }
- /// <summary>
- /// 更新 MachineName 主机名称
- /// </summary>
- /// <param name="host"></param>
- /// <param name="s"></param>
- /// <returns></returns>
- public bool UpdateMachineName(string host, string s)
- {
- if (IsExistByHost(host))
- {
- for (var i = 0; i < TcpClientList.Count; i++)
- {
- if (TcpClientList[i].Host == host)
- {
- TcpClientList[i].MachineName = s;
- return true;
- }
- }
- }
- return false;
- }
- /// <summary>
- /// 更新 UserName 用户名
- /// </summary>
- /// <param name="host"></param>
- /// <param name="s"></param>
- /// <returns></returns>
- public bool UpdateUserName(string host, string s)
- {
- if (IsExistByHost(host))
- {
- for (var i = 0; i < TcpClientList.Count; i++)
- {
- if (TcpClientList[i].Host == host)
- {
- TcpClientList[i].UserName = s;
- return true;
- }
- }
- }
- return false;
- }
- /// <summary>
- /// 更新 AppCode 应用程序编码
- /// </summary>
- /// <param name="host"></param>
- /// <param name="s"></param>
- /// <returns></returns>
- public bool UpdateAppCode(string host, string s)
- {
- if (IsExistByHost(host))
- {
- for (var i = 0; i < TcpClientList.Count; i++)
- {
- if (TcpClientList[i].Host == host)
- {
- TcpClientList[i].AppCode = s;
- return true;
- }
- }
- }
- return false;
- }
- /// <summary>
- /// 更新上行流量
- /// </summary>
- /// <param name="host"></param>
- /// <param name="flow"></param>
- /// <returns></returns>
- public bool UpdateUploadFlowCount(string host, long flow)
- {
- if (IsExistByHost(host))
- {
- for (var i = 0; i < TcpClientList.Count; i++)
- {
- if (TcpClientList[i].Host == host)
- {
- TcpClientList[i].UploadFlowCount += flow;
- TcpClientList[i].LastUploadTime = DateTime.Now;
- return true;
- }
- }
- }
- return false;
- }
- /// <summary>
- /// 更新下行流量
- /// </summary>
- /// <param name="host"></param>
- /// <param name="flow"></param>
- /// <returns></returns>
- public bool UpdateDownloadFlowCount(string host, long flow)
- {
- if (IsExistByHost(host))
- {
- for (var i = 0; i < TcpClientList.Count; i++)
- {
- if (TcpClientList[i].Host == host)
- {
- TcpClientList[i].DownloadFlowCount += flow;
- TcpClientList[i].LastDownloadTime = DateTime.Now;
- return true;
- }
- }
- }
- return false;
- }
- /// <summary>
- /// 更新心跳时间
- /// </summary>
- /// <param name="host"></param>
- /// <returns></returns>
- public bool UpdateHeartbeatTime(string host)
- {
- if (IsExistByHost(host))
- {
- for (var i = 0; i < TcpClientList.Count; i++)
- {
- if (TcpClientList[i].Host == host)
- {
- TcpClientList[i].HeartbeatTime = DateTime.Now;
- return true;
- }
- }
- }
- return false;
- }
- #endregion
- #region 删除项
- public int RemoveByNumber(int number)
- {
- try
- {
- return TcpClientList.RemoveAll(x => x.Number == number);
- }
- catch { }
- return 0;
- }
- public int RemoveByHost(string host)
- {
- try
- {
- return TcpClientList.RemoveAll(x => x.Host == host);
- }
- catch { }
- return 0;
- }
- #endregion
- }
- }
|