This commit is contained in:
2023-10-13 14:15:26 +09:00
parent 044ace6552
commit d4dc58c609
28 changed files with 359 additions and 18 deletions

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace STcpHelper.Utility
{
internal interface ISSerializer
{
string Serialize<T>(T obj);
T Deserialize<T>(string str);
}
}

View File

@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using STcpHelper.Packet;
namespace STcpHelper.Utility
{
internal class SBufferHelper
{
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;
}
public static byte[] ConvertPacketTypeToBuffer(PacketType packetType)
{
return BitConverter.GetBytes(IPAddress.HostToNetworkOrder((int)packetType));
}
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));
}
}
}

View File

@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace STcpHelper.Utility
{
internal class SEncoding
{
public static string GetString(byte[] bytes, int index, int length)
{
return Encoding.UTF8.GetString(bytes, index, length);
}
public static byte[] GetBytes(string s)
{
return Encoding.UTF8.GetBytes(s);
}
}
}

View File

@@ -0,0 +1,22 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace STcpHelper.Utility
{
public class SJsonSerializerHelper : ISSerializer
{
public string Serialize<T>(T obj)
{
return JsonConvert.SerializeObject(obj);
}
public T Deserialize<T>(string str)
{
return JsonConvert.DeserializeObject<T>(str);
}
}
}

View File

@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace STcpHelper.Utility
{
public class SXmlSerializerHelper : ISSerializer
{
public string Serialize<T>(T obj)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (StringWriter sw = new StringWriter())
{
serializer.Serialize(sw, obj);
return sw.ToString();
}
}
public T Deserialize<T>(string str)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (StringReader sr = new StringReader(str))
{
return (T)serializer.Deserialize(sr);
}
}
}
}