This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
DefaultMobileWorkerConnector.cs
144 lines (128 loc) · 5.06 KB
/
DefaultMobileWorkerConnector.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using Improbable.Gdk.Core;
using Improbable.Worker.CInterop;
using UnityEngine;
namespace Improbable.Gdk.Mobile
{
public abstract class DefaultMobileWorkerConnector : WorkerConnector
{
private const string HostIpPlayerPrefsKey = "SpatialOSHostIp";
#pragma warning disable 649
[SerializeField] private string IpAddress;
[SerializeField] private bool ShouldConnectLocally;
#pragma warning restore 649
private void Awake()
{
InitializeClient();
}
protected virtual void InitializeClient()
{
var arguments = LaunchArguments.GetArguments();
var environment = CommandLineUtility.GetCommandLineValue(arguments, RuntimeConfigNames.Environment, string.Empty);
if (string.IsNullOrEmpty(environment))
{
environment = PlayerPrefs.GetString(RuntimeConfigNames.Environment, string.Empty);
}
else
{
PlayerPrefs.SetString(RuntimeConfigNames.Environment, environment);
}
if (!string.IsNullOrEmpty(environment))
{
ShouldConnectLocally = environment == RuntimeConfigDefaults.LocalEnvironment;
}
if (ShouldConnectLocally)
{
IpAddress = GetHostIp();
PlayerPrefs.SetString(HostIpPlayerPrefsKey, IpAddress);
}
else
{
PlayerPrefs.DeleteKey(HostIpPlayerPrefsKey);
}
PlayerPrefs.Save();
}
/// <summary>
/// Extracts the Ip address that should be used to connect via the receptionist. The order is as follows:
/// 1. Try to extract the ip address from command line arguments passed in. This currently only works for Android.
/// 2. If we are running on an Android Emulator: Use the Ip address necessary to connect locally.
/// 3. If we are on a physical device (Android & iOS): Try to extract the value from the stored player preferences.
/// 4. Check if we stored anything inside the IpAddress field and use it, if we have.
/// 5. Return the default ReceptionistHost (localhost).
/// </summary>
/// <returns>Returns the Host IP.</returns>
protected virtual string GetHostIp()
{
var arguments = LaunchArguments.GetArguments();
var hostIp =
CommandLineUtility.GetCommandLineValue(arguments, RuntimeConfigNames.ReceptionistHost, string.Empty);
if (!string.IsNullOrEmpty(hostIp))
{
return hostIp;
}
if (Application.isMobilePlatform)
{
switch (DeviceInfo.ActiveDeviceType)
{
case MobileDeviceType.Virtual:
#if UNITY_ANDROID
return DeviceInfo.AndroidEmulatorDefaultCallbackIp;
#else
break;
#endif
case MobileDeviceType.Physical:
return PlayerPrefs.GetString(HostIpPlayerPrefsKey, IpAddress);
}
}
if (!string.IsNullOrEmpty(IpAddress))
{
return IpAddress;
}
return RuntimeConfigDefaults.ReceptionistHost;
}
protected override ConnectionService GetConnectionService()
{
return ShouldConnectLocally ? ConnectionService.Receptionist : ConnectionService.AlphaLocator;
}
protected override ConnectionParameters GetConnectionParameters(string workerType, ConnectionService service)
{
return new ConnectionParameters
{
WorkerType = workerType,
Network =
{
ConnectionType = NetworkConnectionType.Kcp,
UseExternalIp = true,
Kcp = new KcpNetworkParameters
{
// These are the last tested values
Heartbeat = new HeartbeatParameters()
{
IntervalMillis = 5000,
TimeoutMillis = 10000
}
}
},
EnableProtocolLoggingAtStartup = false,
DefaultComponentVtable = new ComponentVtable(),
};
}
protected override ReceptionistConfig GetReceptionistConfig(string workerType)
{
return new ReceptionistConfig
{
ReceptionistHost = GetHostIp(),
ReceptionistPort = RuntimeConfigDefaults.ReceptionistPort,
WorkerId = CreateNewWorkerId(workerType)
};
}
protected override LocatorConfig GetLocatorConfig()
{
throw new NotImplementedException("The locator flow is not available for mobile workers.");
}
protected override AlphaLocatorConfig GetAlphaLocatorConfig(string workerType)
{
return GetAlphaLocatorConfigViaDevAuthFlow(workerType);
}
}
}