规整代码

This commit is contained in:
sin365 2024-08-23 16:18:37 +08:00
parent 3036ef0e05
commit 744c29d60b
16 changed files with 126 additions and 351 deletions

View File

@ -1,5 +1,4 @@
using System.Net.Sockets; using System.Net.Sockets;
namespace HaoYueNet.ClientNetwork namespace HaoYueNet.ClientNetwork
{ {
public static class BaseData public static class BaseData
@ -8,21 +7,6 @@ namespace HaoYueNet.ClientNetwork
/// 心跳包数据 /// 心跳包数据
/// </summary> /// </summary>
public static byte[] HeartbeatData = new byte[5] { 0x05, 0x00, 0x00, 0x00, 0x00 }; 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 class HunterNet_Heartbeat
{ {
public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs) public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs)
@ -30,119 +14,58 @@ namespace HaoYueNet.ClientNetwork
myreadEventArgs.SetBuffer(HeartbeatData, 0, HeartbeatData.Length); myreadEventArgs.SetBuffer(HeartbeatData, 0, HeartbeatData.Length);
} }
} }
public static class HunterNet_S2C public static class HunterNet_S2C
{ {
public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs, UInt16 CmdID, UInt16 Error, byte[] AddonBytes_Data) public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs, UInt16 CmdID, UInt16 Error, byte[] AddonBytes_Data)
{ {
myreadEventArgs.SetBuffer(CreatePkgData(CmdID, Error, AddonBytes_Data)); myreadEventArgs.SetBuffer(CreatePkgData(CmdID, Error, AddonBytes_Data));
} }
public static byte[] CreatePkgData(UInt16 CmdID, UInt16 Error, byte[] AddonBytes_Data) public static byte[] CreatePkgData(UInt16 CmdID, UInt16 Error, byte[] AddonBytes_Data)
{ {
//用Buffer.BlockCopy拷贝
//包长度 //包长度
int AllLenght = 4 + 2 + 2 + AddonBytes_Data.Length; int AllLenght = 4 + 2 + 2 + AddonBytes_Data.Length;
byte[] BufferData = new byte[AllLenght]; byte[] BufferData = new byte[AllLenght];
//包长度 //包长度
writeInt(BufferData, 0, AllLenght); Buffer.BlockCopy(BitConverter.GetBytes(AllLenght), 0, BufferData, 0, sizeof(int));
//CMDID //CMDID
writeUInt16(BufferData, 4, CmdID); Buffer.BlockCopy(BitConverter.GetBytes(CmdID), 0, BufferData, 4, sizeof(UInt16));
//ErrID
//Error Buffer.BlockCopy(BitConverter.GetBytes(Error), 0, BufferData, 4 + 2, sizeof(UInt16));
writeUInt16(BufferData, 4 + 2, Error);
//DATA //DATA
Buffer.BlockCopy(AddonBytes_Data, 0, BufferData, 4 + 2 + 2, AddonBytes_Data.Length); Buffer.BlockCopy(AddonBytes_Data, 0, BufferData, 4 + 2 + 2, AddonBytes_Data.Length);
return BufferData; return BufferData;
} }
public static void AnalysisPkgData(Span<byte> srcdata, out UInt16 CmdID, out UInt16 Error, out byte[] data) public static void AnalysisPkgData(Span<byte> 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<byte> span_srcdata = srcdata;
//data = span_srcdata.Slice(2 + 2).ToArray();
CmdID = BitConverter.ToUInt16(srcdata.Slice(0, 2)); CmdID = BitConverter.ToUInt16(srcdata.Slice(0, 2));
Error = BitConverter.ToUInt16(srcdata.Slice(2, 2)); Error = BitConverter.ToUInt16(srcdata.Slice(2, 2));
data = srcdata.Slice(2 + 2).ToArray(); data = srcdata.Slice(2 + 2).ToArray();
} }
} }
public static class HunterNet_C2S public static class HunterNet_C2S
{ {
public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs, UInt16 CmdID, byte[] AddonBytes_Data) public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs, UInt16 CmdID, byte[] AddonBytes_Data)
{ {
myreadEventArgs.SetBuffer(CreatePkgData(CmdID, AddonBytes_Data)); myreadEventArgs.SetBuffer(CreatePkgData(CmdID, AddonBytes_Data));
} }
public static byte[] CreatePkgData(UInt16 CmdID, byte[] 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); byte[] AddonBytes_CmdID = BitConverter.GetBytes(CmdID);
int AllLenght = AddonBytes_CmdID.Length + AddonBytes_Data.Length + 4; int AllLenght = AddonBytes_CmdID.Length + AddonBytes_Data.Length + 4;
int LastIndex = 0;
//包长度 //包长度
byte[] AddonBytes_Lenght = BitConverter.GetBytes(AllLenght); byte[] AddonBytes_Lenght = BitConverter.GetBytes(AllLenght);
byte[] BufferData = new byte[AllLenght]; byte[] BufferData = new byte[AllLenght];
//包长度 //包长度
Buffer.BlockCopy(AddonBytes_Lenght, 0, BufferData, LastIndex, AddonBytes_Lenght.Length); Buffer.BlockCopy(AddonBytes_Lenght, 0, BufferData, 0, AddonBytes_Lenght.Length);
LastIndex += AddonBytes_Lenght.Length;
//CMDID //CMDID
Buffer.BlockCopy(AddonBytes_CmdID, 0, BufferData, LastIndex, AddonBytes_CmdID.Length); Buffer.BlockCopy(AddonBytes_CmdID, 0, BufferData, 4, AddonBytes_CmdID.Length);
LastIndex += AddonBytes_CmdID.Length;
//DATA //DATA
Buffer.BlockCopy(AddonBytes_Data, 0, BufferData, LastIndex, AddonBytes_Data.Length); Buffer.BlockCopy(AddonBytes_Data, 0, BufferData, 4 + 2, AddonBytes_Data.Length);
LastIndex += AddonBytes_Data.Length;
return BufferData; return BufferData;
} }
public static void AnalysisPkgData(Span<byte> srcdata, out UInt16 CmdID, out byte[] data) public static void AnalysisPkgData(Span<byte> 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<byte> span_srcdata = srcdata;
//data = span_srcdata.Slice(2).ToArray();
CmdID = BitConverter.ToUInt16(srcdata.Slice(0, 2)); CmdID = BitConverter.ToUInt16(srcdata.Slice(0, 2));
data = srcdata.Slice(2).ToArray(); data = srcdata.Slice(2).ToArray();
} }

