Skip to content
This repository was archived by the owner on Oct 20, 2021. It is now read-only.

Commit 540be2a

Browse files
author
Paul Balaji
authored
Worker connector update (#56)
1 parent a5d1468 commit 540be2a

File tree

6 files changed

+118
-20
lines changed

6 files changed

+118
-20
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Changed
6+
7+
- Upgraded project to the new worker abstraction. [#56](https://github.com/spatialos/gdk-for-unity-blank-project/pull/56)
8+
59
## `0.2.3` - 2019-06-12
610

711
### Changed

gdk.pinned

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
eae6cf5dcdb72655bde2121614001cdf1dde51bf
1+
84243525d98aff511e7aa1f7703c37347017e386

workers/unity/Assets/Scripts/MetricSendSystem.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace BlankProject
99
[DisableAutoCreation]
1010
public class MetricSendSystem : ComponentSystem
1111
{
12-
private Connection connection;
12+
private WorkerSystem worker;
1313

1414
private DateTime timeOfNextUpdate;
1515
private DateTime timeOfLastUpdate;
@@ -32,7 +32,7 @@ public class MetricSendSystem : ComponentSystem
3232
protected override void OnCreate()
3333
{
3434
base.OnCreate();
35-
connection = World.GetExistingSystem<WorkerSystem>().Connection;
35+
worker = World.GetExistingSystem<WorkerSystem>();
3636

3737
targetFps = Application.targetFrameRate == -1
3838
? DefaultTargetFrameRate
@@ -47,7 +47,7 @@ protected override void OnCreate()
4747

4848
protected override void OnUpdate()
4949
{
50-
if (connection == null)
50+
if (worker == null)
5151
{
5252
return;
5353
}
@@ -59,7 +59,7 @@ protected override void OnUpdate()
5959
WorkerMetrics.GaugeMetrics["Unity used heap size"] = GC.GetTotalMemory(false);
6060
WorkerMetrics.Load = CalculateLoad();
6161

62-
connection.SendMetrics(WorkerMetrics);
62+
worker.SendMetrics(WorkerMetrics);
6363

6464
timeOfLastUpdate = DateTime.Now;
6565
timeOfNextUpdate = timeOfLastUpdate.AddSeconds(TimeBetweenMetricUpdatesSecs);
Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,66 @@
1+
using System;
12
using Improbable.Gdk.Core;
23
using Improbable.Gdk.Mobile;
34
using Improbable.Gdk.PlayerLifecycle;
5+
using UnityEngine;
46

57
namespace BlankProject
68
{
7-
public class MobileClientWorkerConnector : DefaultMobileWorkerConnector
9+
public class MobileClientWorkerConnector : WorkerConnector, MobileConnectionFlowInitializer.IMobileSettingsProvider
810
{
11+
#pragma warning disable 649
12+
[SerializeField] private string ipAddress;
13+
#pragma warning restore 649
14+
915
public const string WorkerType = "MobileClient";
1016

11-
private async void Start()
17+
public async void Start()
1218
{
13-
await Connect(WorkerType, new ForwardingDispatcher()).ConfigureAwait(false);
19+
var connParams = CreateConnectionParameters(WorkerType, new MobileConnectionParametersInitializer());
20+
21+
var flowInitializer = new MobileConnectionFlowInitializer(
22+
new MobileConnectionFlowInitializer.CommandLineSettingsProvider(),
23+
new MobileConnectionFlowInitializer.PlayerPrefsSettingsProvider(),
24+
this);
25+
26+
var builder = new SpatialOSConnectionHandlerBuilder()
27+
.SetConnectionParameters(connParams);
28+
29+
switch (flowInitializer.GetConnectionService())
30+
{
31+
case ConnectionService.Receptionist:
32+
builder.SetConnectionFlow(new ReceptionistFlow(CreateNewWorkerId(WorkerType),
33+
flowInitializer));
34+
break;
35+
case ConnectionService.AlphaLocator:
36+
builder.SetConnectionFlow(new AlphaLocatorFlow(flowInitializer));
37+
break;
38+
default:
39+
throw new ArgumentException("Received unsupported connection service.");
40+
}
41+
42+
await Connect(builder, new ForwardingDispatcher()).ConfigureAwait(false);
1443
}
1544

1645
protected override void HandleWorkerConnectionEstablished()
1746
{
1847
PlayerLifecycleHelper.AddClientSystems(Worker.World);
1948
}
49+
50+
public Option<string> GetReceptionistHostIp()
51+
{
52+
return string.IsNullOrEmpty(ipAddress) ? Option<string>.Empty : new Option<string>(ipAddress);
53+
}
54+
55+
public Option<string> GetDevAuthToken()
56+
{
57+
var token = Resources.Load<TextAsset>("DevAuthToken")?.text.Trim();
58+
return token ?? Option<string>.Empty;
59+
}
60+
61+
public Option<ConnectionService> GetConnectionService()
62+
{
63+
return Option<ConnectionService>.Empty;
64+
}
2065
}
2166
}
Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,52 @@
1-
using Improbable.Gdk.Core;
1+
using System;
2+
using Improbable.Gdk.Core;
23
using Improbable.Gdk.PlayerLifecycle;
34
using Improbable.Worker.CInterop;
5+
using UnityEngine;
46

57
namespace BlankProject
68
{
7-
public class UnityClientConnector : DefaultWorkerConnector
9+
public class UnityClientConnector : WorkerConnector
810
{
911
public const string WorkerType = "UnityClient";
10-
12+
1113
private async void Start()
1214
{
13-
await Connect(WorkerType, new ForwardingDispatcher()).ConfigureAwait(false);
15+
var connParams = CreateConnectionParameters(WorkerType);
16+
connParams.Network.ConnectionType = NetworkConnectionType.Kcp;
17+
18+
var builder = new SpatialOSConnectionHandlerBuilder()
19+
.SetConnectionParameters(connParams);
20+
21+
if (!Application.isEditor)
22+
{
23+
var initializer = new CommandLineConnectionFlowInitializer();
24+
switch (initializer.GetConnectionService())
25+
{
26+
case ConnectionService.Receptionist:
27+
builder.SetConnectionFlow(new ReceptionistFlow(CreateNewWorkerId(WorkerType), initializer));
28+
break;
29+
case ConnectionService.Locator:
30+
builder.SetConnectionFlow(new LocatorFlow(initializer));
31+
break;
32+
case ConnectionService.AlphaLocator:
33+
builder.SetConnectionFlow(new AlphaLocatorFlow(initializer));
34+
break;
35+
default:
36+
throw new ArgumentOutOfRangeException();
37+
}
38+
}
39+
else
40+
{
41+
builder.SetConnectionFlow(new ReceptionistFlow(CreateNewWorkerId(WorkerType)));
42+
}
43+
44+
await Connect(builder, new ForwardingDispatcher()).ConfigureAwait(false);
1445
}
1546

1647
protected override void HandleWorkerConnectionEstablished()
1748
{
1849
PlayerLifecycleHelper.AddClientSystems(Worker.World);
1950
}
20-
21-
protected override string SelectDeploymentName(DeploymentList deployments)
22-
{
23-
return deployments.Deployments[0].DeploymentName;
24-
}
2551
}
2652
}

workers/unity/Assets/Scripts/Workers/UnityGameLogicConnector.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,40 @@
22
using Improbable.Gdk.Core;
33
using Improbable.Gdk.PlayerLifecycle;
44
using Improbable.Gdk.TransformSynchronization;
5+
using Improbable.Worker.CInterop;
6+
using UnityEngine;
57

68
namespace BlankProject
79
{
8-
public class UnityGameLogicConnector : DefaultWorkerConnector
10+
public class UnityGameLogicConnector : WorkerConnector
911
{
1012
public const string WorkerType = "UnityGameLogic";
11-
13+
1214
private async void Start()
1315
{
1416
PlayerLifecycleConfig.CreatePlayerEntityTemplate = CreatePlayerEntityTemplate;
15-
await Connect(WorkerType, new ForwardingDispatcher()).ConfigureAwait(false);
17+
18+
IConnectionFlow flow;
19+
ConnectionParameters connectionParameters;
20+
21+
if (Application.isEditor)
22+
{
23+
flow = new ReceptionistFlow(CreateNewWorkerId(WorkerType));
24+
connectionParameters = CreateConnectionParameters(WorkerType);
25+
}
26+
else
27+
{
28+
flow = new ReceptionistFlow(CreateNewWorkerId(WorkerType),
29+
new CommandLineConnectionFlowInitializer());
30+
connectionParameters = CreateConnectionParameters(WorkerType,
31+
new CommandLineConnectionParameterInitializer());
32+
}
33+
34+
var builder = new SpatialOSConnectionHandlerBuilder()
35+
.SetConnectionFlow(flow)
36+
.SetConnectionParameters(connectionParameters);
37+
38+
await Connect(builder, new ForwardingDispatcher()).ConfigureAwait(false);
1639
}
1740

1841
protected override void HandleWorkerConnectionEstablished()

0 commit comments

Comments
 (0)