184 lines
5.2 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
{
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-05-22 15:55:26 +08:00
public override void Pack(NetOutgoingMessage message)
2022-04-02 16:40:24 +02:00
{
#region PacketToNetOutGoingMessage
2022-07-01 12:22:31 +08:00
message.Write((byte)PacketType.FileTransferRequest);
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));
byte[] result = byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
}
2022-05-22 15:55:26 +08:00
public override void Unpack(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
{
public int ID { get; set; }
public FileResponse Response { get; set; }
public override void Pack(NetOutgoingMessage message)
{
message.Write((byte)PacketType.FileTransferResponse);
List<byte> byteArray = new List<byte>();
// The ID from the download
byteArray.AddInt(ID);
byteArray.Add((byte)Response);
byte[] result = byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
}
public override void Unpack(byte[] array)
{
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
{
public int ID { get; set; }
2022-04-02 16:40:24 +02:00
public byte[] FileChunk { get; set; }
2022-05-22 15:55:26 +08:00
public override void Pack(NetOutgoingMessage message)
2022-04-02 16:40:24 +02:00
{
#region PacketToNetOutGoingMessage
2022-07-01 12:22:31 +08:00
message.Write((byte)PacketType.FileTransferChunk);
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
byteArray.AddRange(BitConverter.GetBytes(FileChunk.Length));
byteArray.AddRange(FileChunk);
byte[] result = byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
}
2022-05-22 15:55:26 +08:00
public override void Unpack(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
{
public int ID { get; set; }
2022-04-02 16:40:24 +02:00
2022-05-22 15:55:26 +08:00
public override void Pack(NetOutgoingMessage message)
2022-04-02 16:40:24 +02:00
{
#region PacketToNetOutGoingMessage
2022-07-01 12:22:31 +08:00
message.Write((byte)PacketType.FileTransferComplete);
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
byte[] result = byteArray.ToArray();
message.Write(result.Length);
message.Write(result);
#endregion
}
2022-05-22 15:55:26 +08:00
public override void Unpack(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
{
public override void Pack(NetOutgoingMessage message)
{
#region PacketToNetOutGoingMessage
message.Write((byte)PacketType.AllResourcesSent);
message.Write(0);
#endregion
}
public override void Unpack(byte[] array)
{
#region NetIncomingMessageToPacket
#endregion
}
}
2022-04-02 16:40:24 +02:00
}
}