View File

@ -227,7 +227,11 @@ namespace HaoYueNet.ClientNetwork.IOCPMode
#region Token管理 #region Token管理
public AsyncUserToken GetAsyncUserTokenForSocket(Socket sk) 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) void AddUserToken(AsyncUserToken userToken)
{ {
@ -283,7 +287,6 @@ namespace HaoYueNet.ClientNetwork.IOCPMode
#region #region
public void StartConnect(string ip, int port, Socket socket, SocketAsyncEventArgs connectEventArg = null) public void StartConnect(string ip, int port, Socket socket, SocketAsyncEventArgs connectEventArg = null)
{ {
if (connectEventArg == null) if (connectEventArg == null)

View File

@ -9,7 +9,6 @@ namespace HaoYueNet.ClientNetwork
{ {
private Socket client; private Socket client;
////响应倒计时计数最大值 ////响应倒计时计数最大值
//private static int MaxRevIndexNum = 6; //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;
//}
/// <summary> /// <summary>
/// 供外部调用 发送消息 /// 供外部调用 发送消息
/// </summary> /// </summary>
@ -184,13 +163,6 @@ namespace HaoYueNet.ClientNetwork
/// <param name="data">序列化之后的数据</param> /// <param name="data">序列化之后的数据</param>
public void SendToServer(int CMDID,byte[] data) 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); byte[] _finaldata = HunterNet_C2S.CreatePkgData((ushort)CMDID, data);
SendToSocket(_finaldata); SendToSocket(_finaldata);
} }
@ -247,12 +219,6 @@ namespace HaoYueNet.ClientNetwork
return; return;
} }
/*
HunterNet_S2C _c2s = DeSerizlize<HunterNet_S2C>(data);
OnReceiveData(_c2s.HunterNetCoreCmdID, _c2s.HunterNetCoreERRORCode, _c2s.HunterNetCoreData.ToArray());
*/
HunterNet_S2C.AnalysisPkgData(data, out ushort CmdID, out ushort Error, out byte[] resultdata); HunterNet_S2C.AnalysisPkgData(data, out ushort CmdID, out ushort Error, out byte[] resultdata);
OnReceiveData(CmdID, Error, resultdata); OnReceiveData(CmdID, Error, resultdata);
} }
@ -359,7 +325,6 @@ namespace HaoYueNet.ClientNetwork
public void LogOut(string Msg) public void LogOut(string Msg)
{ {
//Console.WriteLine(Msg);
OnLogOut?.Invoke(Msg); OnLogOut?.Invoke(Msg);
} }

