dev_byteoptimize #1

Merged
sin365 merged 3 commits from dev_byteoptimize into master 2024-01-08 19:06:25 +08:00
5 changed files with 536 additions and 386 deletions
Showing only changes of commit 610b816922 - Show all commits

View File

@ -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;
//}
/// <summary>
/// 供外部调用 发送消息

View File

@ -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 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)
{
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;
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 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];
CmdID = BitConverter.ToUInt16(srcdata, 0);
Error = BitConverter.ToUInt16(srcdata, 2);
Array.Copy(srcdata, 4, data, 0, data.Length);
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 void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs, UInt16 CmdID, UInt16 Error, byte[] AddonBytes_Data)
{
byte[] data = CreatePkgData(CmdID, Error, AddonBytes_Data);
myreadEventArgs.SetBuffer(data,0, data.Length);
}
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).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<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).ToArray(),0);
data = srcdata.Slice(2).ToArray();
}
}
}

View File

@ -11,17 +11,6 @@ namespace HaoYueNet.ClientNetworkNet4x
{
private Socket client;
/// <summary>
/// 心跳包数据
/// </summary>
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;
//}
/// <summary>
/// 供外部调用 发送消息
@ -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<byte> getData_span = getData;
getData_span = getData_span.Slice(StartIndex + 4, CoreLenght);
DataCallBackReady(getData_span.ToArray());
StartIndex += HeadLength;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部)
}
}

View File

@ -6,24 +6,11 @@ using System.Threading;
using static HaoYueNet.ClientNetworkNet4x.BaseData;
namespace HaoYueNet.ClientNetworkNet4x
{
namespace HaoYueNet.ClientNetwork
{
public class NetworkHelperP2PCore
{
private Socket client;
/// <summary>
/// 心跳包数据
/// </summary>
private 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;
@ -99,7 +86,8 @@ namespace HaoYueNet.ClientNetworkNet4x
private void SendToSocket(byte[] data)
{
data = SendDataWithHead(data);
//已拼接包长度,这里不再需要拼接长度
//data = SendDataWithHead(data);
try
{
SendWithIndex(data);
@ -117,7 +105,7 @@ namespace HaoYueNet.ClientNetworkNet4x
{
try
{
SendWithIndex(HeartbeatData);
SendWithIndex(BaseData.HeartbeatData);
}
catch (Exception ex)
{
@ -329,11 +317,17 @@ 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<byte> getData_span = getData;
getData_span = getData_span.Slice(StartIndex + 4, CoreLenght);
DataCallBackReady(getData_span.ToArray());
StartIndex += HeadLength;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部)
}
}
@ -375,4 +369,3 @@ namespace HaoYueNet.ClientNetworkNet4x
}
}
}
}

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="System.Buffers" version="4.5.1" targetFramework="net452" />
<package id="System.Memory" version="4.5.5" targetFramework="net452" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net452" />
</packages>