HaoYueNet/Simple/ServerCore/Manager/ClientManager.cs

216 lines
6.8 KiB
C#
Raw Normal View History

2023-05-25 13:46:54 +08:00
using AxibugProtobuf;
using System.Net.Sockets;
2021-12-10 23:38:40 +08:00
using System.Timers;
2023-05-25 13:46:54 +08:00
namespace ServerCore.Manager
2021-12-10 23:38:40 +08:00
{
public class ClientInfo
{
public long UID { get; set; }
2023-05-25 13:46:54 +08:00
public string Account { get; set; }
2021-12-10 23:38:40 +08:00
public Socket _socket { get; set; }
public bool IsOffline { get; set; } = false;
public DateTime LogOutDT { get; set; }
}
public class ClientManager
{
2023-05-25 13:46:54 +08:00
private List<ClientInfo> ClientList = new List<ClientInfo>();
private Dictionary<Socket, ClientInfo> _DictSocketClient = new Dictionary<Socket, ClientInfo>();
private Dictionary<long?, ClientInfo> _DictUIDClient = new Dictionary<long?, ClientInfo>();
private long TestUIDSeed = 0;
2021-12-10 23:38:40 +08:00
private System.Timers.Timer _ClientCheckTimer;
private long _RemoveOfflineCacheMin;
2023-12-18 12:46:21 +08:00
/// <summary>
/// 初始化并指定检查时间
/// </summary>
/// <param name="ticktime">tick检查毫秒数</param>
/// <param name="RemoveOfflineCache">清理掉线分钟数</param>
2023-05-25 13:46:54 +08:00
public void Init(long ticktime, long RemoveOfflineCacheMin)
2021-12-10 23:38:40 +08:00
{
//换算成毫秒
2023-12-18 12:46:21 +08:00
_RemoveOfflineCacheMin = RemoveOfflineCacheMin;
2021-12-10 23:38:40 +08:00
_ClientCheckTimer = new System.Timers.Timer();
_ClientCheckTimer.Interval = ticktime;
_ClientCheckTimer.AutoReset = true;
_ClientCheckTimer.Elapsed += new ElapsedEventHandler(ClientCheckClearOffline_Elapsed);
_ClientCheckTimer.Enabled = true;
}
2023-05-25 13:46:54 +08:00
public long GetNextUID()
{
return ++TestUIDSeed;
}
private void ClientCheckClearOffline_Elapsed(object sender, ElapsedEventArgs e)
2021-12-10 23:38:40 +08:00
{
DateTime CheckDT = DateTime.Now.AddMinutes(-1 * _RemoveOfflineCacheMin);
ClientInfo[] OfflineClientlist = ClientList.Where(w => w.IsOffline == true && w.LogOutDT < CheckDT).ToArray();
Console.WriteLine("开始清理离线过久的玩家的缓存");
for (int i = 0; i < OfflineClientlist.Length; i++)
{
//to do 掉线处理
RemoveClient(OfflineClientlist[i]);
}
GC.Collect();
}
2023-05-25 13:46:54 +08:00
2021-12-10 23:38:40 +08:00
//通用处理
#region clientlist
2023-05-25 13:46:54 +08:00
public ClientInfo JoinNewClient(Protobuf_Login data, Socket _socket)
{
//也许这个函数需加lock
ClientInfo cinfo = GetClientForSocket(_socket);
//如果连接还在
if (cinfo != null)
{
cinfo.IsOffline = false;
}
else
{
cinfo = new ClientInfo()
{
UID = GetNextUID(),
_socket = _socket,
Account = data.Account,
IsOffline = false,
};
AddClient(cinfo);
}
return cinfo;
}
2021-12-10 23:38:40 +08:00
/// <summary>
/// 增加用户
/// </summary>
/// <param name="client"></param>
2023-05-25 13:46:54 +08:00
void AddClient(ClientInfo clientInfo)
2021-12-10 23:38:40 +08:00
{
try
{
2023-05-25 13:46:54 +08:00
Console.WriteLine("追加连接玩家 UID=>" + clientInfo.UID + " | " + clientInfo.Account);
2021-12-10 23:38:40 +08:00
lock (ClientList)
{
_DictUIDClient.Add(clientInfo.UID, clientInfo);
_DictSocketClient.Add(clientInfo._socket, clientInfo);
ClientList.Add(clientInfo);
}
}
catch (Exception ex)
{
ex.ToString();
}
}
2023-05-25 13:46:54 +08:00
2021-12-10 23:38:40 +08:00
/// <summary>
/// 清理连接
/// </summary>
/// <param name="client"></param>
public void RemoveClient(ClientInfo client)
{
lock (ClientList)
{
2023-05-25 13:46:54 +08:00
if (_DictUIDClient.ContainsKey(client.UID))
2021-12-10 23:38:40 +08:00
_DictUIDClient.Remove(client.UID);
if (_DictSocketClient.ContainsKey(client._socket))
_DictSocketClient.Remove(client._socket);
2023-05-25 13:46:54 +08:00
2021-12-10 23:38:40 +08:00
ClientList.Remove(client);
}
}
public ClientInfo GetClientForSocket(Socket sk)
{
return _DictSocketClient.ContainsKey(sk) ? _DictSocketClient[sk] : null;
}
/// <summary>
/// 获取在线玩家
/// </summary>
/// <returns></returns>
public List<ClientInfo> GetOnlineClientList()
{
return ClientList.Where(w => w.IsOffline == false).ToList();
}
/// <summary>
/// 设置玩家离线
/// </summary>
/// <param name="sk"></param>
public void SetClientOfflineForSocket(Socket sk)
{
if (!_DictSocketClient.ContainsKey(sk))
return;
2023-05-25 13:46:54 +08:00
Console.WriteLine("标记玩家UID" + _DictSocketClient[sk].UID + "为离线");
2021-12-10 23:38:40 +08:00
_DictSocketClient[sk].IsOffline = true;
_DictSocketClient[sk].LogOutDT = DateTime.Now;
}
public void RemoveClientForSocket(Socket sk)
{
if (!_DictSocketClient.ContainsKey(sk))
return;
RemoveClient(_DictSocketClient[sk]);
}
#endregion
2023-05-25 13:46:54 +08:00
public void ClientSendALL(int CMDID, int ERRCODE, byte[] data)
{
ClientSend(ClientList, CMDID, ERRCODE, data);
}
2021-12-10 23:38:40 +08:00
/// <summary>
/// 给一组用户发送数据
/// </summary>
/// <param name="_toclientlist"></param>
/// <param name="CMDID"></param>
/// <param name="ERRCODE"></param>
/// <param name="data"></param>
public void ClientSend(List<ClientInfo> _toclientlist, int CMDID, int ERRCODE, byte[] data)
{
2023-05-25 13:46:54 +08:00
for (int i = 0; i < _toclientlist.Count(); i++)
2021-12-10 23:38:40 +08:00
{
if (_toclientlist[i] == null || _toclientlist[i].IsOffline)
continue;
2023-05-25 13:46:54 +08:00
ServerManager.g_SocketMgr.SendToSocket(_toclientlist[i]._socket, CMDID, ERRCODE, data);
2021-12-10 23:38:40 +08:00
}
}
public void ClientSend(Socket _socket, int CMDID, int ERRCODE, byte[] data)
{
//Console.WriteLine("发送数据 CMDID->"+ CMDID);
ServerManager.g_SocketMgr.SendToSocket(_socket, CMDID, ERRCODE, data);
}
/// <summary>
/// 给一个连接发送数据
/// </summary>
/// <param name="_c"></param>
/// <param name="CMDID"></param>
/// <param name="ERRCODE"></param>
/// <param name="data"></param>
public void ClientSend(ClientInfo _c, int CMDID, int ERRCODE, byte[] data)
{
if (_c == null || _c.IsOffline)
return;
ServerManager.g_SocketMgr.SendToSocket(_c._socket, CMDID, ERRCODE, data);
}
2023-05-25 13:46:54 +08:00
public int GetOnlineClient()
{
return ClientList.Where(w => !w.IsOffline).Count();
}
2021-12-10 23:38:40 +08:00
}
}