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

Commit 0266b65

Browse files
author
Jamie Brynes
authored
Change up worker connector semantics slightly (#1365)
1 parent 3a1fb1c commit 0266b65

File tree

6 files changed

+76
-29
lines changed

6 files changed

+76
-29
lines changed

CHANGELOG.md

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

33
## Unreleased
44

5+
### Breaking Changes
6+
7+
- `WorkerConnector.HandleWorkerConnectionFailure` has been removed and `WorkerConnector.Connect` now throws exceptions for connection errors instead. [#1365](https://github.com/spatialos/gdk-for-unity/pull/1365)
8+
- `WorkerConnector` no longer destroys itself in `Dispose`. [#1365](https://github.com/spatialos/gdk-for-unity/pull/1365)
9+
510
### Added
611

712
- Added the ability to select a specific cluster for deployments in the [Deployment Launcher](https://documentation.improbable.io/gdk-for-unity/docs/deployment-launcher). [#1357](https://github.com/spatialos/gdk-for-unity/pull/1357)

UPGRADE_GUIDE.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,55 @@
11
# Upgrade Guide
22

3+
## From `0.3.5` to `0.3.6`
4+
5+
### WorkerConnector changes
6+
7+
There are two related changes that you will need to adjust for.
8+
9+
The `WorkerConnector.Connect` class would previously trigger the `HandleWorkerConnectionFailure` callback if the connection failed. We've now removed this callback, and `WorkerConnector.Connect` will now throw an exception for a failed connection.
10+
11+
For example:
12+
13+
```csharp
14+
public class MyWorkerConnector : WorkerConnector
15+
{
16+
public async void Start()
17+
{
18+
// Setup connection flow.
19+
var builder = ...;
20+
21+
await Connect(builder, new ForwardingDispatcher());
22+
}
23+
24+
protected override void HandleWorkerConnectionFailure(string errorMessage)
25+
{
26+
Debug.LogError(errorMessage);
27+
}
28+
}
29+
```
30+
31+
Would change to:
32+
33+
```csharp
34+
public class MyWorkerConnector : WorkerConnector
35+
{
36+
public async void Start()
37+
{
38+
// Setup connection flow.
39+
var builder = ...;
40+
41+
try
42+
{
43+
await Connect(builder, new ForwardingDispatcher());
44+
}
45+
catch (Exception e)
46+
{
47+
Debug.LogException(e);
48+
}
49+
}
50+
}
51+
```
52+
353
## From `0.3.4` to `0.3.5`
454

555
### Unity 2019.3 upgrade

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,8 @@ private async void Start()
4343
builder.SetConnectionFlow(new ReceptionistFlow(CreateNewWorkerId(WorkerUtils.UnityClient)));
4444
}
4545

46-
await Connect(builder, new ForwardingDispatcher()).ConfigureAwait(false);
47-
}
46+
await Connect(builder, new ForwardingDispatcher());
4847

49-
protected override void HandleWorkerConnectionEstablished()
50-
{
51-
WorkerUtils.AddClientSystems(Worker.World);
5248
if (level == null)
5349
{
5450
return;
@@ -57,6 +53,11 @@ protected override void HandleWorkerConnectionEstablished()
5753
levelInstance = Instantiate(level, transform.position, transform.rotation);
5854
}
5955

56+
protected override void HandleWorkerConnectionEstablished()
57+
{
58+
WorkerUtils.AddClientSystems(Worker.World);
59+
}
60+
6061
public override void Dispose()
6162
{
6263
if (levelInstance != null)

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,7 @@ private async void Start()
3737
.SetConnectionFlow(flow)
3838
.SetConnectionParameters(connectionParameters);
3939

40-
await Connect(builder, new ForwardingDispatcher()).ConfigureAwait(false);
41-
}
42-
43-
protected override void HandleWorkerConnectionEstablished()
44-
{
45-
WorkerUtils.AddGameLogicSystems(Worker.World);
40+
await Connect(builder, new ForwardingDispatcher());
4641

4742
if (level == null)
4843
{
@@ -52,6 +47,11 @@ protected override void HandleWorkerConnectionEstablished()
5247
levelInstance = Instantiate(level, transform.position, transform.rotation);
5348
}
5449

50+
protected override void HandleWorkerConnectionEstablished()
51+
{
52+
WorkerUtils.AddGameLogicSystems(Worker.World);
53+
}
54+
5555
public override void Dispose()
5656
{
5757
if (levelInstance != null)

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,7 @@ public async void Start()
3939
throw new ArgumentException("Received unsupported connection service.");
4040
}
4141

42-
await Connect(builder, new ForwardingDispatcher()).ConfigureAwait(false);
43-
}
44-
45-
protected override void HandleWorkerConnectionEstablished()
46-
{
47-
WorkerUtils.AddClientSystems(Worker.World);
42+
await Connect(builder, new ForwardingDispatcher());
4843

4944
if (level == null)
5045
{
@@ -54,6 +49,11 @@ protected override void HandleWorkerConnectionEstablished()
5449
levelInstance = Instantiate(level, transform.position, transform.rotation);
5550
}
5651

52+
protected override void HandleWorkerConnectionEstablished()
53+
{
54+
WorkerUtils.AddClientSystems(Worker.World);
55+
}
56+
5757
public override void Dispose()
5858
{
5959
if (levelInstance != null)

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,8 @@ protected async Task Connect(IConnectionHandlerBuilder builder, ILogDispatcher l
107107
PlayerLoopUtils.ResolveSystemGroups(Worker.World);
108108
ScriptBehaviourUpdateOrder.UpdatePlayerLoop(Worker.World, PlayerLoop.GetCurrentPlayerLoop());
109109
}
110-
catch (Exception e)
110+
catch (Exception)
111111
{
112-
logger.HandleLog(LogType.Error, new LogEvent("Failed to create worker")
113-
.WithException(e)
114-
.WithField("WorkerType", builder.WorkerType)
115-
.WithField("Message", e.Message)
116-
.WithField("Stacktrace", e.StackTrace));
117112
#if UNITY_EDITOR
118113
// Temporary warning to be replaced when we can reliably detect if a local runtime is running, or not.
119114
logger.HandleLog(LogType.Warning,
@@ -124,9 +119,10 @@ protected async Task Connect(IConnectionHandlerBuilder builder, ILogDispatcher l
124119
// A check is needed for the case that play mode is exited before the connection can complete.
125120
if (Application.isPlaying)
126121
{
127-
HandleWorkerConnectionFailure(e.Message);
128122
Dispose();
129123
}
124+
125+
throw;
130126
}
131127
finally
132128
{
@@ -155,10 +151,6 @@ protected virtual void HandleWorkerConnectionEstablished()
155151
{
156152
}
157153

158-
protected virtual void HandleWorkerConnectionFailure(string errorMessage)
159-
{
160-
}
161-
162154
private static async Task<WorkerInWorld> ConnectWithRetries(IConnectionHandlerBuilder connectionHandlerBuilder, int maxAttempts,
163155
ILogDispatcher logger, string workerType, Vector3 origin)
164156
{
@@ -251,7 +243,6 @@ public virtual void Dispose()
251243
RemoveFromPlayerLoop();
252244
Worker?.Dispose();
253245
Worker = null;
254-
UnityObjectDestroyer.Destroy(this);
255246
}
256247

257248
private void RemoveFromPlayerLoop()

0 commit comments

Comments
 (0)