147 lines
3.9 KiB
C#
Raw Normal View History

2022-04-02 16:40:24 +02:00
using System;
using System.Collections.Generic;
using System.Text;
using Lidgren.Network;
2022-05-22 15:55:26 +08:00
namespace RageCoop.Core
2022-04-02 16:40:24 +02:00
{
2022-07-01 14:39:43 +08:00
internal enum FileResponse:byte
{
2022-07-01 12:22:31 +08:00
NeedToDownload=0,
AlreadyExists=1,
Completed=2,
Loaded=3,
LoadFailed=4,
}
2022-07-01 14:39:43 +08:00
internal partial class Packets
2022-04-02 16:40:24 +02:00
{
2022-07-01 14:39:43 +08:00
internal class FileTransferRequest : Packet
2022-04-02 16:40:24 +02:00
{
2022-08-06 10:49:55 +08:00
public override PacketType Type => PacketType.FileTransferRequest;
public int ID { get; set; }
2022-04-02 16:40:24 +02:00
public string Name { get; set; }
2022-04-02 16:40:24 +02:00
2022-04-02 18:11:30 +02:00
public long FileLength { get; set; }
2022-04-02 16:40:24 +02:00
2022-08-06 10:43:24 +08:00
public override byte[] Serialize()
2022-04-02 16:40:24 +02:00
{
List<byte> byteArray = new List<byte>();
// The ID from the download
byteArray.AddInt(ID);
2022-04-02 16:40:24 +02:00
// The name of the file
byte[] nameBytes = Encoding.UTF8.GetBytes(Name);
2022-04-02 16:40:24 +02:00
byteArray.AddRange(BitConverter.GetBytes(nameBytes.Length));
byteArray.AddRange(nameBytes);
// The length of the file
byteArray.AddRange(BitConverter.GetBytes(FileLength));
2022-08-06 10:43:24 +08:00
return byteArray.ToArray();
2022-04-02 16:40:24 +02:00
}
2022-08-06 10:43:24 +08:00
public override void Deserialize(byte[] array)
2022-04-02 16:40:24 +02:00
{
BitReader reader = new BitReader(array);
2022-08-06 11:40:38 +08:00
ID = reader.ReadInt32();
Name = reader.ReadString();
FileLength = reader.ReadInt64();
2022-04-02 16:40:24 +02:00
}
}
2022-07-01 14:39:43 +08:00
internal class FileTransferResponse : Packet
2022-07-01 12:22:31 +08:00
{
2022-08-06 10:49:55 +08:00
public override PacketType Type => PacketType.FileTransferResponse;
2022-07-01 12:22:31 +08:00
public int ID { get; set; }
public FileResponse Response { get; set; }
2022-08-06 10:43:24 +08:00
public override byte[] Serialize()
2022-07-01 12:22:31 +08:00
{
List<byte> byteArray = new List<byte>();
// The ID from the download
byteArray.AddInt(ID);
byteArray.Add((byte)Response);
2022-08-06 10:43:24 +08:00
return byteArray.ToArray();
2022-07-01 12:22:31 +08:00
}
2022-08-06 10:43:24 +08:00
public override void Deserialize(byte[] array)
2022-07-01 12:22:31 +08:00
{
BitReader reader = new BitReader(array);
2022-08-06 11:40:38 +08:00
ID = reader.ReadInt32();
2022-07-01 12:22:31 +08:00
Response = (FileResponse)reader.ReadByte();
}
}
2022-07-01 14:39:43 +08:00
internal class FileTransferChunk : Packet
2022-04-02 16:40:24 +02:00
{
2022-08-06 10:49:55 +08:00
public override PacketType Type => PacketType.FileTransferChunk;
public int ID { get; set; }
2022-04-02 16:40:24 +02:00
public byte[] FileChunk { get; set; }
2022-08-06 10:43:24 +08:00
public override byte[] Serialize()
2022-04-02 16:40:24 +02:00
{
List<byte> byteArray = new List<byte>();
// The ID from the download
byteArray.AddInt(ID);
2022-04-02 16:40:24 +02:00
// The chunk of the file
2022-08-06 10:43:24 +08:00
byteArray.AddInt(FileChunk.Length);
2022-04-02 16:40:24 +02:00
byteArray.AddRange(FileChunk);
2022-08-06 10:43:24 +08:00
return byteArray.ToArray();
2022-04-02 16:40:24 +02:00
}
2022-08-06 10:43:24 +08:00
public override void Deserialize(byte[] array)
2022-04-02 16:40:24 +02:00
{
BitReader reader = new BitReader(array);
2022-08-06 11:40:38 +08:00
ID = reader.ReadInt32();
FileChunk = reader.ReadByteArray();
2022-04-02 16:40:24 +02:00
}
}
2022-07-01 14:39:43 +08:00
internal class FileTransferComplete : Packet
2022-04-02 16:40:24 +02:00
{
2022-08-06 10:49:55 +08:00
public override PacketType Type => PacketType.FileTransferComplete;
public int ID { get; set; }
2022-04-02 16:40:24 +02:00
2022-08-06 10:43:24 +08:00
public override byte[] Serialize()
2022-04-02 16:40:24 +02:00
{
List<byte> byteArray = new List<byte>();
// The ID for the download
byteArray.AddInt(ID);
2022-04-02 16:40:24 +02:00
2022-08-06 10:43:24 +08:00
return byteArray.ToArray();
2022-04-02 16:40:24 +02:00
}
2022-08-06 10:43:24 +08:00
public override void Deserialize(byte[] array)
2022-04-02 16:40:24 +02:00
{
BitReader reader = new BitReader(array);
2022-08-06 11:40:38 +08:00
ID = reader.ReadInt32();
2022-04-02 16:40:24 +02:00
}
}
2022-07-01 14:39:43 +08:00
internal class AllResourcesSent : Packet
2022-07-01 12:22:31 +08:00
{
2022-08-06 10:49:55 +08:00
public override PacketType Type => PacketType.AllResourcesSent;
2022-07-01 12:22:31 +08:00
}
2022-04-02 16:40:24 +02:00
}
}