GodotTicTacToe/scripts/net/Peer.cs

118 lines
4.1 KiB
C#
Raw Normal View History

using Godot;
using Network.PacketHandling;
using Network.Syncing;
using System.Threading;
2017-11-25 12:34:17 +01:00
using Util;
using Thread = System.Threading.Thread;
namespace Network {
public abstract class Peer : Object {
2017-11-25 15:23:55 +01:00
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;
2017-11-22 21:56:43 +01:00
public ConnectionList ConnectionList;
public PacketDistributor PacketDistributor;
public Peer(PacketPeerUDP packetPeer, bool isServer) {
PacketPeer = packetPeer;
IsServer = isServer;
Protocol = new Protocol(this);
2017-11-22 21:56:43 +01:00
ConnectionList = new ConnectionList(this);
2017-11-25 15:23:55 +01:00
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);
2017-11-25 15:23:55 +01:00
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);
2017-11-25 15:23:55 +01:00
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) {
2017-11-25 12:34:17 +01:00
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));
}
}
}
}
2017-11-25 15:23:55 +01:00
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);
}
}
}