update
This commit is contained in:
14
MySolution/STcpHelper/Utility/ISSerializer.cs
Normal file
14
MySolution/STcpHelper/Utility/ISSerializer.cs
Normal 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);
|
||||
}
|
||||
}
|
44
MySolution/STcpHelper/Utility/SBufferHelper.cs
Normal file
44
MySolution/STcpHelper/Utility/SBufferHelper.cs
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
21
MySolution/STcpHelper/Utility/SEncoding.cs
Normal file
21
MySolution/STcpHelper/Utility/SEncoding.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
22
MySolution/STcpHelper/Utility/SJsonSerializerHelper.cs
Normal file
22
MySolution/STcpHelper/Utility/SJsonSerializerHelper.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
31
MySolution/STcpHelper/Utility/SXmlSerializerHelper.cs
Normal file
31
MySolution/STcpHelper/Utility/SXmlSerializerHelper.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user