refactoring, file packet
This commit is contained in:
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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user