网络管理 | 事件系统

This commit is contained in:
sin365 2023-05-25 18:04:01 +08:00
parent 646284b987
commit 21b02018ee
31 changed files with 779 additions and 44 deletions

Binary file not shown.

View File

@ -1,8 +1,10 @@
using ClientCore;
using ClientCore.Event;
App.Init("127.0.0.1", 23846);
//注册
App.chat.OnChatMsg += OnChatMsg;
//注册事件
EventSystem.Instance.RegisterEvent<string, string>(EEvent.OnChatMsg, OnChatMsg);
while (true)
{

View File

@ -14,12 +14,14 @@ namespace ClientCore
public static long RID = -1;
public static string IP;
public static int Port;
public static LogManager log;
public static NetworkHelper networkHelper;
public static AppLogin login;
public static AppChat chat;
public static void Init(string IP, int port)
{
log = new LogManager();
networkHelper = new NetworkHelper();
login = new AppLogin();
chat = new AppChat();

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClientCore.Common
{
public static class Helper
{
public static long GetNowTimeStamp()
{
return GetTimeStamp(DateTime.Now);
}
/// <summary>
/// 获取时间戳
/// </summary>
/// <returns></returns>
public static long GetTimeStamp(DateTime dt)
{
TimeSpan ts = dt - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalSeconds);
}
}
}

View File

@ -1,8 +1,8 @@
using Google.Protobuf;
namespace ServerCore.NetWork
namespace ClientCore.Common
{
public static class NetBase
public static class ProtoBufHelper
{
public static byte[] Serizlize(IMessage msg)

View File

@ -1,15 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static HaoYueNet.ClientNetwork.NetworkHelperCore;
namespace ClientCore.Event
{
public class DelegateClass
{
public delegate void dg_Str(string Msg);
public delegate void dg_Str_Str(string Str1, string Str2);
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClientCore.Event
{
public enum EEvent
{
// 添加你自己需要的事件类型
OnChatMsg
}
}

View File

@ -0,0 +1,245 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClientCore.Event
{
public class EventData
{
private static long BaseUid = 0;
private static long GenUid()
{
return (++BaseUid);
}
/// <summary>
/// 唯一id
/// </summary>
public long uid { get; private set; }
/// <summary>
/// 回调
/// </summary>
public Delegate callback { get; private set; }
public EventData(Delegate d)
{
uid = GenUid();
callback = d;
}
}
public class EventSystem
{
private static EventSystem instance = new EventSystem();
public static EventSystem Instance { get { return instance; } }
private Dictionary<EEvent, List<Delegate>> eventDic = new Dictionary<EEvent, List<Delegate>>(128);
private EventSystem() { }
#region RegisterEvent
public void RegisterEvent(EEvent evt, Action callback)
{
InterRegisterEvent(evt, callback);
}
public void RegisterEvent<T1>(EEvent evt, Action<T1> callback)
{
InterRegisterEvent(evt, callback);
}
public void RegisterEvent<T1, T2>(EEvent evt, Action<T1, T2> callback)
{
InterRegisterEvent(evt, callback);
}
public void RegisterEvent<T1, T2, T3>(EEvent evt, Action<T1, T2, T3> callback)
{
InterRegisterEvent(evt, callback);
}
public void RegisterEvent<T1, T2, T3, T4>(EEvent evt, Action<T1, T2, T3, T4> callback)
{
InterRegisterEvent(evt, callback);
}
private void InterRegisterEvent(EEvent evt, Delegate callback)
{
if (eventDic.ContainsKey(evt))
{
if (eventDic[evt].IndexOf(callback) < 0)
{
eventDic[evt].Add(callback);
}
}
else
{
eventDic.Add(evt, new List<Delegate>() { callback });
}
}
#endregion
#region UnregisterEvent
public void UnregisterEvent(EEvent evt, Action callback)
{
Delegate tempDelegate = callback;
InterUnregisterEvent(evt, tempDelegate);
}
public void UnregisterEvent<T1>(EEvent evt, Action<T1> callback)
{
Delegate tempDelegate = callback;
InterUnregisterEvent(evt, tempDelegate);
}
public void UnregisterEvent<T1, T2>(EEvent evt, Action<T1, T2> callback)
{
Delegate tempDelegate = callback;
InterUnregisterEvent(evt, tempDelegate);
}
public void UnregisterEvent<T1, T2, T3>(EEvent evt, Action<T1, T2, T3> callback)
{
Delegate tempDelegate = callback;
InterUnregisterEvent(evt, tempDelegate);
}
public void UnregisterEvent<T1, T2, T3, T4>(EEvent evt, Action<T1, T2, T3, T4> callback)
{
Delegate tempDelegate = callback;
InterUnregisterEvent(evt, tempDelegate);
}
private void InterUnregisterEvent(EEvent evt, Delegate callback)
{
if (eventDic.ContainsKey(evt))
{
eventDic[evt].Remove(callback);
if (eventDic[evt].Count == 0) eventDic.Remove(evt);
}
}
#endregion
#region PostEvent
public void PostEvent<T1, T2, T3, T4>(EEvent evt, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
{
List<Delegate> eventList = GetEventList(evt);
if (eventList != null)
{
foreach (Delegate callback in eventList)
{
try
{
((Action<T1, T2, T3, T4>)callback)(arg1, arg2, arg3, arg4);
}
catch (Exception e)
{
App.log.Error(e.Message);
}
}
}
}
public void PostEvent<T1, T2, T3>(EEvent evt, T1 arg1, T2 arg2, T3 arg3)
{
List<Delegate> eventList = GetEventList(evt);
if (eventList != null)
{
foreach (Delegate callback in eventList)
{
try
{
((Action<T1, T2, T3>)callback)(arg1, arg2, arg3);
}
catch (Exception e)
{
App.log.Error(e.Message);
}
}
}
}
public void PostEvent<T1, T2>(EEvent evt, T1 arg1, T2 arg2)
{
List<Delegate> eventList = GetEventList(evt);
if (eventList != null)
{
foreach (Delegate callback in eventList)
{
try
{
((Action<T1, T2>)callback)(arg1, arg2);
}
catch (Exception e)
{
App.log.Error(e.Message);
}
}
}
}
public void PostEvent<T>(EEvent evt, T arg)
{
List<Delegate> eventList = GetEventList(evt);
if (eventList != null)
{
foreach (Delegate callback in eventList)
{
try
{
((Action<T>)callback)(arg);
}
catch (Exception e)
{
App.log.Error(e.Message + ", method name : " + callback.Method);
}
}
}
}
public void PostEvent(EEvent evt)
{
List<Delegate> eventList = GetEventList(evt);
if (eventList != null)
{
foreach (Delegate callback in eventList)
{
try
{
((Action)callback)();
}
catch (Exception e)
{
App.log.Error(e.Message);
}
}
}
}
#endregion
/// <summary>
/// 获取所有事件
/// </summary>
/// <param name="evt"></param>
/// <returns></returns>
private List<Delegate> GetEventList(EEvent evt)
{
if (eventDic.ContainsKey(evt))
{
List<Delegate> tempList = eventDic[evt];
if (null != tempList)
{
return tempList;
}
}
return null;
}
}
}

View File

@ -1,14 +1,16 @@
using AxibugProtobuf;
using ClientCore.Common;
using ClientCore.Event;
using ClientCore.Network;
using System.Net.Sockets;
using static ClientCore.Event.DelegateClass;
using static HaoYueNet.ClientNetwork.NetworkHelperCore;
namespace ClientCore.Manager
{
public class AppChat
{
public event dg_Str_Str OnChatMsg;
public AppChat()
{
NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdChatmsg, RecvChatMsg);
}
public void SendChatMsg(string ChatMsg)
{
@ -16,13 +18,13 @@ namespace ClientCore.Manager
{
ChatMsg = ChatMsg,
};
App.networkHelper.SendToServer((int)CommandID.CmdChatmsg, NetBase.Serizlize(msg));
App.networkHelper.SendToServer((int)CommandID.CmdChatmsg, ProtoBufHelper.Serizlize(msg));
}
public void RecvChatMsg(byte[] reqData)
{
Protobuf_ChatMsg_RESP msg = NetBase.DeSerizlize<Protobuf_ChatMsg_RESP>(reqData);
OnChatMsg(msg.NickName, msg.ChatMsg);
Protobuf_ChatMsg_RESP msg = ProtoBufHelper.DeSerizlize<Protobuf_ChatMsg_RESP>(reqData);
EventSystem.Instance.PostEvent(EEvent.OnChatMsg, msg.NickName, msg.ChatMsg);
}
}
}

View File

@ -12,7 +12,7 @@ namespace ClientCore.Manager
LoginType = 0,
Account = Account,
};
App.networkHelper.SendToServer((int)CommandID.CmdLogin, NetBase.Serizlize(msg));
App.networkHelper.SendToServer((int)CommandID.CmdLogin, NetworkHelper.Serizlize(msg));
}
}
}

View File

@ -0,0 +1,20 @@
namespace ClientCore.Manager
{
public class LogManager
{
public void Debug(string str)
{
Console.WriteLine(str);
}
public void Warning(string str)
{
Console.WriteLine(str);
}
public void Error(string str)
{
Console.WriteLine(str);
}
}
}

View File

@ -0,0 +1,94 @@
namespace ClientCore.Network
{
public class NetMsg
{
private static NetMsg instance = new NetMsg();
public static NetMsg Instance { get { return instance; } }
private Dictionary<int, List<Delegate>> netEventDic = new Dictionary<int, List<Delegate>>(128);
private NetMsg() { }
#region RegisterMsgEvent
public void RegNetMsgEvent(int cmd, Action<byte[]> callback)
{
InterRegNetMsgEvent(cmd, callback);
}
private void InterRegNetMsgEvent(int cmd, Delegate callback)
{
if (netEventDic.ContainsKey(cmd))
{
if (netEventDic[cmd].IndexOf(callback) < 0)
{
netEventDic[cmd].Add(callback);
}
}
else
{
netEventDic.Add(cmd, new List<Delegate>() { callback });
}
}
#endregion
#region UnregisterCMD
public void UnregisterCMD(int evt, Action<byte[]> callback)
{
Delegate tempDelegate = callback;
InterUnregisterCMD(evt, tempDelegate);
}
private void InterUnregisterCMD(int cmd, Delegate callback)
{
if (netEventDic.ContainsKey(cmd))
{
netEventDic[cmd].Remove(callback);
if (netEventDic[cmd].Count == 0) netEventDic.Remove(cmd);
}
}
#endregion
#region PostEvent
public void PostNetMsgEvent(int cmd, byte[] arg)
{
List<Delegate> eventList = GetNetEventDicList(cmd);
if (eventList != null)
{
foreach (Delegate callback in eventList)
{
try
{
((Action<byte[]>)callback)(arg);
}
catch (Exception e)
{
App.log.Error(e.Message);
}
}
}
}
#endregion
/// <summary>
/// 获取所有事件
/// </summary>
/// <param name="cmd"></param>
/// <returns></returns>
private List<Delegate> GetNetEventDicList(int cmd)
{
if (netEventDic.ContainsKey(cmd))
{
List<Delegate> tempList = netEventDic[cmd];
if (null != tempList)
{
return tempList;
}
}
return null;
}
}
}

View File

@ -55,12 +55,8 @@ namespace ClientCore.Network
NetworkDeBugLog("收到消息 CMDID =>" + CMDID + " ERRCODE =>" + ERRCODE + " 数据长度=>" + data.Length);
try
{
//根据协议ID走不同逻辑
switch ((CommandID)CMDID)
{
case CommandID.CmdLogin: break;
case CommandID.CmdChatmsg: App.chat.RecvChatMsg(data); break;
}
//抛出网络数据
NetMsg.Instance.PostNetMsgEvent(CMDID, data);
}
catch (Exception ex)
{

View File

@ -1,9 +1,10 @@
using Google.Protobuf;
namespace ClientCore.Network
namespace ServerCore.Common
{
public static class NetBase
public static class ProtoBufHelper
{
public static byte[] Serizlize(IMessage msg)
{
return msg.ToByteArray();

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ServerCore.Event
{
public enum EEvent
{
// 添加你自己需要的事件类型
OnUserJoin,
OnUserLeave
}
}

View File

@ -0,0 +1,222 @@
using ServerCore.Manager;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ServerCore.Event
{
public class EventSystem
{
private static EventSystem instance = new EventSystem();
public static EventSystem Instance { get { return instance; } }
private Dictionary<EEvent, List<Delegate>> eventDic = new Dictionary<EEvent, List<Delegate>>(128);
private EventSystem() { }
#region RegisterEvent
public void RegisterEvent(EEvent evt, Action callback)
{
InterRegisterEvent(evt, callback);
}
public void RegisterEvent<T1>(EEvent evt, Action<T1> callback)
{
InterRegisterEvent(evt, callback);
}
public void RegisterEvent<T1, T2>(EEvent evt, Action<T1, T2> callback)
{
InterRegisterEvent(evt, callback);
}
public void RegisterEvent<T1, T2, T3>(EEvent evt, Action<T1, T2, T3> callback)
{
InterRegisterEvent(evt, callback);
}
public void RegisterEvent<T1, T2, T3, T4>(EEvent evt, Action<T1, T2, T3, T4> callback)
{
InterRegisterEvent(evt, callback);
}
private void InterRegisterEvent(EEvent evt, Delegate callback)
{
if (eventDic.ContainsKey(evt))
{
if (eventDic[evt].IndexOf(callback) < 0)
{
eventDic[evt].Add(callback);
}
}
else
{
eventDic.Add(evt, new List<Delegate>() { callback });
}
}
#endregion
#region UnregisterEvent
public void UnregisterEvent(EEvent evt, Action callback)
{
Delegate tempDelegate = callback;
InterUnregisterEvent(evt, tempDelegate);
}
public void UnregisterEvent<T1>(EEvent evt, Action<T1> callback)
{
Delegate tempDelegate = callback;
InterUnregisterEvent(evt, tempDelegate);
}
public void UnregisterEvent<T1, T2>(EEvent evt, Action<T1, T2> callback)
{
Delegate tempDelegate = callback;
InterUnregisterEvent(evt, tempDelegate);
}
public void UnregisterEvent<T1, T2, T3>(EEvent evt, Action<T1, T2, T3> callback)
{
Delegate tempDelegate = callback;
InterUnregisterEvent(evt, tempDelegate);
}
public void UnregisterEvent<T1, T2, T3, T4>(EEvent evt, Action<T1, T2, T3, T4> callback)
{
Delegate tempDelegate = callback;
InterUnregisterEvent(evt, tempDelegate);
}
private void InterUnregisterEvent(EEvent evt, Delegate callback)
{
if (eventDic.ContainsKey(evt))
{
eventDic[evt].Remove(callback);
if (eventDic[evt].Count == 0) eventDic.Remove(evt);
}
}
#endregion
#region PostEvent
public void PostEvent<T1, T2, T3, T4>(EEvent evt, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
{
List<Delegate> eventList = GetEventList(evt);
if (eventList != null)
{
foreach (Delegate callback in eventList)
{
try
{
((Action<T1, T2, T3, T4>)callback)(arg1, arg2, arg3, arg4);
}
catch (Exception e)
{
ServerManager.g_Log.Error(e.Message);
}
}
}
}
public void PostEvent<T1, T2, T3>(EEvent evt, T1 arg1, T2 arg2, T3 arg3)
{
List<Delegate> eventList = GetEventList(evt);
if (eventList != null)
{
foreach (Delegate callback in eventList)
{
try
{
((Action<T1, T2, T3>)callback)(arg1, arg2, arg3);
}
catch (Exception e)
{
ServerManager.g_Log.Error(e.Message);
}
}
}
}
public void PostEvent<T1, T2>(EEvent evt, T1 arg1, T2 arg2)
{
List<Delegate> eventList = GetEventList(evt);
if (eventList != null)
{
foreach (Delegate callback in eventList)
{
try
{
((Action<T1, T2>)callback)(arg1, arg2);
}
catch (Exception e)
{
ServerManager.g_Log.Error(e.Message);
}
}
}
}
public void PostEvent<T>(EEvent evt, T arg)
{
List<Delegate> eventList = GetEventList(evt);
if (eventList != null)
{
foreach (Delegate callback in eventList)
{
try
{
((Action<T>)callback)(arg);
}
catch (Exception e)
{
ServerManager.g_Log.Error(e.Message + ", method name : " + callback.Method);
}
}
}
}
public void PostEvent(EEvent evt)
{
List<Delegate> eventList = GetEventList(evt);
if (eventList != null)
{
foreach (Delegate callback in eventList)
{
try
{
((Action)callback)();
}
catch (Exception e)
{
ServerManager.g_Log.Error(e.Message);
}
}
}
}
#endregion
/// <summary>
/// 获取所有事件
/// </summary>
/// <param name="evt"></param>
/// <returns></returns>
private List<Delegate> GetEventList(EEvent evt)
{
if (eventDic.ContainsKey(evt))
{
List<Delegate> tempList = eventDic[evt];
if (null != tempList)
{
return tempList;
}
}
return null;
}
}
}

View File

@ -7,12 +7,17 @@ namespace ServerCore.Manager
{
public class ChatManager
{
public ChatManager()
{
NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdChatmsg, RecvPlayerChatMsg);
}
public void RecvPlayerChatMsg(Socket sk, byte[] reqData)
{
ClientInfo _c = ServerManager.g_ClientMgr.GetClientForSocket(sk);
ServerManager.g_Log.Debug("收到新的登录请求");
Protobuf_ChatMsg msg = NetBase.DeSerizlize<Protobuf_ChatMsg>(reqData);
byte[] respData = NetBase.Serizlize(new Protobuf_ChatMsg_RESP()
Protobuf_ChatMsg msg = ProtoBufHelper.DeSerizlize<Protobuf_ChatMsg>(reqData);
byte[] respData = ProtoBufHelper.Serizlize(new Protobuf_ChatMsg_RESP()
{
ChatMsg = msg.ChatMsg,
NickName = _c.Account,

View File

@ -1,4 +1,5 @@
using AxibugProtobuf;
using ServerCore.Common;
using ServerCore.NetWork;
using System.Net.Sockets;
@ -6,13 +7,18 @@ namespace ServerCore.Manager
{
public class LoginManager
{
public LoginManager()
{
NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdLogin, UserLogin);
}
public void UserLogin(Socket _socket, byte[] reqData)
{
ServerManager.g_Log.Debug("收到新的登录请求");
Protobuf_Login msg = NetBase.DeSerizlize<Protobuf_Login>(reqData);
Protobuf_Login msg = ProtoBufHelper.DeSerizlize<Protobuf_Login>(reqData);
ClientInfo cinfo = ServerManager.g_ClientMgr.JoinNewClient(msg, _socket);
byte[] respData = NetBase.Serizlize(new Protobuf_Login_RESP()
byte[] respData = ProtoBufHelper.Serizlize(new Protobuf_Login_RESP()
{
Status = LoginResultStatus.Ok,
RegDate = "",

View File

@ -46,11 +46,8 @@ namespace ServerCore.NetWork
ServerManager.g_Log.Debug("收到消息 CMDID =>" + CMDID + " 数据长度=>" + data.Length);
try
{
switch ((CommandID)CMDID)
{
case CommandID.CmdLogin: ServerManager.g_Login.UserLogin(sk, data); break;
case CommandID.CmdChatmsg: ServerManager.g_Chat.RecvPlayerChatMsg(sk, data); break;
}
//抛出网络数据
NetMsg.Instance.PostNetMsgEvent(CMDID, sk, data);
}
catch (Exception ex)
{

View File

@ -0,0 +1,103 @@
using ServerCore.Manager;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace ServerCore.NetWork
{
public class NetMsg
{
private static NetMsg instance = new NetMsg();
public static NetMsg Instance { get { return instance; } }
private Dictionary<int, List<Delegate>> netEventDic = new Dictionary<int, List<Delegate>>(128);
private NetMsg() { }
#region RegisterMsgEvent
public void RegNetMsgEvent(int cmd, Action<Socket, byte[]> callback)
{
InterRegNetMsgEvent(cmd, callback);
}
private void InterRegNetMsgEvent(int cmd, Delegate callback)
{
if (netEventDic.ContainsKey(cmd))
{
if (netEventDic[cmd].IndexOf(callback) < 0)
{
netEventDic[cmd].Add(callback);
}
}
else
{
netEventDic.Add(cmd, new List<Delegate>() { callback });
}
}
#endregion
#region UnregisterCMD
public void UnregisterCMD(int cmd, Action<Socket, byte[]> callback)
{
Delegate tempDelegate = callback;
InterUnregisterCMD(cmd, tempDelegate);
}
private void InterUnregisterCMD(int cmd, Delegate callback)
{
if (netEventDic.ContainsKey(cmd))
{
netEventDic[cmd].Remove(callback);
if (netEventDic[cmd].Count == 0) netEventDic.Remove(cmd);
}
}
#endregion
#region PostEvent
public void PostNetMsgEvent(int cmd, Socket arg1, byte[] arg2)
{
List<Delegate> eventList = GetNetEventDicList(cmd);
if (eventList != null)
{
foreach (Delegate callback in eventList)
{
try
{
((Action<Socket, byte[]>)callback)(arg1, arg2);
}
catch (Exception e)
{
ServerManager.g_Log.Error(e.Message);
}
}
}
}
#endregion
/// <summary>
/// 获取所有事件
/// </summary>
/// <param name="cmd"></param>
/// <returns></returns>
private List<Delegate> GetNetEventDicList(int cmd)
{
if (netEventDic.ContainsKey(cmd))
{
List<Delegate> tempList = netEventDic[cmd];
if (null != tempList)
{
return tempList;
}
}
return null;
}
}
}