Added Godot based server
This commit is contained in:
@ -1,26 +0,0 @@
|
||||
extends Node
|
||||
|
||||
var udp := PacketPeerUDP.new()
|
||||
var handlers: Array[Node] = []
|
||||
|
||||
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)
|
||||
var handler := handlers[type]
|
||||
|
||||
if handler == null:
|
||||
push_warning("Unknown packet type %d" % type)
|
||||
return
|
||||
|
||||
handler.handle_packet(packet.slice(1))
|
||||
|
||||
func add_handler(packet: int, node: Node):
|
||||
assert(node.has_method("handle_packet"))
|
||||
handlers[packet] = node
|
23
network/NetworkNode.gd
Normal file
23
network/NetworkNode.gd
Normal file
@ -0,0 +1,23 @@
|
||||
class_name NetworkNode
|
||||
extends Node
|
||||
|
||||
var handlers: Array[Node] = []
|
||||
|
||||
func _init():
|
||||
handlers.resize(256)
|
||||
|
||||
func get_handler(index: int) -> PacketHandler:
|
||||
return handlers[index]
|
||||
|
||||
func set_handler(index: int, node: PacketHandler):
|
||||
handlers[index] = node
|
||||
|
||||
func handle_packet(packet: PackedByteArray, peer: PacketPeer):
|
||||
var type := packet.decode_u8(0)
|
||||
var handler := get_handler(type)
|
||||
|
||||
if handler == null:
|
||||
push_warning("Unknown packet type %d" % type)
|
||||
return
|
||||
|
||||
handler.handle_packet(packet.slice(1), peer)
|
@ -1,4 +1,5 @@
|
||||
class_name Packet
|
||||
|
||||
enum {
|
||||
PING = 1,
|
||||
LOGIN = 2,
|
||||
|
5
network/PacketHandler.gd
Normal file
5
network/PacketHandler.gd
Normal file
@ -0,0 +1,5 @@
|
||||
class_name PacketHandler
|
||||
extends Node
|
||||
|
||||
func handle_packet(_data: PackedByteArray, _peer: PacketPeer):
|
||||
pass
|
15
network/client/Client.gd
Normal file
15
network/client/Client.gd
Normal file
@ -0,0 +1,15 @@
|
||||
extends NetworkNode
|
||||
|
||||
@export var ip := "127.0.0.1"
|
||||
@export var port := 4242
|
||||
|
||||
var socket := PacketPeerUDP.new()
|
||||
|
||||
func _init():
|
||||
super._init()
|
||||
socket.connect_to_host(ip, port)
|
||||
|
||||
func _process(_delta):
|
||||
while socket.get_available_packet_count() > 0:
|
||||
var packet := socket.get_packet()
|
||||
handle_packet(packet, socket)
|
@ -1,11 +1,11 @@
|
||||
extends Node
|
||||
extends PacketHandler
|
||||
|
||||
@export var playerScene: PackedScene
|
||||
|
||||
var logged_in := false
|
||||
|
||||
func _ready():
|
||||
%Network.add_handler(Packet.LOGIN, self)
|
||||
%Client.set_handler(Packet.LOGIN, self)
|
||||
send_login()
|
||||
|
||||
func send_login():
|
||||
@ -14,16 +14,16 @@ func send_login():
|
||||
|
||||
var buffer := StreamPeerBuffer.new()
|
||||
buffer.put_8(Packet.LOGIN)
|
||||
buffer.put_data("password".to_utf8_buffer())
|
||||
%Network.udp.put_packet(buffer.data_array)
|
||||
print("Connecting...")
|
||||
buffer.put_data(JSON.stringify(["username", "password"]).to_utf8_buffer())
|
||||
%Client.socket.put_packet(buffer.data_array)
|
||||
print("[Client] Connecting...")
|
||||
|
||||
func handle_packet(data: PackedByteArray):
|
||||
func handle_packet(data: PackedByteArray, _peer: PacketPeer):
|
||||
if data[0] != 0:
|
||||
print("Login failed.")
|
||||
print("[Client] Login failed.")
|
||||
return
|
||||
|
||||
print("Login succeeded.")
|
||||
print("[Client] Login succeeded.")
|
||||
logged_in = true
|
||||
Global.player = spawn_player()
|
||||
|
@ -1,4 +1,4 @@
|
||||
extends Node
|
||||
extends PacketHandler
|
||||
|
||||
signal changed(ping: float)
|
||||
|
||||
@ -11,13 +11,13 @@ func _init():
|
||||
history.resize(HISTORY_SIZE)
|
||||
|
||||
func _ready():
|
||||
%Network.add_handler(Packet.PING, self)
|
||||
%Client.set_handler(Packet.PING, self)
|
||||
|
||||
func send_ping():
|
||||
var buffer := StreamPeerBuffer.new()
|
||||
buffer.put_8(Packet.PING)
|
||||
buffer.put_8(count)
|
||||
%Network.udp.put_packet(buffer.data_array)
|
||||
%Client.socket.put_packet(buffer.data_array)
|
||||
|
||||
history[count] = Time.get_unix_time_from_system()
|
||||
count += 1
|
||||
@ -25,8 +25,8 @@ func send_ping():
|
||||
if count >= HISTORY_SIZE:
|
||||
count = 0
|
||||
|
||||
func handle_packet(data: PackedByteArray):
|
||||
var id := data.decode_u8(0)
|
||||
func handle_packet(data: PackedByteArray, _peer: PacketPeer):
|
||||
var id := data[0]
|
||||
var ping := Time.get_unix_time_from_system() - history[id]
|
||||
changed.emit(ping)
|
||||
|
35
network/server/Login.gd
Normal file
35
network/server/Login.gd
Normal file
@ -0,0 +1,35 @@
|
||||
extends PacketHandler
|
||||
|
||||
enum {
|
||||
SUCCESS = 0,
|
||||
FAIL = 1,
|
||||
}
|
||||
|
||||
func _ready():
|
||||
%Server.set_handler(Packet.LOGIN, self)
|
||||
|
||||
func handle_packet(data: PackedByteArray, peer: PacketPeer):
|
||||
var data_string = data.get_string_from_utf8()
|
||||
var login_request = JSON.parse_string(data_string)
|
||||
|
||||
if login_request.size() < 2:
|
||||
fail_login(peer)
|
||||
return
|
||||
|
||||
var username = login_request[0]
|
||||
var password = login_request[1]
|
||||
|
||||
if username != "username" || password != "password":
|
||||
fail_login(peer)
|
||||
return
|
||||
|
||||
var buffer := StreamPeerBuffer.new()
|
||||
buffer.put_8(Packet.LOGIN)
|
||||
buffer.put_8(SUCCESS)
|
||||
peer.put_packet(buffer.data_array)
|
||||
|
||||
func fail_login(peer: PacketPeer):
|
||||
var buffer := StreamPeerBuffer.new()
|
||||
buffer.put_8(Packet.LOGIN)
|
||||
buffer.put_8(FAIL)
|
||||
peer.put_packet(buffer.data_array)
|
20
network/server/Ping.gd
Normal file
20
network/server/Ping.gd
Normal file
@ -0,0 +1,20 @@
|
||||
extends PacketHandler
|
||||
|
||||
func _ready():
|
||||
%Server.set_handler(Packet.PING, self)
|
||||
|
||||
func handle_packet(data: PackedByteArray, peer: PacketPeer):
|
||||
# var response := PackedByteArray()
|
||||
# response.resize(2)
|
||||
# response.encode_u8(0, Packet.PING)
|
||||
# response.encode_u8(1, data[0])
|
||||
# peer.put_packet(response)
|
||||
|
||||
var buffer := StreamPeerBuffer.new()
|
||||
buffer.put_8(Packet.PING)
|
||||
|
||||
if data.size() > 0:
|
||||
buffer.put_8(data[0])
|
||||
|
||||
peer.put_packet(buffer.data_array)
|
||||
|
29
network/server/Server.gd
Normal file
29
network/server/Server.gd
Normal file
@ -0,0 +1,29 @@
|
||||
extends NetworkNode
|
||||
|
||||
@export var port := 4242
|
||||
@export var max_pending_connections := 4096
|
||||
|
||||
var server := UDPServer.new()
|
||||
var last_statistics := Time.get_ticks_msec()
|
||||
var packet_count := 0
|
||||
|
||||
func _init():
|
||||
super._init()
|
||||
server.set_max_pending_connections(max_pending_connections)
|
||||
server.listen(port)
|
||||
|
||||
func _process(_delta):
|
||||
server.poll()
|
||||
|
||||
while server.is_connection_available():
|
||||
var peer: PacketPeerUDP = server.take_connection()
|
||||
|
||||
while peer.get_available_packet_count() > 0:
|
||||
var packet = peer.get_packet()
|
||||
handle_packet(packet, peer)
|
||||
packet_count += 1
|
||||
|
||||
if Time.get_ticks_msec() > last_statistics + 1000:
|
||||
print("[Server] %d packets per second" % packet_count)
|
||||
packet_count = 0
|
||||
last_statistics = Time.get_ticks_msec()
|
Reference in New Issue
Block a user