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\MainMenu.cs" />
|
||||||
<Compile Include="scripts\net\Net.cs" />
|
<Compile Include="scripts\net\Net.cs" />
|
||||||
<Compile Include="scripts\net\packethandling\PacketBuffer.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\Peer.cs" />
|
||||||
<Compile Include="scripts\net\Server.cs" />
|
<Compile Include="scripts\net\Server.cs" />
|
||||||
<Compile Include="scripts\util\Optional.cs" />
|
<Compile Include="scripts\util\Optional.cs" />
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
using Godot;
|
using Godot;
|
||||||
using System;
|
using System;
|
||||||
using Network;
|
using Network;
|
||||||
using Util;
|
using Network.PacketHandling;
|
||||||
|
|
||||||
public class MainMenu : Panel {
|
public class MainMenu : Panel {
|
||||||
|
|
||||||
@ -47,7 +47,6 @@ public class MainMenu : Panel {
|
|||||||
}
|
}
|
||||||
port = defaultPort;
|
port = defaultPort;
|
||||||
if (PortEdit != null && PortEdit.GetText() != "") {
|
if (PortEdit != null && PortEdit.GetText() != "") {
|
||||||
//port = PortEdit.GetText();
|
|
||||||
int temp_port;
|
int temp_port;
|
||||||
Int32.TryParse(PortEdit.GetText(), out temp_port);
|
Int32.TryParse(PortEdit.GetText(), out temp_port);
|
||||||
if (temp_port > 80) {
|
if (temp_port > 80) {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using Godot;
|
using Godot;
|
||||||
using Network.PacketHandling;
|
using Network.PacketHandling;
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace Network {
|
namespace Network {
|
||||||
public class Client : Peer {
|
public class Client : Peer {
|
||||||
@ -13,7 +14,7 @@ namespace Network {
|
|||||||
|
|
||||||
public Client(PacketPeerUDP packetPeer) : base(packetPeer) {
|
public Client(PacketPeerUDP packetPeer) : base(packetPeer) {
|
||||||
PacketPeer = packetPeer;
|
PacketPeer = packetPeer;
|
||||||
TempBuffer = NewPacketBuffer();
|
TempBuffer = PacketBuffer.Create();
|
||||||
TempBuffer.Write(1);
|
TempBuffer.Write(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,8 +11,6 @@ namespace Network {
|
|||||||
private int LastConnectionSended = -1;
|
private int LastConnectionSended = -1;
|
||||||
private Thread ListenerThread;
|
private Thread ListenerThread;
|
||||||
|
|
||||||
private static byte[] ConfirmationBytes = { 154, 211 };
|
|
||||||
|
|
||||||
public ConnectionList ConnectionList;
|
public ConnectionList ConnectionList;
|
||||||
|
|
||||||
public Peer(PacketPeerUDP packetPeer) {
|
public Peer(PacketPeerUDP packetPeer) {
|
||||||
@ -47,14 +45,6 @@ namespace Network {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static PacketBuffer NewPacketBuffer() {
|
|
||||||
PacketBuffer PB = new PacketBuffer();
|
|
||||||
foreach (byte B in ConfirmationBytes) {
|
|
||||||
PB.Write(B);
|
|
||||||
}
|
|
||||||
return PB;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void ListenerThreadMethod() {
|
private static void ListenerThreadMethod() {
|
||||||
GD.print("Started Listener Thread.");
|
GD.print("Started Listener Thread.");
|
||||||
|
|
||||||
@ -66,9 +56,9 @@ namespace Network {
|
|||||||
int Port = PacketPeer.GetPacketPort();
|
int Port = PacketPeer.GetPacketPort();
|
||||||
|
|
||||||
PacketBuffer PB = PacketBuffer.FromByteBuffer(Buffer);
|
PacketBuffer PB = PacketBuffer.FromByteBuffer(Buffer);
|
||||||
if (PB.Length > ConfirmationBytes.Length) {
|
if (PB.Length > PacketBuffer.ConfirmationBytes.Length) {
|
||||||
bool Confirmed = true;
|
bool Confirmed = true;
|
||||||
foreach (byte B in ConfirmationBytes) {
|
foreach (byte B in PacketBuffer.ConfirmationBytes) {
|
||||||
if (PB.Read() != B) {
|
if (PB.Read() != B) {
|
||||||
// Ignore packet, confirmation bytes don't match up.
|
// Ignore packet, confirmation bytes don't match up.
|
||||||
Confirmed = false;
|
Confirmed = false;
|
||||||
|
@ -11,8 +11,12 @@ namespace Network {
|
|||||||
|
|
||||||
public Server(PacketPeerUDP packetPeer) : base(packetPeer) {
|
public Server(PacketPeerUDP packetPeer) : base(packetPeer) {
|
||||||
PacketPeer = packetPeer;
|
PacketPeer = packetPeer;
|
||||||
TempBuffer = NewPacketBuffer();
|
TempBuffer = PacketBuffer.Create();
|
||||||
TempBuffer.Write(2);
|
TempBuffer.Write(150);
|
||||||
|
TempBuffer.Write(145);
|
||||||
|
TempBuffer.Write(154);
|
||||||
|
TempBuffer.Write(154);
|
||||||
|
TempBuffer.Write(157);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Initialize(string address, int port) {
|
public override void Initialize(string address, int port) {
|
||||||
|
@ -7,6 +7,8 @@ namespace Network.PacketHandling {
|
|||||||
private List<byte> ByteList = new List<byte>();
|
private List<byte> ByteList = new List<byte>();
|
||||||
private int ReadCounter = 0;
|
private int ReadCounter = 0;
|
||||||
|
|
||||||
|
public static byte[] ConfirmationBytes = { 154, 211 };
|
||||||
|
|
||||||
public byte[] ByteBuffer { get { return ByteList.ToArray(); } }
|
public byte[] ByteBuffer { get { return ByteList.ToArray(); } }
|
||||||
public int Length { get { return ByteList.Count; } }
|
public int Length { get { return ByteList.Count; } }
|
||||||
public bool HasNext { get { return ReadCounter < ByteList.Count; } }
|
public bool HasNext { get { return ReadCounter < ByteList.Count; } }
|
||||||
@ -19,15 +21,43 @@ namespace Network.PacketHandling {
|
|||||||
return ByteList[ReadCounter++];
|
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) {
|
public void Write(byte toWrite) {
|
||||||
ByteList.Add(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) {
|
public static PacketBuffer FromByteBuffer(byte[] byteBuffer) {
|
||||||
PacketBuffer PB = new PacketBuffer();
|
PacketBuffer PB = new PacketBuffer();
|
||||||
PB.ByteList = new List<byte>(byteBuffer);
|
PB.ByteList = new List<byte>(byteBuffer);
|
||||||
return PB;
|
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