update
This commit is contained in:
@@ -13,5 +13,7 @@ namespace STcpHelper.Packet
|
||||
RES_CLIENT_LIST,
|
||||
REQ_FILE,
|
||||
RES_FILE,
|
||||
REQ_ZOO,
|
||||
RES_ZOO,
|
||||
}
|
||||
}
|
||||
|
@@ -1,43 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace STcpHelper.Packet
|
||||
{
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,21 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace STcpHelper.Packet
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using STcpHelper.Utility;
|
||||
|
||||
namespace STcpHelper.Packet
|
||||
{
|
||||
|
@@ -1,4 +1,4 @@
|
||||
using STcpHelper.Packet;
|
||||
using STcpHelper.Utility;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -12,6 +12,8 @@ namespace STcpHelper.Packet
|
||||
{
|
||||
public class STcpClientListResPacket : ISTcpPacket
|
||||
{
|
||||
private ISSerializer _serializer = new SJsonSerializerHelper();
|
||||
|
||||
public List<string> Clients { get; private set; }
|
||||
|
||||
public STcpClientListResPacket(List<TcpClient> clinets)
|
||||
@@ -29,16 +31,15 @@ namespace STcpHelper.Packet
|
||||
int length = SBufferHelper.ConvertBufferToDataLength(dataBuffer, cursor);
|
||||
cursor += sizeof(int);
|
||||
|
||||
string jsonString = SEncoding.GetString(dataBuffer, cursor, length);
|
||||
List<string> clients = JsonSerializer.Deserialize<List<string>>(jsonString);
|
||||
string serialized = SEncoding.GetString(dataBuffer, cursor, length);
|
||||
|
||||
this.Clients = clients;
|
||||
this.Clients = _serializer.Deserialize<List<string>>(serialized);
|
||||
}
|
||||
|
||||
public byte[] Serialize()
|
||||
{
|
||||
string jsonString = JsonSerializer.Serialize(this.Clients);
|
||||
byte[] data = SEncoding.GetBytes(jsonString);
|
||||
string serialized = _serializer.Serialize(this.Clients);
|
||||
byte[] data = SEncoding.GetBytes(serialized);
|
||||
byte[] dataLength = SBufferHelper.ConvertDataLengthToBuffer(data.Length);
|
||||
|
||||
int dataSize = STcpPacketHeader.GetDataSize(data, dataLength);
|
||||
|
@@ -5,6 +5,7 @@ using System.Net;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using STcpHelper.Utility;
|
||||
|
||||
namespace STcpHelper.Packet
|
||||
{
|
||||
|
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using STcpHelper.Utility;
|
||||
|
||||
namespace STcpHelper.Packet
|
||||
{
|
||||
|
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using STcpHelper.Utility;
|
||||
|
||||
namespace STcpHelper.Packet
|
||||
{
|
||||
|
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using STcpHelper.Utility;
|
||||
|
||||
namespace STcpHelper.Packet
|
||||
{
|
||||
|
24
MySolution/STcpHelper/Packet/STcpZooReqPacket.cs
Normal file
24
MySolution/STcpHelper/Packet/STcpZooReqPacket.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using STcpHelper.Utility;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace STcpHelper.Packet
|
||||
{
|
||||
public class STcpZooReqPacket : ISTcpPacket
|
||||
{
|
||||
public STcpZooReqPacket()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public byte[] Serialize()
|
||||
{
|
||||
STcpPacketHeader header = new STcpPacketHeader(PacketType.REQ_ZOO, 0);
|
||||
|
||||
return SBufferHelper.GetBuffer(STcpPacketHeader.HEADER_LENGTH, header.Serialize());
|
||||
}
|
||||
}
|
||||
}
|
45
MySolution/STcpHelper/Packet/STcpZooResPacket.cs
Normal file
45
MySolution/STcpHelper/Packet/STcpZooResPacket.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using SObject;
|
||||
using STcpHelper.Utility;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace STcpHelper.Packet
|
||||
{
|
||||
public class STcpZooResPacket : ISTcpPacket
|
||||
{
|
||||
private ISSerializer _serializer = new SJsonSerializerHelper();
|
||||
public Zoo Zoo { get; private set; }
|
||||
|
||||
public STcpZooResPacket(Zoo zoo)
|
||||
{
|
||||
this.Zoo = zoo;
|
||||
}
|
||||
|
||||
public STcpZooResPacket(byte[] dataBuffer)
|
||||
{
|
||||
int cursor = 0;
|
||||
int length = SBufferHelper.ConvertBufferToDataLength(dataBuffer, cursor);
|
||||
cursor += sizeof(int);
|
||||
|
||||
string serialized = SEncoding.GetString(dataBuffer, cursor, length);
|
||||
|
||||
this.Zoo = _serializer.Deserialize<Zoo>(serialized);
|
||||
}
|
||||
|
||||
public byte[] Serialize()
|
||||
{
|
||||
string serialized = _serializer.Serialize(this.Zoo);
|
||||
byte[] data = SEncoding.GetBytes(serialized);
|
||||
byte[] dataLength = SBufferHelper.ConvertDataLengthToBuffer(data.Length);
|
||||
|
||||
int dataSize = STcpPacketHeader.GetDataSize(data, dataLength);
|
||||
|
||||
STcpPacketHeader header = new STcpPacketHeader(PacketType.RES_ZOO, dataSize);
|
||||
|
||||
return SBufferHelper.GetBuffer(STcpPacketHeader.HEADER_LENGTH + dataSize, header.Serialize(), dataLength, data);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,31 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace STcpHelper.Packet
|
||||
{
|
||||
static class SXmlSerializerHelper
|
||||
{
|
||||
public static string Serialize<T>(T obj)
|
||||
{
|
||||
XmlSerializer serializer = new XmlSerializer(typeof(T));
|
||||
using (StringWriter sw = new StringWriter())
|
||||
{
|
||||
serializer.Serialize(sw, obj);
|
||||
return sw.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public static 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