byte func optimize

This commit is contained in:
sin365 2024-01-08 16:19:09 +08:00
parent 3183bff42a
commit 4667b74393
7 changed files with 583 additions and 96 deletions

View File

@ -1,48 +1,202 @@
using System; using System.Net.Sockets;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HaoYueNet.ClientNetwork namespace HaoYueNet.ClientNetwork
{ {
public static class BaseData public static class BaseData
{ {
public static class HunterNet_S2C /// <summary>
/// 心跳包数据
/// </summary>
public static byte[] HeartbeatData = new byte[5] { 0x05, 0x00, 0x00, 0x00, 0x00 };
public static void writeInt(byte[] buf, int offset, int value)
{ {
public static byte[] CreatePkgData(UInt16 CmdID, UInt16 Error, byte[] data) buf[offset++] = (byte)(255 & value);
{ buf[offset++] = (byte)(255 & value >> 8);
byte[] newdata = new byte[2 + 2 + data.Length]; buf[offset++] = (byte)(255 & value >> 16);
BitConverter.GetBytes(CmdID).CopyTo(newdata, 0); buf[offset++] = (byte)(255 & value >>> 24);
BitConverter.GetBytes(Error).CopyTo(newdata, 2);
Array.Copy(data, 0, newdata, 4, data.Length);
return newdata;
} }
public static void AnalysisPkgData(byte[] srcdata,out UInt16 CmdID,out UInt16 Error,out byte[] data) public static void writeUInt16(byte[] buf, int offset, int value)
{ {
data = new byte[srcdata.Length - 2 - 2]; buf[offset++] = (byte)(255 & value);
CmdID = BitConverter.ToUInt16(srcdata, 0); buf[offset++] = (byte)(255 & value >> 8);
Error = BitConverter.ToUInt16(srcdata, 2); }
Array.Copy(srcdata, 4, data, 0, data.Length);
public static class HunterNet_Heartbeat
{
public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs)
{
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)
{
//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<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));
Error = BitConverter.ToUInt16(srcdata.Slice(2, 2));
data = srcdata.Slice(2 + 2).ToArray();
} }
} }
public static class HunterNet_C2S 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]; myreadEventArgs.SetBuffer(CreatePkgData(CmdID, AddonBytes_Data));
BitConverter.GetBytes(CmdID).CopyTo(newdata, 0);
Array.Copy(data, 0, newdata, 2, data.Length);
return newdata;
} }
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]; //byte[] AddonBytes_CmdID = BitConverter.GetBytes(CmdID);
CmdID = BitConverter.ToUInt16(srcdata, 0); //int AllLenght = AddonBytes_CmdID.Length + AddonBytes_Data.Length + 4;
Array.Copy(srcdata, 2, data, 0, data.Length); //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<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));
data = srcdata.Slice(2).ToArray();
} }
} }
} }

View File

