From 4667b74393ed341d8a44923cfc706af9c44b5329 Mon Sep 17 00:00:00 2001
From: sin365 <353374337@qq.com>
Date: Mon, 8 Jan 2024 16:19:09 +0800
Subject: [PATCH 1/3] byte func optimize
---
NetLib/HaoYueNet.ClientNetwork/BaseData.cs | 204 ++++++++++++++---
.../NetworkHelperCore.cs | 46 ++--
.../NetworkHelperP2PCore.cs | 23 +-
NetLib/HaoYueNet.ServerNetwork/BaseData.cs | 205 +++++++++++++++---
.../NetWork/MemoryStreamPool.cs | 41 ++++
.../NetWork/TcpSaeaServer.cs | 107 +++++++--
.../NetWork/TokenMsgPool.cs | 53 +++++
7 files changed, 583 insertions(+), 96 deletions(-)
create mode 100644 NetLib/HaoYueNet.ServerNetwork/NetWork/MemoryStreamPool.cs
diff --git a/NetLib/HaoYueNet.ClientNetwork/BaseData.cs b/NetLib/HaoYueNet.ClientNetwork/BaseData.cs
index bf2e832..af43dba 100644
--- a/NetLib/HaoYueNet.ClientNetwork/BaseData.cs
+++ b/NetLib/HaoYueNet.ClientNetwork/BaseData.cs
@@ -1,48 +1,202 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
+using System.Net.Sockets;
namespace HaoYueNet.ClientNetwork
{
public static class BaseData
{
+ ///
+ /// 心跳包数据
+ ///
+ 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)
+ {
+ myreadEventArgs.SetBuffer(HeartbeatData, 0, HeartbeatData.Length);
+ }
+ }
+
public static class HunterNet_S2C
{
- public static byte[] CreatePkgData(UInt16 CmdID, UInt16 Error, byte[] data)
+ public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs, UInt16 CmdID, UInt16 Error, byte[] AddonBytes_Data)
{
- byte[] newdata = new byte[2 + 2 + data.Length];
- BitConverter.GetBytes(CmdID).CopyTo(newdata, 0);
- BitConverter.GetBytes(Error).CopyTo(newdata, 2);
- Array.Copy(data, 0, newdata, 4, data.Length);
- return newdata;
+ myreadEventArgs.SetBuffer(CreatePkgData(CmdID, Error, AddonBytes_Data));
}
- public static void AnalysisPkgData(byte[] srcdata,out UInt16 CmdID,out UInt16 Error,out byte[] data)
+ public static byte[] CreatePkgData(UInt16 CmdID, UInt16 Error, byte[] AddonBytes_Data)
{
- data = new byte[srcdata.Length - 2 - 2];
- CmdID = BitConverter.ToUInt16(srcdata, 0);
- Error = BitConverter.ToUInt16(srcdata, 2);
- Array.Copy(srcdata, 4, data, 0, data.Length);
+ //byte[] AddonBytes_CmdID = BitConverter.GetBytes(CmdID);
+ //byte[] AddonBytes_Error = BitConverter.GetBytes(Error);
+ //int AllLenght = AddonBytes_CmdID.Length + AddonBytes_Error.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;
+
+ ////Error
+ //AddonBytes_Error.CopyTo(BufferData, LastIndex);
+ //LastIndex += AddonBytes_Error.Length;
+
+ ////DATA
+ //AddonBytes_Data.CopyTo(BufferData, LastIndex);
+ //LastIndex += AddonBytes_Data.Length;
+ //return BufferData;
+
+ ////用Buffer.BlockCopy拷贝
+ //byte[] AddonBytes_CmdID = BitConverter.GetBytes(CmdID);
+ //byte[] AddonBytes_Error = BitConverter.GetBytes(Error);
+ //int AllLenght = AddonBytes_CmdID.Length + AddonBytes_Error.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;
+
+ ////CMDID
+ //Buffer.BlockCopy(AddonBytes_CmdID, 0, BufferData, LastIndex, AddonBytes_CmdID.Length);
+ //LastIndex += AddonBytes_CmdID.Length;
+
+ ////Error
+ //Buffer.BlockCopy(AddonBytes_Error, 0, BufferData, LastIndex, AddonBytes_Error.Length);
+ //LastIndex += AddonBytes_Error.Length;
+
+ ////DATA
+ //Buffer.BlockCopy(AddonBytes_Data, 0, BufferData, LastIndex, AddonBytes_Data.Length);
+ //LastIndex += AddonBytes_Data.Length;
+
+
+
+ //用Buffer.BlockCopy拷贝
+ //包长度
+ int AllLenght = 4 + 2 + 2 + AddonBytes_Data.Length;
+ byte[] BufferData = new byte[AllLenght];
+
+ //包长度
+ writeInt(BufferData, 0, AllLenght);
+
+ //CMDID
+ writeUInt16(BufferData, 4, CmdID);
+
+ //Error
+ writeUInt16(BufferData, 4 + 2, CmdID);
+
+ //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 byte[] CreatePkgData(UInt16 CmdID, byte[] data)
+ public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs, UInt16 CmdID, byte[] AddonBytes_Data)
{
- byte[] newdata = new byte[2 + data.Length];
- BitConverter.GetBytes(CmdID).CopyTo(newdata, 0);
- Array.Copy(data, 0, newdata, 2, data.Length);
- return newdata;
+ myreadEventArgs.SetBuffer(CreatePkgData(CmdID, AddonBytes_Data));
}
- public static void AnalysisPkgData(byte[] srcdata, out UInt16 CmdID, out byte[] data)
+ public static byte[] CreatePkgData(UInt16 CmdID, byte[] AddonBytes_Data)
{
- data = new byte[srcdata.Length - 2];
- CmdID = BitConverter.ToUInt16(srcdata, 0);
- Array.Copy(srcdata, 2, data, 0, data.Length);
+ //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;
+
+ //CMDID
+ Buffer.BlockCopy(AddonBytes_CmdID, 0, BufferData, LastIndex, AddonBytes_CmdID.Length);
+ LastIndex += AddonBytes_CmdID.Length;
+
+ //DATA
+ Buffer.BlockCopy(AddonBytes_Data, 0, BufferData, LastIndex, AddonBytes_Data.Length);
+ LastIndex += 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/NetworkHelperCore.cs b/NetLib/HaoYueNet.ClientNetwork/NetworkHelperCore.cs
index 0d7a3b3..bc0c88e 100644
--- a/NetLib/HaoYueNet.ClientNetwork/NetworkHelperCore.cs
+++ b/NetLib/HaoYueNet.ClientNetwork/NetworkHelperCore.cs
@@ -9,10 +9,6 @@ namespace HaoYueNet.ClientNetwork
{
private Socket client;
- ///
- /// 心跳包数据
- ///
- static byte[] HeartbeatData = new byte[5] { 0x05, 0x00, 0x00, 0x00, 0x00 };
////响应倒计时计数最大值
//private static int MaxRevIndexNum = 6;
@@ -115,7 +111,8 @@ namespace HaoYueNet.ClientNetwork
private void SendToSocket(byte[] data)
{
- data = SendDataWithHead(data);
+ //已拼接包长度,这里不再需要拼接长度
+ //data = SendDataWithHead(data);
try
{
SendWithIndex(data);
@@ -156,25 +153,25 @@ namespace HaoYueNet.ClientNetwork
client.Send(data);
}
- //拼接头长度
- private byte[] SendDataWithHead(byte[] message)
- {
+ ////拼接头长度
+ //private byte[] SendDataWithHead(byte[] message)
+ //{
- MemoryStream memoryStream = new MemoryStream();//创建一个内存流
+ // MemoryStream memoryStream = new MemoryStream();//创建一个内存流
- byte[] BagHead = BitConverter.GetBytes(message.Length + 4);//往字节数组中写入包头(包头自身的长度和消息体的长度)的长度
+ // byte[] BagHead = BitConverter.GetBytes(message.Length + 4);//往字节数组中写入包头(包头自身的长度和消息体的长度)的长度
- memoryStream.Write(BagHead, 0, BagHead.Length);//将包头写入内存流
+ // memoryStream.Write(BagHead, 0, BagHead.Length);//将包头写入内存流
- memoryStream.Write(message, 0, message.Length);//将消息体写入内存流
+ // memoryStream.Write(message, 0, message.Length);//将消息体写入内存流
- byte[] HeadAndBody = memoryStream.ToArray();//将内存流中的数据写入字节数组
+ // byte[] HeadAndBody = memoryStream.ToArray();//将内存流中的数据写入字节数组
- memoryStream.Close();//关闭内存
- memoryStream.Dispose();//释放资源
+ // memoryStream.Close();//关闭内存
+ // memoryStream.Dispose();//释放资源
- return HeadAndBody;
- }
+ // return HeadAndBody;
+ //}
///
/// 供外部调用 发送消息
@@ -343,11 +340,18 @@ namespace HaoYueNet.ClientNetwork
//把头去掉,就可以吃了,蛋白质是牛肉的六倍
//DataCallBackReady(getData.Skip(StartIndex+4).Take(HeadLength-4).ToArray());
- //改为Array.Copy 提升效率
int CoreLenght = HeadLength - 4;
- byte[] retData = new byte[CoreLenght];
- Array.Copy(getData, StartIndex + 4, retData, 0, CoreLenght);
- DataCallBackReady(retData);
+
+ //改为Array.Copy 提升效率
+ //byte[] retData = new byte[CoreLenght];
+ //Array.Copy(getData, StartIndex + 4, retData, 0, CoreLenght);
+ //DataCallBackReady(retData);
+
+ //用Span
+ Span getData_span = getData;
+ getData_span = getData_span.Slice(StartIndex + 4,CoreLenght);
+ DataCallBackReady(getData_span.ToArray());
+
StartIndex += HeadLength;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部)
}
}
diff --git a/NetLib/HaoYueNet.ClientNetwork/NetworkHelperP2PCore.cs b/NetLib/HaoYueNet.ClientNetwork/NetworkHelperP2PCore.cs
index 60734f8..8a5984b 100644
--- a/NetLib/HaoYueNet.ClientNetwork/NetworkHelperP2PCore.cs
+++ b/NetLib/HaoYueNet.ClientNetwork/NetworkHelperP2PCore.cs
@@ -8,10 +8,6 @@ namespace HaoYueNet.ClientNetwork
{
private Socket client;
- ///
- /// 心跳包数据
- ///
- private byte[] HeartbeatData = new byte[5] { 0x05, 0x00, 0x00, 0x00, 0x00 };
////响应倒计时计数最大值
//private static int MaxRevIndexNum = 6;
@@ -94,7 +90,8 @@ namespace HaoYueNet.ClientNetwork
private void SendToSocket(byte[] data)
{
- data = SendDataWithHead(data);
+ //已拼接包长度,这里不再需要拼接长度
+ //data = SendDataWithHead(data);
try
{
SendWithIndex(data);
@@ -112,7 +109,7 @@ namespace HaoYueNet.ClientNetwork
{
try
{
- SendWithIndex(HeartbeatData);
+ SendWithIndex(BaseData.HeartbeatData);
}
catch (Exception ex)
{
@@ -324,11 +321,17 @@ namespace HaoYueNet.ClientNetwork
//把头去掉,就可以吃了,蛋白质是牛肉的六倍
//DataCallBackReady(getData.Skip(StartIndex+4).Take(HeadLength-4).ToArray());
- //改为Array.Copy 提升效率
int CoreLenght = HeadLength - 4;
- byte[] retData = new byte[CoreLenght];
- Array.Copy(getData, StartIndex + 4, retData, 0, CoreLenght);
- DataCallBackReady(retData);
+ //改为Array.Copy 提升效率
+ //byte[] retData = new byte[CoreLenght];
+ //Array.Copy(getData, StartIndex + 4, retData, 0, CoreLenght);
+ //DataCallBackReady(retData);
+
+ //用Span
+ Span getData_span = getData;
+ getData_span = getData_span.Slice(StartIndex + 4, CoreLenght);
+ DataCallBackReady(getData_span.ToArray());
+
StartIndex += HeadLength;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部)
}
}
diff --git a/NetLib/HaoYueNet.ServerNetwork/BaseData.cs b/NetLib/HaoYueNet.ServerNetwork/BaseData.cs
index a7149d8..643e534 100644
--- a/NetLib/HaoYueNet.ServerNetwork/BaseData.cs
+++ b/NetLib/HaoYueNet.ServerNetwork/BaseData.cs
@@ -1,48 +1,203 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
+using System.Net.Sockets;
+using static System.Runtime.InteropServices.JavaScript.JSType;
namespace HaoYueNet.ServerNetwork
{
public static class BaseData
{
+ ///
+ /// 心跳包数据
+ ///
+ 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)
+ {
+ myreadEventArgs.SetBuffer(HeartbeatData, 0, HeartbeatData.Length);
+ }
+ }
+
public static class HunterNet_S2C
{
- public static byte[] CreatePkgData(UInt16 CmdID, UInt16 Error, byte[] data)
+ public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs, UInt16 CmdID, UInt16 Error, byte[] AddonBytes_Data)
{
- byte[] newdata = new byte[2 + 2 + data.Length];
- BitConverter.GetBytes(CmdID).CopyTo(newdata, 0);
- BitConverter.GetBytes(Error).CopyTo(newdata, 2);
- Array.Copy(data, 0, newdata, 4, data.Length);
- return newdata;
+ myreadEventArgs.SetBuffer(CreatePkgData(CmdID, Error, AddonBytes_Data));
}
- public static void AnalysisPkgData(byte[] srcdata, out UInt16 CmdID, out UInt16 Error, out byte[] data)
+ public static byte[] CreatePkgData(UInt16 CmdID, UInt16 Error, byte[] AddonBytes_Data)
{
- data = new byte[srcdata.Length - 2 - 2];
- CmdID = BitConverter.ToUInt16(srcdata, 0);
- Error = BitConverter.ToUInt16(srcdata, 2);
- Array.Copy(srcdata, 4, data, 0, data.Length);
+ //byte[] AddonBytes_CmdID = BitConverter.GetBytes(CmdID);
+ //byte[] AddonBytes_Error = BitConverter.GetBytes(Error);
+ //int AllLenght = AddonBytes_CmdID.Length + AddonBytes_Error.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;
+
+ ////Error
+ //AddonBytes_Error.CopyTo(BufferData, LastIndex);
+ //LastIndex += AddonBytes_Error.Length;
+
+ ////DATA
+ //AddonBytes_Data.CopyTo(BufferData, LastIndex);
+ //LastIndex += AddonBytes_Data.Length;
+ //return BufferData;
+
+ ////用Buffer.BlockCopy拷贝
+ //byte[] AddonBytes_CmdID = BitConverter.GetBytes(CmdID);
+ //byte[] AddonBytes_Error = BitConverter.GetBytes(Error);
+ //int AllLenght = AddonBytes_CmdID.Length + AddonBytes_Error.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;
+
+ ////CMDID
+ //Buffer.BlockCopy(AddonBytes_CmdID, 0, BufferData, LastIndex, AddonBytes_CmdID.Length);
+ //LastIndex += AddonBytes_CmdID.Length;
+
+ ////Error
+ //Buffer.BlockCopy(AddonBytes_Error, 0, BufferData, LastIndex, AddonBytes_Error.Length);
+ //LastIndex += AddonBytes_Error.Length;
+
+ ////DATA
+ //Buffer.BlockCopy(AddonBytes_Data, 0, BufferData, LastIndex, AddonBytes_Data.Length);
+ //LastIndex += AddonBytes_Data.Length;
+
+
+
+ //用Buffer.BlockCopy拷贝
+ //包长度
+ int AllLenght = 4 + 2 + 2 + AddonBytes_Data.Length;
+ byte[] BufferData = new byte[AllLenght];
+
+ //包长度
+ writeInt(BufferData, 0, AllLenght);
+
+ //CMDID
+ writeUInt16(BufferData, 4, CmdID);
+
+ //Error
+ writeUInt16(BufferData, 4 + 2, CmdID);
+
+ //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 byte[] CreatePkgData(UInt16 CmdID, byte[] data)
+ public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs, UInt16 CmdID, byte[] AddonBytes_Data)
{
- byte[] newdata = new byte[2 + data.Length];
- BitConverter.GetBytes(CmdID).CopyTo(newdata, 0);
- Array.Copy(data, 0, newdata, 2, data.Length);
- return newdata;
+ myreadEventArgs.SetBuffer(CreatePkgData(CmdID, AddonBytes_Data));
}
- public static void AnalysisPkgData(byte[] srcdata, out UInt16 CmdID, out byte[] data)
+ public static byte[] CreatePkgData(UInt16 CmdID, byte[] AddonBytes_Data)
{
- data = new byte[srcdata.Length - 2];
- CmdID = BitConverter.ToUInt16(srcdata, 0);
- Array.Copy(srcdata, 2, data, 0, data.Length);
+ //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;
+
+ //CMDID
+ Buffer.BlockCopy(AddonBytes_CmdID, 0, BufferData, LastIndex, AddonBytes_CmdID.Length);
+ LastIndex += AddonBytes_CmdID.Length;
+
+ //DATA
+ Buffer.BlockCopy(AddonBytes_Data, 0, BufferData, LastIndex, AddonBytes_Data.Length);
+ LastIndex += 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.ServerNetwork/NetWork/MemoryStreamPool.cs b/NetLib/HaoYueNet.ServerNetwork/NetWork/MemoryStreamPool.cs
new file mode 100644
index 0000000..ef01b0e
--- /dev/null
+++ b/NetLib/HaoYueNet.ServerNetwork/NetWork/MemoryStreamPool.cs
@@ -0,0 +1,41 @@
+using System.Net.Sockets;
+
+namespace HaoYueNet.ServerNetwork
+{
+
+ public class MemoryStreamPool
+ {
+ Stack m_pool;
+
+ public MemoryStreamPool(int capacity)
+ {
+ m_pool = new Stack(capacity);
+ }
+
+ public void Push(MemoryStream item)
+ {
+ if (item == null) { throw new ArgumentNullException("Items added to a MemoryStream cannot be null"); }
+ lock (m_pool)
+ {
+ m_pool.Push(item);
+ }
+ }
+ public MemoryStream Pop()
+ {
+ lock (m_pool)
+ {
+ return m_pool.Pop();
+ }
+ }
+
+ public int Count
+ {
+ get { return m_pool.Count; }
+ }
+
+ public void Clear()
+ {
+ m_pool.Clear();
+ }
+ }
+}
diff --git a/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs b/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs
index f003a96..2c100d2 100644
--- a/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs
+++ b/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs
@@ -7,10 +7,6 @@ namespace HaoYueNet.ServerNetwork
{
public class TcpSaeaServer
{
- ///
- /// 心跳包数据
- ///
- static byte[] HeartbeatData = new byte[5] { 0x05, 0x00, 0x00, 0x00, 0x00 };
//响应倒计时计数最大值
//public int MaxRevIndexNum { get; set; } = 5;
////发送倒计时计数最大值
@@ -523,7 +519,15 @@ namespace HaoYueNet.ServerNetwork
{
TokenWithMsg msg = msg_pool.Dequeue();
//OutNetLog("从信息池取出发送");
- SendMessage(msg.token, msg.message);
+ //是心跳包
+ if (msg.bHeartbeat)
+ {
+ SendHeartbeatMessage(msg.token);
+ }
+ else
+ {
+ SendMessage(msg.token,msg.CMDID,msg.Error,msg.data);
+ }
msg = null;
}
catch
@@ -540,7 +544,7 @@ namespace HaoYueNet.ServerNetwork
}
}
-
+ /*
public void SendMessage(AsyncUserToken token, byte[] message,bool dontNeedHead = false)
{
if (token == null || token.Socket == null || !token.Socket.Connected)
@@ -579,8 +583,80 @@ namespace HaoYueNet.ServerNetwork
OutNetLog(e.ToString());
}
}
+ */
- //拼接长度
+ public void SendMessage(AsyncUserToken token,UInt16 CmdID, UInt16 Error, byte[] data)
+ {
+ if (token == null || token.Socket == null || !token.Socket.Connected)
+ return;
+ try
+ {
+ if (m_Sendpool.Count > 0)
+ {
+ SocketAsyncEventArgs myreadEventArgs = m_Sendpool.Pop();
+ myreadEventArgs.UserToken = token;
+ myreadEventArgs.AcceptSocket = token.Socket;
+ //myreadEventArgs.SetBuffer(message, 0, message.Length); //将数据放置进去.
+ //更换为CMDID和Data直接写入SocketAsyncEventArgs的Buff
+ HunterNet_S2C.SetDataToSocketAsyncEventArgs(myreadEventArgs, CmdID, Error, data);
+
+ //若不需要等待
+ if (!token.Socket.SendAsync(myreadEventArgs))
+ {
+ m_Sendpool.Push(myreadEventArgs);
+ }
+ return;
+ }
+ else
+ {
+ //先压入队列,等待m_Sendpool回收
+ msg_pool.Enqueue(new TokenWithMsg() { token = token, CMDID = CmdID, Error = Error, data = data });
+ //OutNetLog("!!!!压入消息发送队列MSG_Pool");
+ return;
+ }
+ }
+ catch (Exception e)
+ {
+ OutNetLog(e.ToString());
+ }
+ }
+
+ public void SendHeartbeatMessage(AsyncUserToken token)
+ {
+ if (token == null || token.Socket == null || !token.Socket.Connected)
+ return;
+ try
+ {
+ if (m_Sendpool.Count > 0)
+ {
+ SocketAsyncEventArgs myreadEventArgs = m_Sendpool.Pop();
+ myreadEventArgs.UserToken = token;
+ myreadEventArgs.AcceptSocket = token.Socket;
+ //直接写入SocketAsyncEventArgs的Buff
+ HunterNet_Heartbeat.SetDataToSocketAsyncEventArgs(myreadEventArgs);
+
+ //若不需要等待
+ if (!token.Socket.SendAsync(myreadEventArgs))
+ {
+ m_Sendpool.Push(myreadEventArgs);
+ }
+ return;
+ }
+ else
+ {
+ //先压入队列,等待m_Sendpool回收
+ msg_pool.Enqueue(new TokenWithMsg() { token = token, bHeartbeat = true });
+ //OutNetLog("!!!!压入消息发送队列MSG_Pool");
+ return;
+ }
+ }
+ catch (Exception e)
+ {
+ OutNetLog(e.ToString());
+ }
+ }
+
+ //拼接头部长度
private static byte[] SendDataWithHead(byte[] message)
{
@@ -589,7 +665,6 @@ namespace HaoYueNet.ServerNetwork
byte[] BagHead = BitConverter.GetBytes(message.Length + 4);//往字节数组中写入包头(包头自身的长度和消息体的长度)的长度
memoryStream.Write(BagHead, 0, BagHead.Length);//将包头写入内存流
-
memoryStream.Write(message, 0, message.Length);//将消息体写入内存流
byte[] HeadAndBody = memoryStream.ToArray();//将内存流中的数据写入字节数组
@@ -623,12 +698,12 @@ namespace HaoYueNet.ServerNetwork
/// 发送数据并计数
///
///
- private void SendWithIndex(AsyncUserToken token, byte[] data)
+ private void SendWithIndex(AsyncUserToken token, UInt16 CmdID, UInt16 ERRCODE, byte[] data)
{
try
{
//发送数据
- SendMessage(token, data);
+ SendMessage(token, CmdID, ERRCODE, data);
token.SendIndex = MaxSendIndexNum;
}
catch
@@ -650,8 +725,10 @@ namespace HaoYueNet.ServerNetwork
_s2cdata.HunterNetCoreData = ByteString.CopyFrom(data);
_s2cdata.HunterNetCoreERRORCode = ERRCODE;
byte[] _finaldata = Serizlize(_s2cdata);*/
- byte[] _finaldata = HunterNet_S2C.CreatePkgData((ushort)CMDID, (ushort)ERRCODE, data);
- SendWithIndex(token, _finaldata);
+
+ //byte[] _finaldata = HunterNet_S2C.CreatePkgData((ushort)CMDID, (ushort)ERRCODE, data);
+
+ SendWithIndex(token, (ushort)CMDID, (ushort)ERRCODE, data);
}
private void DataCallBackReady(AsyncUserToken sk, byte[] data)
@@ -697,7 +774,7 @@ namespace HaoYueNet.ServerNetwork
///
///
///
- private void SendHeartbeat(AsyncUserToken token)
+ private void SendHeartbeatWithIndex(AsyncUserToken token)
{
if (token == null || token.Socket == null || !token.Socket.Connected)
return;
@@ -705,7 +782,7 @@ namespace HaoYueNet.ServerNetwork
{
//OutNetLog(DateTime.Now.ToString() + "发送心跳包");
token.SendIndex = MaxSendIndexNum;
- SendMessage(token, HeartbeatData, true);
+ SendHeartbeatMessage(token);
}
catch (Exception e)
{
@@ -736,7 +813,7 @@ namespace HaoYueNet.ServerNetwork
{
//重置倒计时计数
m_clients[i].SendIndex = MaxSendIndexNum;
- SendHeartbeat(m_clients[i]);
+ SendHeartbeatWithIndex(m_clients[i]);
}
}
}
diff --git a/NetLib/HaoYueNet.ServerNetwork/NetWork/TokenMsgPool.cs b/NetLib/HaoYueNet.ServerNetwork/NetWork/TokenMsgPool.cs
index 5807533..9eac9c4 100644
--- a/NetLib/HaoYueNet.ServerNetwork/NetWork/TokenMsgPool.cs
+++ b/NetLib/HaoYueNet.ServerNetwork/NetWork/TokenMsgPool.cs
@@ -6,6 +6,58 @@ using System.Text;
namespace HaoYueNet.ServerNetwork
{
+ public class TokenWithMsg
+ {
+ public AsyncUserToken token;
+ public UInt16 CMDID;
+ public UInt16 Error;
+ public byte[] data;
+ public bool bHeartbeat;
+ }
+
+ public class TokenMsgPool
+ {
+ //Stack msg_pool;
+ Queue msg_pool;
+
+ public TokenMsgPool(int capacity)
+ {
+ msg_pool = new Queue(capacity);
+ }
+
+ ///
+ /// 向 Queue 的末尾添加一个对象。
+ ///
+ ///
+ public void Enqueue(TokenWithMsg item)
+ {
+ lock (msg_pool)
+ {
+ msg_pool.Enqueue(item);
+ }
+ }
+
+ //移除并返回在 Queue 的开头的对象。
+ public TokenWithMsg Dequeue()
+ {
+ lock (msg_pool)
+ {
+ return msg_pool.Dequeue();
+ }
+ }
+
+ public int Count
+ {
+ get { return msg_pool.Count; }
+ }
+
+ public void Clear()
+ {
+ msg_pool.Clear();
+ }
+ }
+
+ /*
public class TokenWithMsg
{
public AsyncUserToken token;
@@ -74,4 +126,5 @@ namespace HaoYueNet.ServerNetwork
msg_pool.Clear();
}
}
+ */
}
--
2.36.0.windows.1
From 610b8169222d14ad4c52f23421d6eb3344a06182 Mon Sep 17 00:00:00 2001
From: sin365 <353374337@qq.com>
Date: Mon, 8 Jan 2024 16:48:09 +0800
Subject: [PATCH 2/3] byte func optimize
---
.../NetworkHelperP2PCore.cs | 31 +-
.../HaoYueNet.ClientNetworkNet4x/BaseData.cs | 201 +++++-
.../NetworkHelperCore.cs | 53 +-
.../NetworkHelperP2PCore.cs | 631 +++++++++---------
.../packages.config | 6 +
5 files changed, 536 insertions(+), 386 deletions(-)
create mode 100644 NetLib/HaoYueNet.ClientNetworkNet4x/packages.config
diff --git a/NetLib/HaoYueNet.ClientNetwork/NetworkHelperP2PCore.cs b/NetLib/HaoYueNet.ClientNetwork/NetworkHelperP2PCore.cs
index 8a5984b..de4e0ca 100644
--- a/NetLib/HaoYueNet.ClientNetwork/NetworkHelperP2PCore.cs
+++ b/NetLib/HaoYueNet.ClientNetwork/NetworkHelperP2PCore.cs
@@ -8,13 +8,6 @@ namespace HaoYueNet.ClientNetwork
{
private Socket client;
-
- ////响应倒计时计数最大值
- //private static int MaxRevIndexNum = 6;
-
- ////发送倒计时计数最大值
- //private static int MaxSendIndexNum = 3;
-
//响应倒计时计数最大值
private static int MaxRevIndexNum = 50;
@@ -132,25 +125,25 @@ namespace HaoYueNet.ClientNetwork
client.Send(data);
}
- //拼接头长度
- private byte[] SendDataWithHead(byte[] message)
- {
+ ////拼接头长度
+ //private byte[] SendDataWithHead(byte[] message)
+ //{
- MemoryStream memoryStream = new MemoryStream();//创建一个内存流
+ // MemoryStream memoryStream = new MemoryStream();//创建一个内存流
- byte[] BagHead = BitConverter.GetBytes(message.Length + 4);//往字节数组中写入包头(包头自身的长度和消息体的长度)的长度
+ // byte[] BagHead = BitConverter.GetBytes(message.Length + 4);//往字节数组中写入包头(包头自身的长度和消息体的长度)的长度
- memoryStream.Write(BagHead, 0, BagHead.Length);//将包头写入内存流
+ // memoryStream.Write(BagHead, 0, BagHead.Length);//将包头写入内存流
- memoryStream.Write(message, 0, message.Length);//将消息体写入内存流
+ // memoryStream.Write(message, 0, message.Length);//将消息体写入内存流
- byte[] HeadAndBody = memoryStream.ToArray();//将内存流中的数据写入字节数组
+ // byte[] HeadAndBody = memoryStream.ToArray();//将内存流中的数据写入字节数组
- memoryStream.Close();//关闭内存
- memoryStream.Dispose();//释放资源
+ // memoryStream.Close();//关闭内存
+ // memoryStream.Dispose();//释放资源
- return HeadAndBody;
- }
+ // return HeadAndBody;
+ //}
///
/// 供外部调用 发送消息
diff --git a/NetLib/HaoYueNet.ClientNetworkNet4x/BaseData.cs b/NetLib/HaoYueNet.ClientNetworkNet4x/BaseData.cs
index dc30902..dfb4fef 100644
--- a/NetLib/HaoYueNet.ClientNetworkNet4x/BaseData.cs
+++ b/NetLib/HaoYueNet.ClientNetworkNet4x/BaseData.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
@@ -8,41 +9,201 @@ namespace HaoYueNet.ClientNetworkNet4x
{
public static class BaseData
{
+ ///
+ /// 心跳包数据
+ ///
+ 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)
+ {
+ myreadEventArgs.SetBuffer(HeartbeatData, 0, HeartbeatData.Length);
+ }
+ }
+
public static class HunterNet_S2C
{
- public static byte[] CreatePkgData(UInt16 CmdID, UInt16 Error, byte[] data)
+ public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs, UInt16 CmdID, UInt16 Error, byte[] AddonBytes_Data)
{
- byte[] newdata = new byte[2 + 2 + data.Length];
- BitConverter.GetBytes(CmdID).CopyTo(newdata, 0);
- BitConverter.GetBytes(Error).CopyTo(newdata, 2);
- Array.Copy(data, 0, newdata, 4, data.Length);
- return newdata;
+ byte[] data = CreatePkgData(CmdID, Error, AddonBytes_Data);
+ myreadEventArgs.SetBuffer(data,0, data.Length);
}
- public static void AnalysisPkgData(byte[] srcdata,out UInt16 CmdID,out UInt16 Error,out byte[] data)
+ public static byte[] CreatePkgData(UInt16 CmdID, UInt16 Error, byte[] AddonBytes_Data)
{
- data = new byte[srcdata.Length - 2 - 2];
- CmdID = BitConverter.ToUInt16(srcdata, 0);
- Error = BitConverter.ToUInt16(srcdata, 2);
- Array.Copy(srcdata, 4, data, 0, data.Length);
+ //byte[] AddonBytes_CmdID = BitConverter.GetBytes(CmdID);
+ //byte[] AddonBytes_Error = BitConverter.GetBytes(Error);
+ //int AllLenght = AddonBytes_CmdID.Length + AddonBytes_Error.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;
+
+ ////Error
+ //AddonBytes_Error.CopyTo(BufferData, LastIndex);
+ //LastIndex += AddonBytes_Error.Length;
+
+ ////DATA
+ //AddonBytes_Data.CopyTo(BufferData, LastIndex);
+ //LastIndex += AddonBytes_Data.Length;
+ //return BufferData;
+
+ ////用Buffer.BlockCopy拷贝
+ //byte[] AddonBytes_CmdID = BitConverter.GetBytes(CmdID);
+ //byte[] AddonBytes_Error = BitConverter.GetBytes(Error);
+ //int AllLenght = AddonBytes_CmdID.Length + AddonBytes_Error.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;
+
+ ////CMDID
+ //Buffer.BlockCopy(AddonBytes_CmdID, 0, BufferData, LastIndex, AddonBytes_CmdID.Length);
+ //LastIndex += AddonBytes_CmdID.Length;
+
+ ////Error
+ //Buffer.BlockCopy(AddonBytes_Error, 0, BufferData, LastIndex, AddonBytes_Error.Length);
+ //LastIndex += AddonBytes_Error.Length;
+
+ ////DATA
+ //Buffer.BlockCopy(AddonBytes_Data, 0, BufferData, LastIndex, AddonBytes_Data.Length);
+ //LastIndex += AddonBytes_Data.Length;
+
+
+
+ //用Buffer.BlockCopy拷贝
+ //包长度
+ int AllLenght = 4 + 2 + 2 + AddonBytes_Data.Length;
+ byte[] BufferData = new byte[AllLenght];
+
+ //包长度
+ writeInt(BufferData, 0, AllLenght);
+
+ //CMDID
+ writeUInt16(BufferData, 4, CmdID);
+
+ //Error
+ writeUInt16(BufferData, 4 + 2, CmdID);
+
+ //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).ToArray(), 0);
+ Error = BitConverter.ToUInt16(srcdata.Slice(2, 2).ToArray(), 2);
+ data = srcdata.Slice(2 + 2).ToArray();
}
}
public static class HunterNet_C2S
{
- public static byte[] CreatePkgData(UInt16 CmdID, byte[] data)
+ public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs, UInt16 CmdID, byte[] AddonBytes_Data)
{
- byte[] newdata = new byte[2 + data.Length];
- BitConverter.GetBytes(CmdID).CopyTo(newdata, 0);
- Array.Copy(data, 0, newdata, 2, data.Length);
- return newdata;
+ byte[] data = CreatePkgData(CmdID, AddonBytes_Data);
+ myreadEventArgs.SetBuffer(data, 0, data.Length);
}
- public static void AnalysisPkgData(byte[] srcdata, out UInt16 CmdID, out byte[] data)
+ public static byte[] CreatePkgData(UInt16 CmdID, byte[] AddonBytes_Data)
{
- data = new byte[srcdata.Length - 2];
- CmdID = BitConverter.ToUInt16(srcdata, 0);
- Array.Copy(srcdata, 2, data, 0, data.Length);
+ //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;
+
+ //CMDID
+ Buffer.BlockCopy(AddonBytes_CmdID, 0, BufferData, LastIndex, AddonBytes_CmdID.Length);
+ LastIndex += AddonBytes_CmdID.Length;
+
+ //DATA
+ Buffer.BlockCopy(AddonBytes_Data, 0, BufferData, LastIndex, AddonBytes_Data.Length);
+ LastIndex += 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).ToArray(),0);
+ data = srcdata.Slice(2).ToArray();
}
}
}
diff --git a/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperCore.cs b/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperCore.cs
index 1831bf8..0f55a4f 100644
--- a/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperCore.cs
+++ b/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperCore.cs
@@ -11,17 +11,6 @@ namespace HaoYueNet.ClientNetworkNet4x
{
private Socket client;
- ///
- /// 心跳包数据
- ///
- static byte[] HeartbeatData = new byte[5] { 0x05, 0x00, 0x00, 0x00, 0x00 };
-
- ////响应倒计时计数最大值
- //private static int MaxRevIndexNum = 6;
-
- ////发送倒计时计数最大值
- //private static int MaxSendIndexNum = 3;
-
//响应倒计时计数最大值
private static int MaxRevIndexNum = 50;
@@ -117,7 +106,8 @@ namespace HaoYueNet.ClientNetworkNet4x
private void SendToSocket(byte[] data)
{
- data = SendDataWithHead(data);
+ //已拼接包长度,这里不再需要拼接长度
+ //data = SendDataWithHead(data);
try
{
SendWithIndex(data);
@@ -158,25 +148,25 @@ namespace HaoYueNet.ClientNetworkNet4x
client.Send(data);
}
- //拼接头长度
- private byte[] SendDataWithHead(byte[] message)
- {
+ ////拼接头长度
+ //private byte[] SendDataWithHead(byte[] message)
+ //{
- MemoryStream memoryStream = new MemoryStream();//创建一个内存流
+ // MemoryStream memoryStream = new MemoryStream();//创建一个内存流
- byte[] BagHead = BitConverter.GetBytes(message.Length + 4);//往字节数组中写入包头(包头自身的长度和消息体的长度)的长度
+ // byte[] BagHead = BitConverter.GetBytes(message.Length + 4);//往字节数组中写入包头(包头自身的长度和消息体的长度)的长度
- memoryStream.Write(BagHead, 0, BagHead.Length);//将包头写入内存流
+ // memoryStream.Write(BagHead, 0, BagHead.Length);//将包头写入内存流
- memoryStream.Write(message, 0, message.Length);//将消息体写入内存流
+ // memoryStream.Write(message, 0, message.Length);//将消息体写入内存流
- byte[] HeadAndBody = memoryStream.ToArray();//将内存流中的数据写入字节数组
+ // byte[] HeadAndBody = memoryStream.ToArray();//将内存流中的数据写入字节数组
- memoryStream.Close();//关闭内存
- memoryStream.Dispose();//释放资源
+ // memoryStream.Close();//关闭内存
+ // memoryStream.Dispose();//释放资源
- return HeadAndBody;
- }
+ // return HeadAndBody;
+ //}
///
/// 供外部调用 发送消息
@@ -345,11 +335,18 @@ namespace HaoYueNet.ClientNetworkNet4x
//把头去掉,就可以吃了,蛋白质是牛肉的六倍
//DataCallBackReady(getData.Skip(StartIndex+4).Take(HeadLength-4).ToArray());
- //改为Array.Copy 提升效率
int CoreLenght = HeadLength - 4;
- byte[] retData = new byte[CoreLenght];
- Array.Copy(getData, StartIndex + 4, retData, 0, CoreLenght);
- DataCallBackReady(retData);
+
+ //改为Array.Copy 提升效率
+ //byte[] retData = new byte[CoreLenght];
+ //Array.Copy(getData, StartIndex + 4, retData, 0, CoreLenght);
+ //DataCallBackReady(retData);
+
+ //用Span
+ Span getData_span = getData;
+ getData_span = getData_span.Slice(StartIndex + 4, CoreLenght);
+ DataCallBackReady(getData_span.ToArray());
+
StartIndex += HeadLength;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部)
}
}
diff --git a/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperP2PCore.cs b/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperP2PCore.cs
index d58c7bd..237b2bc 100644
--- a/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperP2PCore.cs
+++ b/NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperP2PCore.cs
@@ -7,372 +7,365 @@ using static HaoYueNet.ClientNetworkNet4x.BaseData;
namespace HaoYueNet.ClientNetworkNet4x
{
- namespace HaoYueNet.ClientNetwork
+ public class NetworkHelperP2PCore
{
- public class NetworkHelperP2PCore
+ private Socket client;
+
+ //响应倒计时计数最大值
+ private static int MaxRevIndexNum = 50;
+
+ //发送倒计时计数最大值
+ private static int MaxSendIndexNum = 3;
+
+ //响应倒计时计数
+ private static int RevIndex = 0;
+ //发送倒计时计数
+ private static int SendIndex = 0;
+
+ //计时器间隔
+ private static int TimerInterval = 3000;
+
+ private System.Timers.Timer _heartTimer;
+
+ public void Init(string IP, int port, bool bBindReuseAddress = false, int bBindport = 0)
{
- private Socket client;
- ///
- /// 心跳包数据
- ///
- private byte[] HeartbeatData = new byte[5] { 0x05, 0x00, 0x00, 0x00, 0x00 };
+ LogOut("==>初始化网络核心");
- ////响应倒计时计数最大值
- //private static int MaxRevIndexNum = 6;
+ RevIndex = MaxRevIndexNum;
+ SendIndex = MaxSendIndexNum;
- ////发送倒计时计数最大值
- //private static int MaxSendIndexNum = 3;
-
- //响应倒计时计数最大值
- private static int MaxRevIndexNum = 50;
-
- //发送倒计时计数最大值
- private static int MaxSendIndexNum = 3;
-
- //响应倒计时计数
- private static int RevIndex = 0;
- //发送倒计时计数
- private static int SendIndex = 0;
-
- //计时器间隔
- private static int TimerInterval = 3000;
-
- private System.Timers.Timer _heartTimer;
-
- public void Init(string IP, int port, bool bBindReuseAddress = false, int bBindport = 0)
+ client = new Socket(SocketType.Stream, ProtocolType.Tcp);
+ if (bBindReuseAddress)
{
+ client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
+ IPEndPoint ipe = new IPEndPoint(IPAddress.Any, Convert.ToInt32(bBindport));
+ client.Bind(ipe);
+ }
+ Connect(IP, port);
+ }
- LogOut("==>初始化网络核心");
+ public bool Connect(string IP, int port)
+ {
+ //带回调的
+ try
+ {
+ LogOut("连接到远程IP " + IP + ":" + port);
+ client.Connect(IP, port);
+ Thread thread = new Thread(Recive);
+ thread.IsBackground = true;
+ thread.Start(client);
+ int localport = ((IPEndPoint)client.LocalEndPoint).Port;
+ LogOut($"连接成功!连接到远程IP->{IP}:{port} | 本地端口->{localport}");
- RevIndex = MaxRevIndexNum;
- SendIndex = MaxSendIndexNum;
-
- client = new Socket(SocketType.Stream, ProtocolType.Tcp);
- if (bBindReuseAddress)
+ if (_heartTimer == null)
{
- client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
- IPEndPoint ipe = new IPEndPoint(IPAddress.Any, Convert.ToInt32(bBindport));
- client.Bind(ipe);
+ _heartTimer = new System.Timers.Timer();
}
- Connect(IP, port);
+ _heartTimer.Interval = TimerInterval;
+ _heartTimer.Elapsed += CheckUpdatetimer_Elapsed;
+ _heartTimer.AutoReset = true;
+ _heartTimer.Enabled = true;
+ LogOut("开启心跳包检测");
+
+ OnConnected(true);
+ return true;
}
-
- public bool Connect(string IP, int port)
+ catch (Exception ex)
{
- //带回调的
- try
- {
- LogOut("连接到远程IP " + IP + ":" + port);
- client.Connect(IP, port);
- Thread thread = new Thread(Recive);
- thread.IsBackground = true;
- thread.Start(client);
- int localport = ((IPEndPoint)client.LocalEndPoint).Port;
- LogOut($"连接成功!连接到远程IP->{IP}:{port} | 本地端口->{localport}");
-
- if (_heartTimer == null)
- {
- _heartTimer = new System.Timers.Timer();
- }
- _heartTimer.Interval = TimerInterval;
- _heartTimer.Elapsed += CheckUpdatetimer_Elapsed;
- _heartTimer.AutoReset = true;
- _heartTimer.Enabled = true;
- LogOut("开启心跳包检测");
-
- OnConnected(true);
- return true;
- }
- catch (Exception ex)
- {
- LogOut("连接失败:" + ex.ToString());
- OnConnected(false);
- return false;
- }
+ LogOut("连接失败:" + ex.ToString());
+ OnConnected(false);
+ return false;
}
+ }
- ~NetworkHelperP2PCore()
+ ~NetworkHelperP2PCore()
+ {
+ client.Close();
+ }
+
+ private void SendToSocket(byte[] data)
+ {
+ //已拼接包长度,这里不再需要拼接长度
+ //data = SendDataWithHead(data);
+ try
{
- client.Close();
+ SendWithIndex(data);
}
-
- private void SendToSocket(byte[] data)
- {
- data = SendDataWithHead(data);
- try
- {
- SendWithIndex(data);
- }
- catch (Exception ex)
- {
- //连接断开
- OnCloseReady();
- return;
- }
- //LogOut("发送消息,消息长度=> "+data.Length);
- }
-
- private void SendHeartbeat()
- {
- try
- {
- SendWithIndex(HeartbeatData);
- }
- catch (Exception ex)
- {
- //连接断开
- OnCloseReady();
- return;
- }
- //LogOut("发送心跳包");
- }
-
- ///
- /// 发送数据并计数
- ///
- ///
- private void SendWithIndex(byte[] data)
- {
- //增加发送计数
- SendIndex = MaxSendIndexNum;
- //发送数据
- 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;
- }
-
- ///
- /// 供外部调用 发送消息
- ///
- ///
- ///
- ///
- 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);
- }
-
- public delegate void OnDataCallBack_Data(int CMDID, int ERRCODE, byte[] data);
-
- public event OnDataCallBack_Data OnDataCallBack;
-
- public delegate void delegate_NoData();
-
- public delegate void delegate_Bool(bool IsConnected);
-
- public event delegate_NoData OnClose;
-
- public event delegate_Bool OnConnected;
-
- public delegate void delegate_str(string Msg);
-
- ///
- /// 网络库调试日志输出
- ///
- public event delegate_str OnLogOut;
-
- /////
- ///// 用于调用者回调的虚函数
- /////
- /////
- //public virtual void DataCallBack(int CMDID,int ERRCODE,byte[] data)
- //{
-
- //}
-
- /////
- ///// 断开连接
- /////
- /////
- //public virtual void OnClose()
- //{
-
- //}
-
- ///
- /// 做好处理的连接管理
- ///
- private void OnCloseReady()
- {
- LogOut("关闭心跳包计数");
- _heartTimer.Enabled = false;
- _heartTimer.Elapsed -= CheckUpdatetimer_Elapsed;
- LogOut("关闭连接");
- //关闭Socket连接
- client.Close();
- OnClose();
- }
-
-
- ///
- /// 主动关闭连接
- ///
- public void CloseConntect()
+ catch (Exception ex)
{
+ //连接断开
OnCloseReady();
+ return;
+ }
+ //LogOut("发送消息,消息长度=> "+data.Length);
+ }
+
+ private void SendHeartbeat()
+ {
+ try
+ {
+ SendWithIndex(BaseData.HeartbeatData);
+ }
+ catch (Exception ex)
+ {
+ //连接断开
+ OnCloseReady();
+ return;
+ }
+ //LogOut("发送心跳包");
+ }
+
+ ///
+ /// 发送数据并计数
+ ///
+ ///
+ private void SendWithIndex(byte[] data)
+ {
+ //增加发送计数
+ SendIndex = MaxSendIndexNum;
+ //发送数据
+ 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;
+ }
+
+ ///
+ /// 供外部调用 发送消息
+ ///
+ ///
+ ///
+ ///
+ 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);
+ }
+
+ public delegate void OnDataCallBack_Data(int CMDID, int ERRCODE, byte[] data);
+
+ public event OnDataCallBack_Data OnDataCallBack;
+
+ public delegate void delegate_NoData();
+
+ public delegate void delegate_Bool(bool IsConnected);
+
+ public event delegate_NoData OnClose;
+
+ public event delegate_Bool OnConnected;
+
+ public delegate void delegate_str(string Msg);
+
+ ///
+ /// 网络库调试日志输出
+ ///
+ public event delegate_str OnLogOut;
+
+ /////
+ ///// 用于调用者回调的虚函数
+ /////
+ /////
+ //public virtual void DataCallBack(int CMDID,int ERRCODE,byte[] data)
+ //{
+
+ //}
+
+ /////
+ ///// 断开连接
+ /////
+ /////
+ //public virtual void OnClose()
+ //{
+
+ //}
+
+ ///
+ /// 做好处理的连接管理
+ ///
+ private void OnCloseReady()
+ {
+ LogOut("关闭心跳包计数");
+ _heartTimer.Enabled = false;
+ _heartTimer.Elapsed -= CheckUpdatetimer_Elapsed;
+ LogOut("关闭连接");
+ //关闭Socket连接
+ client.Close();
+ OnClose();
+ }
+
+
+ ///
+ /// 主动关闭连接
+ ///
+ public void CloseConntect()
+ {
+ OnCloseReady();
+ }
+
+ private void DataCallBackReady(byte[] data)
+ {
+
+ //增加接收计数
+ RevIndex = MaxRevIndexNum;
+
+ //不处理心跳包
+ if (data.Length == 1 && data[0] == 0x00)
+ {
+ //LogOut("收到心跳包");
+ return;
}
- private void DataCallBackReady(byte[] data)
+ /*
+ HunterNet_S2C _c2s = DeSerizlize(data);
+ OnDataCallBack(_c2s.HunterNetCoreCmdID, _c2s.HunterNetCoreERRORCode, _c2s.HunterNetCoreData.ToArray());
+ */
+ HunterNet_S2C.AnalysisPkgData(data, out ushort CmdID, out ushort Error, out byte[] resultdata);
+ OnDataCallBack(CmdID, Error, resultdata);
+ }
+
+ MemoryStream memoryStream = new MemoryStream();//开辟一个内存流
+ private void Recive(object o)
+ {
+ var client = o as Socket;
+ //MemoryStream memoryStream = new MemoryStream();//开辟一个内存流
+
+ while (true)
{
-
- //增加接收计数
- RevIndex = MaxRevIndexNum;
-
- //不处理心跳包
- if (data.Length == 1 && data[0] == 0x00)
+ byte[] buffer = new byte[1024 * 1024 * 2];
+ int effective = 0;
+ try
{
- //LogOut("收到心跳包");
+ effective = client.Receive(buffer);
+ if (effective == 0)
+ {
+ continue;
+ }
+ }
+ catch (Exception ex)
+ {
+ //远程主机强迫关闭了一个现有的连接
+ OnCloseReady();
return;
+ //断开连接
}
- /*
- HunterNet_S2C _c2s = DeSerizlize(data);
- OnDataCallBack(_c2s.HunterNetCoreCmdID, _c2s.HunterNetCoreERRORCode, _c2s.HunterNetCoreData.ToArray());
- */
- HunterNet_S2C.AnalysisPkgData(data, out ushort CmdID, out ushort Error, out byte[] resultdata);
- OnDataCallBack(CmdID, Error, resultdata);
- }
- MemoryStream memoryStream = new MemoryStream();//开辟一个内存流
- private void Recive(object o)
- {
- var client = o as Socket;
- //MemoryStream memoryStream = new MemoryStream();//开辟一个内存流
+ memoryStream.Write(buffer, 0, effective);//将接受到的数据写入内存流中
+ byte[] getData = memoryStream.ToArray();//将内存流中的消息体写入字节数组
+ int StartIndex = 0;//设置一个读取数据的起始下标
while (true)
{
- byte[] buffer = new byte[1024 * 1024 * 2];
- int effective = 0;
- try
+
+
+ if (effective > 0)//如果接受到的消息不为0(不为空)
{
- effective = client.Receive(buffer);
- if (effective == 0)
+ int HeadLength = 0;//包头长度(包头+包体)
+ if (getData.Length - StartIndex < 4)//包头接受不完整
{
- continue;
+ HeadLength = -1;
}
- }
- catch (Exception ex)
- {
- //远程主机强迫关闭了一个现有的连接
- OnCloseReady();
- return;
- //断开连接
- }
-
-
- memoryStream.Write(buffer, 0, effective);//将接受到的数据写入内存流中
- byte[] getData = memoryStream.ToArray();//将内存流中的消息体写入字节数组
- int StartIndex = 0;//设置一个读取数据的起始下标
-
- while (true)
- {
-
-
- if (effective > 0)//如果接受到的消息不为0(不为空)
+ else
{
- int HeadLength = 0;//包头长度(包头+包体)
- if (getData.Length - StartIndex < 4)//包头接受不完整
- {
- HeadLength = -1;
- }
- else
- {
- //如果包头接受完整 转换成int类型的数值
- HeadLength = BitConverter.ToInt32(getData, StartIndex);
- }
- //包头接受完整但是消息体不完整 //包头接受不完整
- //↓↓↓↓↓↓↓↓ ↓↓↓
- if (getData.Length - StartIndex < HeadLength || HeadLength == -1)
- {
- /* 一种清空流的方式
- memoryStream.Close();//关闭内存流
- memoryStream.Dispose();//释放内存资源
- memoryStream = new MemoryStream();//创建新的内存流
- */
+ //如果包头接受完整 转换成int类型的数值
+ HeadLength = BitConverter.ToInt32(getData, StartIndex);
+ }
+ //包头接受完整但是消息体不完整 //包头接受不完整
+ //↓↓↓↓↓↓↓↓ ↓↓↓
+ if (getData.Length - StartIndex < HeadLength || HeadLength == -1)
+ {
+ /* 一种清空流的方式
+ memoryStream.Close();//关闭内存流
+ memoryStream.Dispose();//释放内存资源
+ memoryStream = new MemoryStream();//创建新的内存流
+ */
- //流复用的方式 不用重新new申请
- memoryStream.Position = 0;
- memoryStream.SetLength(0);
+ //流复用的方式 不用重新new申请
+ memoryStream.Position = 0;
+ memoryStream.SetLength(0);
- memoryStream.Write(getData, StartIndex, getData.Length - StartIndex);//从新将接受的消息写入内存流
- break;
- }
- else
- {
- //把头去掉,就可以吃了,蛋白质是牛肉的六倍
- //DataCallBackReady(getData.Skip(StartIndex+4).Take(HeadLength-4).ToArray());
+ memoryStream.Write(getData, StartIndex, getData.Length - StartIndex);//从新将接受的消息写入内存流
+ break;
+ }
+ else
+ {
+ //把头去掉,就可以吃了,蛋白质是牛肉的六倍
+ //DataCallBackReady(getData.Skip(StartIndex+4).Take(HeadLength-4).ToArray());
- //改为Array.Copy 提升效率
- int CoreLenght = HeadLength - 4;
- byte[] retData = new byte[CoreLenght];
- Array.Copy(getData, StartIndex + 4, retData, 0, CoreLenght);
- DataCallBackReady(retData);
- StartIndex += HeadLength;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部)
- }
+ int CoreLenght = HeadLength - 4;
+ //改为Array.Copy 提升效率
+ //byte[] retData = new byte[CoreLenght];
+ //Array.Copy(getData, StartIndex + 4, retData, 0, CoreLenght);
+ //DataCallBackReady(retData);
+
+ //用Span
+ Span getData_span = getData;
+ getData_span = getData_span.Slice(StartIndex + 4, CoreLenght);
+ DataCallBackReady(getData_span.ToArray());
+
+ StartIndex += HeadLength;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部)
}
}
}
}
+ }
- private void CheckUpdatetimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
+ private void CheckUpdatetimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
+ {
+ //接收服务器数据计数
+ RevIndex--;
+ if (RevIndex <= 0)
{
- //接收服务器数据计数
- RevIndex--;
- if (RevIndex <= 0)
- {
- //判定掉线
- OnCloseReady();
- return;
- }
-
- //发送计数
- SendIndex--;
- if (SendIndex <= 0)//需要发送心跳包了
- {
- //重置倒计时计数
- SendIndex = MaxSendIndexNum;
-
- SendHeartbeat();
- }
+ //判定掉线
+ OnCloseReady();
+ return;
}
- public void LogOut(string Msg)
+ //发送计数
+ SendIndex--;
+ if (SendIndex <= 0)//需要发送心跳包了
{
- //Console.WriteLine(Msg);
- OnLogOut(Msg);
- }
+ //重置倒计时计数
+ SendIndex = MaxSendIndexNum;
- public Socket GetClientSocket()
- {
- return client;
+ SendHeartbeat();
}
}
+
+ public void LogOut(string Msg)
+ {
+ //Console.WriteLine(Msg);
+ OnLogOut(Msg);
+ }
+
+ public Socket GetClientSocket()
+ {
+ return client;
+ }
}
}
diff --git a/NetLib/HaoYueNet.ClientNetworkNet4x/packages.config b/NetLib/HaoYueNet.ClientNetworkNet4x/packages.config
new file mode 100644
index 0000000..f81a040
--- /dev/null
+++ b/NetLib/HaoYueNet.ClientNetworkNet4x/packages.config
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
--
2.36.0.windows.1
From 083addab8ddcf83b219b0521cb2ec1254654fd7a Mon Sep 17 00:00:00 2001
From: sin365 <353374337@qq.com>
Date: Mon, 8 Jan 2024 16:50:10 +0800
Subject: [PATCH 3/3] byte func optimize
---
.../HaoYueNet.ClientNetworkNet4x.csproj | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/NetLib/HaoYueNet.ClientNetworkNet4x/HaoYueNet.ClientNetworkNet4x.csproj b/NetLib/HaoYueNet.ClientNetworkNet4x/HaoYueNet.ClientNetworkNet4x.csproj
index 4cd582e..3d2d14f 100644
--- a/NetLib/HaoYueNet.ClientNetworkNet4x/HaoYueNet.ClientNetworkNet4x.csproj
+++ b/NetLib/HaoYueNet.ClientNetworkNet4x/HaoYueNet.ClientNetworkNet4x.csproj
@@ -34,7 +34,16 @@
+
+ ..\..\packages\System.Buffers.4.5.1\lib\netstandard1.1\System.Buffers.dll
+
+
+ ..\..\packages\System.Memory.4.5.5\lib\netstandard1.1\System.Memory.dll
+
+
+ ..\..\packages\System.Runtime.CompilerServices.Unsafe.4.5.3\lib\netstandard1.0\System.Runtime.CompilerServices.Unsafe.dll
+
@@ -44,6 +53,7 @@
+
\ No newline at end of file
--
2.36.0.windows.1