diff --git a/.vs/HaoYueNet/DesignTimeBuild/.dtbcache.v2 b/.vs/HaoYueNet/DesignTimeBuild/.dtbcache.v2 index 2bfafaa..de578c2 100644 Binary files a/.vs/HaoYueNet/DesignTimeBuild/.dtbcache.v2 and b/.vs/HaoYueNet/DesignTimeBuild/.dtbcache.v2 differ diff --git a/.vs/HaoYueNet/FileContentIndex/3f85b86a-e7aa-4890-ba76-98d6a12d19e1.vsidx b/.vs/HaoYueNet/FileContentIndex/3f85b86a-e7aa-4890-ba76-98d6a12d19e1.vsidx new file mode 100644 index 0000000..bcfa08f Binary files /dev/null and b/.vs/HaoYueNet/FileContentIndex/3f85b86a-e7aa-4890-ba76-98d6a12d19e1.vsidx differ diff --git a/.vs/HaoYueNet/FileContentIndex/4276406d-9cd1-4677-b724-afe7ec1c7148.vsidx b/.vs/HaoYueNet/FileContentIndex/4276406d-9cd1-4677-b724-afe7ec1c7148.vsidx deleted file mode 100644 index bb9e0e9..0000000 Binary files a/.vs/HaoYueNet/FileContentIndex/4276406d-9cd1-4677-b724-afe7ec1c7148.vsidx and /dev/null differ diff --git a/.vs/HaoYueNet/FileContentIndex/6a19da00-ad65-49e0-96a6-8070e9505ed2.vsidx b/.vs/HaoYueNet/FileContentIndex/6a19da00-ad65-49e0-96a6-8070e9505ed2.vsidx deleted file mode 100644 index 6321b4c..0000000 Binary files a/.vs/HaoYueNet/FileContentIndex/6a19da00-ad65-49e0-96a6-8070e9505ed2.vsidx and /dev/null differ diff --git a/.vs/HaoYueNet/FileContentIndex/6aaffadf-da73-4dcf-a2c4-55d4edaad44e.vsidx b/.vs/HaoYueNet/FileContentIndex/6aaffadf-da73-4dcf-a2c4-55d4edaad44e.vsidx new file mode 100644 index 0000000..733035b Binary files /dev/null and b/.vs/HaoYueNet/FileContentIndex/6aaffadf-da73-4dcf-a2c4-55d4edaad44e.vsidx differ diff --git a/.vs/HaoYueNet/FileContentIndex/9d5d1701-907e-42e8-aac9-f4fccb63d094.vsidx b/.vs/HaoYueNet/FileContentIndex/9d5d1701-907e-42e8-aac9-f4fccb63d094.vsidx new file mode 100644 index 0000000..26b700b Binary files /dev/null and b/.vs/HaoYueNet/FileContentIndex/9d5d1701-907e-42e8-aac9-f4fccb63d094.vsidx differ diff --git a/.vs/HaoYueNet/FileContentIndex/b164a36b-6b4a-4513-93fd-bd55d2866e91.vsidx b/.vs/HaoYueNet/FileContentIndex/b164a36b-6b4a-4513-93fd-bd55d2866e91.vsidx new file mode 100644 index 0000000..b48890b Binary files /dev/null and b/.vs/HaoYueNet/FileContentIndex/b164a36b-6b4a-4513-93fd-bd55d2866e91.vsidx differ diff --git a/.vs/HaoYueNet/FileContentIndex/d5b6b639-3bec-437c-8aa1-0d19e483a59a.vsidx b/.vs/HaoYueNet/FileContentIndex/d5b6b639-3bec-437c-8aa1-0d19e483a59a.vsidx deleted file mode 100644 index 9cd0b95..0000000 Binary files a/.vs/HaoYueNet/FileContentIndex/d5b6b639-3bec-437c-8aa1-0d19e483a59a.vsidx and /dev/null differ diff --git a/.vs/HaoYueNet/FileContentIndex/f605dade-673c-4f67-ac26-a3f4c502f3b4.vsidx b/.vs/HaoYueNet/FileContentIndex/f605dade-673c-4f67-ac26-a3f4c502f3b4.vsidx deleted file mode 100644 index eaacc07..0000000 Binary files a/.vs/HaoYueNet/FileContentIndex/f605dade-673c-4f67-ac26-a3f4c502f3b4.vsidx and /dev/null differ diff --git a/.vs/HaoYueNet/v17/.futdcache.v2 b/.vs/HaoYueNet/v17/.futdcache.v2 index 57a6f56..0938421 100644 Binary files a/.vs/HaoYueNet/v17/.futdcache.v2 and b/.vs/HaoYueNet/v17/.futdcache.v2 differ diff --git a/.vs/ProjectEvaluation/haoyuenet.metadata.v5.2 b/.vs/ProjectEvaluation/haoyuenet.metadata.v5.2 index ce6c4a4..39b5c55 100644 Binary files a/.vs/ProjectEvaluation/haoyuenet.metadata.v5.2 and b/.vs/ProjectEvaluation/haoyuenet.metadata.v5.2 differ diff --git a/.vs/ProjectEvaluation/haoyuenet.projects.v5.2 b/.vs/ProjectEvaluation/haoyuenet.projects.v5.2 index c299a61..f28fa49 100644 Binary files a/.vs/ProjectEvaluation/haoyuenet.projects.v5.2 and b/.vs/ProjectEvaluation/haoyuenet.projects.v5.2 differ diff --git a/Simple/Client-Cli/Program.cs b/Simple/Client-Cli/Program.cs index 30e119f..c0dd0de 100644 --- a/Simple/Client-Cli/Program.cs +++ b/Simple/Client-Cli/Program.cs @@ -1,8 +1,10 @@ using ClientCore; +using ClientCore.Event; + App.Init("127.0.0.1", 23846); -//注册 -App.chat.OnChatMsg += OnChatMsg; +//注册事件 +EventSystem.Instance.RegisterEvent(EEvent.OnChatMsg, OnChatMsg); while (true) { diff --git a/Simple/ClientCore/App.cs b/Simple/ClientCore/App.cs index c2567bc..f1fb5f3 100644 --- a/Simple/ClientCore/App.cs +++ b/Simple/ClientCore/App.cs @@ -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(); diff --git a/Simple/ClientCore/Common/Helper.cs b/Simple/ClientCore/Common/Helper.cs new file mode 100644 index 0000000..0643fd4 --- /dev/null +++ b/Simple/ClientCore/Common/Helper.cs @@ -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); + } + + /// + /// 获取时间戳 + /// + /// + public static long GetTimeStamp(DateTime dt) + { + TimeSpan ts = dt - new DateTime(1970, 1, 1, 0, 0, 0, 0); + return Convert.ToInt64(ts.TotalSeconds); + } + } +} diff --git a/Simple/ServerCore/NetWork/NetBase.cs b/Simple/ClientCore/Common/ProtoBufHelper.cs similarity index 85% rename from Simple/ServerCore/NetWork/NetBase.cs rename to Simple/ClientCore/Common/ProtoBufHelper.cs index 404f542..27e19a4 100644 --- a/Simple/ServerCore/NetWork/NetBase.cs +++ b/Simple/ClientCore/Common/ProtoBufHelper.cs @@ -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) diff --git a/Simple/ClientCore/Event/DelegateClass.cs b/Simple/ClientCore/Event/DelegateClass.cs deleted file mode 100644 index 3c972cb..0000000 --- a/Simple/ClientCore/Event/DelegateClass.cs +++ /dev/null @@ -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); - } -} diff --git a/Simple/ClientCore/Event/EEvent.cs b/Simple/ClientCore/Event/EEvent.cs new file mode 100644 index 0000000..17a2d22 --- /dev/null +++ b/Simple/ClientCore/Event/EEvent.cs @@ -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 + } +} diff --git a/Simple/ClientCore/Event/EventSystem.cs b/Simple/ClientCore/Event/EventSystem.cs new file mode 100644 index 0000000..dc0ee60 --- /dev/null +++ b/Simple/ClientCore/Event/EventSystem.cs @@ -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); + } + + /// + /// 唯一id + /// + public long uid { get; private set; } + + /// + /// 回调 + /// + 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> eventDic = new Dictionary>(128); + + private EventSystem() { } + + + #region RegisterEvent + public void RegisterEvent(EEvent evt, Action callback) + { + InterRegisterEvent(evt, callback); + } + + public void RegisterEvent(EEvent evt, Action callback) + { + InterRegisterEvent(evt, callback); + } + + public void RegisterEvent(EEvent evt, Action callback) + { + InterRegisterEvent(evt, callback); + } + + public void RegisterEvent(EEvent evt, Action callback) + { + InterRegisterEvent(evt, callback); + } + + public void RegisterEvent(EEvent evt, Action 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() { callback }); + } + } + #endregion + + #region UnregisterEvent + + public void UnregisterEvent(EEvent evt, Action callback) + { + Delegate tempDelegate = callback; + InterUnregisterEvent(evt, tempDelegate); + } + + public void UnregisterEvent(EEvent evt, Action callback) + { + Delegate tempDelegate = callback; + InterUnregisterEvent(evt, tempDelegate); + } + + public void UnregisterEvent(EEvent evt, Action callback) + { + Delegate tempDelegate = callback; + InterUnregisterEvent(evt, tempDelegate); + } + + public void UnregisterEvent(EEvent evt, Action callback) + { + Delegate tempDelegate = callback; + InterUnregisterEvent(evt, tempDelegate); + } + + public void UnregisterEvent(EEvent evt, Action 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(EEvent evt, T1 arg1, T2 arg2, T3 arg3, T4 arg4) + { + List eventList = GetEventList(evt); + if (eventList != null) + { + foreach (Delegate callback in eventList) + { + try + { + ((Action)callback)(arg1, arg2, arg3, arg4); + } + catch (Exception e) + { + App.log.Error(e.Message); + } + } + } + } + + public void PostEvent(EEvent evt, T1 arg1, T2 arg2, T3 arg3) + { + List eventList = GetEventList(evt); + if (eventList != null) + { + foreach (Delegate callback in eventList) + { + try + { + ((Action)callback)(arg1, arg2, arg3); + } + catch (Exception e) + { + App.log.Error(e.Message); + } + } + } + } + + public void PostEvent(EEvent evt, T1 arg1, T2 arg2) + { + List eventList = GetEventList(evt); + if (eventList != null) + { + foreach (Delegate callback in eventList) + { + try + { + ((Action)callback)(arg1, arg2); + } + catch (Exception e) + { + App.log.Error(e.Message); + } + } + } + } + + public void PostEvent(EEvent evt, T arg) + { + List eventList = GetEventList(evt); + if (eventList != null) + { + foreach (Delegate callback in eventList) + { + try + { + ((Action)callback)(arg); + } + catch (Exception e) + { + App.log.Error(e.Message + ", method name : " + callback.Method); + } + } + } + + } + + public void PostEvent(EEvent evt) + { + List eventList = GetEventList(evt); + if (eventList != null) + { + foreach (Delegate callback in eventList) + { + try + { + ((Action)callback)(); + } + catch (Exception e) + { + App.log.Error(e.Message); + } + } + } + } + #endregion + + /// + /// 获取所有事件 + /// + /// + /// + private List GetEventList(EEvent evt) + { + if (eventDic.ContainsKey(evt)) + { + List tempList = eventDic[evt]; + if (null != tempList) + { + return tempList; + } + } + return null; + } + } +} diff --git a/Simple/ClientCore/Manager/AppChat.cs b/Simple/ClientCore/Manager/AppChat.cs index 8e5533b..9340b53 100644 --- a/Simple/ClientCore/Manager/AppChat.cs +++ b/Simple/ClientCore/Manager/AppChat.cs @@ -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(reqData); - OnChatMsg(msg.NickName, msg.ChatMsg); + Protobuf_ChatMsg_RESP msg = ProtoBufHelper.DeSerizlize(reqData); + EventSystem.Instance.PostEvent(EEvent.OnChatMsg, msg.NickName, msg.ChatMsg); } } } diff --git a/Simple/ClientCore/Manager/AppLogin.cs b/Simple/ClientCore/Manager/AppLogin.cs index e2529d7..23a25a9 100644 --- a/Simple/ClientCore/Manager/AppLogin.cs +++ b/Simple/ClientCore/Manager/AppLogin.cs @@ -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)); } } } diff --git a/Simple/ClientCore/Manager/LogManager.cs b/Simple/ClientCore/Manager/LogManager.cs new file mode 100644 index 0000000..612ec1b --- /dev/null +++ b/Simple/ClientCore/Manager/LogManager.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/Simple/ClientCore/Network/NetMsg.cs b/Simple/ClientCore/Network/NetMsg.cs new file mode 100644 index 0000000..f2f2d74 --- /dev/null +++ b/Simple/ClientCore/Network/NetMsg.cs @@ -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> netEventDic = new Dictionary>(128); + + private NetMsg() { } + + + #region RegisterMsgEvent + + public void RegNetMsgEvent(int cmd, Action 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() { callback }); + } + } + #endregion + + #region UnregisterCMD + + public void UnregisterCMD(int evt, Action 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 eventList = GetNetEventDicList(cmd); + if (eventList != null) + { + foreach (Delegate callback in eventList) + { + try + { + ((Action)callback)(arg); + } + catch (Exception e) + { + App.log.Error(e.Message); + } + } + } + } + #endregion + + /// + /// 获取所有事件 + /// + /// + /// + private List GetNetEventDicList(int cmd) + { + if (netEventDic.ContainsKey(cmd)) + { + List tempList = netEventDic[cmd]; + if (null != tempList) + { + return tempList; + } + } + return null; + } + } +} diff --git a/Simple/ClientCore/Network/NetworkHelper.cs b/Simple/ClientCore/Network/NetworkHelper.cs index 39f4efb..4c45fcc 100644 --- a/Simple/ClientCore/Network/NetworkHelper.cs +++ b/Simple/ClientCore/Network/NetworkHelper.cs @@ -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) { diff --git a/Simple/ClientCore/Network/NetBase.cs b/Simple/ServerCore/Common/ProtoBufHelper.cs similarity index 85% rename from Simple/ClientCore/Network/NetBase.cs rename to Simple/ServerCore/Common/ProtoBufHelper.cs index c760407..04e9c46 100644 --- a/Simple/ClientCore/Network/NetBase.cs +++ b/Simple/ServerCore/Common/ProtoBufHelper.cs @@ -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(); diff --git a/Simple/ServerCore/Event/EEvent.cs b/Simple/ServerCore/Event/EEvent.cs new file mode 100644 index 0000000..a2d126e --- /dev/null +++ b/Simple/ServerCore/Event/EEvent.cs @@ -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 + } +} diff --git a/Simple/ServerCore/Event/EventSystem.cs b/Simple/ServerCore/Event/EventSystem.cs new file mode 100644 index 0000000..a011153 --- /dev/null +++ b/Simple/ServerCore/Event/EventSystem.cs @@ -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> eventDic = new Dictionary>(128); + + private EventSystem() { } + + + #region RegisterEvent + public void RegisterEvent(EEvent evt, Action callback) + { + InterRegisterEvent(evt, callback); + } + + public void RegisterEvent(EEvent evt, Action callback) + { + InterRegisterEvent(evt, callback); + } + + public void RegisterEvent(EEvent evt, Action callback) + { + InterRegisterEvent(evt, callback); + } + + public void RegisterEvent(EEvent evt, Action callback) + { + InterRegisterEvent(evt, callback); + } + + public void RegisterEvent(EEvent evt, Action 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() { callback }); + } + } + #endregion + + #region UnregisterEvent + + public void UnregisterEvent(EEvent evt, Action callback) + { + Delegate tempDelegate = callback; + InterUnregisterEvent(evt, tempDelegate); + } + + public void UnregisterEvent(EEvent evt, Action callback) + { + Delegate tempDelegate = callback; + InterUnregisterEvent(evt, tempDelegate); + } + + public void UnregisterEvent(EEvent evt, Action callback) + { + Delegate tempDelegate = callback; + InterUnregisterEvent(evt, tempDelegate); + } + + public void UnregisterEvent(EEvent evt, Action callback) + { + Delegate tempDelegate = callback; + InterUnregisterEvent(evt, tempDelegate); + } + + public void UnregisterEvent(EEvent evt, Action 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(EEvent evt, T1 arg1, T2 arg2, T3 arg3, T4 arg4) + { + List eventList = GetEventList(evt); + if (eventList != null) + { + foreach (Delegate callback in eventList) + { + try + { + ((Action)callback)(arg1, arg2, arg3, arg4); + } + catch (Exception e) + { + ServerManager.g_Log.Error(e.Message); + } + } + } + } + + public void PostEvent(EEvent evt, T1 arg1, T2 arg2, T3 arg3) + { + List eventList = GetEventList(evt); + if (eventList != null) + { + foreach (Delegate callback in eventList) + { + try + { + ((Action)callback)(arg1, arg2, arg3); + } + catch (Exception e) + { + ServerManager.g_Log.Error(e.Message); + } + } + } + } + + public void PostEvent(EEvent evt, T1 arg1, T2 arg2) + { + List eventList = GetEventList(evt); + if (eventList != null) + { + foreach (Delegate callback in eventList) + { + try + { + ((Action)callback)(arg1, arg2); + } + catch (Exception e) + { + ServerManager.g_Log.Error(e.Message); + } + } + } + } + + public void PostEvent(EEvent evt, T arg) + { + List eventList = GetEventList(evt); + if (eventList != null) + { + foreach (Delegate callback in eventList) + { + try + { + ((Action)callback)(arg); + } + catch (Exception e) + { + ServerManager.g_Log.Error(e.Message + ", method name : " + callback.Method); + } + } + } + + } + + public void PostEvent(EEvent evt) + { + List eventList = GetEventList(evt); + if (eventList != null) + { + foreach (Delegate callback in eventList) + { + try + { + ((Action)callback)(); + } + catch (Exception e) + { + ServerManager.g_Log.Error(e.Message); + } + } + } + } + #endregion + + /// + /// 获取所有事件 + /// + /// + /// + private List GetEventList(EEvent evt) + { + if (eventDic.ContainsKey(evt)) + { + List tempList = eventDic[evt]; + if (null != tempList) + { + return tempList; + } + } + return null; + } + } +} diff --git a/Simple/ServerCore/Manager/ChatManager.cs b/Simple/ServerCore/Manager/ChatManager.cs index 7106aba..c44ae7e 100644 --- a/Simple/ServerCore/Manager/ChatManager.cs +++ b/Simple/ServerCore/Manager/ChatManager.cs @@ -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(reqData); - byte[] respData = NetBase.Serizlize(new Protobuf_ChatMsg_RESP() + Protobuf_ChatMsg msg = ProtoBufHelper.DeSerizlize(reqData); + byte[] respData = ProtoBufHelper.Serizlize(new Protobuf_ChatMsg_RESP() { ChatMsg = msg.ChatMsg, NickName = _c.Account, diff --git a/Simple/ServerCore/Manager/LoginManager.cs b/Simple/ServerCore/Manager/LoginManager.cs index e936177..05d39b8 100644 --- a/Simple/ServerCore/Manager/LoginManager.cs +++ b/Simple/ServerCore/Manager/LoginManager.cs @@ -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(reqData); + Protobuf_Login msg = ProtoBufHelper.DeSerizlize(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 = "", diff --git a/Simple/ServerCore/NetWork/IOCPNetWork.cs b/Simple/ServerCore/NetWork/IOCPNetWork.cs index ea03c91..ad84594 100644 --- a/Simple/ServerCore/NetWork/IOCPNetWork.cs +++ b/Simple/ServerCore/NetWork/IOCPNetWork.cs @@ -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) { diff --git a/Simple/ServerCore/NetWork/NetMsg.cs b/Simple/ServerCore/NetWork/NetMsg.cs new file mode 100644 index 0000000..39acb0f --- /dev/null +++ b/Simple/ServerCore/NetWork/NetMsg.cs @@ -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> netEventDic = new Dictionary>(128); + + private NetMsg() { } + + + #region RegisterMsgEvent + + public void RegNetMsgEvent(int cmd, Action 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() { callback }); + } + } + #endregion + + #region UnregisterCMD + + public void UnregisterCMD(int cmd, Action 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 eventList = GetNetEventDicList(cmd); + if (eventList != null) + { + foreach (Delegate callback in eventList) + { + try + { + ((Action)callback)(arg1, arg2); + } + catch (Exception e) + { + ServerManager.g_Log.Error(e.Message); + } + } + } + } + #endregion + + /// + /// 获取所有事件 + /// + /// + /// + private List GetNetEventDicList(int cmd) + { + if (netEventDic.ContainsKey(cmd)) + { + List tempList = netEventDic[cmd]; + if (null != tempList) + { + return tempList; + } + } + return null; + } + } +}