HaoYueNet/NetLib/HaoYueNet.ClientNetworkNet4x/BaseData.cs

168 lines
6.6 KiB
C#
Raw Permalink Normal View History

using System;
2024-01-08 16:48:09 +08:00
using System.Net.Sockets;
namespace HaoYueNet.ClientNetworkNet4x
{
public static class BaseData
{
2024-01-08 16:48:09 +08:00
/// <summary>
/// 心跳包数据
/// </summary>
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);
2024-04-14 20:34:55 +08:00
//buf[offset++] = (byte)(255 & value >>> 24);
2024-01-08 16:48:09 +08:00
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
{
2024-01-08 16:48:09 +08:00
public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs, UInt16 CmdID, UInt16 Error, byte[] AddonBytes_Data)
{
2024-04-14 20:34:55 +08:00
byte[] data = CreatePkgData(CmdID, Error, AddonBytes_Data);
2024-01-08 16:48:09 +08:00
myreadEventArgs.SetBuffer(data,0, data.Length);
}
2024-01-08 16:48:09 +08:00
public static byte[] CreatePkgData(UInt16 CmdID, UInt16 Error, byte[] AddonBytes_Data)
{
2024-01-08 16:48:09 +08:00
//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;
}
2024-04-14 20:34:55 +08:00
public static void AnalysisPkgData(byte[] srcdata, out UInt16 CmdID, out UInt16 Error, out byte[] data)
2024-01-08 16:48:09 +08:00
{
2024-04-14 20:34:55 +08:00
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);
}
}
public static class HunterNet_C2S
{
2024-01-08 16:48:09 +08:00
public static void SetDataToSocketAsyncEventArgs(SocketAsyncEventArgs myreadEventArgs, UInt16 CmdID, byte[] AddonBytes_Data)
{
byte[] data = CreatePkgData(CmdID, AddonBytes_Data);
myreadEventArgs.SetBuffer(data, 0, data.Length);
}
public static byte[] CreatePkgData(UInt16 CmdID, byte[] AddonBytes_Data)
{
2024-01-08 16:48:09 +08:00
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;
}
2024-04-14 20:34:55 +08:00
public static void AnalysisPkgData(byte[] srcdata, out UInt16 CmdID, out byte[] data)
{
2024-04-14 20:34:55 +08:00
data = new byte[srcdata.Length - 2];
CmdID = BitConverter.ToUInt16(srcdata, 0);
Array.Copy(srcdata, 2, data, 0, data.Length);
}
}
}
}