From 8bc8b692a855e526ab7c55fa4e9374662af01548 Mon Sep 17 00:00:00 2001 From: teascade Date: Tue, 21 Nov 2017 23:23:12 +0200 Subject: [PATCH] Made Peer handle Listening --- scenes/MainMenu.tscn | 2 +- scripts/Client.cs | 5 ++++ scripts/Net.cs | 54 ++++++++++++++++++++++++++++++++++---------- scripts/Peer.cs | 36 +++++++++++++++++++++++++---- scripts/Server.cs | 21 +++++++---------- 5 files changed, 88 insertions(+), 30 deletions(-) diff --git a/scenes/MainMenu.tscn b/scenes/MainMenu.tscn index 9755ac0..ee368d6 100644 --- a/scenes/MainMenu.tscn +++ b/scenes/MainMenu.tscn @@ -179,7 +179,7 @@ size_flags_horizontal = 1 size_flags_vertical = 1 expand_to_len = false focus_mode = 2 -placeholder_text = "8080" +placeholder_text = "7070" placeholder_alpha = 0.6 caret_blink = false caret_blink_speed = 0.65 diff --git a/scripts/Client.cs b/scripts/Client.cs index 4171520..56a695f 100644 --- a/scripts/Client.cs +++ b/scripts/Client.cs @@ -17,6 +17,11 @@ public class Client : Peer { ServerConn = new Connection(address, port); } + public override void ReceivePacket(byte[] buffer, string address, int port) { + GD.print("Client received stuff from " + address + " : " + port + " :"); + GD.print(buffer[0]); + } + public override void Update(float delta) { GD.print("Update client."); SendBuffer(TempBuffer, ServerConn); diff --git a/scripts/Net.cs b/scripts/Net.cs index 8c9bd36..3fa6cad 100644 --- a/scripts/Net.cs +++ b/scripts/Net.cs @@ -6,32 +6,62 @@ public class Net : Node { private Client Client; private Server Server; + public bool IsClient { get { return Client != null; } } + public bool IsServer { get { return Server != null; } } + public override void _Ready() { PacketPeer = new PacketPeerUDP(); } public override void _Process(float delta) { - if (IsServer()) { Server.Update(delta); } - if (IsClient()) { Client.Update(delta); } + if (IsServer) { Server.Update(delta); } + if (IsClient) { Client.Update(delta); } + } + + public override void _ExitTree() { + if (IsServer) { Server.Free(); } + if (IsClient) { Client.Free(); } } public void StartClient() { //if (IsClient() || IsServer()) { return; } Client = new Client(PacketPeer); - Client.Initialize("localhost", 8080); + Client.Initialize("localhost", 7070); } public void StartServer() { //if (IsClient() || IsServer()) { return; } Server = new Server(PacketPeer); - Server.Initialize("*", 8080); - } - - public bool IsClient() { - return Client != null; - } - - public bool IsServer() { - return Server != null; + Server.Initialize("*", 7070); } } + +//extends Node2D + +//# Member variables +//var thread = Thread.new() + +//# This function runs in a thread! +//# Threads always take one userdata argument +//func _bg_load(path): +// print("THREAD FUNC!") +// # Load the resource +// var tex = ResourceLoader.load(path) +// # Call _bg_load_done on main thread +// call_deferred("_bg_load_done") +// return tex # return it + + +//func _bg_load_done(): +// # Wait for the thread to complete, get the returned value +// var tex = thread.wait_to_finish() +// # Set to the sprite +// get_node("sprite").set_texture(tex) + + +//func _on_load_pressed(): +// if (thread.is_active()): +// # Already working +// return +// print("START THREAD!") +// thread.start(self, "_bg_load", "res://mona.png") \ No newline at end of file diff --git a/scripts/Peer.cs b/scripts/Peer.cs index 3434652..de8e62a 100644 --- a/scripts/Peer.cs +++ b/scripts/Peer.cs @@ -1,10 +1,13 @@ using Godot; +using System.Threading; +using Thread = System.Threading.Thread; public abstract class Peer : Object { - private PacketPeerUDP PacketPeer; - + private static PacketPeerUDP PacketPeer; + private static Peer Singleton; private int LastConnectionSended = -1; + private Thread ListenerThread; public Peer(PacketPeerUDP packetPeer) { PacketPeer = packetPeer; @@ -12,11 +15,36 @@ public abstract class Peer : Object { public abstract void Initialize(string address, int port); public abstract void Update(float delta); + public abstract void ReceivePacket(byte[] buffer, string address, int port); public void SendBuffer(byte[] buffer, Connection to) { + int temp1 = PacketPeer.SetDestAddress(to.Address, to.Port); + int temp2 = PacketPeer.PutPacket(buffer); GD.print("Putting stuff to " + to.Address + ":" + to.Port); - PacketPeer.SetDestAddress(to.Address, to.Port); - PacketPeer.PutPacket(buffer); + } + + 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; + } + + public static void ListenerThreadMethod() { + GD.print("Started Listener Thread."); + while (true) { + PacketPeer.Wait(); + byte[] Buffer = PacketPeer.GetPacket(); + string Address = PacketPeer.GetPacketIp(); + int Port = PacketPeer.GetPacketPort(); + Singleton.ReceivePacket(Buffer, Address, Port); + } } } \ No newline at end of file diff --git a/scripts/Server.cs b/scripts/Server.cs index 873f3e6..f107bb9 100644 --- a/scripts/Server.cs +++ b/scripts/Server.cs @@ -10,22 +10,17 @@ public class Server : Peer { } 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(); + StartListening(port, address); GD.print("Server initialization finished."); } + public override void ReceivePacket(byte[] buffer, string address, int port) { + GD.print("Server received stuff from " + address + " : " + port + " :"); + for (int i = 0; i < buffer.Length; i++) { + GD.print(buffer[i]); + } + } + 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]); - } - } } \ No newline at end of file