How to await for a RPC finishes before disconnect peer?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By maiervf

Hello! I’m trying to setup an simple authentication server. All the comunication between server and clients work very well, but I’m having a problem when I try to send an RPC call and right after disconnect the peer.

The connection is closed before the RPC been sent (or before been finished, I dont know for sure) and the client not receive the response from the server. This is simple as that:

@rpc ("any_peer")
func authenticatePlayer(username, password):
	var loged = false
	var playerId = multiplayer.get_remote_sender_id()
	if username == "test" and password == "123":
		loged = true
	authenticationResult(loged, playerId)

@rpc ("call_local")
func authenticationResult(loged, playerId):
	rpc_id(playerId, "authenticationResult", loged)
	multiplayer.disconnect_peer(playerId) <- seems like this run before the rpc

At the client, also very simple:

@rpc ("call_local") 
func authenticatePlayer(login, password):
	rpc_id(1, "authenticatePlayer", login, password)
	
@rpc ("authority")
func authenticationResult(loged):
	print('here we go') <- those 2 lines are never printed
	print(loged) <- those 2 lines are never printed

I receive the peer connected/disconnected signal on the client, only the last rpc(authenticationResult) that not arrives. Also no errors on inspector.
Any sugestions on what should I do?
Thanks!

:bust_in_silhouette: Reply From: TRAILtheGREAT

As I understand it, when you send an RPC, the sending client packs up the request and puts it in a queue. At the end of the “network frame” all of the queued RPCs get sent off to the receiving client. When the receiving client gets the package, it unpacks it and calls all of the RPCs at the beginning of its own “network frame”.

I think the reason that your last RPC isn’t running is because its being queued up by the server, but then the server disconnects the client, so at the end of the network frame when the server sends out all of the queued RPCs, it discards the queued RPC because it sees that that client isn’t connected anymore. You can test this by removing the line that disconnects the peer.

A couple of possible solutions in order of ascending hacky-ness:

  • Add another RPC that the client sends back after it receives the authenticationResult() RPC. Only then do you disconnect the client.
  • Just make a timer that goes off a frame or two later and disconnects the client.