浏览代码

progress on taking review feedback

/main
David Woodruff 4 年前
当前提交
71e86f34
共有 25 个文件被更改,包括 140 次插入126 次删除
  1. 2
      Assets/Scenes/MainMenu.unity
  2. 4
      Assets/Scripts/Client/Game/ClientCharSelectBRState.cs
  3. 5
      Assets/Scripts/Client/Game/ClientGameBRState.cs
  4. 2
      Assets/Scripts/Client/Game/ClientMainMenuBRState.cs
  5. 8
      Assets/Scripts/Client/Net/ClientGNHLogic.cs
  6. 4
      Assets/Scripts/Client/UI/MainMenuUI.cs
  7. 5
      Assets/Scripts/Server/Game/ServerCharSelectBRState.cs
  8. 5
      Assets/Scripts/Server/Game/ServerGameBRState.cs
  9. 10
      Assets/Scripts/Shared/Game/BossRoomStateManager.cs
  10. 18
      Assets/Scripts/Shared/Net/GameNetHub.cs
  11. 102
      Assets/Scripts/Server/Net/ServerGNHLogic.cs
  12. 101
      Assets/Scripts/Server/Net/GNH_Server.cs
  13. 0
      /Assets/Scripts/Client/Game/ClientCharSelectBRState.cs.meta
  14. 0
      /Assets/Scripts/Client/Game/ClientGameBRState.cs.meta
  15. 0
      /Assets/Scripts/Client/Game/ClientMainMenuBRState.cs.meta
  16. 0
      /Assets/Scripts/Client/Game/ClientCharSelectBRState.cs
  17. 0
      /Assets/Scripts/Client/Game/ClientGameBRState.cs
  18. 0
      /Assets/Scripts/Client/Game/ClientMainMenuBRState.cs
  19. 0
      /Assets/Scripts/Client/Net/ClientGNHLogic.cs.meta
  20. 0
      /Assets/Scripts/Client/Net/ClientGNHLogic.cs
  21. 0
      /Assets/Scripts/Server/Game/ServerCharSelectBRState.cs.meta
  22. 0
      /Assets/Scripts/Server/Game/ServerGameBRState.cs.meta
  23. 0
      /Assets/Scripts/Server/Game/ServerCharSelectBRState.cs
  24. 0
      /Assets/Scripts/Server/Game/ServerGameBRState.cs
  25. 0
      /Assets/Scripts/Server/Net/ServerGNHLogic.cs.meta

2
Assets/Scenes/MainMenu.unity


m_Name:
m_EditorClassIdentifier:
GameHubGO: {fileID: 308991157}
InputTextGO: {fileID: 714400691}
--- !u!1 &1359040525
GameObject:
m_ObjectHideFlags: 0

ReceiveTickrate: 0
MaxReceiveEventsPerTickRate: 1000
EventTickrate: 0
MaxObjectUpdatesPerTick: -1
ClientConnectionBufferTimeout: 10
ConnectionApproval: 1
ConnectionData:

4
Assets/Scripts/Client/Game/ClientCharSelectBRState.cs


