Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit e6b54b8

Browse files
authored
splitting worker connector into default and mobile (#588)
1 parent 3d056d1 commit e6b54b8

File tree

7 files changed

+102
-82
lines changed

7 files changed

+102
-82
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- Changed the allocation type used internally for Unity ECS chunk iteration from `Temp` to `TempJob`
1717
- Running a build in the Editor no longer automatically selects all scenes in the Unity build configuration
1818
- `Improbable.Gdk.Core.Snapshot.AddEntity` now returns the `EntityId` assigned in the snapshot.
19+
- Changed the `WorkerConnector` to be more generic and have an explicit `StandaloneWorkerConnector` for any workers running on OSX/Linux/Windows.
1920

2021
### Fixed
2122

workers/unity/Assets/Playground/Scripts/Worker/ClientWorkerConnector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Playground
66
{
7-
public class ClientWorkerConnector : WorkerConnector
7+
public class ClientWorkerConnector : DefaultWorkerConnector
88
{
99
[SerializeField] private GameObject level;
1010

workers/unity/Assets/Playground/Scripts/Worker/GameLogicWorkerConnector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Playground
55
{
6-
public class GameLogicWorkerConnector : WorkerConnector
6+
public class GameLogicWorkerConnector : DefaultWorkerConnector
77
{
88
[SerializeField] private GameObject level;
99

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using UnityEngine;
3+
4+
namespace Improbable.Gdk.Core
5+
{
6+
public abstract class DefaultWorkerConnector : WorkerConnector
7+
{
8+
/// <summary>
9+
/// Denotes whether to connect using an external IP address.
10+
/// </summary>
11+
public bool UseExternalIp;
12+
13+
/// <summary>
14+
/// Determines whether to connect via the locator.
15+
/// </summary>
16+
/// <returns>True, if should connect via the Locator, false otherwise.</returns>
17+
protected override bool ShouldUseLocator()
18+
{
19+
if (Application.isEditor)
20+
{
21+
return false;
22+
}
23+
24+
var commandLineArguments = Environment.GetCommandLineArgs();
25+
var commandLineArgs = CommandLineUtility.ParseCommandLineArgs(commandLineArguments);
26+
var shouldUseLocator = commandLineArgs.ContainsKey(RuntimeConfigNames.LoginToken) ||
27+
commandLineArgs.ContainsKey(RuntimeConfigNames.SteamDeploymentTag) ||
28+
commandLineArgs.ContainsKey(RuntimeConfigNames.SteamTicket);
29+
return shouldUseLocator;
30+
}
31+
32+
protected override ReceptionistConfig GetReceptionistConfig(string workerType)
33+
{
34+
ReceptionistConfig config;
35+
36+
if (Application.isEditor)
37+
{
38+
config = new ReceptionistConfig
39+
{
40+
WorkerType = workerType,
41+
WorkerId = CreateNewWorkerId(workerType),
42+
UseExternalIp = UseExternalIp
43+
};
44+
}
45+
else
46+
{
47+
var commandLineArguments = Environment.GetCommandLineArgs();
48+
var commandLineArgs = CommandLineUtility.ParseCommandLineArgs(commandLineArguments);
49+
config = ReceptionistConfig.CreateConnectionConfigFromCommandLine(commandLineArgs);
50+
config.WorkerType = workerType;
51+
config.UseExternalIp = UseExternalIp;
52+
if (!commandLineArgs.ContainsKey(RuntimeConfigNames.WorkerId))
53+
{
54+
config.WorkerId = CreateNewWorkerId(workerType);
55+
}
56+
}
57+
58+
return config;
59+
}
60+
61+
protected override LocatorConfig GetLocatorConfig(string workerType)
62+
{
63+
var commandLineArguments = Environment.GetCommandLineArgs();
64+
var commandLineArgs = CommandLineUtility.ParseCommandLineArgs(commandLineArguments);
65+
var config = LocatorConfig.CreateConnectionConfigFromCommandLine(commandLineArgs);
66+
config.WorkerType = workerType;
67+
config.WorkerId = CreateNewWorkerId(workerType);
68+
return config;
69+
}
70+
}
71+
}

workers/unity/Packages/com.improbable.gdk.core/Worker/DefaultWorkerConnector.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

workers/unity/Packages/com.improbable.gdk.core/Worker/WorkerConnector.cs

Lines changed: 12 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Improbable.Gdk.Core
1313
/// <summary>
1414
/// Connect workers via Monobehaviours.
1515
/// </summary>
16-
public class WorkerConnector : MonoBehaviour, IDisposable
16+
public abstract class WorkerConnector : MonoBehaviour, IDisposable
1717
{
1818
private delegate Task<Worker> ConnectionDelegate();
1919

@@ -22,11 +22,6 @@ public class WorkerConnector : MonoBehaviour, IDisposable
2222
/// </summary>
2323
public int MaxConnectionAttempts = 3;
2424

25-
/// <summary>
26-
/// Denotes whether to connect using an external IP address.
27-
/// </summary>
28-
public bool UseExternalIp;
29-
3025
/// <summary>
3126
/// Represents a SpatialOS worker.
3227
/// </summary>
@@ -149,30 +144,7 @@ await Worker.CreateWorkerAsync(GetReceptionistConfig(workerType), logger, origin
149144
/// Determines whether to connect via the locator.
150145
/// </summary>
151146
/// <returns>True, if should connect via the Locator, false otherwise.</returns>
152-
protected virtual bool ShouldUseLocator()
153-
{
154-
if (Application.isEditor)
155-
{
156-
return false;
157-
}
158-
159-
var commandLineArguments = Environment.GetCommandLineArgs();
160-
var commandLineArgs = CommandLineUtility.ParseCommandLineArgs(commandLineArguments);
161-
var shouldUseLocator = commandLineArgs.ContainsKey(RuntimeConfigNames.LoginToken) ||
162-
commandLineArgs.ContainsKey(RuntimeConfigNames.SteamDeploymentTag) ||
163-
commandLineArgs.ContainsKey(RuntimeConfigNames.SteamTicket);
164-
return shouldUseLocator;
165-
}
166-
167-
/// <summary>
168-
/// Selects which deployment to connect to.
169-
/// </summary>
170-
/// <param name="deployments">The list of deployments.</param>
171-
/// <returns>The name of the deployment to connect to.</returns>
172-
protected virtual string SelectDeploymentName(DeploymentList deployments)
173-
{
174-
return null;
175-
}
147+
protected abstract bool ShouldUseLocator();
176148

177149
/// <summary>
178150
/// Creates a Receptionist configuration.
@@ -185,34 +157,7 @@ protected virtual string SelectDeploymentName(DeploymentList deployments)
185157
/// </remarks>
186158
/// <param name="workerType">The type of the worker to create.</param>
187159
/// <returns>The Receptionist connection configuration</returns>
188-
protected virtual ReceptionistConfig GetReceptionistConfig(string workerType)
189-
{
190-
ReceptionistConfig config;
191-
192-
if (Application.isEditor)
193-
{
194-
config = new ReceptionistConfig
195-
{
196-
WorkerType = workerType,
197-
WorkerId = CreateNewWorkerId(workerType),
198-
UseExternalIp = UseExternalIp
199-
};
200-
}
201-
else
202-
{
203-
var commandLineArguments = Environment.GetCommandLineArgs();
204-
var commandLineArgs = CommandLineUtility.ParseCommandLineArgs(commandLineArguments);
205-
config = ReceptionistConfig.CreateConnectionConfigFromCommandLine(commandLineArgs);
206-
config.WorkerType = workerType;
207-
config.UseExternalIp = UseExternalIp;
208-
if (!commandLineArgs.ContainsKey(RuntimeConfigNames.WorkerId))
209-
{
210-
config.WorkerId = CreateNewWorkerId(workerType);
211-
}
212-
}
213-
214-
return config;
215-
}
160+
protected abstract ReceptionistConfig GetReceptionistConfig(string workerType);
216161

217162
/// <summary>
218163
/// Creates the Locator configuration.
@@ -222,14 +167,16 @@ protected virtual ReceptionistConfig GetReceptionistConfig(string workerType)
222167
/// </remarks>
223168
/// <param name="workerType">The type of the worker to create.</param>
224169
/// <returns>The Locator connection configuration</returns>
225-
protected virtual LocatorConfig GetLocatorConfig(string workerType)
170+
protected abstract LocatorConfig GetLocatorConfig(string workerType);
171+
172+
/// <summary>
173+
/// Selects which deployment to connect to.
174+
/// </summary>
175+
/// <param name="deployments">The list of deployments.</param>
176+
/// <returns>The name of the deployment to connect to.</returns>
177+
protected virtual string SelectDeploymentName(DeploymentList deployments)
226178
{
227-
var commandLineArguments = Environment.GetCommandLineArgs();
228-
var commandLineArgs = CommandLineUtility.ParseCommandLineArgs(commandLineArguments);
229-
var config = LocatorConfig.CreateConnectionConfigFromCommandLine(commandLineArgs);
230-
config.WorkerType = workerType;
231-
config.WorkerId = CreateNewWorkerId(workerType);
232-
return config;
179+
return null;
233180
}
234181

235182
protected virtual void HandleWorkerConnectionEstablished()
Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using Improbable.Gdk.Core;
22
using Improbable.Worker;
3-
#if UNITY_EDITOR
4-
using UnityEditor;
5-
#endif
63

74
namespace Improbable.Gdk.Mobile
85
{
@@ -17,26 +14,19 @@ protected override ReceptionistConfig GetReceptionistConfig(string workerType)
1714
ReceptionistHost = GetHostIp(),
1815
WorkerType = workerType,
1916
WorkerId = CreateNewWorkerId(workerType),
20-
UseExternalIp = UseExternalIp,
17+
UseExternalIp = true,
2118
LinkProtocol = NetworkConnectionType.Tcp,
2219
};
2320
}
2421

25-
protected override bool ShouldUseLocator()
22+
protected override LocatorConfig GetLocatorConfig(string workerType)
2623
{
27-
// We currently only support local deployments for Mobile
28-
return false;
24+
throw new System.NotImplementedException("The locator flow is currently not available for mobile workers.");
2925
}
3026

31-
private void OnValidate()
27+
protected override bool ShouldUseLocator()
3228
{
33-
if (UseExternalIp == false)
34-
{
35-
UseExternalIp = true;
36-
#if UNITY_EDITOR
37-
EditorUtility.DisplayDialog("Invalid configuration", "Use External Ip needs to be set to true for mobile client workers.", "Ok");
38-
#endif
39-
}
29+
return false;
4030
}
4131
}
4232
}

0 commit comments

Comments
 (0)