Browse Source

Use clientData to pass connection arguments to callback functions; cleans things up nicely at the expense of a bit more boilerplate code

master
Nico de Poel 5 years ago
parent
commit
4b9599b73b
  1. 40
      Assets/Scripts/EOSVoiceChat.cs

40
Assets/Scripts/EOSVoiceChat.cs

@ -46,6 +46,8 @@ public class EOSVoiceChat
{ {
DisconnectChat(); // Leave any currently connected chat lobby DisconnectChat(); // Leave any currently connected chat lobby
var connectArgs = new ChatConnectArgs(chatLobbyName, onCompleted, maxChatPlayers);
lobbyInterface.CreateLobbySearch(new CreateLobbySearchOptions { MaxResults = 1 }, out var searchHandle); lobbyInterface.CreateLobbySearch(new CreateLobbySearchOptions { MaxResults = 1 }, out var searchHandle);
searchHandle.SetLobbyId(new LobbySearchSetLobbyIdOptions { LobbyId = chatLobbyName }); searchHandle.SetLobbyId(new LobbySearchSetLobbyIdOptions { LobbyId = chatLobbyName });
@ -66,8 +68,8 @@ public class EOSVoiceChat
LobbyDetailsHandle = lobbyDetails, LobbyDetailsHandle = lobbyDetails,
PresenceEnabled = false, PresenceEnabled = false,
}, },
null,
data => HandleLobbyJoined(data, onCompleted)
connectArgs,
HandleLobbyJoined
); );
break; break;
default: default:
@ -86,51 +88,53 @@ public class EOSVoiceChat
BucketId = Application.productName, // TODO: do we need anything more specific than this? BucketId = Application.productName, // TODO: do we need anything more specific than this?
EnableRTCRoom = true, EnableRTCRoom = true,
}, },
chatLobbyName,
data => HandleLobbyCreated(data, onCompleted)
connectArgs,
HandleLobbyCreated
); );
break; break;
} }
}); });
} }
private void HandleLobbyCreated(CreateLobbyCallbackInfo data, Action<bool> onCompleted)
private void HandleLobbyCreated(CreateLobbyCallbackInfo data)
{ {
var connectArgs = (ChatConnectArgs)data.ClientData;
switch (data.ResultCode) switch (data.ResultCode)
{ {
case Result.Success: case Result.Success:
connectedLobbyId = data.LobbyId; connectedLobbyId = data.LobbyId;
Debug.Log($"Chat lobby creation successful, lobby ID = {connectedLobbyId}"); Debug.Log($"Chat lobby creation successful, lobby ID = {connectedLobbyId}");
onCompleted?.Invoke(true);
connectArgs.onCompleted?.Invoke(true);
break; break;
case Result.LobbyLobbyAlreadyExists: case Result.LobbyLobbyAlreadyExists:
// This can happen if two clients try to create the same lobby at the same time, a classic race condition. // This can happen if two clients try to create the same lobby at the same time, a classic race condition.
// Try to join the other client's newly created chat lobby instead. // Try to join the other client's newly created chat lobby instead.
connectedLobbyId = null; connectedLobbyId = null;
Debug.Log("Chat lobby already exists, attempting to join it..."); Debug.Log("Chat lobby already exists, attempting to join it...");
ConnectToChat((string)data.ClientData, onCompleted);
ConnectToChat(connectArgs.chatLobbyName, connectArgs.onCompleted, connectArgs.maxChatPlayers);
break; break;
default: default:
connectedLobbyId = null; connectedLobbyId = null;
Debug.LogError($"Chat lobby creation failed, result code = {data.ResultCode}"); Debug.LogError($"Chat lobby creation failed, result code = {data.ResultCode}");
onCompleted?.Invoke(false);
connectArgs.onCompleted?.Invoke(false);
break; break;
} }
} }
private void HandleLobbyJoined(JoinLobbyCallbackInfo data, Action<bool> onCompleted)
private void HandleLobbyJoined(JoinLobbyCallbackInfo data)
{ {
var connectArgs = (ChatConnectArgs)data.ClientData;
switch (data.ResultCode) switch (data.ResultCode)
{ {
case Result.Success: case Result.Success:
connectedLobbyId = data.LobbyId; connectedLobbyId = data.LobbyId;
Debug.Log($"Chat lobby joined successfully, lobby ID = {connectedLobbyId}"); Debug.Log($"Chat lobby joined successfully, lobby ID = {connectedLobbyId}");
onCompleted?.Invoke(true);
connectArgs.onCompleted?.Invoke(true);
break; break;
default: default:
connectedLobbyId = null; connectedLobbyId = null;
Debug.LogError($"Chat lobby join failed, result code = {data.ResultCode}"); Debug.LogError($"Chat lobby join failed, result code = {data.ResultCode}");
onCompleted?.Invoke(false);
connectArgs.onCompleted?.Invoke(false);
break; break;
} }
} }
@ -184,4 +188,18 @@ public class EOSVoiceChat
if (!IsConnected) if (!IsConnected)
return; return;
} }
private class ChatConnectArgs
{
public readonly string chatLobbyName;
public readonly Action<bool> onCompleted;
public readonly uint maxChatPlayers;
public ChatConnectArgs(string chatLobbyName, Action<bool> onCompleted, uint maxChatPlayers)
{
this.chatLobbyName = chatLobbyName;
this.onCompleted = onCompleted;
this.maxChatPlayers = maxChatPlayers;
}
}
} }
Loading…
Cancel
Save