diff --git a/main.tscn b/main.tscn index cc7f622..472bfe2 100644 --- a/main.tscn +++ b/main.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=9 format=2] +[gd_scene load_steps=11 format=2] [ext_resource path="res://terminal.tscn" type="PackedScene" id=1] [ext_resource path="res://main.gd" type="Script" id=2] @@ -7,6 +7,8 @@ [ext_resource path="res://fonts/default.tres" type="DynamicFont" id=5] [ext_resource path="res://text_editor.gd" type="Script" id=6] [ext_resource path="res://fonts/big.tres" type="DynamicFont" id=7] +[ext_resource path="res://tcp_server.gd" type="Script" id=8] +[ext_resource path="res://test.gd" type="Script" id=9] [sub_resource type="StyleBoxFlat" id=1] bg_color = Color( 0.847059, 0.0666667, 0.0666667, 1 ) @@ -152,6 +154,25 @@ text = "Reload" __meta__ = { "_edit_use_anchors_": false } + +[node name="Test" type="Node2D" parent="."] +visible = false +script = ExtResource( 9 ) + +[node name="TCPServer" type="Node" parent="Test"] +script = ExtResource( 8 ) +port = 6666 + +[node name="LineEdit" type="LineEdit" parent="Test"] +margin_left = 1362.49 +margin_top = 827.517 +margin_right = 1772.49 +margin_bottom = 914.517 +__meta__ = { +"_edit_use_anchors_": false +} [connection signal="pressed" from="TextEditor/SaveButton" to="." method="save_message"] [connection signal="pressed" from="NextLevelButton" to="." method="load_next_level"] [connection signal="pressed" from="ReloadButton" to="." method="reload_level"] +[connection signal="data_received" from="Test/TCPServer" to="Test" method="data"] +[connection signal="text_entered" from="Test/LineEdit" to="Test" method="send"] diff --git a/scripts/net-test b/scripts/net-test new file mode 100755 index 0000000..9efeb7b --- /dev/null +++ b/scripts/net-test @@ -0,0 +1,23 @@ +#!/usr/bin/env perl + +use IO::Socket; + +$socket = IO::Socket::INET->new(PeerAddr => "127.0.0.1", + PeerPort => 6666, + Proto => "tcp", + Type => SOCK_STREAM); + +$s = "Hey äöü!"; + +$socket->send(chr(length($s))); +$socket->send($s); + +while(true) { + my $len; + $socket->recv($len, 4); + my $actual_len = unpack("L", $len); + my $s2; + $socket->recv($s2, ord($len)); + print($s2); + STDOUT->flush(); +} diff --git a/shell.gd b/shell.gd index 3a96b22..6a8eba1 100644 --- a/shell.gd +++ b/shell.gd @@ -79,13 +79,20 @@ func run_async_thread(command): while not s.is_connection_available(): pass var c = s.take_connection() - print("ok") while c.get_status() == StreamPeerTCP.STATUS_CONNECTED: - var available = c.get_available_bytes() - if available > 0: - var data = c.get_utf8_string(available) - emit_signal("output", data) - print(data) + read_from(c) + OS.delay_msec(1000/30) + read_from(c) c.disconnect_from_host() s.stop() +func read_from(c): + var total_available = c.get_available_bytes() + print(str(total_available)+" bytes available") + while total_available > 0: + var available = min(1024, total_available) + total_available -= available + print("reading "+str(available)) + var data = c.get_utf8_string(available) + #emit_signal("output", data) + print(data.size()) diff --git a/tcp_server.gd b/tcp_server.gd new file mode 100644 index 0000000..d25517e --- /dev/null +++ b/tcp_server.gd @@ -0,0 +1,39 @@ +extends Node + +signal data_received(string) + +export var port: int + +var _s = TCP_Server.new() +var _c +var _connected = false + +func _ready(): + start() + +func start(): + _s.listen(port) + +func _process(delta): + if _s.is_connection_available(): + if _connected: + push_error("Dropping active connection") + _c = _s.take_connection() + _connected = true + print("connected!") + + if _connected: + if _c.get_status() != StreamPeerTCP.STATUS_CONNECTED: + _connected = false + print("disconnected") + var available = _c.get_available_bytes() + while available > 0: + var data = _c.get_utf8_string(available) + emit_signal("data_received", data) + available = _c.get_available_bytes() + +func send(text): + if _connected: + _c.put_utf8_string(text) + else: + push_error("Trying to send data on closed connection") diff --git a/terminal.gd b/terminal.gd index e492430..f66948d 100644 --- a/terminal.gd +++ b/terminal.gd @@ -60,8 +60,9 @@ func send_command(command): thread.start(self, "run_command_in_a_thread", command) func send_command_async(command): - repo.shell.run_async(command) + output.text += "$ "+command+"\n" input.text = "" + repo.shell.run_async(command) func run_command_in_a_thread(command): var o = repo.shell.run(command) diff --git a/test.gd b/test.gd new file mode 100644 index 0000000..da60621 --- /dev/null +++ b/test.gd @@ -0,0 +1,12 @@ +extends Node + +func _ready(): + pass + +func data(s): + print(s) + + +func send(new_text): + print("sending "+new_text) + $TCPServer.send(new_text)