Improved network handling

This commit is contained in:
Eduard Urbach 2024-01-16 00:22:14 +01:00
parent b885d70625
commit 8f0f3d9998
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
14 changed files with 178 additions and 126 deletions

View File

@ -1,27 +0,0 @@
extends Node
const PLAYER = preload("res://player/Player.tscn")
var udp := PacketPeerUDP.new()
func _ready():
udp.connect_to_host("127.0.0.1", 4242)
send_login()
func _process(_delta):
if Client.udp.get_available_packet_count() <= 0:
return
var packet := Client.udp.get_packet()
var type := packet.decode_u8(0)
print("Packet type %d data size %d" % [type, packet.size()-1])
func send_login():
var login_data = PackedByteArray()
login_data.push_back(2)
udp.put_packet(login_data)
print("Connecting...")
func spawn_player():
var player = PLAYER.instantiate()
add_child(player)

26
network/Login.gd Normal file
View File

@ -0,0 +1,26 @@
extends Node
const PLAYER = preload("res://player/Player.tscn")
func _ready():
%Network.add_handler(%Network.Packet.LOGIN, self)
send_login()
func send_login():
var buffer := StreamPeerBuffer.new()
buffer.put_8(%Network.Packet.LOGIN)
buffer.put_data("password".to_utf8_buffer())
%Network.udp.put_packet(buffer.data_array)
print("Connecting...")
func handle_packet(data: PackedByteArray):
if data[1] != 0:
print("Login failed.")
return
print("Login succeeded.", data)
spawn_player()
func spawn_player():
var player = PLAYER.instantiate()
add_child(player)

33
network/Network.gd Normal file
View File

@ -0,0 +1,33 @@
extends Node
var udp := PacketPeerUDP.new()
var handlers: Array[Node] = []
enum Packet {
PING = 1,
LOGIN = 2,
LOGOUT = 3,
MOVE = 10,
}
func _init():
handlers.resize(256)
udp.connect_to_host("127.0.0.1", 4242)
func _process(_delta):
if udp.get_available_packet_count() <= 0:
return
var packet := udp.get_packet()
var type := packet.decode_u8(0) as Packet
var handler := handlers[type]
if handler == null:
push_warning("Unknown packet type %d" % type)
return
handler.handle_packet(packet)
func add_handler(packet: Packet, node: Node):
assert(node.has_method("handle_packet"))
handlers[packet] = node

38
network/Ping.gd Normal file
View File

@ -0,0 +1,38 @@
extends Node
signal changed(ping: float)
const HISTORY_SIZE = 8
var count := 0
var history: Array[float] = []
func _init():
history.resize(HISTORY_SIZE)
func _ready():
%Network.add_handler(%Network.Packet.PING, self)
var timer := Timer.new()
add_child(timer)
timer.wait_time = 1
timer.timeout.connect(_ping)
timer.start()
func _ping():
var buffer := StreamPeerBuffer.new()
buffer.put_8(%Network.Packet.PING)
buffer.put_8(count)
%Network.udp.put_packet(buffer.data_array)
history[count] = Time.get_unix_time_from_system()
count += 1
if count >= HISTORY_SIZE:
count = 0
func handle_packet(data: PackedByteArray):
print("Handled ping!", data)
var id := data.decode_u8(1)
var ping := Time.get_unix_time_from_system() - history[id]
changed.emit(ping)

View File

@ -15,10 +15,6 @@ run/main_scene="res://world/Game.tscn"
config/features=PackedStringArray("4.2", "Forward Plus")
config/icon="res://ui/icon.svg"
[autoload]
Client="*res://client/Client.gd"
[display]
window/vsync/vsync_mode=0

View File

