using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BossRoom
{
public enum ConnectStatus
{
SUCCESS, //client successfully connected. This may also be a successful reconnect.
ESERVERFULL, //can't join, server is already at capacity.
EMATCHSTARTED, //can't join, match is already in progress.
EUNKNOWN //can't join, reason unknown.
}
///
/// The GameNetHub is a general-purpose relay 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.
///
///
///
///
public class GameNetHub : MLAPI.NetworkedBehaviour
{
public GameObject NetworkingManagerGO;
private BossRoomClient.GNH_Client m_clientLogic;
private BossRoomServer.GNH_Server m_serverLogic;
public MLAPI.NetworkingManager NetManager { get; private set; }
// Start is called before the first frame update
void Start()
{
Object.DontDestroyOnLoad(this.gameObject);
Object.DontDestroyOnLoad(NetworkingManagerGO);
NetManager = NetworkingManagerGO.GetComponent();
}
public override void NetworkStart()
{
if (NetManager.IsClient)
{
m_clientLogic = new BossRoomClient.GNH_Client(this);
}
if ( NetManager.IsServer )
{
m_serverLogic = new BossRoomServer.GNH_Server(this);
//special host code. This is what kicks off the flow that happens on a regular client
//when it has finished connecting successfully. A dedicated server would remove this.
RecvConnectFinished(ConnectStatus.SUCCESS, BossRoomState.CHARSELECT);
}
}
///
/// Wraps the invocation of NetworkingManager.StartClient, including our GUID as the payload.
///
/// the IP address of the host to connect to.
/// The port of the host to connect to.
public void StartClient(string ipaddress, int port)
{
BossRoomClient.GNH_Client.StartClient(this, ipaddress, port);
}
//Server->Client RPCs
public void S2C_ConnectResult( ulong netId, ConnectStatus status, BossRoomState targetState )
{
InvokeClientRpcOnClient("RecvConnectResult",
netId,
"MLAPI_INTERNAL", // channelID. Must be MLAPI_INTERNAL Because it is called as part of the StartClient flow (before possible reject comes back).
MLAPI.Security.SecuritySendFlags.None,
MLAPI.Security.SecuritySendFlags.None,
status, // this is the actual payload
targetState); // ""
}
[MLAPI.Messaging.ClientRPC()]
private void RecvConnectFinished( ConnectStatus status, BossRoomState targetState )
{
m_clientLogic.RecvConnectFinished( status, targetState );
}
}
}