Added de/serialization to primitive types
This commit is contained in:
parent
8af4a05cbb
commit
eafccb4923
@ -56,6 +56,7 @@
|
||||
<Compile Include="scripts\MainMenu.cs" />
|
||||
<Compile Include="scripts\net\Net.cs" />
|
||||
<Compile Include="scripts\net\packethandling\PacketBuffer.cs" />
|
||||
<Compile Include="scripts\net\packethandling\PrimitiveSerializer.cs" />
|
||||
<Compile Include="scripts\net\Peer.cs" />
|
||||
<Compile Include="scripts\net\Server.cs" />
|
||||
<Compile Include="scripts\util\Optional.cs" />
|
||||
|
@ -1,7 +1,7 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using Network;
|
||||
using Util;
|
||||
using Network.PacketHandling;
|
||||
|
||||
public class MainMenu : Panel {
|
||||
|
||||
@ -47,7 +47,6 @@ public class MainMenu : Panel {
|
||||
}
|
||||
port = defaultPort;
|
||||
if (PortEdit != null && PortEdit.GetText() != "") {
|
||||
//port = PortEdit.GetText();
|
||||
int temp_port;
|
||||
Int32.TryParse(PortEdit.GetText(), out temp_port);
|
||||
if (temp_port > 80) {
|
||||
|
@ -1,5 +1,6 @@
|
||||
using Godot;
|
||||
using Network.PacketHandling;
|
||||
using System;
|
||||
|
||||
namespace Network {
|
||||
public class Client : Peer {
|
||||
@ -13,7 +14,7 @@ namespace Network {
|
||||
|
||||
public Client(PacketPeerUDP packetPeer) : base(packetPeer) {
|
||||
PacketPeer = packetPeer;
|
||||
TempBuffer = NewPacketBuffer();
|
||||
TempBuffer = PacketBuffer.Create();
|
||||
TempBuffer.Write(1);
|
||||
}
|
||||
|
||||
|
@ -11,8 +11,6 @@ namespace Network {
|
||||
private int LastConnectionSended = -1;
|
||||
private Thread ListenerThread;
|
||||
|
||||
private static byte[] ConfirmationBytes = { 154, 211 };
|
||||
|
||||
public ConnectionList ConnectionList;
|
||||
|
||||
public Peer(PacketPeerUDP packetPeer) {
|
||||
@ -47,14 +45,6 @@ namespace Network {
|
||||
return true;
|
||||
}
|
||||
|
||||
public static PacketBuffer NewPacketBuffer() {
|
||||
PacketBuffer PB = new PacketBuffer();
|
||||
foreach (byte B in ConfirmationBytes) {
|
||||
PB.Write(B);
|
||||
}
|
||||
return PB;
|
||||
}
|
||||
|
||||
private static void ListenerThreadMethod() {
|
||||
GD.print("Started Listener Thread.");
|
||||
|
||||
@ -66,9 +56,9 @@ namespace Network {
|
||||
int Port = PacketPeer.GetPacketPort();
|
||||
|
||||
PacketBuffer PB = PacketBuffer.FromByteBuffer(Buffer);
|
||||
if (PB.Length > ConfirmationBytes.Length) {
|
||||
if (PB.Length > PacketBuffer.ConfirmationBytes.Length) {
|
||||
bool Confirmed = true;
|
||||
foreach (byte B in ConfirmationBytes) {
|
||||
foreach (byte B in PacketBuffer.ConfirmationBytes) {
|
||||
if (PB.Read() != B) {
|
||||
// Ignore packet, confirmation bytes don't match up.
|
||||
Confirmed = false;
|
||||
|
@ -11,8 +11,12 @@ namespace Network {
|
||||
|
||||
public Server(PacketPeerUDP packetPeer) : base(packetPeer) {
|
||||
PacketPeer = packetPeer;
|
||||
TempBuffer = NewPacketBuffer();
|
||||
TempBuffer.Write(2);
|
||||
TempBuffer = PacketBuffer.Create();
|
||||
TempBuffer.Write(150);
|
||||
TempBuffer.Write(145);
|
||||
TempBuffer.Write(154);
|
||||
TempBuffer.Write(154);
|
||||
TempBuffer.Write(157);
|
||||
}
|
||||
|
||||
public override void Initialize(string address, int port) {
|
||||
|
@ -7,6 +7,8 @@ namespace Network.PacketHandling {
|
||||
private List<byte> ByteList = new List<byte>();
|
||||
private int ReadCounter = 0;
|
||||
|
||||
public static byte[] ConfirmationBytes = { 154, 211 };
|
||||
|
||||
public byte[] ByteBuffer { get { return ByteList.ToArray(); } }
|
||||
public int Length { get { return ByteList.Count; } }
|
||||
public bool HasNext { get { return ReadCounter < ByteList.Count; } }
|
||||
@ -19,15 +21,43 @@ namespace Network.PacketHandling {
|
||||
return ByteList[ReadCounter++];
|
||||
}
|
||||
|
||||
public short ReadShort() { return PrimitiveSerializer.DeserializeShort(this); }
|
||||
|
||||
public int ReadInt() { return PrimitiveSerializer.DeserializeInt(this); }
|
||||
|
||||
public long ReadLong() { return PrimitiveSerializer.DeserializeLong(this); }
|
||||
|
||||
public float ReadFloat() { return PrimitiveSerializer.DeserializeFloat(this); }
|
||||
|
||||
public string ReadString() { return PrimitiveSerializer.DeserializeString(this); }
|
||||
|
||||
public void Write(byte toWrite) {
|
||||
ByteList.Add(toWrite);
|
||||
}
|
||||
|
||||
public void Write(short toWrite) { PrimitiveSerializer.SerializeShort(toWrite, this); }
|
||||
|
||||
public void Write(int toWrite) { PrimitiveSerializer.SerializeInt(toWrite, this); }
|
||||
|
||||
public void Write(long toWrite) { PrimitiveSerializer.SerializeLong(toWrite, this); }
|
||||
|
||||
public void Write(float toWrite) { PrimitiveSerializer.SerializeFloat(toWrite, this); }
|
||||
|
||||
public void Write(string toWrite) { PrimitiveSerializer.SerializeString(toWrite, this); }
|
||||
|
||||
public static PacketBuffer FromByteBuffer(byte[] byteBuffer) {
|
||||
PacketBuffer PB = new PacketBuffer();
|
||||
PB.ByteList = new List<byte>(byteBuffer);
|
||||
return PB;
|
||||
}
|
||||
|
||||
public static PacketBuffer Create() {
|
||||
PacketBuffer PB = new PacketBuffer();
|
||||
foreach (byte B in ConfirmationBytes) {
|
||||
PB.Write(B);
|
||||
}
|
||||
return PB;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
89
scripts/net/packethandling/PrimitiveSerializer.cs
Normal file
89
scripts/net/packethandling/PrimitiveSerializer.cs
Normal file
@ -0,0 +1,89 @@
|
||||
|
||||
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Network.PacketHandling {
|
||||
public static class PrimitiveSerializer {
|
||||
|
||||
public static byte[] GetBytes(PacketBuffer packetBuffer, int size) {
|
||||
List<byte> Bytes = new List<byte>();
|
||||
for (int i = 0; i < size; i++) {
|
||||
Bytes.Add(packetBuffer.Read());
|
||||
}
|
||||
return Bytes.ToArray();
|
||||
}
|
||||
|
||||
public static void SerializeShort(short serializable, PacketBuffer packetBuffer) {
|
||||
byte[] Bytes = BitConverter.GetBytes(serializable);
|
||||
foreach (byte B in Bytes) {
|
||||
packetBuffer.Write(B);
|
||||
}
|
||||
}
|
||||
|
||||
public static short DeserializeShort(PacketBuffer packetBuffer) {
|
||||
return BitConverter.ToInt16(GetBytes(packetBuffer, 2), 0);
|
||||
}
|
||||
|
||||
public static void SerializeInt(int serializable, PacketBuffer packetBuffer) {
|
||||
byte[] Bytes = BitConverter.GetBytes(serializable);
|
||||
foreach (byte B in Bytes) {
|
||||
packetBuffer.Write(B);
|
||||
}
|
||||
}
|
||||
|
||||
public static int DeserializeInt(PacketBuffer packetBuffer) {
|
||||
return BitConverter.ToInt32(GetBytes(packetBuffer, 4), 0);
|
||||
}
|
||||
|
||||
public static void SerializeLong(long serializable, PacketBuffer packetBuffer) {
|
||||
byte[] Bytes = BitConverter.GetBytes(serializable);
|
||||
foreach (byte B in Bytes) {
|
||||
packetBuffer.Write(B);
|
||||
}
|
||||
}
|
||||
|
||||
public static long DeserializeLong(PacketBuffer packetBuffer) {
|
||||
return BitConverter.ToInt64(GetBytes(packetBuffer, 8), 0);
|
||||
}
|
||||
|
||||
public static void SerializeFloat(float serializable, PacketBuffer packetBuffer) {
|
||||
byte[] Bytes = BitConverter.GetBytes(serializable);
|
||||
foreach (byte B in Bytes) {
|
||||
packetBuffer.Write(B);
|
||||
}
|
||||
}
|
||||
|
||||
public static float DeserializeFloat(PacketBuffer packetBuffer) {
|
||||
return BitConverter.ToSingle(GetBytes(packetBuffer, 4), 0);
|
||||
}
|
||||
|
||||
public static void SerializeBool(bool serializable, PacketBuffer packetBuffer) {
|
||||
packetBuffer.Write((byte) (serializable ? 1 : 0));
|
||||
}
|
||||
|
||||
public static bool DeserializeBool(PacketBuffer packetBuffer) {
|
||||
return packetBuffer.Read() == 1;
|
||||
}
|
||||
|
||||
public static void SerializeString(string serializable, PacketBuffer packetBuffer) {
|
||||
byte[] Bytes = Encoding.Unicode.GetBytes(serializable);
|
||||
packetBuffer.Write((byte) Bytes.Length);
|
||||
foreach (byte B in Bytes) {
|
||||
packetBuffer.Write(B);
|
||||
}
|
||||
}
|
||||
|
||||
public static string DeserializeString(PacketBuffer packetBuffer) {
|
||||
int length = packetBuffer.Read();
|
||||
List<byte> Bytes = new List<byte>();
|
||||
for (int i = 0; i < length; i++) {
|
||||
Bytes.Add(packetBuffer.Read());
|
||||
}
|
||||
return Encoding.Unicode.GetString(Bytes.ToArray());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user