@ -9,10 +9,6 @@ namespace HaoYueNet.ClientNetwork
{ {
private Socket client; private Socket client;
/// <summary>
/// 心跳包数据
/// </summary>
static byte[] HeartbeatData = new byte[5] { 0x05, 0x00, 0x00, 0x00, 0x00 };
////响应倒计时计数最大值 ////响应倒计时计数最大值
//private static int MaxRevIndexNum = 6; //private static int MaxRevIndexNum = 6;
@ -115,7 +111,8 @@ namespace HaoYueNet.ClientNetwork
private void SendToSocket(byte[] data) private void SendToSocket(byte[] data)
{ {
data = SendDataWithHead(data); //已拼接包长度,这里不再需要拼接长度
//data = SendDataWithHead(data);
try try
{ {
SendWithIndex(data); SendWithIndex(data);
@ -156,25 +153,25 @@ namespace HaoYueNet.ClientNetwork
client.Send(data); 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.Close();//关闭内存
memoryStream.Dispose();//释放资源 // memoryStream.Dispose();//释放资源
return HeadAndBody; // return HeadAndBody;
} //}
/// <summary> /// <summary>
/// 供外部调用 发送消息 /// 供外部调用 发送消息
@ -343,11 +340,18 @@ namespace HaoYueNet.ClientNetwork
//把头去掉,就可以吃了,蛋白质是牛肉的六倍 //把头去掉,就可以吃了,蛋白质是牛肉的六倍
//DataCallBackReady(getData.Skip(StartIndex+4).Take(HeadLength-4).ToArray()); //DataCallBackReady(getData.Skip(StartIndex+4).Take(HeadLength-4).ToArray());
//改为Array.Copy 提升效率
int CoreLenght = HeadLength - 4; int CoreLenght = HeadLength - 4;
byte[] retData = new byte[CoreLenght];
Array.Copy(getData, StartIndex + 4, retData, 0, CoreLenght); //改为Array.Copy 提升效率
DataCallBackReady(retData); //byte[] retData = new byte[CoreLenght];
//Array.Copy(getData, StartIndex + 4, retData, 0, CoreLenght);
//DataCallBackReady(retData);
//用Span
Span<byte> getData_span = getData;
getData_span = getData_span.Slice(StartIndex + 4,CoreLenght);
DataCallBackReady(getData_span.ToArray());
StartIndex += HeadLength;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部) StartIndex += HeadLength;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部)
} }
} }

View File

@ -8,10 +8,6 @@ namespace HaoYueNet.ClientNetwork
{ {
private Socket client; private Socket client;
/// <summary>
/// 心跳包数据
/// </summary>
private byte[] HeartbeatData = new byte[5] { 0x05, 0x00, 0x00, 0x00, 0x00 };
////响应倒计时计数最大值 ////响应倒计时计数最大值
//private static int MaxRevIndexNum = 6; //private static int MaxRevIndexNum = 6;
@ -94,7 +90,8 @@ namespace HaoYueNet.ClientNetwork
private void SendToSocket(byte[] data) private void SendToSocket(byte[] data)
{ {
data = SendDataWithHead(data); //已拼接包长度,这里不再需要拼接长度
//data = SendDataWithHead(data);
try try
{ {
SendWithIndex(data); SendWithIndex(data);
@ -112,7 +109,7 @@ namespace HaoYueNet.ClientNetwork
{ {
try try
{ {
SendWithIndex(HeartbeatData); SendWithIndex(BaseData.HeartbeatData);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -324,11 +321,17 @@ namespace HaoYueNet.ClientNetwork
//把头去掉,就可以吃了,蛋白质是牛肉的六倍 //把头去掉,就可以吃了,蛋白质是牛肉的六倍
//DataCallBackReady(getData.Skip(StartIndex+4).Take(HeadLength-4).ToArray()); //DataCallBackReady(getData.Skip(StartIndex+4).Take(HeadLength-4).ToArray());
//改为Array.Copy 提升效率
int CoreLenght = HeadLength - 4; int CoreLenght = HeadLength - 4;
byte[] retData = new byte[CoreLenght]; //改为Array.Copy 提升效率
Array.Copy(getData, StartIndex + 4, retData, 0, CoreLenght); //byte[] retData = new byte[CoreLenght];
DataCallBackReady(retData); //Array.Copy(getData, StartIndex + 4, retData, 0, CoreLenght);
//DataCallBackReady(retData);
//用Span
Span<byte> getData_span = getData;
getData_span = getData_span.Slice(StartIndex + 4, CoreLenght);
DataCallBackReady(getData_span.ToArray());
StartIndex += HeadLength;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部) StartIndex += HeadLength;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部)
} }
} }

View File

@ -1,48 +1,203 @@
using System; using System.Net.Sockets;
using System.Collections.Generic; using static System.Runtime.InteropServices.JavaScript.JSType;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HaoYueNet.ServerNetwork namespace HaoYueNet.ServerNetwork
{ {
public static class BaseData public static class BaseData
{ {
public static class HunterNet_S2C /// <summary>
/// 心跳包数据
/// </summary>
public static byte[] HeartbeatData = new byte[5] { 0x05, 0x00, 0x00, 0x00, 0x00 };
public static void writeInt(byte[] buf, int offset, int value)
{ {
public static byte[] CreatePkgData(UInt16 CmdID, UInt16 Error, byte[] data) buf[offset++] = (byte)(255 & value);
{ buf[offset++] = (byte)(255 & value >> 8);
byte[] newdata = new byte[2 + 2 + data.Length]; buf[offset++] = (byte)(255 & value >> 16);
BitConverter.GetBytes(CmdID).CopyTo(newdata, 0); buf[offset++] = (byte)(255 & value >>> 24);
BitConverter.GetBytes(Error).CopyTo(newdata, 2);
Array.Copy(data, 0, newdata, 4, data.Length);
return newdata;
} }
public static void AnalysisPkgData(byte[] srcdata, out UInt16 CmdID, out UInt16 Error, out byte[] data) public static void writeUInt16(byte[] buf, int offset, int value)
{ {
data = new byte[srcdata.Length - 2 - 2]; buf[offset++] = (byte)(255 & value);
CmdID = BitConverter.ToUInt16(srcdata, 0); buf[offset++] = (byte)(255 & value >> 8);
Error = BitConverter.ToUInt16(srcdata, 2); }
Array.Copy(srcdata, 4, data, 0, data.Length);
public static class HunterNet_Heartbeat
{
public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs)
{
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)
{
//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<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));
Error = BitConverter.ToUInt16(srcdata.Slice(2, 2));
data = srcdata.Slice(2 + 2).ToArray();
} }
} }
public static class HunterNet_C2S 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]; myreadEventArgs.SetBuffer(CreatePkgData(CmdID, AddonBytes_Data));
BitConverter.GetBytes(CmdID).CopyTo(newdata, 0);
Array.Copy(data, 0, newdata, 2, data.Length);
return newdata;
} }
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]; //byte[] AddonBytes_CmdID = BitConverter.GetBytes(CmdID);
CmdID = BitConverter.ToUInt16(srcdata, 0); //int AllLenght = AddonBytes_CmdID.Length + AddonBytes_Data.Length + 4;
Array.Copy(srcdata, 2, data, 0, data.Length); //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<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));
data = srcdata.Slice(2).ToArray();
} }
} }
} }