View File

@ -124,26 +124,6 @@ namespace HaoYueNet.ClientNetwork
client.Send(data); 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;
//}
/// <summary> /// <summary>
/// 供外部调用 发送消息 /// 供外部调用 发送消息
/// </summary> /// </summary>
@ -152,13 +132,6 @@ namespace HaoYueNet.ClientNetwork
/// <param name="data"></param> /// <param name="data"></param>
public void SendToSocket(int CMDID, int ERRCODE, byte[] data) 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); byte[] _finaldata = HunterNet_S2C.CreatePkgData((ushort)CMDID, (ushort)ERRCODE, data);
SendToSocket(_finaldata); SendToSocket(_finaldata);
} }
@ -324,7 +297,6 @@ namespace HaoYueNet.ClientNetwork
public void LogOut(string Msg) public void LogOut(string Msg)
{ {
//Console.WriteLine(Msg);
OnLogOut(Msg); OnLogOut(Msg);
} }

View File

@ -1,6 +1,4 @@
using System.Net.Sockets; using System.Net.Sockets;
using System.Runtime.InteropServices;
namespace HaoYueNet.ServerNetwork namespace HaoYueNet.ServerNetwork
{ {
public static class BaseData public static class BaseData
@ -9,21 +7,6 @@ namespace HaoYueNet.ServerNetwork
/// 心跳包数据 /// 心跳包数据
/// </summary> /// </summary>
public static byte[] HeartbeatData = new byte[5] { 0x05, 0x00, 0x00, 0x00, 0x00 }; 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 class HunterNet_Heartbeat
{ {
public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs) public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs)
@ -31,128 +14,58 @@ namespace HaoYueNet.ServerNetwork
myreadEventArgs.SetBuffer(HeartbeatData, 0, HeartbeatData.Length); myreadEventArgs.SetBuffer(HeartbeatData, 0, HeartbeatData.Length);
} }
} }
public static class HunterNet_S2C public static class HunterNet_S2C
{ {
public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs, UInt16 CmdID, UInt16 Error, byte[] AddonBytes_Data) public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs, UInt16 CmdID, UInt16 Error, byte[] AddonBytes_Data)
{ {
myreadEventArgs.SetBuffer(CreatePkgData(CmdID, Error, AddonBytes_Data)); myreadEventArgs.SetBuffer(CreatePkgData(CmdID, Error, AddonBytes_Data));
} }
public static byte[] CreatePkgData(UInt16 CmdID, UInt16 Error, byte[] AddonBytes_Data) public static byte[] CreatePkgData(UInt16 CmdID, UInt16 Error, byte[] AddonBytes_Data)
{ {
//用Buffer.BlockCopy拷贝
//包长度 //包长度
int AllLenght = 4 + 2 + 2 + AddonBytes_Data.Length; int AllLenght = 4 + 2 + 2 + AddonBytes_Data.Length;
byte[] BufferData = new byte[AllLenght]; byte[] BufferData = new byte[AllLenght];
/*
//包长度
writeInt(BufferData, 0, AllLenght);
//CMDID
writeUInt16(BufferData, 4, CmdID);
//Error
writeUInt16(BufferData, 4 + 2, Error);
*/
//包长度 //包长度
Buffer.BlockCopy(BitConverter.GetBytes(AllLenght), 0, BufferData, 0, sizeof(int)); Buffer.BlockCopy(BitConverter.GetBytes(AllLenght), 0, BufferData, 0, sizeof(int));
//CMDID //CMDID
Buffer.BlockCopy(BitConverter.GetBytes(CmdID), 0, BufferData, 4, sizeof(UInt16)); Buffer.BlockCopy(BitConverter.GetBytes(CmdID), 0, BufferData, 4, sizeof(UInt16));
//CMDID //ErrID
Buffer.BlockCopy(BitConverter.GetBytes(Error), 0, BufferData, 4 + 2, sizeof(UInt16)); Buffer.BlockCopy(BitConverter.GetBytes(Error), 0, BufferData, 4 + 2, sizeof(UInt16));
//DATA //DATA
Buffer.BlockCopy(AddonBytes_Data, 0, BufferData, 4 + 2 + 2, AddonBytes_Data.Length); Buffer.BlockCopy(AddonBytes_Data, 0, BufferData, 4 + 2 + 2, AddonBytes_Data.Length);
return BufferData; return BufferData;
} }
public static void AnalysisPkgData(Span<byte> srcdata, out UInt16 CmdID, out UInt16 Error, out byte[] data) public static void AnalysisPkgData(Span<byte> 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<byte> span_srcdata = srcdata;
//data = span_srcdata.Slice(2 + 2).ToArray();
CmdID = BitConverter.ToUInt16(srcdata.Slice(0, 2)); CmdID = BitConverter.ToUInt16(srcdata.Slice(0, 2));
Error = BitConverter.ToUInt16(srcdata.Slice(2, 2)); Error = BitConverter.ToUInt16(srcdata.Slice(2, 2));
data = srcdata.Slice(2 + 2).ToArray(); data = srcdata.Slice(2 + 2).ToArray();
} }
} }
public static class HunterNet_C2S public static class HunterNet_C2S
{ {
public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs, UInt16 CmdID, byte[] AddonBytes_Data) public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs, UInt16 CmdID, byte[] AddonBytes_Data)
{ {
myreadEventArgs.SetBuffer(CreatePkgData(CmdID, AddonBytes_Data)); myreadEventArgs.SetBuffer(CreatePkgData(CmdID, AddonBytes_Data));
} }
public static byte[] CreatePkgData(UInt16 CmdID, byte[] 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); byte[] AddonBytes_CmdID = BitConverter.GetBytes(CmdID);
int AllLenght = AddonBytes_CmdID.Length + AddonBytes_Data.Length + 4; int AllLenght = AddonBytes_CmdID.Length + AddonBytes_Data.Length + 4;
int LastIndex = 0;
//包长度 //包长度
byte[] AddonBytes_Lenght = BitConverter.GetBytes(AllLenght); byte[] AddonBytes_Lenght = BitConverter.GetBytes(AllLenght);
byte[] BufferData = new byte[AllLenght]; byte[] BufferData = new byte[AllLenght];
//包长度 //包长度
Buffer.BlockCopy(AddonBytes_Lenght, 0, BufferData, LastIndex, AddonBytes_Lenght.Length); Buffer.BlockCopy(AddonBytes_Lenght, 0, BufferData, 0, AddonBytes_Lenght.Length);
LastIndex += AddonBytes_Lenght.Length;
//CMDID //CMDID
Buffer.BlockCopy(AddonBytes_CmdID, 0, BufferData, LastIndex, AddonBytes_CmdID.Length); Buffer.BlockCopy(AddonBytes_CmdID, 0, BufferData, 4, AddonBytes_CmdID.Length);
LastIndex += AddonBytes_CmdID.Length;
//DATA //DATA
Buffer.BlockCopy(AddonBytes_Data, 0, BufferData, LastIndex, AddonBytes_Data.Length); Buffer.BlockCopy(AddonBytes_Data, 0, BufferData, 4 + 2, AddonBytes_Data.Length);
LastIndex += AddonBytes_Data.Length;
return BufferData; return BufferData;
} }
public static void AnalysisPkgData(Span<byte> srcdata,out UInt16 CmdID, out byte[] data) public static void AnalysisPkgData(Span<byte> 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<byte> span_srcdata = srcdata;
//data = span_srcdata.Slice(2).ToArray();
CmdID = BitConverter.ToUInt16(srcdata.Slice(0, 2)); CmdID = BitConverter.ToUInt16(srcdata.Slice(0, 2));
data = srcdata.Slice(2).ToArray(); data = srcdata.Slice(2).ToArray();
} }

