-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartingHostState.cs
91 lines (79 loc) · 3.12 KB
/
StartingHostState.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using Unity.BossRoom.Infrastructure;
using Unity.BossRoom.UnityServices.Lobbies;
using Unity.Multiplayer.Samples.BossRoom;
using Unity.Netcode;
using UnityEngine;
using VContainer;
namespace Unity.BossRoom.ConnectionManagement
{
/// <summary>
/// Connection state corresponding to a host starting up. Starts the host when entering the state. If successful,
/// transitions to the Hosting state, if not, transitions back to the Offline state.
/// </summary>
class StartingHostState : OnlineState
{
[Inject]
LobbyServiceFacade m_LobbyServiceFacade;
[Inject]
LocalLobby m_LocalLobby;
ConnectionMethodBase m_ConnectionMethod;
public StartingHostState Configure(ConnectionMethodBase baseConnectionMethod)
{
m_ConnectionMethod = baseConnectionMethod;
return this;
}
public override void Enter()
{
StartHost();
}
public override void Exit() { }
public override void OnServerStarted()
{
m_ConnectStatusPublisher.Publish(ConnectStatus.Success);
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Hosting);
}
public override void ApprovalCheck(NetworkManager.ConnectionApprovalRequest request, NetworkManager.ConnectionApprovalResponse response)
{
var connectionData = request.Payload;
var clientId = request.ClientNetworkId;
// This happens when starting as a host, before the end of the StartHost call. In that case, we simply approve ourselves.
if (clientId == m_ConnectionManager.NetworkManager.LocalClientId)
{
var payload = System.Text.Encoding.UTF8.GetString(connectionData);
var connectionPayload = JsonUtility.FromJson<ConnectionPayload>(payload); // https://docs.unity3d.com/2020.2/Documentation/Manual/JSONSerialization.html
SessionManager<SessionPlayerData>.Instance.SetupConnectingPlayerSessionData(clientId, connectionPayload.playerId,
new SessionPlayerData(clientId, connectionPayload.playerName, new NetworkGuid(), 0, true));
// connection approval will create a player object for you
response.Approved = true;
response.CreatePlayerObject = true;
}
}
public override void OnServerStopped()
{
StartHostFailed();
}
async void StartHost()
{
try
{
await m_ConnectionMethod.SetupHostConnectionAsync();
// NGO's StartHost launches everything
if (!m_ConnectionManager.NetworkManager.StartHost())
{
StartHostFailed();
}
}
catch (Exception)
{
StartHostFailed();
throw;
}
}
void StartHostFailed()
{
m_ConnectStatusPublisher.Publish(ConnectStatus.StartHostFailed);
m_ConnectionManager.ChangeState(m_ConnectionManager.m_Offline);
}
}
}