using Godot;
using System.Threading;

public class Server : Peer {

    private static PacketPeerUDP PacketPeer;

    public Server(PacketPeerUDP packetPeer) : base(packetPeer) {
        PacketPeer = packetPeer;
    }

    public override void Initialize(string address, int port) {
        PacketPeer.Listen(port, address);
        ThreadStart childref = new ThreadStart(ListenerThread);
        System.Threading.Thread thread = new System.Threading.Thread(childref);
        thread.Start();
        GD.print("Server initialization finished.");
    }

    public override void Update(float delta) {
    }

    public static void ListenerThread() {
        GD.print("Started Listener Thread.");
        while (true) {
            PacketPeer.Wait();
            GD.print("Received stuff from " + PacketPeer.GetPacketIp() + " : " + PacketPeer.GetPacketPort() + " :");
            GD.print(PacketPeer.GetPacket()[0]);
        }
    }
}