Experimental TCP Server node

This commit is contained in:
Sebastian Morr 2020-09-15 09:30:19 +02:00
parent 148b1be477
commit 7df489baf8
6 changed files with 111 additions and 8 deletions

View file

@ -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://terminal.tscn" type="PackedScene" id=1]
[ext_resource path="res://main.gd" type="Script" id=2] [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://fonts/default.tres" type="DynamicFont" id=5]
[ext_resource path="res://text_editor.gd" type="Script" id=6] [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://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] [sub_resource type="StyleBoxFlat" id=1]
bg_color = Color( 0.847059, 0.0666667, 0.0666667, 1 ) bg_color = Color( 0.847059, 0.0666667, 0.0666667, 1 )
@ -152,6 +154,25 @@ text = "Reload"
__meta__ = { __meta__ = {
"_edit_use_anchors_": false "_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="TextEditor/SaveButton" to="." method="save_message"]
[connection signal="pressed" from="NextLevelButton" to="." method="load_next_level"] [connection signal="pressed" from="NextLevelButton" to="." method="load_next_level"]
[connection signal="pressed" from="ReloadButton" to="." method="reload_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"]

23
scripts/net-test Executable file
View file

@ -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();
}

View file

@ -79,13 +79,20 @@ func run_async_thread(command):
while not s.is_connection_available(): while not s.is_connection_available():
pass pass
var c = s.take_connection() var c = s.take_connection()
print("ok")
while c.get_status() == StreamPeerTCP.STATUS_CONNECTED: while c.get_status() == StreamPeerTCP.STATUS_CONNECTED:
var available = c.get_available_bytes() read_from(c)
if available > 0: OS.delay_msec(1000/30)
var data = c.get_utf8_string(available) read_from(c)
emit_signal("output", data)
print(data)
c.disconnect_from_host() c.disconnect_from_host()
s.stop() 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())

39
tcp_server.gd Normal file
View file

@ -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")

View file

@ -60,8 +60,9 @@ func send_command(command):
thread.start(self, "run_command_in_a_thread", command) thread.start(self, "run_command_in_a_thread", command)
func send_command_async(command): func send_command_async(command):
repo.shell.run_async(command) output.text += "$ "+command+"\n"
input.text = "" input.text = ""
repo.shell.run_async(command)
func run_command_in_a_thread(command): func run_command_in_a_thread(command):
var o = repo.shell.run(command) var o = repo.shell.run(command)

12
test.gd Normal file
View file

@ -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)