Added propar packeting and syncing systems

This commit is contained in:
Sofia 2017-11-25 12:26:55 +02:00
parent eafccb4923
commit ffb0555fd3
10 changed files with 265 additions and 30 deletions

View File

@ -55,10 +55,15 @@
<Compile Include="scripts\net\Connection.cs" />
<Compile Include="scripts\MainMenu.cs" />
<Compile Include="scripts\net\Net.cs" />
<Compile Include="scripts\net\packethandling\packets\SyncPkt.cs" />
<Compile Include="scripts\net\packethandling\Protocol.cs" />
<Compile Include="scripts\net\packethandling\PacketBuffer.cs" />
<Compile Include="scripts\net\packethandling\PrimitiveSerializer.cs" />
<Compile Include="scripts\net\packethandling\Serializable.cs" />
<Compile Include="scripts\net\Peer.cs" />
<Compile Include="scripts\net\Server.cs" />
<Compile Include="scripts\net\syncing\PacketDistributor.cs" />
<Compile Include="scripts\net\syncing\ConnectionHandler.cs" />
<Compile Include="scripts\util\Optional.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

View File

@ -12,9 +12,9 @@ namespace Network {
private PacketBuffer TempBuffer;
private float Timer = 0;
public Client(PacketPeerUDP packetPeer) : base(packetPeer) {
public Client(PacketPeerUDP packetPeer) : base(packetPeer, false) {
PacketPeer = packetPeer;
TempBuffer = PacketBuffer.Create();
TempBuffer = PacketBuffer.WithSignature();
TempBuffer.Write(1);
}
@ -27,20 +27,13 @@ namespace Network {
StartListening(port, "*");
}
public override void Update(float delta) {
public override void Process(float delta) {
Timer += delta;
if (Timer < 1) { return; }
Timer = 0;
SendBuffer(TempBuffer, ServerConn);
}
public override void ReceivePacket(PacketBuffer packetBuffer, Connection connection) {
GD.print("Client received stuff from " + connection.Name + " at " + connection.Address + " : " + connection.Port + " :");
while (packetBuffer.HasNext) {
GD.print(packetBuffer.Read());
}
}
public override void Connected(Connection conn) {
GD.print("Connected to " + conn.Name + " at " + conn.Address + ":" + conn.Port);
}

View File

@ -1,5 +1,6 @@
using Godot;
using Network.PacketHandling;
using Network.Syncing;
using System.Threading;
using Thread = System.Threading.Thread;
@ -8,19 +9,31 @@ namespace Network {
private static PacketPeerUDP PacketPeer;
private static Peer Singleton;
public readonly bool IsServer;
private Protocol Protocol;
private int LastConnectionSended = -1;
private Thread ListenerThread;
public ConnectionList ConnectionList;
public PacketDistributor PacketDistributor;
public Peer(PacketPeerUDP packetPeer) {
public Peer(PacketPeerUDP packetPeer, bool isServer) {
PacketPeer = packetPeer;
IsServer = isServer;
ConnectionList = new ConnectionList(this);
PacketDistributor = new PacketDistributor(this, 0.5f);
}
public void Update(float delta) {
PacketDistributor.Update(delta);
Process(delta);
}
public abstract void Initialize(string address, int port);
public abstract void Update(float delta);
public abstract void ReceivePacket(PacketBuffer packetBuffer, Connection connection);
public abstract void Process(float delta);
public abstract void Connected(Connection conn);
public void SendBuffer(PacketBuffer packetBuffer, Connection to) {
@ -56,19 +69,22 @@ namespace Network {
int Port = PacketPeer.GetPacketPort();
PacketBuffer PB = PacketBuffer.FromByteBuffer(Buffer);
if (PB.Length > PacketBuffer.ConfirmationBytes.Length) {
if (PB.Length > PacketBuffer.SignatureBytes.Length) {
bool Confirmed = true;
foreach (byte B in PacketBuffer.ConfirmationBytes) {
foreach (byte B in PacketBuffer.SignatureBytes) {
if (PB.Read() != B) {
// Ignore packet, confirmation bytes don't match up.
Confirmed = false;
break;
}
}
if (Confirmed) {
Connection conn = new Connection(Address, Port);
Singleton.ConnectionList.AddConnection(conn);
Singleton.ReceivePacket(PB, Singleton.ConnectionList.GetOriginal(conn));
Singleton.PacketDistributor.AddHandler(conn);
Singleton.PacketDistributor.HandleRawPacket(PB, Singleton.ConnectionList.GetOriginal(conn));
}
}
}

View File

@ -9,9 +9,9 @@ namespace Network {
private PacketBuffer TempBuffer;
public Server(PacketPeerUDP packetPeer) : base(packetPeer) {
public Server(PacketPeerUDP packetPeer) : base(packetPeer, true) {
PacketPeer = packetPeer;
TempBuffer = PacketBuffer.Create();
TempBuffer = PacketBuffer.WithSignature();
TempBuffer.Write(150);
TempBuffer.Write(145);
TempBuffer.Write(154);
@ -25,15 +25,7 @@ namespace Network {
GD.print("Started server on " + address + ":" + port);
}
public override void Update(float delta) {
}
public override void ReceivePacket(PacketBuffer packetBuffer, Connection connection) {
GD.print("Server received stuff from " + connection.Name + " at " + connection.Address + " : " + connection.Port + " :");
while (packetBuffer.HasNext) {
GD.print(packetBuffer.Read());
}
SendBuffer(TempBuffer, connection);
public override void Process(float delta) {
}
public override void Connected(Connection conn) {

View File

@ -7,7 +7,7 @@ namespace Network.PacketHandling {
private List<byte> ByteList = new List<byte>();
private int ReadCounter = 0;
public static byte[] ConfirmationBytes = { 154, 211 };
public static byte[] SignatureBytes = { 154, 211 };
public byte[] ByteBuffer { get { return ByteList.ToArray(); } }
public int Length { get { return ByteList.Count; } }
@ -29,6 +29,8 @@ namespace Network.PacketHandling {
public float ReadFloat() { return PrimitiveSerializer.DeserializeFloat(this); }
public bool ReadBool() { return PrimitiveSerializer.DeserializeBool(this); }
public string ReadString() { return PrimitiveSerializer.DeserializeString(this); }
public void Write(byte toWrite) {
@ -43,6 +45,8 @@ namespace Network.PacketHandling {
public void Write(float toWrite) { PrimitiveSerializer.SerializeFloat(toWrite, this); }
public void Write(bool toWrite) { PrimitiveSerializer.SerializeBool(toWrite, this); }
public void Write(string toWrite) { PrimitiveSerializer.SerializeString(toWrite, this); }
public static PacketBuffer FromByteBuffer(byte[] byteBuffer) {
@ -51,13 +55,21 @@ namespace Network.PacketHandling {
return PB;
}
public static PacketBuffer Create() {
public static PacketBuffer WithSignature() {
PacketBuffer PB = new PacketBuffer();
foreach (byte B in ConfirmationBytes) {
foreach (byte B in SignatureBytes) {
PB.Write(B);
}
return PB;
}
public static PacketBuffer operator +(PacketBuffer first, PacketBuffer second) {
PacketBuffer PB = new PacketBuffer();
PB.ByteList.AddRange(first.ByteList);
PB.ByteList.AddRange(second.ByteList);
return PB;
}
}
}

View File

@ -0,0 +1,12 @@

namespace Network.PacketHandling {
public class Protocol {
private Peer Peer;
public Protocol(Peer peer) {
Peer = peer;
}
}
}

View File

@ -0,0 +1,26 @@

using Network.PacketHandling;
namespace Net.PacketHandling {
public abstract class Serializable {
public bool Reliable;
public int ID;
public void Serialize(PacketBuffer packetBuffer) {
packetBuffer.Write(Reliable);
packetBuffer.Write(ID);
WriteContents(packetBuffer);
}
public void Deserialize(PacketBuffer packetBuffer) {
Reliable = packetBuffer.ReadBool();
ID = packetBuffer.ReadInt();
ReadContents(packetBuffer);
}
public abstract void WriteContents(PacketBuffer packetBuffer);
public abstract void ReadContents(PacketBuffer packetBuffer);
}
}

View File

@ -0,0 +1,29 @@

using Network.PacketHandling;
namespace Net.PacketHandling.Packets {
public class SyncPkt : Serializable {
public int LastReliableIDReceived = -1;
public int LastUnreliableIDReceived = -1;
public int AmountOfUnreliablePackets = 0;
public int AmountOfReliablePackets = 0;
public override void WriteContents(PacketBuffer packetBuffer) {
packetBuffer.Write(LastReliableIDReceived);
packetBuffer.Write(LastUnreliableIDReceived);
packetBuffer.Write(AmountOfUnreliablePackets);
packetBuffer.Write(AmountOfReliablePackets);
}
public override void ReadContents(PacketBuffer packetBuffer) {
LastReliableIDReceived = packetBuffer.ReadInt();
LastUnreliableIDReceived = packetBuffer.ReadInt();
AmountOfUnreliablePackets = packetBuffer.ReadInt();
AmountOfReliablePackets = packetBuffer.ReadInt();
}
}
}

View File

@ -0,0 +1,65 @@

using Godot;
using Net.PacketHandling;
using Net.PacketHandling.Packets;
using Network.PacketHandling;
using System.Collections.Generic;
namespace Network.Syncing {
public class ConnectionHandler {
public readonly Connection Connection;
private readonly Peer Peer;
private int LastUnreliableIDReceived = -1;
private int LastReliableIDReceived = -1;
private int LastUnreliableIDTheyReceived = -1;
private int LastReliableIDTheyReceived = -1;
private float TimeSinceLastPacket = 0;
private List<Serializable> ReliableQueue = new List<Serializable>();
private List<Serializable> UnreliableQueue = new List<Serializable>();
public ConnectionHandler(Peer peer, Connection connection) {
Peer = peer;
Connection = connection;
}
public void SerializeAndSendQueues() {
PacketBuffer Buffer = PacketBuffer.WithSignature();
SyncPkt Sync = new SyncPkt();
Sync.Serialize(Buffer);
// Add Queues
Peer.SendBuffer(Buffer, Connection);
}
public void ReceiveBuffer(PacketBuffer packetBuffer) {
SyncPkt Sync = new SyncPkt();
Sync.Deserialize(packetBuffer);
GD.print("Received sync-" + Sync.ID);
// Read Queues
}
public void AddReliable(Serializable serializable, int ReliableID) {
serializable.Reliable = true;
serializable.ID = ReliableID;
ReliableQueue.Add(serializable);
}
public void AddUnreliable(Serializable serializable, int UnreliableID) {
serializable.Reliable = false;
serializable.ID = UnreliableID;
UnreliableQueue.Add(serializable);
}
}
}

View File

@ -0,0 +1,85 @@

using Net.PacketHandling;
using Network.PacketHandling;
using System.Collections.Generic;
using Util;
namespace Network.Syncing {
public class PacketDistributor {
private Peer Peer;
private List<ConnectionHandler> HandlerList = new List<ConnectionHandler>();
private int UnreliableIDCounter = 0;
private int ReliableIDCounter = 0;
private float TimeSinceLastSync = 0;
private readonly float Interval;
public PacketDistributor(Peer peer, float interval) {
Peer = peer;
Interval = interval;
}
public void Update(float delta) {
TimeSinceLastSync += delta;
if (TimeSinceLastSync > Interval) {
TimeSinceLastSync -= Interval;
foreach (ConnectionHandler Handler in HandlerList) {
Handler.SerializeAndSendQueues();
}
}
}
public void HandleRawPacket(PacketBuffer packetBuffer, Connection connection) {
var Handler = GetHandlerFor(connection);
if (!Handler.HasValue) {
Handler = new ConnectionHandler(Peer, connection);
HandlerList.Add(Handler);
}
}
public void AddHandler(Connection connection) {
Handler = new ConnectionHandler(Peer, connection);
HandlerList.Add(Handler);
}
public void AddReliableFor(Connection connection, Serializable serializable) {
var ConnHandlerOpt = GetHandlerFor(connection);
if (ConnHandlerOpt.HasValue) {
ConnHandlerOpt.Value.AddReliable(serializable, ReliableIDCounter++);
}
}
public void AddUnreliableFor(Connection connection, Serializable serializable) {
var ConnHandlerOpt = GetHandlerFor(connection);
if (ConnHandlerOpt.HasValue) {
ConnHandlerOpt.Value.AddUnreliable(serializable, UnreliableIDCounter++);
}
}
public void AddReliableForAll(Serializable serializable) {
foreach (ConnectionHandler Handler in HandlerList) {
Handler.AddReliable(serializable, ReliableIDCounter++);
}
}
public void AddUnreliableForAll(Serializable serializable) {
foreach (ConnectionHandler Handler in HandlerList) {
Handler.AddUnreliable(serializable, UnreliableIDCounter++);
}
}
private Optional<ConnectionHandler> GetHandlerFor(Connection connection) {
foreach (ConnectionHandler Curr in HandlerList) {
if (Curr.Connection == connection) {
return Curr;
}
}
return null;
}
}
}