View File

@ -0,0 +1,41 @@
using System.Net.Sockets;
namespace HaoYueNet.ServerNetwork
{
public class MemoryStreamPool
{
Stack<MemoryStream> m_pool;
public MemoryStreamPool(int capacity)
{
m_pool = new Stack<MemoryStream>(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();
}
}
}

View File

@ -7,10 +7,6 @@ namespace HaoYueNet.ServerNetwork
{ {
public class TcpSaeaServer public class TcpSaeaServer
{ {
/// <summary>
/// 心跳包数据
/// </summary>
static byte[] HeartbeatData = new byte[5] { 0x05, 0x00, 0x00, 0x00, 0x00 };
//响应倒计时计数最大值 //响应倒计时计数最大值
//public int MaxRevIndexNum { get; set; } = 5; //public int MaxRevIndexNum { get; set; } = 5;
////发送倒计时计数最大值 ////发送倒计时计数最大值
@ -523,7 +519,15 @@ namespace HaoYueNet.ServerNetwork
{ {
TokenWithMsg msg = msg_pool.Dequeue(); TokenWithMsg msg = msg_pool.Dequeue();
//OutNetLog("从信息池取出发送"); //OutNetLog("从信息池取出发送");
SendMessage(msg.token, msg.message); //是心跳包
if (msg.bHeartbeat)
{
SendHeartbeatMessage(msg.token);
}
else
{
SendMessage(msg.token,msg.CMDID,msg.Error,msg.data);
}
msg = null; msg = null;
} }
catch catch
@ -540,7 +544,7 @@ namespace HaoYueNet.ServerNetwork
} }
} }
/*
public void SendMessage(AsyncUserToken token, byte[] message,bool dontNeedHead = false) public void SendMessage(AsyncUserToken token, byte[] message,bool dontNeedHead = false)
{ {
if (token == null || token.Socket == null || !token.Socket.Connected) if (token == null || token.Socket == null || !token.Socket.Connected)
@ -579,8 +583,80 @@ namespace HaoYueNet.ServerNetwork
OutNetLog(e.ToString()); 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) private static byte[] SendDataWithHead(byte[] message)
{ {
@ -589,7 +665,6 @@ namespace HaoYueNet.ServerNetwork
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();//将内存流中的数据写入字节数组
@ -623,12 +698,12 @@ namespace HaoYueNet.ServerNetwork
/// 发送数据并计数 /// 发送数据并计数
/// </summary> /// </summary>
/// <param name="data"></param> /// <param name="data"></param>
private void SendWithIndex(AsyncUserToken token, byte[] data) private void SendWithIndex(AsyncUserToken token, UInt16 CmdID, UInt16 ERRCODE, byte[] data)
{ {
try try
{ {
//发送数据 //发送数据
SendMessage(token, data); SendMessage(token, CmdID, ERRCODE, data);
token.SendIndex = MaxSendIndexNum; token.SendIndex = MaxSendIndexNum;
} }
catch catch
@ -650,8 +725,10 @@ namespace HaoYueNet.ServerNetwork
_s2cdata.HunterNetCoreData = ByteString.CopyFrom(data); _s2cdata.HunterNetCoreData = ByteString.CopyFrom(data);
_s2cdata.HunterNetCoreERRORCode = ERRCODE; _s2cdata.HunterNetCoreERRORCode = ERRCODE;
byte[] _finaldata = Serizlize(_s2cdata);*/ 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) private void DataCallBackReady(AsyncUserToken sk, byte[] data)
@ -697,7 +774,7 @@ namespace HaoYueNet.ServerNetwork
/// </summary> /// </summary>
/// <param name="sk"></param> /// <param name="sk"></param>
/// ///
private void SendHeartbeat(AsyncUserToken token) private void SendHeartbeatWithIndex(AsyncUserToken token)
{ {
if (token == null || token.Socket == null || !token.Socket.Connected) if (token == null || token.Socket == null || !token.Socket.Connected)
return; return;
@ -705,7 +782,7 @@ namespace HaoYueNet.ServerNetwork
{ {
//OutNetLog(DateTime.Now.ToString() + "发送心跳包"); //OutNetLog(DateTime.Now.ToString() + "发送心跳包");
token.SendIndex = MaxSendIndexNum; token.SendIndex = MaxSendIndexNum;
SendMessage(token, HeartbeatData, true); SendHeartbeatMessage(token);
} }
catch (Exception e) catch (Exception e)
{ {
@ -736,7 +813,7 @@ namespace HaoYueNet.ServerNetwork
{ {
//重置倒计时计数 //重置倒计时计数
m_clients[i].SendIndex = MaxSendIndexNum; m_clients[i].SendIndex = MaxSendIndexNum;
SendHeartbeat(m_clients[i]); SendHeartbeatWithIndex(m_clients[i]);
} }
} }
} }

View File

@ -6,6 +6,58 @@ using System.Text;
namespace HaoYueNet.ServerNetwork 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<TokenWithMsg> msg_pool;
Queue<TokenWithMsg> msg_pool;
public TokenMsgPool(int capacity)
{
msg_pool = new Queue<TokenWithMsg>(capacity);
}
/// <summary>
/// 向 Queue 的末尾添加一个对象。
/// </summary>
/// <param name="item"></param>
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 class TokenWithMsg
{ {
public AsyncUserToken token; public AsyncUserToken token;
@ -74,4 +126,5 @@ namespace HaoYueNet.ServerNetwork
msg_pool.Clear(); msg_pool.Clear();
} }
} }
*/
} }