@ -1,6 +1,7 @@
package main
import (
"bytes"
"fmt"
"server/core"
"server/packet"
@ -9,5 +10,12 @@ import (
// login checks the account credentials and gives a network peer access to an account.
func login(data []byte, client *core.Client) {
fmt.Println("2 - login!")
server.Send(packet.LOGIN, nil, client)
if bytes.Equal(data, []byte("password")) {
fmt.Println("login success")
server.Send(packet.LOGIN, []byte{0}, client)
} else {
fmt.Println("login failure")
server.Send(packet.LOGIN, []byte{1}, client)
}
}

View File

@ -8,7 +8,6 @@ import (
// ping is used as a heartbeat and latency check.
func ping(data []byte, client *core.Client) {
fmt.Println("1 - ping!")
fmt.Println("1 - ping")
server.Send(packet.PING, data, client)
fmt.Println(server.Count(), "clients")
}

View File

@ -1,8 +1,5 @@
extends Label
func _ready():
DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_DISABLED)
func _process(_delta):
var fps = Engine.get_frames_per_second()
text = str(fps)

View File

@ -1,37 +0,0 @@
extends Label
const HISTORY_SIZE = 8
var pingCount := 0
var pingSent: Array[float] = []
func _ready():
var timer := Timer.new()
add_child(timer)
timer.wait_time = 1
timer.connect("timeout", self._ping)
timer.start()
pingSent.resize(HISTORY_SIZE)
func _process(_delta):
pass
#if Client.udp.get_available_packet_count() > 0:
#var bytes := Client.udp.get_packet()
#var count := bytes.decode_u8(1)
#var timeSent := pingSent[count]
#var duration := Time.get_unix_time_from_system() - timeSent
#var ping := duration * 1000
#text = str(snapped(ping, 0.01))
func _ping():
var buffer := StreamPeerBuffer.new()
buffer.put_8(1)
buffer.put_8(pingCount)
Client.udp.put_packet(buffer.data_array)
pingSent[pingCount] = Time.get_unix_time_from_system()
pingCount += 1
if pingCount >= HISTORY_SIZE:
pingCount = 0

7
ui/PingLabel.gd Normal file
View File

@ -0,0 +1,7 @@
extends Label
func _ready():
%Ping.connect("changed", on_ping_changed)
func on_ping_changed(ping):
text = str(snapped(ping * 1000, 0.01))

View File

@ -1,41 +0,0 @@
[gd_scene load_steps=3 format=3 uid="uid://bxotvk73tbgw0"]
[ext_resource type="Script" path="res://ui/FPS.gd" id="1_128dk"]
[ext_resource type="Script" path="res://ui/Ping.gd" id="2_m7fhx"]
[node name="UI" type="Control"]
layout_mode = 3
anchors_preset = 0
[node name="CanvasLayer" type="CanvasLayer" parent="."]
[node name="MarginContainer" type="MarginContainer" parent="CanvasLayer"]
offset_right = 40.0
offset_bottom = 50.0
[node name="VBoxContainer" type="VBoxContainer" parent="CanvasLayer/MarginContainer"]
layout_mode = 2
[node name="HBoxContainer" type="HBoxContainer" parent="CanvasLayer/MarginContainer/VBoxContainer"]
layout_mode = 2
[node name="FPSLabel" type="Label" parent="CanvasLayer/MarginContainer/VBoxContainer/HBoxContainer"]
layout_mode = 2
text = "FPS:"
[node name="FPS" type="Label" parent="CanvasLayer/MarginContainer/VBoxContainer/HBoxContainer"]
layout_mode = 2
text = "0"
script = ExtResource("1_128dk")
[node name="HBoxContainer2" type="HBoxContainer" parent="CanvasLayer/MarginContainer/VBoxContainer"]
layout_mode = 2
[node name="PingLabel" type="Label" parent="CanvasLayer/MarginContainer/VBoxContainer/HBoxContainer2"]
layout_mode = 2
text = "Ping:"
[node name="Ping" type="Label" parent="CanvasLayer/MarginContainer/VBoxContainer/HBoxContainer2"]
layout_mode = 2
text = "0"
script = ExtResource("2_m7fhx")

View File

@ -7,4 +7,4 @@ ambient_light_source = 3
ambient_light_color = Color(0.607843, 0.756863, 0.92549, 1)
ambient_light_sky_contribution = 0.17
tonemap_mode = 2
adjustment_saturation = 0.01
ssao_intensity = 16.0

View File

@ -10,12 +10,15 @@ func _ready():
func _input(event):
if event.is_action_pressed("toggle_fullscreen"):
var mode = DisplayServer.window_get_mode()
toggle_fullscreen()
match mode:
DisplayServer.WINDOW_MODE_FULLSCREEN:
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
_:
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
func toggle_fullscreen():
var mode = DisplayServer.window_get_mode()
return
match mode:
DisplayServer.WINDOW_MODE_FULLSCREEN:
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
_:
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
return

View File

@ -1,12 +1,16 @@
[gd_scene load_steps=16 format=3 uid="uid://b40y7iuskv1ar"]
[gd_scene load_steps=20 format=3 uid="uid://b40y7iuskv1ar"]
[ext_resource type="Script" path="res://world/Game.gd" id="1_xmqq4"]
[ext_resource type="PackedScene" uid="uid://bxotvk73tbgw0" path="res://ui/UI.tscn" id="2_x1l7l"]
[ext_resource type="Script" path="res://world/RotateY.gd" id="3_4gn6n"]
[ext_resource type="Script" path="res://ui/FPSLabel.gd" id="3_k5d80"]
[ext_resource type="Script" path="res://ui/PingLabel.gd" id="4_1a3hc"]
[ext_resource type="Script" path="res://network/Network.gd" id="4_ao4cj"]
[ext_resource type="Shader" path="res://world/shader/Outline.gdshader" id="4_gweie"]
[ext_resource type="Script" path="res://network/Ping.gd" id="4_vx388"]
[ext_resource type="Environment" uid="uid://dixa0yso2s1u3" path="res://world/Environment.tres" id="5_bll74"]
[ext_resource type="Script" path="res://world/Sun.gd" id="5_pf5uw"]
[ext_resource type="CameraAttributesPractical" uid="uid://b835orxyqq6w5" path="res://world/CameraAttributes.tres" id="6_8wfwf"]
[ext_resource type="Script" path="res://network/Login.gd" id="6_augbg"]
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_r8n03"]
diffuse_mode = 3
@ -67,7 +71,53 @@ mesh = SubResource("TorusMesh_mu45b")
skeleton = NodePath("../../SubViewportContainer/SubViewport")
script = ExtResource("3_4gn6n")
[node name="UI" parent="." instance=ExtResource("2_x1l7l")]
[node name="UI" type="Control" parent="."]
layout_mode = 3
anchors_preset = 0
[node name="CanvasLayer" type="CanvasLayer" parent="UI"]
[node name="MarginContainer" type="MarginContainer" parent="UI/CanvasLayer"]
offset_right = 40.0
offset_bottom = 50.0
[node name="VBoxContainer" type="VBoxContainer" parent="UI/CanvasLayer/MarginContainer"]
layout_mode = 2
[node name="HBoxContainer" type="HBoxContainer" parent="UI/CanvasLayer/MarginContainer/VBoxContainer"]
layout_mode = 2
[node name="FPSLabel" type="Label" parent="UI/CanvasLayer/MarginContainer/VBoxContainer/HBoxContainer"]
layout_mode = 2
text = "FPS:"
[node name="FPS" type="Label" parent="UI/CanvasLayer/MarginContainer/VBoxContainer/HBoxContainer"]
layout_mode = 2
text = "0"
script = ExtResource("3_k5d80")
[node name="HBoxContainer2" type="HBoxContainer" parent="UI/CanvasLayer/MarginContainer/VBoxContainer"]
layout_mode = 2
[node name="PingLabel" type="Label" parent="UI/CanvasLayer/MarginContainer/VBoxContainer/HBoxContainer2"]
layout_mode = 2
text = "Ping:"
[node name="Ping" type="Label" parent="UI/CanvasLayer/MarginContainer/VBoxContainer/HBoxContainer2"]
layout_mode = 2
text = "0"
script = ExtResource("4_1a3hc")
[node name="Network" type="Node" parent="."]
unique_name_in_owner = true
script = ExtResource("4_ao4cj")
[node name="Ping" type="Node" parent="Network"]
unique_name_in_owner = true
script = ExtResource("4_vx388")
[node name="Login" type="Node" parent="Network"]
script = ExtResource("6_augbg")
[node name="SubViewportContainer" type="SubViewportContainer" parent="."]
texture_filter = 1