134 lines
3.8 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
{
public enum FileType:byte
{
Resource=0,
Custom=1,
}
2022-05-22 15:55:26 +08:00
public partial class Packets
2022-04-02 16:40:24 +02:00
{
2022-04-03 19:28:25 +02:00
public 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-04-03 02:27:30 +02:00
message.Write((byte)PacketTypes.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
}
}
public 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
message.Write((byte)PacketTypes.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
}
}
public class FileTransferComplete : Packet
{
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-04-03 02:27:30 +02:00
message.Write((byte)PacketTypes.FileTransferComplete);
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
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
}
}
}
}