Files
dotNetStudyWithGPT/MySolution/STcpHelper/Utility/SBufferHelper.cs

45 lines
1.2 KiB
C#
Raw Normal View History

2023-10-10 17:55:14 +09:00
using System;
using System.Collections.Generic;
using System.Linq;
2023-10-12 16:21:04 +09:00
using System.Net;
2023-10-10 17:55:14 +09:00
using System.Text;
using System.Threading.Tasks;
2023-10-13 14:15:26 +09:00
using STcpHelper.Packet;
2023-10-10 17:55:14 +09:00
2023-10-13 14:15:26 +09:00
namespace STcpHelper.Utility
2023-10-10 17:55:14 +09:00
{
2023-10-11 11:51:44 +09:00
internal class SBufferHelper
2023-10-10 17:55:14 +09:00
{
public static byte[] GetBuffer(int size, params byte[][] args)
{
byte[] bytes = new byte[size];
int cursor = 0;
for (int i = 0; i < args.Length; i++)
{
byte[] data = args[i];
Array.Copy(data, 0, bytes, cursor, data.Length);
cursor += data.Length;
}
return bytes;
}
2023-10-12 16:21:04 +09:00
public static byte[] ConvertPacketTypeToBuffer(PacketType packetType)
{
2023-10-13 14:15:26 +09:00
return BitConverter.GetBytes(IPAddress.HostToNetworkOrder((int)packetType));
2023-10-12 16:21:04 +09:00
}
public static byte[] ConvertDataLengthToBuffer(int dataLength)
{
return BitConverter.GetBytes(IPAddress.HostToNetworkOrder(dataLength));
}
public static int ConvertBufferToDataLength(byte[] dataBuffer, int cursor)
{
return IPAddress.NetworkToHostOrder(BitConverter.ToInt32(dataBuffer, cursor));
}
2023-10-10 17:55:14 +09:00
}
}