function update
This commit is contained in:
@@ -9,6 +9,7 @@ namespace STcpHelper.Packet
|
||||
public enum PacketType
|
||||
{
|
||||
TEXT = 0,
|
||||
|
||||
REQ_CLIENT_LIST,
|
||||
RES_CLIENT_LIST,
|
||||
}
|
||||
}
|
||||
|
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace STcpHelper.Packet
|
||||
{
|
||||
static class SBufferHelper
|
||||
internal class SBufferHelper
|
||||
{
|
||||
public static byte[] GetBuffer(int size, params byte[][] args)
|
||||
{
|
||||
|
21
MySolution/STcpHelper/Packet/SEncoding.cs
Normal file
21
MySolution/STcpHelper/Packet/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.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);
|
||||
}
|
||||
}
|
||||
}
|
25
MySolution/STcpHelper/Packet/STcpClientListReqPacket.cs
Normal file
25
MySolution/STcpHelper/Packet/STcpClientListReqPacket.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace STcpHelper.Packet
|
||||
{
|
||||
public class STcpClientListReqPacket : ISTcpPacket
|
||||
{
|
||||
public STcpClientListReqPacket()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public byte[] Serialize()
|
||||
{
|
||||
// 4bytes header
|
||||
STcpPacketHeader header = new STcpPacketHeader(PacketType.REQ_CLIENT_LIST, 0);
|
||||
|
||||
// [header]
|
||||
return SBufferHelper.GetBuffer(4, header.Serialize());
|
||||
}
|
||||
}
|
||||
}
|
51
MySolution/STcpHelper/Packet/STcpClientListResPacket.cs
Normal file
51
MySolution/STcpHelper/Packet/STcpClientListResPacket.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using STcpHelper.Packet;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace STcpHelper.Packet
|
||||
{
|
||||
public class STcpClientListResPacket : ISTcpPacket
|
||||
{
|
||||
public List<string> Clients { get; private set; }
|
||||
|
||||
public STcpClientListResPacket(List<TcpClient> clinets)
|
||||
{
|
||||
this.Clients = new List<string>();
|
||||
foreach (var client in clinets)
|
||||
{
|
||||
this.Clients.Add(client.Client.RemoteEndPoint.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public STcpClientListResPacket(byte[] dataBuffer)
|
||||
{
|
||||
int cursor = 0;
|
||||
short length = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(dataBuffer, cursor));
|
||||
cursor += sizeof(short);
|
||||
|
||||
string jsonString = SEncoding.GetString(dataBuffer, cursor, length);
|
||||
List<string> clients = JsonSerializer.Deserialize<List<string>>(jsonString);
|
||||
|
||||
this.Clients = clients;
|
||||
}
|
||||
|
||||
public byte[] Serialize()
|
||||
{
|
||||
string jsonString = JsonSerializer.Serialize(this.Clients);
|
||||
byte[] data = SEncoding.GetBytes(jsonString);
|
||||
byte[] dataLength = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)data.Length));
|
||||
|
||||
// 4bytes header
|
||||
STcpPacketHeader header = new STcpPacketHeader(PacketType.RES_CLIENT_LIST, dataLength.Length + data.Length);
|
||||
|
||||
// [header][2bytes size][n bytes string]
|
||||
return SBufferHelper.GetBuffer(4 + dataLength.Length + data.Length, header.Serialize(), dataLength, data);
|
||||
}
|
||||
}
|
||||
}
|
@@ -19,22 +19,22 @@ namespace STcpHelper.Packet
|
||||
public STcpTextPacket(byte[] dataBuffer)
|
||||
{
|
||||
int cursor = 0;
|
||||
short textSize = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(dataBuffer, cursor));
|
||||
short length = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(dataBuffer, cursor));
|
||||
cursor += sizeof(short);
|
||||
|
||||
this.Text = Encoding.UTF8.GetString(dataBuffer, cursor, textSize);
|
||||
this.Text = SEncoding.GetString(dataBuffer, cursor, length);
|
||||
}
|
||||
|
||||
public byte[] Serialize()
|
||||
{
|
||||
byte[] text = Encoding.UTF8.GetBytes(Text);
|
||||
byte[] textSize = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)text.Length));
|
||||
byte[] data = SEncoding.GetBytes(Text);
|
||||
byte[] dataLength = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)data.Length));
|
||||
|
||||
// 4byte header
|
||||
STcpPacketHeader header = new STcpPacketHeader(PacketType.TEXT, text.Length + textSize.Length);
|
||||
// 4bytes header
|
||||
STcpPacketHeader header = new STcpPacketHeader(PacketType.TEXT, dataLength.Length + data.Length);
|
||||
|
||||
// [2bytes text size][n bytes text]
|
||||
return SBufferHelper.GetBuffer(4 + textSize.Length + text.Length, header.Serialize(), textSize, text);
|
||||
// [header][2bytes text size][n bytes text]
|
||||
return SBufferHelper.GetBuffer(4 + dataLength.Length + data.Length, header.Serialize(), dataLength, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
31
MySolution/STcpHelper/Packet/SXmlSerializerHelper.cs
Normal file
31
MySolution/STcpHelper/Packet/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.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