View File

@ -30,16 +30,10 @@ namespace HaoYueNet.ServerNetwork
/// </summary> /// </summary>
public object UserInfo { get; set; } public object UserInfo { get; set; }
/// <summary>
/// 数据缓存区
/// </summary>
//public List<byte> Buffer { get; set; }
public MemoryStream memoryStream { get; set; } public MemoryStream memoryStream { get; set; }
public AsyncUserToken() public AsyncUserToken()
{ {
//this.Buffer = new List<byte>();
this.memoryStream = new MemoryStream(); this.memoryStream = new MemoryStream();
} }
/// <summary> /// <summary>

View File

@ -1,8 +1,5 @@
using System.Net.Sockets; namespace HaoYueNet.ServerNetwork
namespace HaoYueNet.ServerNetwork
{ {
public class MemoryStreamPool public class MemoryStreamPool
{ {
Stack<MemoryStream> m_pool; Stack<MemoryStream> m_pool;

View File

@ -2,7 +2,6 @@
namespace HaoYueNet.ServerNetwork namespace HaoYueNet.ServerNetwork
{ {
public class SocketEventPool public class SocketEventPool
{ {
Stack<SocketAsyncEventArgs> m_pool; Stack<SocketAsyncEventArgs> m_pool;

View File

@ -1,6 +1,4 @@
//using HunterProtobufCore; using System.Net;
using System.IO;
using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using static HaoYueNet.ServerNetwork.BaseData; using static HaoYueNet.ServerNetwork.BaseData;
@ -231,7 +229,11 @@ namespace HaoYueNet.ServerNetwork
#region Token管理 #region Token管理
public AsyncUserToken GetAsyncUserTokenForSocket(Socket sk) 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) void AddUserToken(AsyncUserToken userToken)
{ {
@ -605,58 +607,5 @@ namespace HaoYueNet.ServerNetwork
OnNetLog?.Invoke(msg); OnNetLog?.Invoke(msg);
} }
#endregion #endregion
#region
/*
/// <summary>
/// 发送心跳包
/// </summary>
/// <param name="sk"></param>
///
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);
}
}
/// <summary>
/// 心跳包时钟事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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
} }
} }

View File

@ -1,10 +1,4 @@
using System; namespace HaoYueNet.ServerNetwork
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
namespace HaoYueNet.ServerNetwork
{ {
public class TokenWithMsg_SourceMode public class TokenWithMsg_SourceMode
{ {
@ -15,7 +9,6 @@ namespace HaoYueNet.ServerNetwork
public class TokenMsgPool_SourceMode public class TokenMsgPool_SourceMode
{ {
//Stack<TokenWithMsg> msg_pool;
Queue<TokenWithMsg_SourceMode> msg_pool; Queue<TokenWithMsg_SourceMode> msg_pool;
public TokenMsgPool_SourceMode(int capacity) public TokenMsgPool_SourceMode(int capacity)

View File

@ -231,7 +231,11 @@ namespace HaoYueNet.ServerNetwork
#region Token管理 #region Token管理
public AsyncUserToken GetAsyncUserTokenForSocket(Socket sk) 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) void AddUserToken(AsyncUserToken userToken)
{ {

View File

@ -234,7 +234,11 @@ namespace HaoYueNet.ServerNetwork.Standard2
#region Token管理 #region Token管理
public AsyncUserToken GetAsyncUserTokenForSocket(Socket sk) 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) void AddUserToken(AsyncUserToken userToken)
{ {

View File

@ -234,7 +234,11 @@ namespace HaoYueNet.ServerNetwork.Standard2
#region Token管理 #region Token管理
public AsyncUserToken GetAsyncUserTokenForSocket(Socket sk) 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) void AddUserToken(AsyncUserToken userToken)
{ {

View File

@ -63,17 +63,29 @@
private void InterRegisterEvent(EEvent evt, Delegate callback) private void InterRegisterEvent(EEvent evt, Delegate callback)
{ {
if (eventDic.ContainsKey(evt)) if (eventDic.TryGetValue(evt, out List<Delegate> dlist))
{ {
if (eventDic[evt].IndexOf(callback) < 0) if (dlist.IndexOf(callback) < 0)
{ {
eventDic[evt].Add(callback); dlist.Add(callback);
} }
} }
else else
{ {
eventDic.Add(evt, new List<Delegate>() { callback }); eventDic.Add(evt, new List<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 #endregion
@ -111,11 +123,16 @@
private void InterUnregisterEvent(EEvent evt, Delegate callback) private void InterUnregisterEvent(EEvent evt, Delegate callback)
{ {
if (eventDic.ContainsKey(evt)) if (eventDic.TryGetValue(evt, out List<Delegate> dlist))
{ {
eventDic[evt].Remove(callback); dlist.Remove(callback);
if (eventDic[evt].Count == 0) eventDic.Remove(evt); if (dlist.Count == 0) eventDic.Remove(evt);
} }
//if (eventDic.ContainsKey(evt))
//{
// eventDic[evt].Remove(callback);
// if (eventDic[evt].Count == 0) eventDic.Remove(evt);
//}
} }
#endregion #endregion
@ -224,14 +241,21 @@
/// <returns></returns> /// <returns></returns>
private List<Delegate> GetEventList(EEvent evt) private List<Delegate> GetEventList(EEvent evt)
{ {
if (eventDic.ContainsKey(evt)) if (eventDic.TryGetValue(evt,out List<Delegate> dlist))
{ {
List<Delegate> tempList = eventDic[evt]; if (null != dlist)
if (null != tempList)
{ {
return tempList; return dlist;
} }
} }
//if (eventDic.ContainsKey(evt))
//{
// List<Delegate> tempList = eventDic[evt];
// if (null != tempList)
// {
// return tempList;
// }
//}
return null; return null;
} }
} }

View File

@ -20,17 +20,28 @@
private void InterRegNetMsgEvent(int cmd, Delegate callback) private void InterRegNetMsgEvent(int cmd, Delegate callback)
{ {
if (netEventDic.ContainsKey(cmd)) if (netEventDic.TryGetValue(cmd, out List<Delegate> dlist))
{ {
if (netEventDic[cmd].IndexOf(callback) < 0) if (dlist.IndexOf(callback) < 0)
{ {
netEventDic[cmd].Add(callback); dlist.Add(callback);
} }
} }
else else
{ {
netEventDic.Add(cmd, new List<Delegate>() { callback }); netEventDic.Add(cmd, new List<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 #endregion
@ -44,11 +55,16 @@
private void InterUnregisterCMD(int cmd, Delegate callback) private void InterUnregisterCMD(int cmd, Delegate callback)
{ {
if (netEventDic.ContainsKey(cmd)) if (netEventDic.TryGetValue(cmd, out List<Delegate> dlist))
{ {
netEventDic[cmd].Remove(callback); dlist.Remove(callback);
if (netEventDic[cmd].Count == 0) netEventDic.Remove(cmd); if (dlist.Count == 0) netEventDic.Remove(cmd);
} }
//if (netEventDic.ContainsKey(cmd))
//{
// netEventDic[cmd].Remove(callback);
// if (netEventDic[cmd].Count == 0) netEventDic.Remove(cmd);
//}
} }
#endregion #endregion
@ -80,14 +96,22 @@
/// <returns></returns> /// <returns></returns>
private List<Delegate> GetNetEventDicList(int cmd) private List<Delegate> GetNetEventDicList(int cmd)
{ {
if (netEventDic.ContainsKey(cmd))
if (netEventDic.TryGetValue(cmd, out List<Delegate> dlist))
{ {
List<Delegate> tempList = netEventDic[cmd]; if (null != dlist)
if (null != tempList)
{ {
return tempList; return dlist;
} }
} }
//if (netEventDic.ContainsKey(cmd))
//{
// List<Delegate> tempList = netEventDic[cmd];
// if (null != tempList)
// {
// return tempList;
// }
//}
return null; return null;
} }
} }

View File

@ -202,14 +202,21 @@ namespace ServerCore.Event
/// <returns></returns> /// <returns></returns>
private List<Delegate> GetEventList(EEvent evt) private List<Delegate> GetEventList(EEvent evt)
{ {
if (eventDic.ContainsKey(evt)) if (eventDic.TryGetValue(evt, out List<Delegate> dlist))
{ {
List<Delegate> tempList = eventDic[evt]; if (null != dlist)
if (null != tempList)
{ {
return tempList; return dlist;
} }
} }
//if (eventDic.ContainsKey(evt))
//{
// List<Delegate> tempList = eventDic[evt];
// if (null != tempList)
// {
// return tempList;
// }
//}
return null; return null;
} }
} }