namespace BossRoomClient
{
class CCharSelectBRState : CharSelectBRState
class ClientCharSelectBRState : CharSelectBRState
// !! STUB CLASS !!
// this will be fleshed out with all client-side logic for the character select BossRoom state.
public override void Destroy()
{

5
Assets/Scripts/Client/Game/ClientGameBRState.cs


namespace BossRoomClient
{
class CGameBRState : GameBRState
class ClientGameBRState : GameBRState
// !! STUB CLASS !!
// this will be fleshed out with all client-side logic for the Game BossRoom state.
public override void Destroy()
{
base.Destroy();

2
Assets/Scripts/Client/Game/ClientMainMenuBRState.cs


/// The BossRoom state logic that runs during MainMenu. Unlike most states, there is only a client variant of this (you are always
/// a client when sitting at the Main Menu screen).
/// </summary>
class CMainMenuBRState : IBossRoomState
class ClientMainMenuBRState : IBossRoomState
{
private BossRoomStateManager m_manager;

8
Assets/Scripts/Client/Net/ClientGNHLogic.cs


/// <summary>
/// Client logic for the GameNetHub. Contains implementations for all of GameNetHub's S2C RPCs.
/// </summary>
public class GNH_Client
public class ClientGNHLogic
public GNH_Client(GameNetHub hub)
public ClientGNHLogic(GameNetHub hub)
{
m_hub = hub;
}

/// </summary>
/// <remarks>
/// This method must be static because, when it is invoked, the client still doesn't know it's a client yet, and in particular, GameNetHub hasn't
/// yet initialized its GNH_Client and GNH_Server objects yet (which it does in NetworkStart, based on the role that the current player is performing).
/// yet initialized its client and server GNHLogic objects yet (which it does in NetworkStart, based on the role that the current player is performing).
/// </remarks>
/// <param name="ipaddress">the IP address of the host to connect to. (currently IPV4 only)</param>
/// <param name="port">The port of the host to connect to. </param>

hub.NetManager.NetworkConfig.ConnectionData = payload_bytes;
//and...we're off! MLAPI will establish a socket connection to the host.
// If the socket connection fails, we'll hear back by [???] (fixme: where do we get transport-layer failures?)
// If the socket connection fails, we'll hear back by [???] (FIXME: GOMPS-79, need to handle transport layer failures too).
// If the socket connection succeeds, we'll get our RecvConnectFinished invoked. This is where game-layer failures will be reported.
hub.NetManager.StartClient();
}

4
Assets/Scripts/Client/UI/MainMenuUI.cs


public class MainMenuUI : MonoBehaviour
{
public GameObject GameHubGO;
public GameObject InputTextGO;
private BossRoom.GameNetHub m_netHub;
// Start is called before the first frame update

/// <returns>IP address entered by user, in string form. </returns>
private string GetIPAddress()
{
string iptext = GameObject.Find("IPInputText").GetComponent<UnityEngine.UI.Text>().text;
string iptext = InputTextGO.GetComponent<UnityEngine.UI.Text>().text;
if( iptext == "" )
{
return "127.0.0.1";

5
Assets/Scripts/Server/Game/ServerCharSelectBRState.cs


namespace BossRoomServer
{
class SCharSelectBRState : CharSelectBRState
class ServerCharSelectBRState : CharSelectBRState
// !! STUB CLASS !!
// this will be fleshed out with all server-side logic for the character select BossRoom state.
public override void Destroy()
{
base.Destroy();

5
Assets/Scripts/Server/Game/ServerGameBRState.cs


namespace BossRoomServer
{
class SGameBRState : GameBRState
class ServerGameBRState : GameBRState
// !! STUB CLASS !!
// this will be fleshed out with all server-side logic for the Game BossRoom state.
public override void Destroy()
{
base.Destroy();

10
Assets/Scripts/Shared/Game/BossRoomStateManager.cs


case BossRoomState.MAINMENU:
{
DestroyStates();
m_clientState = new BossRoomClient.CMainMenuBRState();
m_clientState = new BossRoomClient.ClientMainMenuBRState();
m_clientState.Initialize(this, stateParams);
break;
}

if( NetHub.NetManager.IsServer )
{
m_serverState = new BossRoomServer.SCharSelectBRState();
m_serverState = new BossRoomServer.ServerCharSelectBRState();
m_clientState = new BossRoomClient.CCharSelectBRState();
m_clientState = new BossRoomClient.ClientCharSelectBRState();
m_clientState.Initialize(this, stateParams);
}

if( NetHub.NetManager.IsServer )
{
m_serverState = new BossRoomServer.SGameBRState();
m_serverState = new BossRoomServer.ServerGameBRState();
m_clientState = new BossRoomClient.CGameBRState();
m_clientState = new BossRoomClient.ClientGameBRState();
m_clientState.Initialize(this, stateParams);
}

18
Assets/Scripts/Shared/Net/GameNetHub.cs


}
/// <summary>
/// The GameNetHub is a general-purpose relay for game network messages between the client and server. It is available
/// The GameNetHub is the general purpose entry-point for game network messages between the client and server. It is available
/// as soon as the initial network connection has completed, and persists across all scenes. Its purpose is to move non-GameObject-specific
/// methods between server and client. Generally these have to do with connection, and match end conditions.
/// </summary>

{
public GameObject NetworkingManagerGO;
private BossRoomClient.GNH_Client m_clientLogic;
private BossRoomServer.GNH_Server m_serverLogic;
private BossRoomClient.ClientGNHLogic m_ClientLogic;
private BossRoomServer.ServerGNHLogic m_ServerLogic;
public MLAPI.NetworkingManager NetManager { get; private set; }

{
ConnectStatus status = (ConnectStatus)reader.ReadInt32();
BossRoomState state = (BossRoomState)reader.ReadInt32();
m_clientLogic.RecvConnectFinished(status, state);
m_ClientLogic.RecvConnectFinished(status, state);
}
});
}

{
if (NetManager.IsClient)
{
m_clientLogic = new BossRoomClient.GNH_Client(this);
m_ClientLogic = new BossRoomClient.ClientGNHLogic(this);
m_serverLogic = new BossRoomServer.GNH_Server(this);
m_ServerLogic = new BossRoomServer.ServerGNHLogic(this);
RegisterServerMessageHandlers();
}
if( NetManager.IsHost )

m_clientLogic.RecvConnectFinished(ConnectStatus.SUCCESS, BossRoomState.CHARSELECT);
m_ClientLogic.RecvConnectFinished(ConnectStatus.SUCCESS, BossRoomState.CHARSELECT);
}
}

/// <param name="port">The port of the host to connect to. </param>
public void StartClient(string ipaddress, int port)
{
BossRoomClient.GNH_Client.StartClient(this, ipaddress, port);
BossRoomClient.ClientGNHLogic.StartClient(this, ipaddress, port);
}
/// <summary>

/// <param name="port">The port we should listen on. </param>
public void StartHost(string ipaddress, int port )
{
BossRoomServer.GNH_Server.StartHost(this, ipaddress, port);
BossRoomServer.ServerGNHLogic.StartHost(this, ipaddress, port);
}
//Server->Client RPCs

102
Assets/Scripts/Server/Net/ServerGNHLogic.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BossRoom;
namespace BossRoomServer
{
/// <summary>
/// Server logic plugin for the GameNetHub. Contains implementations for all GameNetHub's C2S RPCs.
/// </summary>
public class ServerGNHLogic
{
private GameNetHub m_Hub;
// used in ApprovalCheck. This is intended as a bit of light protection against DOS attacks that rely on sending silly big buffers of garbage.
private const int k_MaxConnectPayload = 1024;
public ServerGNHLogic(GameNetHub hub)
{
m_Hub = hub;
m_Hub.NetManager.ConnectionApprovalCallback += this.ApprovalCheck;
}
/// <summary>
/// Initializes host mode on this client. Call this and then other clients should connect to us!
/// </summary>
/// <remarks>
/// See notes in GNH_Client.StartClient about why this must be static.
/// </remarks>
/// <param name="hub">The GameNetHub that is invoking us. </param>
/// <param name="ipaddress">The IP address to connect to (currently IPV4 only).</param>
/// <param name="port">The port to connect to. </param>
public static void StartHost(GameNetHub hub, string ipaddress, int port )
{
//DMW_NOTE: non-portable. We need to be updated when moving to UTP transport.
var transport = hub.NetworkingManagerGO.GetComponent<MLAPI.Transports.UNET.UnetTransport>();
transport.ConnectAddress = ipaddress;
transport.ServerListenPort = port;
hub.NetManager.StartHost();
}
/// <summary>
/// This logic plugs into the "ConnectionApprovalCallback" exposed by MLAPI.NetworkingManager, and is run every time a client connects to us.
/// See GNH_Client.StartClient for the complementary logic that runs when the client starts its connection.
/// </summary>
/// <remarks>
/// Since our game doesn't have to interact with some third party authentication service to validate the identity of the new connection, our ApprovalCheck
/// method is simple, and runs synchronously, invoking "callback" to signal approval at the end of the method. MLAPI currently doesn't support the ability
/// to send back more than a "true/false", which means we have to work a little harder to provide a useful error return to the client. To do that, we invoke a
/// client RPC in the same channel that MLAPI uses for its connection callback. Since that channel ("MLAPI_INTERNAL") is both reliable and sequenced, we can be
/// confident that our login result message will execute before any disconnect message.
/// </remarks>
/// <param name="connectionData">binary data passed into StartClient. In our case this is the client's GUID, which is a unique identifier for their install of the game that persists across app restarts. </param>
/// <param name="clientId">This is the clientId that MLAPI assigned us on login. It does not persist across multiple logins from the same client. </param>
/// <param name="callback">The delegate we must invoke to signal that the connection was approved or not. </param>
private void ApprovalCheck( byte[] connectionData, ulong clientId, MLAPI.NetworkingManager.ConnectionApprovedDelegate callback )
{
if( connectionData.Length > k_MaxConnectPayload )
{
callback(false, 0, false, null, null );
return;
}
string payload = System.Text.Encoding.UTF8.GetString(connectionData);
string[] config_lines = payload.Split('\n');
var payload_config = new Dictionary<string, string>();
foreach( var line in config_lines )
{
//key-value pair.
if( line.Contains("=") )
{
string[] kv = line.Split('=');
payload_config.Add(kv[0], kv[1]);
}
else if( line.Trim() != "" )
{
//single token, with no value present.
payload_config.Add(line, null);
}
}
//TODO: GOMPS-78. We'll need to save our client guid so that we can handle reconnect.
Debug.Log("host ApprovalCheck: client payload was: " + payload);
Debug.Log("host ApprovalCheck: client guid was: " + payload_config["client_guid"]);
//TODO: GOMPS-79 handle different error cases.
callback(false, 0, true, null, null);
//FIXME_DMW: it is weird to do this after the callback, but the custom message won't be delivered if we call it beforehand.
//This creates an "honor system" scenario where it is up to the client to politely leave on failure. Probably
//we should add a NetManager.DisconnectClient call directly below this line, when we are rejecting the connection.
m_Hub.S2C_ConnectResult(clientId, ConnectStatus.SUCCESS, BossRoomState.CHARSELECT);
}
}
}

101
Assets/Scripts/Server/Net/GNH_Server.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BossRoom;
namespace BossRoomServer
{
/// <summary>
/// Server logic plugin for the GameNetHub. Contains implementations for all GameNetHub's C2S RPCs.
/// </summary>
public class GNH_Server
{
private GameNetHub m_hub;
// used in ApprovalCheck. This is intended as a bit of light protection against DOS attacks that rely on sending silly big buffers of garbage.
private const int MAX_CONNECT_PAYLOAD = 1024;
public GNH_Server(GameNetHub hub)
{
m_hub = hub;
m_hub.NetManager.ConnectionApprovalCallback += this.ApprovalCheck;
}
/// <summary>
/// Initializes host mode on this client. Call this and then other clients should connect to us!
/// </summary>
/// <remarks>
/// See notes in GNH_Client.StartClient about why this must be static.
/// </remarks>
/// <param name="hub">The GameNetHub that is invoking us. </param>
/// <param name="ipaddress">The IP address to connect to (currently IPV4 only).</param>
/// <param name="port">The port to connect to. </param>
public static void StartHost(GameNetHub hub, string ipaddress, int port )
{
//DMW_NOTE: non-portable. We need to be updated when moving to UTP transport.
var transport = hub.NetworkingManagerGO.GetComponent<MLAPI.Transports.UNET.UnetTransport>();
transport.ConnectAddress = ipaddress;
transport.ServerListenPort = port;
hub.NetManager.StartHost();
}
/// <summary>
/// This logic plugs into the "ConnectionApprovalCallback" exposed by MLAPI.NetworkingManager, and is run every time a client connects to us.
/// See GNH_Client.StartClient for the complementary logic that runs when the client starts its connection.
/// </summary>
/// <remarks>
/// Since our game doesn't have to interact with some third party authentication service to validate the identity of the new connection, our ApprovalCheck
/// method is simple, and runs synchronously, invoking "callback" to signal approval at the end of the method. MLAPI currently doesn't support the ability
/// to send back more than a "true/false", which means we have to work a little harder to provide a useful error return to the client. To do that, we invoke a
/// client RPC in the same channel that MLAPI uses for its connection callback. Since that channel ("MLAPI_INTERNAL") is both reliable and sequenced, we can be
/// confident that our login result message will execute before any disconnect message.
/// </remarks>
/// <param name="connectionData">binary data passed into StartClient. In our case this is the client's GUID, which is a unique identifier for their install of the game that persists across app restarts. </param>
/// <param name="clientId">This is the clientId that MLAPI assigned us on login. It does not persist across multiple logins from the same client. </param>
/// <param name="callback">The delegate we must invoke to signal that the connection was approved or not. </param>
private void ApprovalCheck( byte[] connectionData, ulong clientId, MLAPI.NetworkingManager.ConnectionApprovedDelegate callback )
{
if( connectionData.Length > MAX_CONNECT_PAYLOAD )
{
callback(false, 0, false, null, null );
return;
}
string payload = System.Text.Encoding.UTF8.GetString(connectionData);
string[] config_lines = payload.Split('\n');
Dictionary<string, string> payload_config = new Dictionary<string, string>();
foreach( var line in config_lines )
{
//key-value pair.
if( line.Contains("=") )
{
string[] kv = line.Split('=');
payload_config.Add(kv[0], kv[1]);
}
else if( line.Trim() != "" )
{
//single token, with no value present.
payload_config.Add(line, null);
}
}
//TODO_DMW: save off the player's Guid.
Debug.Log("host ApprovalCheck: client payload was: " + payload);
Debug.Log("host ApprovalCheck: client guid was: " + payload_config["client_guid"]);
//TODO_DMW: handle different cases based on gamestate (e.g. GameState.PLAYING would cause a reject if this isn't a reconnect case).
callback(false, 0, true, null, null);
//FIXME_DMW: it is weird to do this after the callback, but the custom message won't be delivered if we call it beforehand.
//This creates an "honor system" scenario where it is up to the client to politely leave on failure. Probably
//we should add a NetManager.DisconnectClient call directly below this line, when we are rejecting the connection.
m_hub.S2C_ConnectResult(clientId, ConnectStatus.SUCCESS, BossRoomState.CHARSELECT);
}
}
}

/Assets/Scripts/Client/Game/CCharSelectBRState.cs.meta → /Assets/Scripts/Client/Game/ClientCharSelectBRState.cs.meta

/Assets/Scripts/Client/Game/CGameBRState.cs.meta → /Assets/Scripts/Client/Game/ClientGameBRState.cs.meta

/Assets/Scripts/Client/Game/CMainMenuBRState.cs.meta → /Assets/Scripts/Client/Game/ClientMainMenuBRState.cs.meta

/Assets/Scripts/Client/Game/CCharSelectBRState.cs → /Assets/Scripts/Client/Game/ClientCharSelectBRState.cs

/Assets/Scripts/Client/Game/CGameBRState.cs → /Assets/Scripts/Client/Game/ClientGameBRState.cs

/Assets/Scripts/Client/Game/CMainMenuBRState.cs → /Assets/Scripts/Client/Game/ClientMainMenuBRState.cs

/Assets/Scripts/Client/Net/GNH_Client.cs.meta → /Assets/Scripts/Client/Net/ClientGNHLogic.cs.meta

/Assets/Scripts/Client/Net/GNH_Client.cs → /Assets/Scripts/Client/Net/ClientGNHLogic.cs

/Assets/Scripts/Server/Game/SCharSelectBRState.cs.meta → /Assets/Scripts/Server/Game/ServerCharSelectBRState.cs.meta

/Assets/Scripts/Server/Game/SGameBRState.cs.meta → /Assets/Scripts/Server/Game/ServerGameBRState.cs.meta

/Assets/Scripts/Server/Game/SCharSelectBRState.cs → /Assets/Scripts/Server/Game/ServerCharSelectBRState.cs

/Assets/Scripts/Server/Game/SGameBRState.cs → /Assets/Scripts/Server/Game/ServerGameBRState.cs

/Assets/Scripts/Server/Net/GNH_Server.cs.meta → /Assets/Scripts/Server/Net/ServerGNHLogic.cs.meta

正在加载...
取消
保存