From d9b6cc2165f18f33905f0d4a1eaae7c9d1fd7428 Mon Sep 17 00:00:00 2001 From: teascade Date: Wed, 22 Nov 2017 00:33:16 +0200 Subject: [PATCH] Made client listen and made UI matter maybe --- .gitignore | 1 - Properties/AssemblyInfo.cs | 25 ++++++++++++++ scenes/MainMenu.tscn | 4 +-- scripts/Client.cs | 3 +- scripts/Net.cs | 68 +++++++++++++++++++------------------- scripts/Server.cs | 4 +++ 6 files changed, 67 insertions(+), 38 deletions(-) create mode 100644 Properties/AssemblyInfo.cs diff --git a/.gitignore b/.gitignore index 093aa9e..28ac51f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,4 @@ /.mono/ /mono/ /dist/ -/Properties/ *.import diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..48b5364 --- /dev/null +++ b/Properties/AssemblyInfo.cs @@ -0,0 +1,25 @@ +using System.Reflection; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle("TicTacToe")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] diff --git a/scenes/MainMenu.tscn b/scenes/MainMenu.tscn index ee368d6..0b41d94 100644 --- a/scenes/MainMenu.tscn +++ b/scenes/MainMenu.tscn @@ -169,9 +169,9 @@ anchor_top = 0.5 anchor_right = 0.5 anchor_bottom = 0.5 margin_left = -84.0 -margin_top = -39.0 +margin_top = -36.0 margin_right = 83.0 -margin_bottom = -15.0 +margin_bottom = -12.0 rect_pivot_offset = Vector2( 0, 0 ) rect_clip_content = false mouse_filter = 0 diff --git a/scripts/Client.cs b/scripts/Client.cs index 56a695f..cdedfb7 100644 --- a/scripts/Client.cs +++ b/scripts/Client.cs @@ -14,12 +14,13 @@ public class Client : Peer { public override void Initialize(string address, int port) { GD.print("Start client."); + GD.print("Starting to send stuff to " + address + ":" + port); ServerConn = new Connection(address, port); + StartListening(port, address); } 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) { diff --git a/scripts/Net.cs b/scripts/Net.cs index 3fa6cad..0b37e19 100644 --- a/scripts/Net.cs +++ b/scripts/Net.cs @@ -1,4 +1,5 @@ using Godot; +using System; public class Net : Node { @@ -6,11 +7,21 @@ public class Net : Node { private Client Client; private Server Server; + private LineEdit IPAddressEdit; + private LineEdit PortEdit; + public bool IsClient { get { return Client != null; } } public bool IsServer { get { return Server != null; } } public override void _Ready() { PacketPeer = new PacketPeerUDP(); + var IPAddressNode = GetNode("IPAddress"); + var PortNode = GetNode("Port"); + if (IPAddressNode is LineEdit) { + IPAddressEdit = (LineEdit) IPAddressNode; + } if (PortNode is LineEdit) { + PortEdit = (LineEdit) PortNode; + } } public override void _Process(float delta) { @@ -24,44 +35,33 @@ public class Net : Node { } public void StartClient() { - //if (IsClient() || IsServer()) { return; } + if (IsClient || IsServer) { return; } Client = new Client(PacketPeer); - Client.Initialize("localhost", 7070); + string address; + int port; + SetAddressAndPort(out address, out port, "localhost", 7070); + Client.Initialize(address, port); } public void StartServer() { - //if (IsClient() || IsServer()) { return; } + if (IsClient || IsServer) { return; } Server = new Server(PacketPeer); - Server.Initialize("*", 7070); + string address; + int port; + SetAddressAndPort(out address, out port, "*", 7070); + Server.Initialize(address, port); + } + + private void SetAddressAndPort(out string address, out int port, string defaultAddress, int defaultPort) { + if (IPAddressEdit == null || IPAddressEdit.GetText() == "") { + address = defaultAddress; + } else { + address = IPAddressEdit.GetText(); + } + port = defaultPort; + if (PortEdit != null && PortEdit.GetText() != "") { + //port = PortEdit.GetText(); + Int32.TryParse(PortEdit.GetText(), out port); + } } } - -//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/Server.cs b/scripts/Server.cs index f107bb9..1488da1 100644 --- a/scripts/Server.cs +++ b/scripts/Server.cs @@ -5,6 +5,8 @@ public class Server : Peer { private static PacketPeerUDP PacketPeer; + private byte[] TempBuffer = { 1 }; + public Server(PacketPeerUDP packetPeer) : base(packetPeer) { PacketPeer = packetPeer; } @@ -12,6 +14,7 @@ public class Server : Peer { public override void Initialize(string address, int port) { StartListening(port, address); GD.print("Server initialization finished."); + GD.print("Started server on " + address + ":" + port); } public override void ReceivePacket(byte[] buffer, string address, int port) { @@ -19,6 +22,7 @@ public class Server : Peer { for (int i = 0; i < buffer.Length; i++) { GD.print(buffer[i]); } + SendBuffer(TempBuffer, new Connection(address, port)); } public override void Update(float delta) {