diff --git a/client/network/Client.gd b/client/network/Client.gd index 5dd4c2d..4cbe7b4 100644 --- a/client/network/Client.gd +++ b/client/network/Client.gd @@ -3,7 +3,11 @@ extends NetworkNode @export var host: String @export var port: int +signal download_changed(down: int) +signal upload_changed(up: int) + var socket := PacketPeerUDP.new() +var upload := 0 func _enter_tree(): socket.connect_to_host(host, port) @@ -17,4 +21,14 @@ func _enter_tree(): func _process(_delta): while socket.get_available_packet_count() > 0: var packet := socket.get_packet() - handle_packet(packet, socket) \ No newline at end of file + handle_packet(packet, socket) + +func update_statistics(): + download_changed.emit(download) + upload_changed.emit(upload) + download = 0 + upload = 0 + +func send(data: PackedByteArray): + socket.put_packet(data) + upload += data.size() \ No newline at end of file diff --git a/client/network/Login.gd b/client/network/Login.gd index 8f53c09..3bcc687 100644 --- a/client/network/Login.gd +++ b/client/network/Login.gd @@ -32,7 +32,7 @@ func send_login(): var buffer := StreamPeerBuffer.new() buffer.put_8(Packet.LOGIN) buffer.put_data(JSON.stringify([Global.username, password]).to_utf8_buffer()) - %Client.socket.put_packet(buffer.data_array) + %Client.send(buffer.data_array) print("[%s] Connecting..." % Global.username) func is_logged_in() -> bool: diff --git a/client/network/Ping.gd b/client/network/Ping.gd index f75a7d8..d1ebc80 100644 --- a/client/network/Ping.gd +++ b/client/network/Ping.gd @@ -19,7 +19,7 @@ func send_ping(): var buffer := StreamPeerBuffer.new() buffer.put_8(Packet.PING) buffer.put_8(count) - %Client.socket.put_packet(buffer.data_array) + %Client.send(buffer.data_array) history[count] = get_time() count += 1 diff --git a/client/network/PlayerMove.gd b/client/network/PlayerMove.gd index 5075c26..2975c74 100644 --- a/client/network/PlayerMove.gd +++ b/client/network/PlayerMove.gd @@ -40,4 +40,4 @@ func send_position(): buffer.put_float(Global.player.position.x) # buffer.put_float(Global.player.position.y) buffer.put_float(Global.player.position.z) - %Client.socket.put_packet(buffer.data_array) + %Client.send(buffer.data_array) diff --git a/client/network/shared/NetworkNode.gd b/client/network/shared/NetworkNode.gd index bdba115..581d393 100644 --- a/client/network/shared/NetworkNode.gd +++ b/client/network/shared/NetworkNode.gd @@ -2,6 +2,7 @@ class_name NetworkNode extends Node var handlers: Array[Node] = [] +var download := 0 func _init(): handlers.resize(256) @@ -21,3 +22,4 @@ func handle_packet(packet: PackedByteArray, peer: PacketPeer): return handler.handle_packet(packet.slice(1), peer) + download += packet.size() diff --git a/client/ui/UI.gd b/client/ui/UI.gd index 6521bef..ccb08ad 100644 --- a/client/ui/UI.gd +++ b/client/ui/UI.gd @@ -1,6 +1,10 @@ extends Control var on_ping_changed: Signal +var on_download_changed: Signal +var on_upload_changed: Signal func _enter_tree(): - on_ping_changed = %Ping.changed \ No newline at end of file + on_ping_changed = %Ping.changed + on_download_changed = %Client.download_changed + on_upload_changed = %Client.upload_changed diff --git a/client/ui/UI.tscn b/client/ui/UI.tscn index 70ea3d9..f988407 100644 --- a/client/ui/UI.tscn +++ b/client/ui/UI.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=7 format=3 uid="uid://dagn5bf7ou3sd"] +[gd_scene load_steps=9 format=3 uid="uid://dagn5bf7ou3sd"] [ext_resource type="PackedScene" uid="uid://cch67vqpsmtej" path="res://ui/debug/DebugLabel.tscn" id="1_7s8uu"] [ext_resource type="Script" path="res://ui/UI.gd" id="1_l5b6o"] @@ -6,6 +6,8 @@ [ext_resource type="Script" path="res://ui/debug/PingLabel.gd" id="3_xjdws"] [ext_resource type="Script" path="res://ui/debug/PositionLabel.gd" id="4_beqf6"] [ext_resource type="Script" path="res://ui/debug/VelocityLabel.gd" id="5_8lm6a"] +[ext_resource type="Script" path="res://ui/debug/UploadLabel.gd" id="7_cfnpx"] +[ext_resource type="Script" path="res://ui/debug/DownloadLabel.gd" id="8_ogt38"] [node name="UI" type="Control"] layout_mode = 3 @@ -47,3 +49,11 @@ script = ExtResource("4_beqf6") [node name="Velocity" parent="CanvasLayer/BottomLeftMargin/VBoxContainer" instance=ExtResource("1_7s8uu")] layout_mode = 2 script = ExtResource("5_8lm6a") + +[node name="Send" parent="CanvasLayer/BottomLeftMargin/VBoxContainer" instance=ExtResource("1_7s8uu")] +layout_mode = 2 +script = ExtResource("7_cfnpx") + +[node name="Receive" parent="CanvasLayer/BottomLeftMargin/VBoxContainer" instance=ExtResource("1_7s8uu")] +layout_mode = 2 +script = ExtResource("8_ogt38") diff --git a/client/ui/debug/DownloadLabel.gd b/client/ui/debug/DownloadLabel.gd new file mode 100644 index 0000000..2914023 --- /dev/null +++ b/client/ui/debug/DownloadLabel.gd @@ -0,0 +1,7 @@ +extends DebugLabel + +func _ready(): + owner.on_download_changed.connect(on_download_changed) + +func on_download_changed(download): + text = "%d bytes" % download diff --git a/client/ui/debug/UploadLabel.gd b/client/ui/debug/UploadLabel.gd new file mode 100644 index 0000000..87579fb --- /dev/null +++ b/client/ui/debug/UploadLabel.gd @@ -0,0 +1,7 @@ +extends DebugLabel + +func _ready(): + owner.on_upload_changed.connect(on_upload_changed) + +func on_upload_changed(upload): + text = "%d bytes" % upload diff --git a/client/world/Client.tscn b/client/world/Client.tscn index ba4f471..58e32c8 100644 --- a/client/world/Client.tscn +++ b/client/world/Client.tscn @@ -57,6 +57,9 @@ packet_type = 10 script = ExtResource("7_rjgcp") packet_type = 12 +[node name="Statistics" type="Timer" parent="Client"] +autostart = true + [node name="World" type="Node3D" parent="."] [node name="Sun" type="DirectionalLight3D" parent="World"] @@ -130,3 +133,4 @@ mesh = SubResource("QuadMesh_7yiqd") [connection signal="timeout" from="Client/Ping/Timer" to="Client/Ping" method="send_ping"] [connection signal="timeout" from="Client/Login/Timer" to="Client/Login" method="send_login"] +[connection signal="timeout" from="Client/Statistics" to="Client" method="update_statistics"]