163 lines
4.5 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:43:24 +08:00
public override PacketType Type { get { return 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
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);
ID = reader.ReadInt();
2022-04-02 16:40:24 +02:00
int nameArrayLength = reader.ReadInt();
Name = reader.ReadString(nameArrayLength);
2022-04-02 18:11:30 +02:00
FileLength = reader.ReadLong();
2022-04-02 16:40:24 +02:00
#endregion
}
}
2022-07-01 14:39:43 +08:00
internal class FileTransferResponse : Packet
2022-07-01 12:22:31 +08:00
{
2022-08-06 10:43:24 +08:00
public override PacketType Type { get { return 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);
ID = reader.ReadInt();
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:43:24 +08:00
public override PacketType Type { get { return 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
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);
ID = reader.ReadInt();
2022-04-02 16:40:24 +02:00
int chunkLength = reader.ReadInt();
FileChunk = reader.ReadByteArray(chunkLength);
#endregion
}
}
2022-07-01 14:39:43 +08:00
internal class FileTransferComplete : Packet
2022-04-02 16:40:24 +02:00
{
2022-08-06 10:43:24 +08:00
public override PacketType Type { get { return 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
{
#region NetIncomingMessageToPacket
BitReader reader = new BitReader(array);
ID = reader.ReadInt();
2022-04-02 16:40:24 +02:00
#endregion
}
}
2022-07-01 14:39:43 +08:00
internal class AllResourcesSent : Packet
2022-07-01 12:22:31 +08:00
{
2022-08-06 10:43:24 +08:00
public override PacketType Type { get { return PacketType.AllResourcesSent; } }
public override byte[] Serialize()
2022-07-01 12:22:31 +08:00
{
2022-08-06 10:43:24 +08:00
return new byte[0];
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
{
}
}
2022-04-02 16:40:24 +02:00
}
}