Improved connection handling
This commit is contained in:
parent
1f73d22cd6
commit
b01ae8543b
6
network/Account.gd
Normal file
6
network/Account.gd
Normal file
@ -0,0 +1,6 @@
|
||||
class_name Account
|
||||
|
||||
var password: String
|
||||
|
||||
func _init(pw: String):
|
||||
password = pw
|
@ -14,7 +14,7 @@ func send_login():
|
||||
|
||||
var buffer := StreamPeerBuffer.new()
|
||||
buffer.put_8(Packet.LOGIN)
|
||||
buffer.put_data(JSON.stringify(["username", "password"]).to_utf8_buffer())
|
||||
buffer.put_data(JSON.stringify(["user1", "password"]).to_utf8_buffer())
|
||||
%Client.socket.put_packet(buffer.data_array)
|
||||
print("[Client] Connecting...")
|
||||
|
||||
|
4
network/server/Connection.gd
Normal file
4
network/server/Connection.gd
Normal file
@ -0,0 +1,4 @@
|
||||
class_name Connection
|
||||
|
||||
var peer: PacketPeerUDP
|
||||
var last_packet: int
|
@ -5,6 +5,11 @@ enum {
|
||||
FAIL = 1,
|
||||
}
|
||||
|
||||
var accounts := {
|
||||
"user1": Account.new("password"),
|
||||
"user2": Account.new("password"),
|
||||
}
|
||||
|
||||
func _ready():
|
||||
%Server.set_handler(Packet.LOGIN, self)
|
||||
|
||||
@ -19,7 +24,11 @@ func handle_packet(data: PackedByteArray, peer: PacketPeer):
|
||||
var username = login_request[0]
|
||||
var password = login_request[1]
|
||||
|
||||
if username != "username" || password != "password":
|
||||
if !accounts.has(username):
|
||||
fail_login(peer)
|
||||
return
|
||||
|
||||
if accounts[username].password != password:
|
||||
fail_login(peer)
|
||||
return
|
||||
|
||||
|
@ -4,12 +4,6 @@ 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)
|
||||
|
||||
|
@ -1,11 +1,19 @@
|
||||
extends NetworkNode
|
||||
|
||||
## Port number.
|
||||
@export var port := 4242
|
||||
|
||||
## Timeout in milliseconds.
|
||||
@export var timeout := 5000
|
||||
|
||||
## Maximum number of pending connections.
|
||||
@export var max_pending_connections := 4096
|
||||
|
||||
var server := UDPServer.new()
|
||||
var last_statistics := Time.get_ticks_msec()
|
||||
var clients := {}
|
||||
var packet_count := 0
|
||||
var now := 0
|
||||
|
||||
func _init():
|
||||
super._init()
|
||||
@ -14,16 +22,44 @@ func _init():
|
||||
|
||||
func _process(_delta):
|
||||
server.poll()
|
||||
now = Time.get_ticks_msec()
|
||||
|
||||
while server.is_connection_available():
|
||||
var peer: PacketPeerUDP = server.take_connection()
|
||||
var address := "%s:%d" % [peer.get_packet_ip(), peer.get_packet_port()]
|
||||
|
||||
if !clients.has(address):
|
||||
var connection = Connection.new()
|
||||
connection.peer = peer
|
||||
clients[address] = connection
|
||||
peer_connected(address)
|
||||
|
||||
clients[address].last_packet = now
|
||||
|
||||
for key in clients:
|
||||
var client = clients[key]
|
||||
var peer = client.peer
|
||||
|
||||
while peer.get_available_packet_count() > 0:
|
||||
var packet = peer.get_packet()
|
||||
handle_packet(packet, peer)
|
||||
client.last_packet = now
|
||||
packet_count += 1
|
||||
|
||||
if Time.get_ticks_msec() > last_statistics + 1000:
|
||||
print("[Server] %d packets per second" % packet_count)
|
||||
if now > last_statistics + 1000:
|
||||
for address in clients.keys():
|
||||
var last_packet_time = clients[address].last_packet
|
||||
|
||||
if now - last_packet_time > timeout:
|
||||
peer_disconnected(address)
|
||||
clients.erase(address)
|
||||
|
||||
print("[Server] %d clients | %d packets" % [clients.size(), packet_count])
|
||||
packet_count = 0
|
||||
last_statistics = Time.get_ticks_msec()
|
||||
|
||||
func peer_connected(_address: String):
|
||||
pass
|
||||
|
||||
func peer_disconnected(address: String):
|
||||
print("TIMEOUT ", address)
|
||||
|
@ -14,7 +14,8 @@ const (
|
||||
|
||||
var (
|
||||
numClients = flag.Int("c", 10, "number of clients")
|
||||
message = []byte{1}
|
||||
sleepTime = flag.Duration("s", time.Second, "sleep time for each client")
|
||||
message = []byte{1, 0}
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -39,11 +40,13 @@ func udpClient(wg *sync.WaitGroup) {
|
||||
|
||||
for {
|
||||
_, err := conn.Write(message)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Error sending message:", err)
|
||||
return
|
||||
}
|
||||
time.Sleep(50 * time.Millisecond) // Adjust the sleep duration as needed
|
||||
|
||||
time.Sleep(*sleepTime)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user