HaoYueTunnel/ServerCore/Manager/ClientManager.cs

239 lines
7.6 KiB
C#
Raw Normal View History

2023-05-25 13:24:51 +08:00
using AxibugProtobuf;
2023-06-15 15:45:58 +08:00
using ServerCore.Common.Enum;
2023-05-25 18:30:22 +08:00
using ServerCore.Event;
2023-05-25 13:24:51 +08:00
using System.Net.Sockets;
2023-05-23 17:57:24 +08:00
using System.Timers;
2023-05-25 18:30:22 +08:00
namespace ServerCore.Manager
2023-05-23 17:57:24 +08:00
{
public class ClientInfo
{
public long UID { get; set; }
2023-05-25 18:30:22 +08:00
public string NickName { get; set; }
2023-05-23 17:57:24 +08:00
public Socket _socket { get; set; }
public bool IsOffline { get; set; } = false;
public DateTime LogOutDT { get; set; }
2023-05-25 18:30:22 +08:00
public int State { get; set; } = 0;
2023-05-23 17:57:24 +08:00
}
public class ClientManager
{
2023-05-25 13:24:51 +08:00
private List<ClientInfo> ClientList = new List<ClientInfo>();
private Dictionary<Socket, ClientInfo> _DictSocketClient = new Dictionary<Socket, ClientInfo>();
2023-05-25 18:30:22 +08:00
private Dictionary<long, ClientInfo> _DictUIDClient = new Dictionary<long, ClientInfo>();
2023-05-25 13:24:51 +08:00
private long TestUIDSeed = 0;
2023-05-23 17:57:24 +08:00
private System.Timers.Timer _ClientCheckTimer;
private long _RemoveOfflineCacheMin;
2023-05-25 18:30:22 +08:00
2023-06-15 15:45:58 +08:00
public ClientManager()
{
//事件
EventSystem.Instance.RegisterEvent<ServerType,Socket>(EEvent.OnSocketDisconnect, OnSocketDisconnect);
}
2023-05-25 18:30:22 +08:00
2023-06-15 15:45:58 +08:00
#region
void OnSocketDisconnect(ServerType serverType, Socket socket)
{
if (serverType != ServerType.MainServer)
return;
2023-05-25 18:30:22 +08:00
2023-06-15 15:45:58 +08:00
RemoveClientBySocket(socket);
}
2023-05-25 18:30:22 +08:00
#endregion
2023-06-15 15:45:58 +08:00
//public void Init(long ticktime, long RemoveOfflineCacheMin)
//{
// //换算成毫秒
// _RemoveOfflineCacheMin = RemoveOfflineCacheMin * 1000;
// _ClientCheckTimer = new System.Timers.Timer();
// _ClientCheckTimer.Interval = ticktime;
// _ClientCheckTimer.AutoReset = true;
// _ClientCheckTimer.Elapsed += new ElapsedEventHandler(ClientCheckClearOffline_Elapsed);
// _ClientCheckTimer.Enabled = true;
//}
2023-05-23 17:57:24 +08:00
public long GetNextUID()
{
return ++TestUIDSeed;
}
2023-05-25 13:24:51 +08:00
2023-06-15 15:45:58 +08:00
//private void ClientCheckClearOffline_Elapsed(object sender, ElapsedEventArgs e)
//{
// DateTime CheckDT = DateTime.Now.AddMinutes(-1 * _RemoveOfflineCacheMin);
// ClientInfo[] OfflineClientlist = ClientList.Where(w => w.IsOffline == true && w.LogOutDT < CheckDT).ToArray();
2023-05-23 17:57:24 +08:00
2023-06-15 15:45:58 +08:00
// Console.WriteLine("开始清理离线过久的玩家的缓存");
// for (int i = 0; i < OfflineClientlist.Length; i++)
// {
// //to do 掉线处理
// RemoveClient(OfflineClientlist[i]);
// }
// GC.Collect();
//}
2023-05-23 17:57:24 +08:00
//通用处理
#region clientlist
2023-05-25 13:24:51 +08:00
public ClientInfo JoinNewClient(Protobuf_Login data, Socket _socket)
2023-05-23 17:57:24 +08:00
{
//也许这个函数需加lock
ClientInfo cinfo = GetClientForSocket(_socket);
//如果连接还在
if (cinfo != null)
{
2023-05-25 13:24:51 +08:00
cinfo.IsOffline = false;
2023-05-23 17:57:24 +08:00
}
else
{
cinfo = new ClientInfo()
{
UID = GetNextUID(),
_socket = _socket,
2023-05-25 18:30:22 +08:00
NickName = data.Account,
2023-05-25 13:24:51 +08:00
IsOffline = false,
2023-05-23 17:57:24 +08:00
};
AddClient(cinfo);
}
2023-05-25 18:30:22 +08:00
EventSystem.Instance.PostEvent(EEvent.OnUserJoin, cinfo.UID);
2023-05-23 17:57:24 +08:00
return cinfo;
}
/// <summary>
/// 增加用户
/// </summary>
/// <param name="client"></param>
void AddClient(ClientInfo clientInfo)
{
try
{
2023-05-25 18:30:22 +08:00
Console.WriteLine("追加连接玩家 UID=>" + clientInfo.UID + " | " + clientInfo.NickName);
2023-05-23 17:57:24 +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:24:51 +08:00
2023-05-23 17:57:24 +08:00
/// <summary>
/// 清理连接
/// </summary>
/// <param name="client"></param>
public void RemoveClient(ClientInfo client)
{
2023-06-15 15:45:58 +08:00
EventSystem.Instance.PostEvent(EEvent.OnUserLeave, client.UID);
2023-05-23 17:57:24 +08:00
lock (ClientList)
{
2023-05-25 13:24:51 +08:00
if (_DictUIDClient.ContainsKey(client.UID))
2023-05-23 17:57:24 +08:00
_DictUIDClient.Remove(client.UID);
if (_DictSocketClient.ContainsKey(client._socket))
_DictSocketClient.Remove(client._socket);
2023-05-25 13:24:51 +08:00
2023-05-23 17:57:24 +08:00
ClientList.Remove(client);
}
}
2023-05-25 18:30:22 +08:00
public ClientInfo GetClientForUID(long UID)
{
return _DictUIDClient.ContainsKey(UID) ? _DictUIDClient[UID] : null;
}
2023-05-23 17:57:24 +08:00
public ClientInfo GetClientForSocket(Socket sk)
{
return _DictSocketClient.ContainsKey(sk) ? _DictSocketClient[sk] : null;
}
/// <summary>
/// 获取在线玩家
/// </summary>
/// <returns></returns>
2023-05-25 18:30:22 +08:00
public ClientInfo[] GetOnlineClientList()
2023-05-23 17:57:24 +08:00
{
2023-05-25 18:30:22 +08:00
return ClientList.Where(w => w.IsOffline == false).ToArray();
2023-05-23 17:57:24 +08:00
}
2023-06-15 15:45:58 +08:00
///// <summary>
///// 设置玩家离线
///// </summary>
///// <param name="sk"></param>
//public void SetClientOfflineForSocket(Socket sk)
//{
// if (!_DictSocketClient.ContainsKey(sk))
// return;
// Console.WriteLine("标记玩家UID" + _DictSocketClient[sk].UID + "为离线");
// _DictSocketClient[sk].IsOffline = true;
// _DictSocketClient[sk].LogOutDT = DateTime.Now;
// EventSystem.Instance.PostEvent(EEvent.OnUserLeave, _DictSocketClient[sk].UID);
//}
public void RemoveClientBySocket(Socket sk)
2023-05-23 17:57:24 +08:00
{
if (!_DictSocketClient.ContainsKey(sk))
return;
RemoveClient(_DictSocketClient[sk]);
}
#endregion
2023-05-25 13:24:51 +08:00
public void ClientSendALL(int CMDID, int ERRCODE, byte[] data)
{
2023-05-25 18:30:22 +08:00
ClientSend(ClientList, CMDID, ERRCODE, data);
2023-05-25 13:24:51 +08:00
}
2023-05-23 17:57:24 +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 18:30:22 +08:00
for (int i = 0; i < _toclientlist.Count(); i++)
2023-05-23 17:57:24 +08:00
{
if (_toclientlist[i] == null || _toclientlist[i].IsOffline)
continue;
2023-05-25 18:30:22 +08:00
ServerManager.g_SocketMgr.SendToSocket(_toclientlist[i]._socket, CMDID, ERRCODE, data);
2023-05-23 17:57:24 +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:24:51 +08:00
2023-05-25 18:30:22 +08:00
public int GetOnlineClientCount()
2023-05-25 13:24:51 +08:00
{
return ClientList.Where(w => !w.IsOffline).Count();
}
2023-05-23 17:57:24 +08:00
}
}