This repository has been archived on 2023-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
defend-together/client/scripts/network/NetworkManager.gd

130 lines
3.5 KiB
GDScript3
Raw Normal View History

2020-05-01 01:16:46 +02:00
extends Node
2020-05-02 22:19:46 +02:00
var server_host : String = "127.0.0.1"
2020-05-01 01:16:46 +02:00
const SERVER_PORT : int = 7777
signal disconnection
signal connection_established
signal error_occured
signal logged_in
signal world_data_recieved
2020-05-04 01:35:58 +02:00
signal chat_message_recieved
2020-05-01 01:16:46 +02:00
var username : String = ""
var client : GDNetHost = null
var peer : GDNetPeer = null
var server_address : GDNetAddress = null
var connected : bool = false
var packetQueue = []
var error_info = ""
var world_data : String = ""
var connection_timer : Timer
func _init():
server_address = GDNetAddress.new()
client = GDNetHost.new()
2020-05-04 01:35:58 +02:00
client.set_max_channels(2)
2020-05-01 01:16:46 +02:00
client.set_max_peers(1)
client.set_event_wait(250)
client.bind()
func connect_to_server():
2020-05-02 22:36:51 +02:00
if peer:
peer.disconnect_now()
2020-05-02 22:19:46 +02:00
server_address.set_host(server_host)
server_address.set_port(SERVER_PORT)
2020-05-01 01:16:46 +02:00
peer = client.host_connect(server_address)
if not connection_timer:
connection_timer = Timer.new()
add_child(connection_timer)
connection_timer.connect("timeout", self, "timeout_check")
connection_timer.start(1)
func timeout_check():
if username == "":
display_error("Connection failed!")
connection_timer.stop()
func request_world_map():
var request_packet : PoolByteArray = "2|".to_ascii()
2020-05-04 01:35:58 +02:00
send_packet(request_packet)
2020-05-01 01:16:46 +02:00
func connect_as_user(username : String):
connect_to_server()
var username_packet : PoolByteArray = ("1|" + username).to_ascii()
2020-05-04 01:35:58 +02:00
send_packet(username_packet)
2020-05-01 01:16:46 +02:00
func display_error(error = "Unknown error occured!"):
error_info = error
print("Error: " + error)
emit_signal("error_occured")
func disconnect_from_server():
peer.disconnect_later()
func process_events():
if(client.is_event_available()):
var event : GDNetEvent = client.get_event()
var event_type = event.get_event_type()
if(event_type == GDNetEvent.RECEIVE):
2020-05-04 01:35:58 +02:00
if event.get_channel_id() == 0:
var ascii_data : String = str(event.get_packet().get_string_from_ascii())
if len(ascii_data) > 0:
if ascii_data[0] == '1':
if ascii_data.substr(2,2) == "OK":
username = ascii_data.substr(4)
print("Logged in as: " + username)
emit_signal("logged_in")
else:
display_error("Username not accepted! Reason: " + ascii_data.substr(2))
elif ascii_data[0] == '2':
world_data = ascii_data.substr(2)
emit_signal("world_data_recieved")
elif event.get_channel_id() == 1:
var chat_message : String = str(event.get_packet().get_string_from_ascii())
emit_signal("chat_message_recieved", chat_message)
2020-05-01 01:16:46 +02:00
elif(event_type == GDNetEvent.CONNECT):
print("Connected to server with hostname: " + server_address.get_host() + ":" + str(server_address.get_port()))
connected = true
emit_signal("connection_established")
elif(event_type == GDNetEvent.DISCONNECT):
print("Disconnected from server")
connected = false
emit_signal("disconnection")
2020-05-04 01:35:58 +02:00
func send_chat_message(msg : String):
var pckt : PoolByteArray = (msg + '\n').to_ascii()
send_packet(pckt, 1)
func send_packet(packet : PoolByteArray, channel = 0):
packetQueue.append({'channel':channel, 'packet' : packet})
2020-05-01 01:16:46 +02:00
func move_player(x,y):
var pckt : PoolByteArray = ("3|" + str(x) + "," + str(y)).to_ascii()
2020-05-04 01:35:58 +02:00
send_packet(pckt)
2020-05-01 01:16:46 +02:00
func _process(delta):
process_events()
if len(packetQueue) > 0 and connected:
2020-05-04 01:35:58 +02:00
peer.send_packet(packetQueue[0]['packet'], packetQueue[0]['channel'], GDNetMessage.RELIABLE)
2020-05-01 01:16:46 +02:00
packetQueue.remove(0)
if(Input.is_action_just_pressed("ui_cancel")):
print("PACKET")
peer.send_packet("TEST".to_ascii(), 0, GDNetMessage.RELIABLE)