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

Commit c50e13b

Browse files
authored
Typed command ids (#1372)
* Command request ID's are now typed and split between internal and normal Moved some files around to the right namespaces and locations * Made CommandRequestIds sortable * Changelog + Upgrade Guide
1 parent 49d8a43 commit c50e13b

File tree

49 files changed

+391
-221
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+391
-221
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- `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)
88
- `WorkerConnector` no longer destroys itself in `Dispose`. [#1365](https://github.com/spatialos/gdk-for-unity/pull/1365)
99
- `MultiThreadedSpatialOSConnectionHandler` and `SpatialOSConnectionHandlerBuilder.SetThreadingMode` have been removed. [#1367](https://github.com/spatialos/gdk-for-unity/pull/1367)
10+
- Command request IDs are now typed as `CommandRequestID` instead of `long`. [#1372](https://github.com/spatialos/gdk-for-unity/pull/1372)
1011

1112
### Added
1213

UPGRADE_GUIDE.md

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

33
## From `0.3.5` to `0.3.6`
44

5+
### Command Request ID's are now typed
6+
7+
The return value of `CommandSystem.SendCommand` has changed from `long` to `CommandRequestID`.
8+
A similar change has been made to all APIs that take request IDs as a parameter.
9+
10+
Anywhere you store request IDs, you need to change the type from `long` to `CommandRequestId`.
11+
512
### Multithreaded connection removed
613

714
The `MultiThreadedSpatialOSConnectionHandler` has been removed and the method `SpatialOSConnectionHandlerBuilder.SetThreadingMode` has been removed to reflect this change.

workers/unity/Packages/io.improbable.gdk.core/.codegen/Source/Generators/Core/CommandDiffDeserializerGenerator.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public static CodeWriter Generate(UnityComponentDetails componentDetails)
1414
{
1515
cgw.UsingDirectives(
1616
"Improbable.Gdk.Core",
17+
"Improbable.Gdk.Core.Commands",
1718
"Improbable.Worker.CInterop"
1819
);
1920

@@ -60,8 +61,9 @@ public void AddResponseToDiff(CommandResponseOp op, ViewDiff diff, CommandMetaDa
6061
rawResponse = {command.FqnResponseType}.Serialization.Deserialize(op.Response.SchemaData.Value.GetObject());
6162
}}
6263
63-
var commandContext = commandMetaData.GetContext<{command.FqnRequestType}>(ComponentId, {command.CommandIndex}, op.RequestId);
64-
commandMetaData.RemoveRequest(ComponentId, {command.CommandIndex}, op.RequestId);
64+
var internalRequestId = new InternalCommandRequestId(op.RequestId);
65+
var commandContext = commandMetaData.GetContext<{command.FqnRequestType}>(ComponentId, {command.CommandIndex}, internalRequestId);
66+
commandMetaData.RemoveRequest(ComponentId, {command.CommandIndex}, internalRequestId);
6567
6668
var response = new {command.PascalCaseName}.ReceivedResponse(
6769
commandContext.SendingEntity,

workers/unity/Packages/io.improbable.gdk.core/.codegen/Source/Generators/Core/CommandMetaDataStorageGenerator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public static CodeWriter Generate(UnityComponentDetails componentDetails)
1313
return CodeWriter.Populate(cgw =>
1414
{
1515
cgw.UsingDirectives(
16-
"Improbable.Gdk.Core"
16+
"Improbable.Gdk.Core",
17+
"Improbable.Gdk.Core.Commands"
1718
);
1819

1920
cgw.Namespace(componentDetails.Namespace, ns =>

workers/unity/Packages/io.improbable.gdk.core/.codegen/Source/Generators/Core/UnityCommandPayloadGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public Response(long requestId, string failureMessage)
115115
public readonly {commandDetails.FqnResponseType}? ResponsePayload;
116116
public readonly {commandDetails.FqnRequestType} RequestPayload;
117117
public readonly global::System.Object Context;
118-
public readonly long RequestId;
118+
public readonly CommandRequestId RequestId;
119119
120120
public ReceivedResponse(
121121
Unity.Entities.Entity sendingEntity,
@@ -125,7 +125,7 @@ public ReceivedResponse(
125125
{commandDetails.FqnResponseType}? response,
126126
{commandDetails.FqnRequestType} request,
127127
global::System.Object context,
128-
long requestId)
128+
CommandRequestId requestId)
129129
{{
130130
SendingEntity = sendingEntity;
131131
EntityId = entityId;
@@ -137,7 +137,7 @@ public ReceivedResponse(
137137
RequestId = requestId;
138138
}}
139139
140-
long IReceivedCommandResponse.RequestId => RequestId;
140+
CommandRequestId IReceivedCommandResponse.RequestId => RequestId;
141141
}}
142142
143143
public readonly struct RawReceivedResponse : IRawReceivedCommandResponse

workers/unity/Packages/io.improbable.gdk.core/Collections/ReceivedMessageListExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ internal static class ReceivedMessageListExtensions
77
{
88
// binary search for the command for given request ID
99
// invariant: lower <= target <= upper
10-
public static int? GetResponseIndex<T>(this MessageList<T> list, long requestId)
10+
public static int? GetResponseIndex<T>(this MessageList<T> list, CommandRequestId requestId)
1111
where T : struct, IReceivedCommandResponse
1212
{
1313
var targetId = requestId;

workers/unity/Packages/io.improbable.gdk.core/Commands/CommandComponents.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public interface IReceivedCommandResponse : IReceivedMessage
2323
/// Gets the request ID from the request. For use in generic methods.
2424
/// </summary>
2525
/// <returns> The request ID associated with the request </returns>
26-
long RequestId { get; }
26+
CommandRequestId RequestId { get; }
2727
}
2828

2929
public interface IRawReceivedCommandResponse

workers/unity/Packages/io.improbable.gdk.core/Commands/CommandDiffStorageBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public MessagesSpan<TResponse> GetResponses()
6969
return responseStorage.Slice();
7070
}
7171

72-
public MessagesSpan<TResponse> GetResponse(long requestId)
72+
public MessagesSpan<TResponse> GetResponse(CommandRequestId requestId)
7373
{
7474
if (!responsesSorted)
7575
{
@@ -83,15 +83,15 @@ public MessagesSpan<TResponse> GetResponse(long requestId)
8383
: MessagesSpan<TResponse>.Empty();
8484
}
8585

86-
private class RequestComparer<T> : IComparer<T> where T : struct, IReceivedCommandRequest
86+
private sealed class RequestComparer<T> : IComparer<T> where T : struct, IReceivedCommandRequest
8787
{
8888
public int Compare(T x, T y)
8989
{
9090
return x.EntityId.Id.CompareTo(y.EntityId.Id);
9191
}
9292
}
9393

94-
private class ResponseComparer<T> : IComparer<T> where T : struct, IReceivedCommandResponse
94+
private sealed class ResponseComparer<T> : IComparer<T> where T : struct, IReceivedCommandResponse
9595
{
9696
public int Compare(T x, T y)
9797
{

workers/unity/Packages/io.improbable.gdk.core/Worker/CommandMetaData.cs renamed to workers/unity/Packages/io.improbable.gdk.core/Commands/CommandMetaData.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
using System.Linq;
44
using Unity.Entities;
55

6-
namespace Improbable.Gdk.Core
6+
namespace Improbable.Gdk.Core.Commands
77
{
88
public readonly struct CommandContext<T>
99
{
1010
public readonly Entity SendingEntity;
1111
public readonly T Request;
1212
public readonly object Context;
13-
public readonly long RequestId;
13+
public readonly CommandRequestId RequestId;
1414

15-
public CommandContext(Entity sendingEntity, T request, object context, long requestId)
15+
public CommandContext(Entity sendingEntity, T request, object context, CommandRequestId requestId)
1616
{
1717
SendingEntity = sendingEntity;
1818
Request = request;
@@ -24,24 +24,24 @@ public CommandContext(Entity sendingEntity, T request, object context, long requ
2424
public class CommandMetaData
2525
{
2626
// Cache the types needed to instantiate a new CommandMetaData
27-
private static List<(uint componentId, Type command)> storageTypes;
27+
private static readonly List<(uint componentId, Type command)> StorageTypes;
2828

29-
private readonly HashSet<long> internalRequestIds = new HashSet<long>();
29+
private readonly HashSet<InternalCommandRequestId> internalRequestIds = new HashSet<InternalCommandRequestId>();
3030

3131
private readonly Dictionary<(uint componentId, uint commandId), ICommandMetaDataStorage> componentCommandToStorage =
3232
new Dictionary<(uint componentId, uint commandId), ICommandMetaDataStorage>();
3333

34-
public CommandMetaData()
34+
static CommandMetaData()
3535
{
36-
if (storageTypes == null)
37-
{
38-
storageTypes = ComponentDatabase.Metaclasses
39-
.SelectMany(type => type.Value.Commands
40-
.Select(c => (componentId: type.Value.ComponentId, command: c.MetaDataStorage)))
41-
.ToList();
42-
}
36+
StorageTypes = ComponentDatabase.Metaclasses
37+
.SelectMany(type => type.Value.Commands
38+
.Select(c => (componentId: type.Value.ComponentId, command: c.MetaDataStorage)))
39+
.ToList();
40+
}
4341

44-
foreach (var (componentId, type) in storageTypes)
42+
public CommandMetaData()
43+
{
44+
foreach (var (componentId, type) in StorageTypes)
4545
{
4646
var instance = (ICommandMetaDataStorage) Activator.CreateInstance(type);
4747
componentCommandToStorage.Add((componentId, instance.CommandId), instance);
@@ -50,7 +50,7 @@ public CommandMetaData()
5050
componentCommandToStorage.Add((0, 0), new WorldCommandMetaDataStorage());
5151
}
5252

53-
public void RemoveRequest(uint componentId, uint commandId, long internalRequestId)
53+
public void RemoveRequest(uint componentId, uint commandId, InternalCommandRequestId internalRequestId)
5454
{
5555
var commandMetaDataStorage = GetCommandDiffStorage(componentId, commandId);
5656
commandMetaDataStorage.RemoveMetaData(internalRequestId);
@@ -63,14 +63,14 @@ public void AddRequest<T>(uint componentId, uint commandId, in CommandContext<T>
6363
commandPayloadStorage.AddRequest(in context);
6464
}
6565

66-
public void AddInternalRequestId(uint componentId, uint commandId, long requestId, long internalRequestId)
66+
public void AddInternalRequestId(uint componentId, uint commandId, CommandRequestId requestId, InternalCommandRequestId internalRequestId)
6767
{
6868
internalRequestIds.Add(internalRequestId);
6969
var commandMetaDataStorage = GetCommandDiffStorage(componentId, commandId);
7070
commandMetaDataStorage.SetInternalRequestId(internalRequestId, requestId);
7171
}
7272

73-
public CommandContext<T> GetContext<T>(uint componentId, uint commandId, long internalRequestId)
73+
public CommandContext<T> GetContext<T>(uint componentId, uint commandId, InternalCommandRequestId internalRequestId)
7474
{
7575
var commandPayloadStorage = (ICommandPayloadStorage<T>) GetCommandDiffStorage(componentId, commandId);
7676
return commandPayloadStorage.GetPayload(internalRequestId);

workers/unity/Packages/io.improbable.gdk.core/Worker/CommandMetaData.cs.meta renamed to workers/unity/Packages/io.improbable.gdk.core/Commands/CommandMetaData.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)