From a28bf59b86f1d47a4aef4dbeaa36286c52818ea5 Mon Sep 17 00:00:00 2001 From: Bojidar Marinov Date: Mon, 28 Sep 2015 20:36:05 +0300 Subject: [PATCH] Add send_to_all to tcp/server class. Made a global message queue for this kind of messages. Closes #3 --- lib/tcp/server.gd | 13 +++++++++++++ preview/index.xscn | 27 +++++++++++++++------------ preview/preview.gd | 13 +++++++++++++ 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/lib/tcp/server.gd b/lib/tcp/server.gd index 0144453..8469b4f 100644 --- a/lib/tcp/server.gd +++ b/lib/tcp/server.gd @@ -5,6 +5,7 @@ const ReadWriteLock = preload("../ReadWriteLock.gd") var tcp_server var server_running +var global_message_queue = [] var M_tcp_server = Mutex.new() var connections = [] @@ -54,7 +55,12 @@ func loop(data): for message in connection.message_queue: connection.packet_peer.put_var(message) + + for message in global_message_queue: + connection.packet_peer.put_var(message) + connection.message_queue.clear() + global_message_queue.clear() M_tcp_server.unlock() OS.delay_msec(100) @@ -73,4 +79,11 @@ func send_to(id, message): M_tcp_server.unlock() +func send_to_all(message): + M_tcp_server.lock() + + global_message_queue.push_back(message) + + M_tcp_server.unlock() + diff --git a/preview/index.xscn b/preview/index.xscn index f039b07..6c159b2 100644 --- a/preview/index.xscn +++ b/preview/index.xscn @@ -1,15 +1,15 @@ - + "conn_count" - 3 + 4 "conns" - 2, 0, 28, 27, 2, 0, 5, 0, 28, 29, 2, 0, 6, 0, 28, 30, 2, 0 + 2, 0, 28, 27, 2, 0, 3, 0, 28, 29, 2, 0, 6, 0, 28, 30, 2, 0, 7, 0, 28, 31, 2, 0 "names" - + "Playground" "Control" "anchor/right" @@ -30,24 +30,25 @@ "toggle_mode" "text" "flat" + "Send" + "margin/top" "Result" "TextEdit" - "margin/top" "custom_fonts/font" "Client" "anchor/left" - "Send" "start_server" "pressed" + "send_server" "start_client" "send_client" "node_count" - 8 + 9 "nodes" - -1, -1, 1, 0, -1, 11, 2, 0, 3, 0, 4, 1, 5, 2, 6,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 18, 17, 19, 1, 0, 5, 0, 15, 20, -1, 10, 2, 0, 21, 12, 16, 13, 4, 1, 5, 2, 6, 3, 7, 3, 17, 1, 18, 18, 19, 1, 0, 5, 0, 23, 22, -1, 8, 2, 0, 3, 0, 21, 15, 4, 1, 5, 2, 6, 3, 7, 3, 24, 16, 0 "variants" - + 1 False True @@ -55,7 +56,7 @@ "__editor_plugin_screen__" - "Script" + "2D" "__editor_plugin_states__" "2D" @@ -190,15 +191,17 @@ 255, 255, 255, 1 + 0, 1, 1, 1 255, 0, 255, 1 3 41 "Start Server" + 50 + 91 + "Send message to all" 100 "Start Client" - 50 - 91 "Send message" "version" diff --git a/preview/preview.gd b/preview/preview.gd index 095d1eb..5626ea3 100644 --- a/preview/preview.gd +++ b/preview/preview.gd @@ -58,6 +58,19 @@ func send_client(): add_client_result(str("Sent message: ", msg.to_json())) else: add_client_result("Turn on first, please!") + +func send_server(): + var possible_messages = [ + {"dead": str("rat-", floor(rand_range(0, 42)))}, + {"move": str("rat-", floor(rand_range(0, 42))), "amount": [0, 1]}, + {"move": str("rat-", floor(rand_range(0, 42))), "amount": [0, -1]}, + {"move": str("rat-", floor(rand_range(0, 42))), "amount": [1, 0]}, + {"move": str("rat-", floor(rand_range(0, 42))), "amount": [-1, 0]}, + {"notification": str("A new level was added to the collection!"), "from": "!level_bot"} + ] + var msg = possible_messages[rand_range(0, possible_messages.size())] + server.send_to_all(msg) + add_server_result(str("Sent message: ", msg.to_json())) func new_connection(id): add_server_result(str(id, " Connected!"))