refactoring, file packet
This commit is contained in:
13
MySolution/STcpHelper/Packet/ISTcpAsyncPacket.cs
Normal file
13
MySolution/STcpHelper/Packet/ISTcpAsyncPacket.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace STcpHelper.Packet
|
||||
{
|
||||
public interface ISTcpAsyncPacket
|
||||
{
|
||||
Task<byte[]> Serialize();
|
||||
}
|
||||
}
|
@@ -11,5 +11,7 @@ namespace STcpHelper.Packet
|
||||
TEXT = 0,
|
||||
REQ_CLIENT_LIST,
|
||||
RES_CLIENT_LIST,
|
||||
REQ_FILE,
|
||||
RES_FILE,
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -23,5 +24,20 @@ namespace STcpHelper.Packet
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -15,11 +15,9 @@ namespace STcpHelper.Packet
|
||||
|
||||
public byte[] Serialize()
|
||||
{
|
||||
// 4bytes header
|
||||
STcpPacketHeader header = new STcpPacketHeader(PacketType.REQ_CLIENT_LIST, 0);
|
||||
|
||||
// [header]
|
||||
return SBufferHelper.GetBuffer(4, header.Serialize());
|
||||
return SBufferHelper.GetBuffer(STcpPacketHeader.HEADER_LENGTH, header.Serialize());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -26,8 +26,8 @@ namespace STcpHelper.Packet
|
||||
public STcpClientListResPacket(byte[] dataBuffer)
|
||||
{
|
||||
int cursor = 0;
|
||||
short length = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(dataBuffer, cursor));
|
||||
cursor += sizeof(short);
|
||||
int length = SBufferHelper.ConvertBufferToDataLength(dataBuffer, cursor);
|
||||
cursor += sizeof(int);
|
||||
|
||||
string jsonString = SEncoding.GetString(dataBuffer, cursor, length);
|
||||
List<string> clients = JsonSerializer.Deserialize<List<string>>(jsonString);
|
||||
@@ -39,13 +39,13 @@ namespace STcpHelper.Packet
|
||||
{
|
||||
string jsonString = JsonSerializer.Serialize(this.Clients);
|
||||
byte[] data = SEncoding.GetBytes(jsonString);
|
||||
byte[] dataLength = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)data.Length));
|
||||
byte[] dataLength = SBufferHelper.ConvertDataLengthToBuffer(data.Length);
|
||||
|
||||
// 4bytes header
|
||||
STcpPacketHeader header = new STcpPacketHeader(PacketType.RES_CLIENT_LIST, dataLength.Length + data.Length);
|
||||
int dataSize = STcpPacketHeader.GetDataSize(data, dataLength);
|
||||
|
||||
// [header][2bytes size][n bytes string]
|
||||
return SBufferHelper.GetBuffer(4 + dataLength.Length + data.Length, header.Serialize(), dataLength, data);
|
||||
STcpPacketHeader header = new STcpPacketHeader(PacketType.RES_CLIENT_LIST, dataSize);
|
||||
|
||||
return SBufferHelper.GetBuffer(STcpPacketHeader.HEADER_LENGTH + dataSize, header.Serialize(), dataLength, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
41
MySolution/STcpHelper/Packet/STcpFileReqPacket.cs
Normal file
41
MySolution/STcpHelper/Packet/STcpFileReqPacket.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace STcpHelper.Packet
|
||||
{
|
||||
public class STcpFileReqPacket : ISTcpPacket
|
||||
{
|
||||
public string Path { get; private set; }
|
||||
|
||||
public STcpFileReqPacket(string path)
|
||||
{
|
||||
this.Path = path;
|
||||
}
|
||||
|
||||
public STcpFileReqPacket(byte[] dataBuffer)
|
||||
{
|
||||
int cursor = 0;
|
||||
int length = SBufferHelper.ConvertBufferToDataLength(dataBuffer, cursor);
|
||||
cursor += sizeof(int);
|
||||
|
||||
this.Path = SEncoding.GetString(dataBuffer, cursor, length);
|
||||
}
|
||||
|
||||
public byte[] Serialize()
|
||||
{
|
||||
byte[] data = SEncoding.GetBytes(this.Path);
|
||||
byte[] dataLength = SBufferHelper.ConvertDataLengthToBuffer(data.Length);
|
||||
|
||||
int dataSize = STcpPacketHeader.GetDataSize(data, dataLength);
|
||||
|
||||
STcpPacketHeader header = new STcpPacketHeader(PacketType.REQ_FILE, dataSize);
|
||||
|
||||
return SBufferHelper.GetBuffer(STcpPacketHeader.HEADER_LENGTH + dataSize, header.Serialize(), dataLength, data);
|
||||
}
|
||||
}
|
||||
}
|
67
MySolution/STcpHelper/Packet/STcpFileResPacket.cs
Normal file
67
MySolution/STcpHelper/Packet/STcpFileResPacket.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace STcpHelper.Packet
|
||||
{
|
||||
public class STcpFileResPacket : ISTcpAsyncPacket
|
||||
{
|
||||
private string _path;
|
||||
|
||||
public string FileName { get; private set; }
|
||||
public byte[] FileBinary { get; private set; }
|
||||
|
||||
public STcpFileResPacket(string path)
|
||||
{
|
||||
this.FileName = Path.GetFileName(path);
|
||||
_path = path;
|
||||
}
|
||||
|
||||
public STcpFileResPacket(byte[] dataBuffer, string path)
|
||||
{
|
||||
int cursor = 0;
|
||||
int nameLength = SBufferHelper.ConvertBufferToDataLength(dataBuffer, cursor);
|
||||
cursor += sizeof(int);
|
||||
|
||||
this.FileName = SEncoding.GetString(dataBuffer, cursor, nameLength);
|
||||
cursor += nameLength;
|
||||
|
||||
int fileSize = SBufferHelper.ConvertBufferToDataLength(dataBuffer, cursor);
|
||||
cursor += sizeof(int);
|
||||
|
||||
this.FileBinary = new byte[fileSize];
|
||||
Array.Copy(dataBuffer, cursor, this.FileBinary, 0, fileSize);
|
||||
|
||||
File.WriteAllBytes(path + @$"\{FileName}", this.FileBinary);
|
||||
}
|
||||
|
||||
public async Task<byte[]> Serialize()
|
||||
{
|
||||
byte[] name = SEncoding.GetBytes(this.FileName);
|
||||
byte[] nameLength = SBufferHelper.ConvertDataLengthToBuffer(name.Length);
|
||||
byte[] data = await ReadFileAsync(_path);
|
||||
byte[] dataLength = SBufferHelper.ConvertDataLengthToBuffer(data.Length);
|
||||
|
||||
int dataSize = STcpPacketHeader.GetDataSize(name, nameLength, data, dataLength);
|
||||
|
||||
// 4bytes header
|
||||
STcpPacketHeader header = new STcpPacketHeader(PacketType.RES_FILE, dataSize);
|
||||
|
||||
// [heaer][2byte name size][n bytes name][2bytes file size][n bytes string]
|
||||
return SBufferHelper.GetBuffer(STcpPacketHeader.HEADER_LENGTH + dataSize, header.Serialize(), nameLength, name, dataLength, data);
|
||||
}
|
||||
|
||||
private async Task<byte[]> ReadFileAsync(string path)
|
||||
{
|
||||
using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 1024, useAsync: true))
|
||||
{
|
||||
byte[] buffer = new byte[stream.Length];
|
||||
await stream.ReadAsync(buffer, 0, (int)stream.Length);
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -12,31 +12,45 @@ namespace STcpHelper.Packet
|
||||
/// </summary>
|
||||
public class STcpPacketHeader : ISTcpPacket
|
||||
{
|
||||
private readonly int TYPE_LENGTH = 4;
|
||||
public static readonly int HEADER_LENGTH = 8;
|
||||
|
||||
public PacketType Type { get; private set; }
|
||||
public short DataLength { get; private set; }
|
||||
public int DataLength { get; private set; }
|
||||
|
||||
public STcpPacketHeader(PacketType type, int dataLength)
|
||||
{
|
||||
Type = type;
|
||||
DataLength = (short)dataLength;
|
||||
DataLength = dataLength;
|
||||
}
|
||||
|
||||
public STcpPacketHeader(byte[] buffer)
|
||||
{
|
||||
int cursor = 0;
|
||||
this.Type = (PacketType)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, cursor));
|
||||
cursor += 2;
|
||||
this.Type = (PacketType)SBufferHelper.ConvertBufferToDataLength(buffer, cursor);
|
||||
cursor += TYPE_LENGTH;
|
||||
|
||||
this.DataLength = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, cursor));
|
||||
this.DataLength = SBufferHelper.ConvertBufferToDataLength(buffer, cursor);
|
||||
}
|
||||
|
||||
// [2bytes Type][2bytes Size]
|
||||
public byte[] Serialize()
|
||||
{
|
||||
byte[] type = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)this.Type));
|
||||
byte[] size = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(this.DataLength));
|
||||
|
||||
return SBufferHelper.GetBuffer(type.Length + size.Length, type, size);
|
||||
byte[] type = SBufferHelper.ConvertPacketTypeToBuffer(this.Type);
|
||||
byte[] dataLength = SBufferHelper.ConvertDataLengthToBuffer(this.DataLength);
|
||||
|
||||
return SBufferHelper.GetBuffer(HEADER_LENGTH, type, dataLength);
|
||||
}
|
||||
|
||||
public static int GetDataSize(params byte[][] data)
|
||||
{
|
||||
int size = 0;
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
{
|
||||
size += data[i].Length;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -19,8 +19,8 @@ namespace STcpHelper.Packet
|
||||
public STcpTextPacket(byte[] dataBuffer)
|
||||
{
|
||||
int cursor = 0;
|
||||
short length = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(dataBuffer, cursor));
|
||||
cursor += sizeof(short);
|
||||
int length = SBufferHelper.ConvertBufferToDataLength(dataBuffer, cursor);
|
||||
cursor += sizeof(int);
|
||||
|
||||
this.Text = SEncoding.GetString(dataBuffer, cursor, length);
|
||||
}
|
||||
@@ -28,13 +28,14 @@ namespace STcpHelper.Packet
|
||||
public byte[] Serialize()
|
||||
{
|
||||
byte[] data = SEncoding.GetBytes(Text);
|
||||
byte[] dataLength = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)data.Length));
|
||||
byte[] dataLength = SBufferHelper.ConvertDataLengthToBuffer(data.Length);
|
||||
|
||||
// 4bytes header
|
||||
STcpPacketHeader header = new STcpPacketHeader(PacketType.TEXT, dataLength.Length + data.Length);
|
||||
int dataSize = STcpPacketHeader.GetDataSize(data, dataLength);
|
||||
|
||||
// [header][2bytes text size][n bytes text]
|
||||
return SBufferHelper.GetBuffer(4 + dataLength.Length + data.Length, header.Serialize(), dataLength, data);
|
||||
STcpPacketHeader header = new STcpPacketHeader(PacketType.TEXT, dataSize);
|
||||
|
||||
return SBufferHelper.GetBuffer(
|
||||
STcpPacketHeader.HEADER_LENGTH + dataSize, header.Serialize(), dataLength, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user