Serverless scaling and work to authorizer
This commit is contained in:
		@ -24,6 +24,7 @@ config/icon="res://icon.png"
 | 
			
		||||
MusicManager="*res://nodes/MusicManager.tscn"
 | 
			
		||||
NetworkManager="*res://nodes/NetworkManager.tscn"
 | 
			
		||||
ImportantEntities="*res://scripts/singletons/ImportantEntities.gd"
 | 
			
		||||
Authorizer="*res://scripts/network/Authorizer.gd"
 | 
			
		||||
 | 
			
		||||
[input]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										61
									
								
								client/scripts/network/Authorizer.gd
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								client/scripts/network/Authorizer.gd
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,61 @@
 | 
			
		||||
extends Node
 | 
			
		||||
 | 
			
		||||
signal auth_connected
 | 
			
		||||
signal auth_disconnected
 | 
			
		||||
 | 
			
		||||
var client : StreamPeerTCP = null
 | 
			
		||||
var server_hostname : String = "127.0.0.1"
 | 
			
		||||
var server_port = 7778
 | 
			
		||||
 | 
			
		||||
func _ready():
 | 
			
		||||
	client = StreamPeerTCP.new()
 | 
			
		||||
	client.set_no_delay(true)
 | 
			
		||||
	set_process(false)
 | 
			
		||||
	
 | 
			
		||||
func auth_connect(host=server_hostname, port=server_port):
 | 
			
		||||
	
 | 
			
		||||
	# Connect if not connected
 | 
			
		||||
	if !client.is_connected_to_host():
 | 
			
		||||
		server_hostname = host
 | 
			
		||||
		server_port = port
 | 
			
		||||
 | 
			
		||||
		# Connect Socket & Create Stream
 | 
			
		||||
		client.connect_to_host(server_hostname, port)
 | 
			
		||||
		
 | 
			
		||||
		# Start listening
 | 
			
		||||
		set_process(true)
 | 
			
		||||
		
 | 
			
		||||
		# Validate intial connection
 | 
			
		||||
		if client.is_connected_to_host():
 | 
			
		||||
			client.put_string("Hey there daddy!")
 | 
			
		||||
			emit_signal("auth_connected")
 | 
			
		||||
			return true
 | 
			
		||||
		else:
 | 
			
		||||
			# Timeout implemented in `process` loop
 | 
			
		||||
			print("Waiting for host connection...")
 | 
			
		||||
			return false
 | 
			
		||||
	else:
 | 
			
		||||
		print("Client is already connected to server!")
 | 
			
		||||
		return false
 | 
			
		||||
 | 
			
		||||
func auth_disconnect():
 | 
			
		||||
	client.disconnect_from_host()
 | 
			
		||||
	set_process(false) # Disable listening loop
 | 
			
		||||
	print_debug("Disconnected from host.")
 | 
			
		||||
	emit_signal("auth_disconnected")
 | 
			
		||||
 | 
			
		||||
var count = 0
 | 
			
		||||
func _process(delta):
 | 
			
		||||
 | 
			
		||||
	if client.get_available_bytes() > 0:
 | 
			
		||||
		print(client.get_available_bytes())
 | 
			
		||||
		
 | 
			
		||||
		print(client.get_string(client.get_available_bytes()))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	# Await for client connection
 | 
			
		||||
	if client.get_status()==1:
 | 
			
		||||
		count= count+delta
 | 
			
		||||
	if count>1: # if it took more than 1s to connect, error
 | 
			
		||||
		print_debug("Failed connect, disconnecting...")
 | 
			
		||||
		auth_disconnect() #interrupts connection to nothing
 | 
			
		||||
@ -1,17 +1,31 @@
 | 
			
		||||
extends Node
 | 
			
		||||
 | 
			
		||||
var auth
 | 
			
		||||
 | 
			
		||||
func _ready():
 | 
			
		||||
	$"/root/MusicManager".play_music("wizards")
 | 
			
		||||
	$Button.connect("button_down", self, "_on_button_press")
 | 
			
		||||
	$"/root/NetworkManager".connect("error_occured", self, "_on_error")
 | 
			
		||||
	$"/root/NetworkManager".connect("logged_in", self, "_on_login")
 | 
			
		||||
	auth = $"/root/Authorizer"
 | 
			
		||||
	auth.connect("auth_connected", self, "_on_auth_connection")
 | 
			
		||||
	auth.connect("auth_disconnected", self, "_on_auth_disconnection")
 | 
			
		||||
	
 | 
			
		||||
 | 
			
		||||
func _on_error():
 | 
			
		||||
	$ErrorDialog/ErrorLabel.text = $"/root/NetworkManager".error_info
 | 
			
		||||
	$ErrorDialog.popup_centered()
 | 
			
		||||
 | 
			
		||||
func _on_button_press():
 | 
			
		||||
	auth.auth_connect()
 | 
			
		||||
	
 | 
			
		||||
func _on_auth_connection():
 | 
			
		||||
	$Button.disabled = true
 | 
			
		||||
	
 | 
			
		||||
func _on_auth_disconnection():
 | 
			
		||||
	$Button.disabled = false
 | 
			
		||||
 | 
			
		||||
func _on_button_press_OLD():
 | 
			
		||||
	if($"/root/NetworkManager".connected):
 | 
			
		||||
		$"/root/NetworkManager".disconnect_from_server()
 | 
			
		||||
	else:
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user