From 744c29d60bcc6fbce8d2be1714e6b8d54ea1cefe Mon Sep 17 00:00:00 2001 From: sin365 <353374337@qq.com> Date: Fri, 23 Aug 2024 16:18:37 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=84=E6=95=B4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NetLib/HaoYueNet.ClientNetwork/BaseData.cs | 91 ++------------- .../IOCPMode/TcpSaeaClient.cs | 7 +- .../NetworkHelperCore.cs | 35 ------ .../NetworkHelperP2PCore.cs | 28 ----- NetLib/HaoYueNet.ServerNetwork/BaseData.cs | 109 ++---------------- .../NetWork/AsyncUserToken.cs | 6 - .../NetWork/MemoryStreamPool.cs | 5 +- .../NetWork/SocketEventPool.cs | 1 - .../SourceMode/TcpSaeaServer_SourceMode.cs | 63 +--------- .../SourceMode/TokenMsgPool_SourceMode.cs | 9 +- .../NetWork/TcpSaeaServer.cs | 6 +- .../SourceMode/TcpSaeaServer_SourceMode.cs | 6 +- .../NetWork/TcpSaeaServer.cs | 6 +- Simple/ClientCore/Event/EventSystem.cs | 46 ++++++-- Simple/ClientCore/Network/NetMsg.cs | 44 +++++-- Simple/ServerCore/Event/EventSystem.cs | 15 ++- 16 files changed, 126 insertions(+), 351 deletions(-) diff --git a/NetLib/HaoYueNet.ClientNetwork/BaseData.cs b/NetLib/HaoYueNet.ClientNetwork/BaseData.cs index b60b043..9a1e53c 100644 --- a/NetLib/HaoYueNet.ClientNetwork/BaseData.cs +++ b/NetLib/HaoYueNet.ClientNetwork/BaseData.cs @@ -1,5 +1,4 @@ using System.Net.Sockets; - namespace HaoYueNet.ClientNetwork { public static class BaseData @@ -8,21 +7,6 @@ namespace HaoYueNet.ClientNetwork /// 心跳包数据 /// public static byte[] HeartbeatData = new byte[5] { 0x05, 0x00, 0x00, 0x00, 0x00 }; - - public static void writeInt(byte[] buf, int offset, int value) - { - buf[offset++] = (byte)(255 & value); - buf[offset++] = (byte)(255 & value >> 8); - buf[offset++] = (byte)(255 & value >> 16); - buf[offset++] = (byte)(255 & value >>> 24); - } - - public static void writeUInt16(byte[] buf, int offset, int value) - { - buf[offset++] = (byte)(255 & value); - buf[offset++] = (byte)(255 & value >> 8); - } - public static class HunterNet_Heartbeat { public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs) @@ -30,119 +14,58 @@ namespace HaoYueNet.ClientNetwork myreadEventArgs.SetBuffer(HeartbeatData, 0, HeartbeatData.Length); } } - public static class HunterNet_S2C { public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs, UInt16 CmdID, UInt16 Error, byte[] AddonBytes_Data) { myreadEventArgs.SetBuffer(CreatePkgData(CmdID, Error, AddonBytes_Data)); } - public static byte[] CreatePkgData(UInt16 CmdID, UInt16 Error, byte[] AddonBytes_Data) { - //用Buffer.BlockCopy拷贝 //包长度 int AllLenght = 4 + 2 + 2 + AddonBytes_Data.Length; byte[] BufferData = new byte[AllLenght]; - //包长度 - writeInt(BufferData, 0, AllLenght); - + Buffer.BlockCopy(BitConverter.GetBytes(AllLenght), 0, BufferData, 0, sizeof(int)); //CMDID - writeUInt16(BufferData, 4, CmdID); - - //Error - writeUInt16(BufferData, 4 + 2, Error); - + Buffer.BlockCopy(BitConverter.GetBytes(CmdID), 0, BufferData, 4, sizeof(UInt16)); + //ErrID + Buffer.BlockCopy(BitConverter.GetBytes(Error), 0, BufferData, 4 + 2, sizeof(UInt16)); //DATA Buffer.BlockCopy(AddonBytes_Data, 0, BufferData, 4 + 2 + 2, AddonBytes_Data.Length); - return BufferData; } - public static void AnalysisPkgData(Span srcdata, out UInt16 CmdID, out UInt16 Error, out byte[] data) { - //CmdID = BitConverter.ToUInt16(srcdata, 0); - //Error = BitConverter.ToUInt16(srcdata, 2); - //data = new byte[srcdata.Length - 2 - 2]; - //Array.Copy(srcdata, 4, data, 0, data.Length); - - //CmdID = BitConverter.ToUInt16(srcdata, 0); - //Error = BitConverter.ToUInt16(srcdata, 2); - //Span span_srcdata = srcdata; - //data = span_srcdata.Slice(2 + 2).ToArray(); - CmdID = BitConverter.ToUInt16(srcdata.Slice(0, 2)); Error = BitConverter.ToUInt16(srcdata.Slice(2, 2)); data = srcdata.Slice(2 + 2).ToArray(); } } - public static class HunterNet_C2S { public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs, UInt16 CmdID, byte[] AddonBytes_Data) { myreadEventArgs.SetBuffer(CreatePkgData(CmdID, AddonBytes_Data)); } - public static byte[] CreatePkgData(UInt16 CmdID, byte[] AddonBytes_Data) { - //byte[] AddonBytes_CmdID = BitConverter.GetBytes(CmdID); - //int AllLenght = AddonBytes_CmdID.Length + AddonBytes_Data.Length + 4; - //int LastIndex = 0; - ////包长度 - //byte[] AddonBytes_Lenght = BitConverter.GetBytes(AllLenght); - - //byte[] BufferData = new byte[AllLenght]; - - ////包长度 - //AddonBytes_Lenght.CopyTo(BufferData, LastIndex); - //LastIndex += AddonBytes_Lenght.Length; - - ////CMDID - //AddonBytes_CmdID.CopyTo(BufferData, LastIndex); - //LastIndex += AddonBytes_CmdID.Length; - - ////DATA - //AddonBytes_Data.CopyTo(BufferData, LastIndex); - //LastIndex += AddonBytes_Data.Length; - - //myreadEventArgs.SetBuffer(BufferData, 0, BufferData.Length); - //return BufferData; - - //用Buffer.BlockCopy拷贝 - byte[] AddonBytes_CmdID = BitConverter.GetBytes(CmdID); int AllLenght = AddonBytes_CmdID.Length + AddonBytes_Data.Length + 4; - int LastIndex = 0; //包长度 byte[] AddonBytes_Lenght = BitConverter.GetBytes(AllLenght); - byte[] BufferData = new byte[AllLenght]; //包长度 - Buffer.BlockCopy(AddonBytes_Lenght, 0, BufferData, LastIndex, AddonBytes_Lenght.Length); - LastIndex += AddonBytes_Lenght.Length; - + Buffer.BlockCopy(AddonBytes_Lenght, 0, BufferData, 0, AddonBytes_Lenght.Length); //CMDID - Buffer.BlockCopy(AddonBytes_CmdID, 0, BufferData, LastIndex, AddonBytes_CmdID.Length); - LastIndex += AddonBytes_CmdID.Length; - + Buffer.BlockCopy(AddonBytes_CmdID, 0, BufferData, 4, AddonBytes_CmdID.Length); //DATA - Buffer.BlockCopy(AddonBytes_Data, 0, BufferData, LastIndex, AddonBytes_Data.Length); - LastIndex += AddonBytes_Data.Length; + Buffer.BlockCopy(AddonBytes_Data, 0, BufferData, 4 + 2, AddonBytes_Data.Length); return BufferData; } public static void AnalysisPkgData(Span srcdata, out UInt16 CmdID, out byte[] data) { - //data = new byte[srcdata.Length - 2]; - //CmdID = BitConverter.ToUInt16(srcdata, 0); - //Array.Copy(srcdata, 2, data, 0, data.Length); - - //CmdID = BitConverter.ToUInt16(srcdata, 0); - //Span span_srcdata = srcdata; - //data = span_srcdata.Slice(2).ToArray(); - CmdID = BitConverter.ToUInt16(srcdata.Slice(0, 2)); data = srcdata.Slice(2).ToArray(); } diff --git a/NetLib/HaoYueNet.ClientNetwork/IOCPMode/TcpSaeaClient.cs b/NetLib/HaoYueNet.ClientNetwork/IOCPMode/TcpSaeaClient.cs index 5cae85c..d24940e 100644 --- a/NetLib/HaoYueNet.ClientNetwork/IOCPMode/TcpSaeaClient.cs +++ b/NetLib/HaoYueNet.ClientNetwork/IOCPMode/TcpSaeaClient.cs @@ -227,7 +227,11 @@ namespace HaoYueNet.ClientNetwork.IOCPMode #region Token管理 public AsyncUserToken GetAsyncUserTokenForSocket(Socket sk) { - return _DictSocketAsyncUserToken.ContainsKey(sk) ? _DictSocketAsyncUserToken[sk] : null; + AsyncUserToken result; + if (_DictSocketAsyncUserToken.TryGetValue(sk, out result)) + return result; + return null; + //return _DictSocketAsyncUserToken.ContainsKey(sk) ? _DictSocketAsyncUserToken[sk] : null; } void AddUserToken(AsyncUserToken userToken) { @@ -283,7 +287,6 @@ namespace HaoYueNet.ClientNetwork.IOCPMode #region 连接 - public void StartConnect(string ip, int port, Socket socket, SocketAsyncEventArgs connectEventArg = null) { if (connectEventArg == null) diff --git a/NetLib/HaoYueNet.ClientNetwork/NetworkHelperCore.cs b/NetLib/HaoYueNet.ClientNetwork/NetworkHelperCore.cs index 8648f3a..18f947d 100644 --- a/NetLib/HaoYueNet.ClientNetwork/NetworkHelperCore.cs +++ b/NetLib/HaoYueNet.ClientNetwork/NetworkHelperCore.cs @@ -9,7 +9,6 @@ namespace HaoYueNet.ClientNetwork { private Socket client; - ////响应倒计时计数最大值 //private static int MaxRevIndexNum = 6; @@ -157,26 +156,6 @@ namespace HaoYueNet.ClientNetwork } } - ////拼接头长度 - //private byte[] SendDataWithHead(byte[] message) - //{ - - // MemoryStream memoryStream = new MemoryStream();//创建一个内存流 - - // byte[] BagHead = BitConverter.GetBytes(message.Length + 4);//往字节数组中写入包头(包头自身的长度和消息体的长度)的长度 - - // memoryStream.Write(BagHead, 0, BagHead.Length);//将包头写入内存流 - - // memoryStream.Write(message, 0, message.Length);//将消息体写入内存流 - - // byte[] HeadAndBody = memoryStream.ToArray();//将内存流中的数据写入字节数组 - - // memoryStream.Close();//关闭内存 - // memoryStream.Dispose();//释放资源 - - // return HeadAndBody; - //} - /// /// 供外部调用 发送消息 /// @@ -184,13 +163,6 @@ namespace HaoYueNet.ClientNetwork /// 序列化之后的数据 public void SendToServer(int CMDID,byte[] data) { - //LogOut("准备数据 CMDID=> "+CMDID); - /* - HunterNet_C2S _c2sdata = new HunterNet_C2S(); - _c2sdata.HunterNetCoreCmdID = CMDID; - _c2sdata.HunterNetCoreData = ByteString.CopyFrom(data); - byte[] _finaldata = Serizlize(_c2sdata); - */ byte[] _finaldata = HunterNet_C2S.CreatePkgData((ushort)CMDID, data); SendToSocket(_finaldata); } @@ -247,12 +219,6 @@ namespace HaoYueNet.ClientNetwork return; } - /* - HunterNet_S2C _c2s = DeSerizlize(data); - - OnReceiveData(_c2s.HunterNetCoreCmdID, _c2s.HunterNetCoreERRORCode, _c2s.HunterNetCoreData.ToArray()); - */ - HunterNet_S2C.AnalysisPkgData(data, out ushort CmdID, out ushort Error, out byte[] resultdata); OnReceiveData(CmdID, Error, resultdata); } @@ -359,7 +325,6 @@ namespace HaoYueNet.ClientNetwork public void LogOut(string Msg) { - //Console.WriteLine(Msg); OnLogOut?.Invoke(Msg); } diff --git a/NetLib/HaoYueNet.ClientNetwork/NetworkHelperP2PCore.cs b/NetLib/HaoYueNet.ClientNetwork/NetworkHelperP2PCore.cs index 7dab00d..f8cbfb2 100644 --- a/NetLib/HaoYueNet.ClientNetwork/NetworkHelperP2PCore.cs +++ b/NetLib/HaoYueNet.ClientNetwork/NetworkHelperP2PCore.cs @@ -124,26 +124,6 @@ namespace HaoYueNet.ClientNetwork client.Send(data); } - ////拼接头长度 - //private byte[] SendDataWithHead(byte[] message) - //{ - - // MemoryStream memoryStream = new MemoryStream();//创建一个内存流 - - // byte[] BagHead = BitConverter.GetBytes(message.Length + 4);//往字节数组中写入包头(包头自身的长度和消息体的长度)的长度 - - // memoryStream.Write(BagHead, 0, BagHead.Length);//将包头写入内存流 - - // memoryStream.Write(message, 0, message.Length);//将消息体写入内存流 - - // byte[] HeadAndBody = memoryStream.ToArray();//将内存流中的数据写入字节数组 - - // memoryStream.Close();//关闭内存 - // memoryStream.Dispose();//释放资源 - - // return HeadAndBody; - //} - /// /// 供外部调用 发送消息 /// @@ -152,13 +132,6 @@ namespace HaoYueNet.ClientNetwork /// public void SendToSocket(int CMDID, int ERRCODE, byte[] data) { - //LogOut("准备数据 CMDID=> "+CMDID); - /*HunterNet_S2C _s2sdata = new HunterNet_S2C(); - _s2sdata.HunterNetCoreCmdID = CMDID; - _s2sdata.HunterNetCoreERRORCode = ERRCODE; - _s2sdata.HunterNetCoreData = ByteString.CopyFrom(data); - byte[] _finaldata = Serizlize(_s2sdata);*/ - byte[] _finaldata = HunterNet_S2C.CreatePkgData((ushort)CMDID, (ushort)ERRCODE, data); SendToSocket(_finaldata); } @@ -324,7 +297,6 @@ namespace HaoYueNet.ClientNetwork public void LogOut(string Msg) { - //Console.WriteLine(Msg); OnLogOut(Msg); } diff --git a/NetLib/HaoYueNet.ServerNetwork/BaseData.cs b/NetLib/HaoYueNet.ServerNetwork/BaseData.cs index 5de4c0e..ca6ce1b 100644 --- a/NetLib/HaoYueNet.ServerNetwork/BaseData.cs +++ b/NetLib/HaoYueNet.ServerNetwork/BaseData.cs @@ -1,6 +1,4 @@ using System.Net.Sockets; -using System.Runtime.InteropServices; - namespace HaoYueNet.ServerNetwork { public static class BaseData @@ -9,21 +7,6 @@ namespace HaoYueNet.ServerNetwork /// 心跳包数据 /// public static byte[] HeartbeatData = new byte[5] { 0x05, 0x00, 0x00, 0x00, 0x00 }; - - public static void writeInt(byte[] buf, int offset, int value) - { - buf[offset++] = (byte)(255 & value); - buf[offset++] = (byte)(255 & value >> 8); - buf[offset++] = (byte)(255 & value >> 16); - buf[offset++] = (byte)(255 & value >>> 24); - } - - public static void writeUInt16(byte[] buf, int offset, int value) - { - buf[offset++] = (byte)(255 & value); - buf[offset++] = (byte)(255 & value >> 8); - } - public static class HunterNet_Heartbeat { public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs) @@ -31,128 +14,58 @@ namespace HaoYueNet.ServerNetwork myreadEventArgs.SetBuffer(HeartbeatData, 0, HeartbeatData.Length); } } - public static class HunterNet_S2C { public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs, UInt16 CmdID, UInt16 Error, byte[] AddonBytes_Data) { myreadEventArgs.SetBuffer(CreatePkgData(CmdID, Error, AddonBytes_Data)); } - public static byte[] CreatePkgData(UInt16 CmdID, UInt16 Error, byte[] AddonBytes_Data) { - //用Buffer.BlockCopy拷贝 //包长度 int AllLenght = 4 + 2 + 2 + AddonBytes_Data.Length; byte[] BufferData = new byte[AllLenght]; - - /* //包长度 - writeInt(BufferData, 0, AllLenght); - + Buffer.BlockCopy(BitConverter.GetBytes(AllLenght), 0, BufferData, 0, sizeof(int)); //CMDID - writeUInt16(BufferData, 4, CmdID); - - //Error - writeUInt16(BufferData, 4 + 2, Error); - */ - - - //包长度 - Buffer.BlockCopy(BitConverter.GetBytes(AllLenght), 0, BufferData, 0, sizeof(int)); - //CMDID - Buffer.BlockCopy(BitConverter.GetBytes(CmdID), 0, BufferData, 4, sizeof(UInt16)); - //CMDID - Buffer.BlockCopy(BitConverter.GetBytes(Error), 0, BufferData, 4 + 2, sizeof(UInt16)); - //DATA - Buffer.BlockCopy(AddonBytes_Data, 0, BufferData, 4 + 2 + 2, AddonBytes_Data.Length); - + Buffer.BlockCopy(BitConverter.GetBytes(CmdID), 0, BufferData, 4, sizeof(UInt16)); + //ErrID + Buffer.BlockCopy(BitConverter.GetBytes(Error), 0, BufferData, 4 + 2, sizeof(UInt16)); + //DATA + Buffer.BlockCopy(AddonBytes_Data, 0, BufferData, 4 + 2 + 2, AddonBytes_Data.Length); return BufferData; } - public static void AnalysisPkgData(Span srcdata, out UInt16 CmdID, out UInt16 Error, out byte[] data) { - //CmdID = BitConverter.ToUInt16(srcdata, 0); - //Error = BitConverter.ToUInt16(srcdata, 2); - //data = new byte[srcdata.Length - 2 - 2]; - //Array.Copy(srcdata, 4, data, 0, data.Length); - - //CmdID = BitConverter.ToUInt16(srcdata, 0); - //Error = BitConverter.ToUInt16(srcdata, 2); - //Span span_srcdata = srcdata; - //data = span_srcdata.Slice(2 + 2).ToArray(); - CmdID = BitConverter.ToUInt16(srcdata.Slice(0, 2)); Error = BitConverter.ToUInt16(srcdata.Slice(2, 2)); data = srcdata.Slice(2 + 2).ToArray(); } } - public static class HunterNet_C2S { public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs, UInt16 CmdID, byte[] AddonBytes_Data) { myreadEventArgs.SetBuffer(CreatePkgData(CmdID, AddonBytes_Data)); - } - + } public static byte[] CreatePkgData(UInt16 CmdID, byte[] AddonBytes_Data) { - //byte[] AddonBytes_CmdID = BitConverter.GetBytes(CmdID); - //int AllLenght = AddonBytes_CmdID.Length + AddonBytes_Data.Length + 4; - //int LastIndex = 0; - ////包长度 - //byte[] AddonBytes_Lenght = BitConverter.GetBytes(AllLenght); - - //byte[] BufferData = new byte[AllLenght]; - - ////包长度 - //AddonBytes_Lenght.CopyTo(BufferData, LastIndex); - //LastIndex += AddonBytes_Lenght.Length; - - ////CMDID - //AddonBytes_CmdID.CopyTo(BufferData, LastIndex); - //LastIndex += AddonBytes_CmdID.Length; - - ////DATA - //AddonBytes_Data.CopyTo(BufferData, LastIndex); - //LastIndex += AddonBytes_Data.Length; - - //myreadEventArgs.SetBuffer(BufferData, 0, BufferData.Length); - //return BufferData; - - //用Buffer.BlockCopy拷贝 - byte[] AddonBytes_CmdID = BitConverter.GetBytes(CmdID); int AllLenght = AddonBytes_CmdID.Length + AddonBytes_Data.Length + 4; - int LastIndex = 0; //包长度 byte[] AddonBytes_Lenght = BitConverter.GetBytes(AllLenght); - byte[] BufferData = new byte[AllLenght]; //包长度 - Buffer.BlockCopy(AddonBytes_Lenght, 0, BufferData, LastIndex, AddonBytes_Lenght.Length); - LastIndex += AddonBytes_Lenght.Length; - + Buffer.BlockCopy(AddonBytes_Lenght, 0, BufferData, 0, AddonBytes_Lenght.Length); //CMDID - Buffer.BlockCopy(AddonBytes_CmdID, 0, BufferData, LastIndex, AddonBytes_CmdID.Length); - LastIndex += AddonBytes_CmdID.Length; - + Buffer.BlockCopy(AddonBytes_CmdID, 0, BufferData, 4, AddonBytes_CmdID.Length); //DATA - Buffer.BlockCopy(AddonBytes_Data, 0, BufferData, LastIndex, AddonBytes_Data.Length); - LastIndex += AddonBytes_Data.Length; + Buffer.BlockCopy(AddonBytes_Data, 0, BufferData, 4 + 2, AddonBytes_Data.Length); return BufferData; } - public static void AnalysisPkgData(Span srcdata,out UInt16 CmdID, out byte[] data) + public static void AnalysisPkgData(Span srcdata, out UInt16 CmdID, out byte[] data) { - //data = new byte[srcdata.Length - 2]; - //CmdID = BitConverter.ToUInt16(srcdata, 0); - //Array.Copy(srcdata, 2, data, 0, data.Length); - - //CmdID = BitConverter.ToUInt16(srcdata, 0); - //Span span_srcdata = srcdata; - //data = span_srcdata.Slice(2).ToArray(); - CmdID = BitConverter.ToUInt16(srcdata.Slice(0, 2)); data = srcdata.Slice(2).ToArray(); } diff --git a/NetLib/HaoYueNet.ServerNetwork/NetWork/AsyncUserToken.cs b/NetLib/HaoYueNet.ServerNetwork/NetWork/AsyncUserToken.cs index 1b6df2c..54b8d84 100644 --- a/NetLib/HaoYueNet.ServerNetwork/NetWork/AsyncUserToken.cs +++ b/NetLib/HaoYueNet.ServerNetwork/NetWork/AsyncUserToken.cs @@ -30,16 +30,10 @@ namespace HaoYueNet.ServerNetwork /// public object UserInfo { get; set; } - /// - /// 数据缓存区 - /// - //public List Buffer { get; set; } - public MemoryStream memoryStream { get; set; } public AsyncUserToken() { - //this.Buffer = new List(); this.memoryStream = new MemoryStream(); } /// diff --git a/NetLib/HaoYueNet.ServerNetwork/NetWork/MemoryStreamPool.cs b/NetLib/HaoYueNet.ServerNetwork/NetWork/MemoryStreamPool.cs index ef01b0e..b5cb74b 100644 --- a/NetLib/HaoYueNet.ServerNetwork/NetWork/MemoryStreamPool.cs +++ b/NetLib/HaoYueNet.ServerNetwork/NetWork/MemoryStreamPool.cs @@ -1,8 +1,5 @@ -using System.Net.Sockets; - -namespace HaoYueNet.ServerNetwork +namespace HaoYueNet.ServerNetwork { - public class MemoryStreamPool { Stack m_pool; diff --git a/NetLib/HaoYueNet.ServerNetwork/NetWork/SocketEventPool.cs b/NetLib/HaoYueNet.ServerNetwork/NetWork/SocketEventPool.cs index 5b6603a..492985f 100644 --- a/NetLib/HaoYueNet.ServerNetwork/NetWork/SocketEventPool.cs +++ b/NetLib/HaoYueNet.ServerNetwork/NetWork/SocketEventPool.cs @@ -2,7 +2,6 @@ namespace HaoYueNet.ServerNetwork { - public class SocketEventPool { Stack m_pool; diff --git a/NetLib/HaoYueNet.ServerNetwork/NetWork/SourceMode/TcpSaeaServer_SourceMode.cs b/NetLib/HaoYueNet.ServerNetwork/NetWork/SourceMode/TcpSaeaServer_SourceMode.cs index 1236617..9dddef4 100644 --- a/NetLib/HaoYueNet.ServerNetwork/NetWork/SourceMode/TcpSaeaServer_SourceMode.cs +++ b/NetLib/HaoYueNet.ServerNetwork/NetWork/SourceMode/TcpSaeaServer_SourceMode.cs @@ -1,6 +1,4 @@ -//using HunterProtobufCore; -using System.IO; -using System.Net; +using System.Net; using System.Net.Sockets; using static HaoYueNet.ServerNetwork.BaseData; @@ -231,7 +229,11 @@ namespace HaoYueNet.ServerNetwork #region Token管理 public AsyncUserToken GetAsyncUserTokenForSocket(Socket sk) { - return _DictSocketAsyncUserToken.ContainsKey(sk) ? _DictSocketAsyncUserToken[sk] : null; + AsyncUserToken result; + if (_DictSocketAsyncUserToken.TryGetValue(sk,out result)) + return result; + return null; + //return _DictSocketAsyncUserToken.ContainsKey(sk) ? _DictSocketAsyncUserToken[sk] : null; } void AddUserToken(AsyncUserToken userToken) { @@ -605,58 +607,5 @@ namespace HaoYueNet.ServerNetwork OnNetLog?.Invoke(msg); } #endregion - - #region 心跳包 - /* - /// - /// 发送心跳包 - /// - /// - /// - private void SendHeartbeatWithIndex(AsyncUserToken token) - { - if (token == null || token.Socket == null || !token.Socket.Connected) - return; - try - { - //OutNetLog(DateTime.Now.ToString() + "发送心跳包"); - token.SendIndex = MaxSendIndexNum; - SendHeartbeatMessage(token); - } - catch (Exception e) - { - CloseReady(token); - } - } - /// - /// 心跳包时钟事件 - /// - /// - /// - private void CheckUpdatetimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) - { - for (int i = 0; i < m_clients.Count(); i++) - { - //接收服务器数据计数 - m_clients[i].RevIndex--; - if (m_clients[i].RevIndex <= 0) - { - //判定掉线 - CloseReady(m_clients[i]); - return; - } - - //发送计数 - m_clients[i].SendIndex--; - if (m_clients[i].SendIndex <= 0)//需要发送心跳包了 - { - //重置倒计时计数 - m_clients[i].SendIndex = MaxSendIndexNum; - SendHeartbeatWithIndex(m_clients[i]); - } - } - } - */ - #endregion } } diff --git a/NetLib/HaoYueNet.ServerNetwork/NetWork/SourceMode/TokenMsgPool_SourceMode.cs b/NetLib/HaoYueNet.ServerNetwork/NetWork/SourceMode/TokenMsgPool_SourceMode.cs index ffb1c06..2b0116a 100644 --- a/NetLib/HaoYueNet.ServerNetwork/NetWork/SourceMode/TokenMsgPool_SourceMode.cs +++ b/NetLib/HaoYueNet.ServerNetwork/NetWork/SourceMode/TokenMsgPool_SourceMode.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net.Sockets; -using System.Text; - -namespace HaoYueNet.ServerNetwork +namespace HaoYueNet.ServerNetwork { public class TokenWithMsg_SourceMode { @@ -15,7 +9,6 @@ namespace HaoYueNet.ServerNetwork public class TokenMsgPool_SourceMode { - //Stack msg_pool; Queue msg_pool; public TokenMsgPool_SourceMode(int capacity) diff --git a/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs b/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs index f54f5f0..78152ef 100644 --- a/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs +++ b/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs @@ -231,7 +231,11 @@ namespace HaoYueNet.ServerNetwork #region Token管理 public AsyncUserToken GetAsyncUserTokenForSocket(Socket sk) { - return _DictSocketAsyncUserToken.ContainsKey(sk) ? _DictSocketAsyncUserToken[sk] : null; + AsyncUserToken result; + if (_DictSocketAsyncUserToken.TryGetValue(sk, out result)) + return result; + return null; + //return _DictSocketAsyncUserToken.ContainsKey(sk) ? _DictSocketAsyncUserToken[sk] : null; } void AddUserToken(AsyncUserToken userToken) { diff --git a/NetLib_Standard2/HaoYueNet.ServerNetwork.Standard2/NetWork/SourceMode/TcpSaeaServer_SourceMode.cs b/NetLib_Standard2/HaoYueNet.ServerNetwork.Standard2/NetWork/SourceMode/TcpSaeaServer_SourceMode.cs index 4eb1c00..6d91ed1 100644 --- a/NetLib_Standard2/HaoYueNet.ServerNetwork.Standard2/NetWork/SourceMode/TcpSaeaServer_SourceMode.cs +++ b/NetLib_Standard2/HaoYueNet.ServerNetwork.Standard2/NetWork/SourceMode/TcpSaeaServer_SourceMode.cs @@ -234,7 +234,11 @@ namespace HaoYueNet.ServerNetwork.Standard2 #region Token管理 public AsyncUserToken GetAsyncUserTokenForSocket(Socket sk) { - return _DictSocketAsyncUserToken.ContainsKey(sk) ? _DictSocketAsyncUserToken[sk] : null; + AsyncUserToken result; + if (_DictSocketAsyncUserToken.TryGetValue(sk, out result)) + return result; + return null; + //return _DictSocketAsyncUserToken.ContainsKey(sk) ? _DictSocketAsyncUserToken[sk] : null; } void AddUserToken(AsyncUserToken userToken) { diff --git a/NetLib_Standard2/HaoYueNet.ServerNetwork.Standard2/NetWork/TcpSaeaServer.cs b/NetLib_Standard2/HaoYueNet.ServerNetwork.Standard2/NetWork/TcpSaeaServer.cs index 54b7675..a7adabc 100644 --- a/NetLib_Standard2/HaoYueNet.ServerNetwork.Standard2/NetWork/TcpSaeaServer.cs +++ b/NetLib_Standard2/HaoYueNet.ServerNetwork.Standard2/NetWork/TcpSaeaServer.cs @@ -234,7 +234,11 @@ namespace HaoYueNet.ServerNetwork.Standard2 #region Token管理 public AsyncUserToken GetAsyncUserTokenForSocket(Socket sk) { - return _DictSocketAsyncUserToken.ContainsKey(sk) ? _DictSocketAsyncUserToken[sk] : null; + AsyncUserToken result; + if (_DictSocketAsyncUserToken.TryGetValue(sk, out result)) + return result; + return null; + //return _DictSocketAsyncUserToken.ContainsKey(sk) ? _DictSocketAsyncUserToken[sk] : null; } void AddUserToken(AsyncUserToken userToken) { diff --git a/Simple/ClientCore/Event/EventSystem.cs b/Simple/ClientCore/Event/EventSystem.cs index fee8162..64263a5 100644 --- a/Simple/ClientCore/Event/EventSystem.cs +++ b/Simple/ClientCore/Event/EventSystem.cs @@ -63,17 +63,29 @@ private void InterRegisterEvent(EEvent evt, Delegate callback) { - if (eventDic.ContainsKey(evt)) + if (eventDic.TryGetValue(evt, out List dlist)) { - if (eventDic[evt].IndexOf(callback) < 0) + if (dlist.IndexOf(callback) < 0) { - eventDic[evt].Add(callback); + dlist.Add(callback); } } else - { + { eventDic.Add(evt, new List() { callback }); } + + //if (eventDic.ContainsKey(evt)) + //{ + // if (eventDic[evt].IndexOf(callback) < 0) + // { + // eventDic[evt].Add(callback); + // } + //} + //else + //{ + // eventDic.Add(evt, new List() { callback }); + //} } #endregion @@ -111,11 +123,16 @@ private void InterUnregisterEvent(EEvent evt, Delegate callback) { - if (eventDic.ContainsKey(evt)) + if (eventDic.TryGetValue(evt, out List dlist)) { - eventDic[evt].Remove(callback); - if (eventDic[evt].Count == 0) eventDic.Remove(evt); + dlist.Remove(callback); + if (dlist.Count == 0) eventDic.Remove(evt); } + //if (eventDic.ContainsKey(evt)) + //{ + // eventDic[evt].Remove(callback); + // if (eventDic[evt].Count == 0) eventDic.Remove(evt); + //} } #endregion @@ -224,14 +241,21 @@ /// private List GetEventList(EEvent evt) { - if (eventDic.ContainsKey(evt)) + if (eventDic.TryGetValue(evt,out List dlist)) { - List tempList = eventDic[evt]; - if (null != tempList) + if (null != dlist) { - return tempList; + return dlist; } } + //if (eventDic.ContainsKey(evt)) + //{ + // List tempList = eventDic[evt]; + // if (null != tempList) + // { + // return tempList; + // } + //} return null; } } diff --git a/Simple/ClientCore/Network/NetMsg.cs b/Simple/ClientCore/Network/NetMsg.cs index f2f2d74..45e3f02 100644 --- a/Simple/ClientCore/Network/NetMsg.cs +++ b/Simple/ClientCore/Network/NetMsg.cs @@ -20,17 +20,28 @@ private void InterRegNetMsgEvent(int cmd, Delegate callback) { - if (netEventDic.ContainsKey(cmd)) + if (netEventDic.TryGetValue(cmd, out List dlist)) { - if (netEventDic[cmd].IndexOf(callback) < 0) + if (dlist.IndexOf(callback) < 0) { - netEventDic[cmd].Add(callback); + dlist.Add(callback); } } else { netEventDic.Add(cmd, new List() { callback }); } + //if (netEventDic.ContainsKey(cmd)) + //{ + // if (netEventDic[cmd].IndexOf(callback) < 0) + // { + // netEventDic[cmd].Add(callback); + // } + //} + //else + //{ + // netEventDic.Add(cmd, new List() { callback }); + //} } #endregion @@ -44,11 +55,16 @@ private void InterUnregisterCMD(int cmd, Delegate callback) { - if (netEventDic.ContainsKey(cmd)) + if (netEventDic.TryGetValue(cmd, out List dlist)) { - netEventDic[cmd].Remove(callback); - if (netEventDic[cmd].Count == 0) netEventDic.Remove(cmd); + dlist.Remove(callback); + if (dlist.Count == 0) netEventDic.Remove(cmd); } + //if (netEventDic.ContainsKey(cmd)) + //{ + // netEventDic[cmd].Remove(callback); + // if (netEventDic[cmd].Count == 0) netEventDic.Remove(cmd); + //} } #endregion @@ -80,14 +96,22 @@ /// private List GetNetEventDicList(int cmd) { - if (netEventDic.ContainsKey(cmd)) + + if (netEventDic.TryGetValue(cmd, out List dlist)) { - List tempList = netEventDic[cmd]; - if (null != tempList) + if (null != dlist) { - return tempList; + return dlist; } } + //if (netEventDic.ContainsKey(cmd)) + //{ + // List tempList = netEventDic[cmd]; + // if (null != tempList) + // { + // return tempList; + // } + //} return null; } } diff --git a/Simple/ServerCore/Event/EventSystem.cs b/Simple/ServerCore/Event/EventSystem.cs index 900c61a..201c21c 100644 --- a/Simple/ServerCore/Event/EventSystem.cs +++ b/Simple/ServerCore/Event/EventSystem.cs @@ -202,14 +202,21 @@ namespace ServerCore.Event /// private List GetEventList(EEvent evt) { - if (eventDic.ContainsKey(evt)) + if (eventDic.TryGetValue(evt, out List dlist)) { - List tempList = eventDic[evt]; - if (null != tempList) + if (null != dlist) { - return tempList; + return dlist; } } + //if (eventDic.ContainsKey(evt)) + //{ + // List tempList = eventDic[evt]; + // if (null != tempList) + // { + // return tempList; + // } + //} return null; } }