GodotTicTacToe/scripts/net/Peer.cs

118 lines
4.1 KiB
C#

using Godot;
using Network.PacketHandling;
using Network.Syncing;
using System.Threading;
using Util;
using Thread = System.Threading.Thread;
namespace Network {
public abstract class Peer : Object {
public const float TIMEOUT = 4f; // 4 seconds
private static PacketPeerUDP PacketPeer;
private static Peer Singleton;
public readonly bool IsServer;
public Protocol Protocol;
private int LastConnectionSended = -1;
private Thread ListenerThread;
public ConnectionList ConnectionList;
public PacketDistributor PacketDistributor;
public Peer(PacketPeerUDP packetPeer, bool isServer) {
PacketPeer = packetPeer;
IsServer = isServer;
Protocol = new Protocol(this);
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 Process(float delta);
public abstract void Connected(Connection conn);
public abstract void Disconnected(Connection conn);
public void SendBuffer(PacketBuffer packetBuffer, Connection to) {
if (LastConnectionSended != to.ID) {
PacketPeer.SetDestAddress(to.Address, to.Port);
LastConnectionSended = to.ID;
}
PacketPeer.PutPacket(packetBuffer.ByteBuffer);
GD.print("Sent PacketBuffer with size " + packetBuffer.ByteBuffer.Length + " bytes.");
}
public bool StartListening(int port, string address = "*") {
if (PacketPeer.IsListening() || ListenerThread != null) {
GD.printerr("The Peer is already listening or the thread is active!");
return false;
}
Singleton = this;
PacketPeer.Listen(port, address);
ThreadStart ChildRef = new ThreadStart(ListenerThreadMethod);
ListenerThread = new Thread(ChildRef);
ListenerThread.Start();
return true;
}
private static void ListenerThreadMethod() {
GD.print("Started Listener Thread.");
while (true) {
PacketPeer.Wait();
byte[] Buffer = PacketPeer.GetPacket();
string Address = PacketPeer.GetPacketIp();
int Port = PacketPeer.GetPacketPort();
PacketBuffer PB = PacketBuffer.FromByteBuffer(Buffer);
if (PB.Length > PacketBuffer.SignatureBytes.Length) {
bool Confirmed = true;
foreach (byte B in PacketBuffer.SignatureBytes) {
if (PB.Read() != B) {
// Ignore packet, confirmation bytes don't match up.
Confirmed = false;
break;
}
}
if (Confirmed) {
Optional<Connection> Conn = Singleton.ConnectionList.GetOriginal(Address, Port);
if (Conn.isEmpty) {
Conn = new Connection(Address, Port);
Singleton.ConnectionList.AddConnection(Conn);
Singleton.PacketDistributor.AddHandler(Conn);
}
Singleton.PacketDistributor.HandleRawPacket(PB, Singleton.ConnectionList.GetOriginal(Conn));
}
}
}
}
public void Disconnect(Connection conn, DisconnectReason reason) {
GD.print(conn + " disconnected. Reason:");
switch (reason) {
case DisconnectReason.TIMEOUT: {
GD.print("TIMEOUT");
break;
}
default: {
GD.print("Unknown.");
break;
}
}
ConnectionList.RemoveConnection(conn);
PacketDistributor.RemoveHandler(conn);
}
}
}