-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathSpacetimeDBClient.cs
More file actions
1130 lines (971 loc) · 45.4 KB
/
SpacetimeDBClient.cs
File metadata and controls
1130 lines (971 loc) · 45.4 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using SpacetimeDB.BSATN;
using SpacetimeDB.Internal;
using SpacetimeDB.ClientApi;
using Thread = System.Threading.Thread;
using System.Diagnostics;
namespace SpacetimeDB
{
public sealed class DbConnectionBuilder<DbConnection>
where DbConnection : IDbConnection, new()
{
readonly DbConnection conn = new();
string? uri;
string? nameOrAddress;
string? token;
Compression? compression;
bool light;
public DbConnection Build()
{
if (uri == null)
{
throw new InvalidOperationException("Building DbConnection with a null uri. Call WithUri() first.");
}
if (nameOrAddress == null)
{
throw new InvalidOperationException("Building DbConnection with a null nameOrAddress. Call WithModuleName() first.");
}
conn.Connect(token, uri, nameOrAddress, compression ?? Compression.Brotli, light);
#if UNITY_5_3_OR_NEWER
if (SpacetimeDBNetworkManager._instance != null)
{
SpacetimeDBNetworkManager._instance.AddConnection(conn);
}
#endif
return conn;
}
public DbConnectionBuilder<DbConnection> WithUri(string uri)
{
this.uri = uri;
return this;
}
public DbConnectionBuilder<DbConnection> WithModuleName(string nameOrAddress)
{
this.nameOrAddress = nameOrAddress;
return this;
}
public DbConnectionBuilder<DbConnection> WithToken(string? token)
{
this.token = token;
return this;
}
public DbConnectionBuilder<DbConnection> WithCompression(Compression compression)
{
this.compression = compression;
return this;
}
public DbConnectionBuilder<DbConnection> WithLightMode(bool light)
{
this.light = light;
return this;
}
public delegate void ConnectCallback(DbConnection conn, Identity identity, string token);
public DbConnectionBuilder<DbConnection> OnConnect(ConnectCallback cb)
{
conn.AddOnConnect((identity, token) => cb(conn, identity, token));
return this;
}
public delegate void ConnectErrorCallback(Exception e);
public DbConnectionBuilder<DbConnection> OnConnectError(ConnectErrorCallback cb)
{
conn.AddOnConnectError(e => cb(e));
return this;
}
public delegate void DisconnectCallback(DbConnection conn, Exception? e);
public DbConnectionBuilder<DbConnection> OnDisconnect(DisconnectCallback cb)
{
conn.AddOnDisconnect(e => cb(conn, e));
return this;
}
}
public interface IDbConnection
{
internal void Connect(string? token, string uri, string addressOrName, Compression compression, bool light);
internal void AddOnConnect(Action<Identity, string> cb);
internal void AddOnConnectError(WebSocket.ConnectErrorEventHandler cb);
internal void AddOnDisconnect(WebSocket.CloseEventHandler cb);
internal void LegacySubscribe(ISubscriptionHandle handle, string[] querySqls);
internal QueryId? Subscribe(ISubscriptionHandle handle, string[] querySqls);
internal void Unsubscribe(QueryId queryId);
void FrameTick();
void Disconnect();
internal Task<T[]> RemoteQuery<T>(string query) where T : IStructuralReadWrite, new();
void InternalCallReducer<T>(T args, CallReducerFlags flags)
where T : IReducerArgs, new();
}
public abstract class DbConnectionBase<DbConnection, Tables, Reducer> : IDbConnection
where DbConnection : DbConnectionBase<DbConnection, Tables, Reducer>, new()
where Tables : RemoteTablesBase
{
public static DbConnectionBuilder<DbConnection> Builder() => new();
internal event Action<Identity, string>? onConnect;
/// <summary>
/// Called when an exception occurs when sending a message.
/// </summary>
[Obsolete]
public event Action<Exception>? onSendError;
/// <summary>
/// Dictionary of legacy subscriptions, keyed by request ID rather than query ID.
/// Only used for `SubscribeToAllTables()`.
/// </summary>
private readonly Dictionary<uint, ISubscriptionHandle> legacySubscriptions = new();
/// <summary>
/// Dictionary of subscriptions, keyed by query ID.
/// </summary>
private readonly Dictionary<uint, ISubscriptionHandle> subscriptions = new();
/// <summary>
/// Allocates query IDs.
/// </summary>
private UintAllocator queryIdAllocator;
public readonly ConnectionId ConnectionId = ConnectionId.Random();
public Identity? Identity { get; private set; }
internal WebSocket webSocket;
private bool connectionClosed;
public abstract Tables Db { get; }
protected abstract Reducer ToReducer(TransactionUpdate update);
protected abstract IEventContext ToEventContext(Event<Reducer> Event);
protected abstract IReducerEventContext ToReducerEventContext(ReducerEvent<Reducer> reducerEvent);
protected abstract ISubscriptionEventContext MakeSubscriptionEventContext();
protected abstract IErrorContext ToErrorContext(Exception errorContext);
private readonly Dictionary<Guid, TaskCompletionSource<OneOffQueryResponse>> waitingOneOffQueries = new();
private bool isClosing;
private readonly Thread networkMessageParseThread;
public readonly Stats stats = new();
protected DbConnectionBase()
{
var options = new WebSocket.ConnectOptions
{
//v1.bin.spacetimedb
//v1.text.spacetimedb
Protocol = "v1.bsatn.spacetimedb"
};
webSocket = new WebSocket(options);
webSocket.OnMessage += OnMessageReceived;
webSocket.OnSendError += a => onSendError?.Invoke(a);
#if UNITY_5_3_OR_NEWER
webSocket.OnClose += (e) =>
{
if (SpacetimeDBNetworkManager._instance != null)
{
SpacetimeDBNetworkManager._instance.RemoveConnection(this);
}
};
#if UNITY_WEBGL && !UNITY_EDITOR
if (SpacetimeDBNetworkManager._instance != null)
SpacetimeDBNetworkManager._instance.StartCoroutine(ParseMessages());
#endif
#endif
#if !(UNITY_WEBGL && !UNITY_EDITOR)
// For targets other than webgl we start a thread to parse messages
networkMessageParseThread = new Thread(ParseMessages);
networkMessageParseThread.Start();
#endif
}
internal struct UnparsedMessage
{
/// <summary>
/// The bytes of the message.
/// </summary>
public byte[] bytes;
/// <summary>
/// The timestamp the message came off the wire.
/// </summary>
public DateTime timestamp;
/// <summary>
/// The ID assigned by the message parsing queue tracker.
/// </summary>
public uint parseQueueTrackerId;
}
internal struct ParsedDatabaseUpdate
{
// Map: table handles -> (primary key -> IStructuralReadWrite).
// If a particular table has no primary key, the "primary key" is just the row itself.
// This is valid because any [SpacetimeDB.Type] automatically has a correct Equals and HashSet implementation.
public Dictionary<IRemoteTableHandle, MultiDictionaryDelta<object, IStructuralReadWrite>> Updates;
// Can't override the default constructor. Make sure you use this one!
public static ParsedDatabaseUpdate New()
{
ParsedDatabaseUpdate result;
result.Updates = new();
return result;
}
public MultiDictionaryDelta<object, IStructuralReadWrite> DeltaForTable(IRemoteTableHandle table)
{
if (!Updates.TryGetValue(table, out var delta))
{
delta = new MultiDictionaryDelta<object, IStructuralReadWrite>(EqualityComparer<object>.Default, EqualityComparer<object>.Default);
Updates[table] = delta;
}
return delta;
}
}
internal struct ParsedMessage
{
public ServerMessage message;
public ParsedDatabaseUpdate dbOps;
public DateTime receiveTimestamp;
public uint applyQueueTrackerId;
public ReducerEvent<Reducer>? reducerEvent;
}
private readonly BlockingCollection<UnparsedMessage> _parseQueue =
new(new ConcurrentQueue<UnparsedMessage>());
private readonly BlockingCollection<ParsedMessage> _applyQueue =
new(new ConcurrentQueue<ParsedMessage>());
internal static bool IsTesting;
internal bool HasMessageToApply => _applyQueue.Count > 0;
private readonly CancellationTokenSource _parseCancellationTokenSource = new();
private CancellationToken _parseCancellationToken => _parseCancellationTokenSource.Token;
/// <summary>
/// Decode a row for a table, producing a primary key.
/// If the table has a specific column marked `#[primary_key]`, use that.
/// If not, the BSATN for the entire row is used instead.
/// </summary>
/// <param name="table"></param>
/// <param name="reader"></param>
/// <param name="primaryKey"></param>
/// <returns></returns>
internal static IStructuralReadWrite Decode(IRemoteTableHandle table, BinaryReader reader, out object primaryKey)
{
var obj = table.DecodeValue(reader);
// TODO(1.1): we should exhaustively check that GenericEqualityComparer works
// for all types that are allowed to be primary keys.
var primaryKey_ = table.GetPrimaryKey(obj);
primaryKey_ ??= obj;
primaryKey = primaryKey_;
return obj;
}
private static readonly Status Committed = new Status.Committed(default);
private static readonly Status OutOfEnergy = new Status.OutOfEnergy(default);
internal enum CompressionAlgos : byte
{
None = 0,
Brotli = 1,
Gzip = 2,
}
private static BrotliStream BrotliReader(Stream stream)
{
return new BrotliStream(stream, CompressionMode.Decompress);
}
private static GZipStream GzipReader(Stream stream)
{
return new GZipStream(stream, CompressionMode.Decompress);
}
private static ServerMessage DecompressDecodeMessage(byte[] bytes)
{
using var stream = new MemoryStream(bytes);
// The stream will never be empty. It will at least contain the compression algo.
var compression = (CompressionAlgos)stream.ReadByte();
// Conditionally decompress and decode.
Stream decompressedStream = compression switch
{
CompressionAlgos.None => stream,
CompressionAlgos.Brotli => BrotliReader(stream),
CompressionAlgos.Gzip => GzipReader(stream),
_ => throw new InvalidOperationException("Unknown compression type"),
};
// TODO: consider pooling these.
// DO NOT TRY TO TAKE THIS OUT. The BrotliStream ReadByte() implementation allocates an array
// PER BYTE READ. You have to do it all at once to avoid that problem.
MemoryStream memoryStream = new MemoryStream();
decompressedStream.CopyTo(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
return new ServerMessage.BSATN().Read(new BinaryReader(memoryStream));
}
private static QueryUpdate DecompressDecodeQueryUpdate(CompressableQueryUpdate update)
{
Stream decompressedStream;
switch (update)
{
case CompressableQueryUpdate.Uncompressed(var qu):
return qu;
case CompressableQueryUpdate.Brotli(var bytes):
decompressedStream = BrotliReader(new MemoryStream(bytes.ToArray()));
break;
case CompressableQueryUpdate.Gzip(var bytes):
decompressedStream = GzipReader(new MemoryStream(bytes.ToArray()));
break;
default:
throw new InvalidOperationException();
}
// TODO: consider pooling these.
// DO NOT TRY TO TAKE THIS OUT. The BrotliStream ReadByte() implementation allocates an array
// PER BYTE READ. You have to do it all at once to avoid that problem.
MemoryStream memoryStream = new MemoryStream();
decompressedStream.CopyTo(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
return new QueryUpdate.BSATN().Read(new BinaryReader(memoryStream));
}
/// <summary>
/// Prepare to read a BsatnRowList.
///
/// This could return an IEnumerable, but we return the reader and row count directly to avoid an allocation.
/// It is legitimate to repeatedly call <c>IStructuralReadWrite.Read<T></c> <c>rowCount</c> times on the resulting
/// BinaryReader:
/// Our decoding infrastructure guarantees that reading a value consumes the correct number of bytes
/// from the BinaryReader. (This is easy because BSATN doesn't have padding.)
///
/// Previously here we were using LINQ to do what we're now doing with a custsom reader.
///
/// Why are we no longer using LINQ?
///
/// The calls in question, namely `Skip().Take()`, were fast under the Mono runtime,
/// but *much* slower when compiled AOT with IL2CPP.
/// Apparently Mono's JIT is smart enough to optimize away these LINQ ops,
/// resulting in a linear scan of the `BsatnRowList`.
/// Unfortunately IL2CPP could not, resulting in a quadratic scan.
/// See: https://github.com/clockworklabs/com.clockworklabs.spacetimedbsdk/pull/306
/// </summary>
/// <param name="list"></param>
/// <returns>A reader for the rows of the list and a count of rows.</returns>
private static (BinaryReader reader, int rowCount) ParseRowList(BsatnRowList list) =>
(
new BinaryReader(new ListStream(list.RowsData)),
list.SizeHint switch
{
RowSizeHint.FixedSize(var size) => list.RowsData.Count / size,
RowSizeHint.RowOffsets(var offsets) => offsets.Count,
_ => throw new NotImplementedException()
}
);
/// <summary>
/// Get a description of a message suitable for storing in the tracker metadata.
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
internal string TrackerMetadataForMessage(ServerMessage message) => message switch
{
ServerMessage.TransactionUpdate(var transactionUpdate) => $"type={nameof(ServerMessage.TransactionUpdate)},reducer={transactionUpdate.ReducerCall.ReducerName}",
_ => $"type={message.GetType().Name}"
};
#if UNITY_WEBGL && !UNITY_EDITOR
internal IEnumerator ParseMessages()
#else
internal void ParseMessages()
#endif
{
while (!isClosing)
{
#if UNITY_WEBGL && !UNITY_EDITOR
yield return null;
while (_messageQueue.Count > 0)
#endif
try
{
var message = _parseQueue.Take(_parseCancellationToken);
var parsedMessage = ParseMessage(message);
_applyQueue.Add(parsedMessage, _parseCancellationToken);
}
catch (OperationCanceledException)
{
#if UNITY_WEBGL && !UNITY_EDITOR
break;
#else
return; // Normal shutdown
#endif
}
}
IEnumerable<(IRemoteTableHandle, TableUpdate)> GetTables(DatabaseUpdate updates)
{
foreach (var update in updates.Tables)
{
var tableName = update.TableName;
var table = Db.GetTable(tableName);
if (table == null)
{
Log.Error($"Unknown table name: {tableName}");
continue;
}
yield return (table, update);
}
}
ParsedDatabaseUpdate ParseLegacySubscription(InitialSubscription initSub)
{
var dbOps = ParsedDatabaseUpdate.New();
// This is all of the inserts
int cap = initSub.DatabaseUpdate.Tables.Sum(a => (int)a.NumRows);
// First apply all of the state
foreach (var (table, update) in GetTables(initSub.DatabaseUpdate))
{
ParseInsertOnlyTable(table, update, dbOps);
}
return dbOps;
}
/// <summary>
/// TODO: the dictionary is here for backwards compatibility and can be removed
/// once we get rid of legacy subscriptions.
/// </summary>
ParsedDatabaseUpdate ParseSubscribeMultiApplied(SubscribeMultiApplied subscribeMultiApplied)
{
var dbOps = ParsedDatabaseUpdate.New();
foreach (var (table, update) in GetTables(subscribeMultiApplied.Update))
{
ParseInsertOnlyTable(table, update, dbOps);
}
return dbOps;
}
void ParseInsertOnlyTable(IRemoteTableHandle table, TableUpdate update, ParsedDatabaseUpdate dbOps)
{
var delta = dbOps.DeltaForTable(table);
foreach (var cqu in update.Updates)
{
var qu = DecompressDecodeQueryUpdate(cqu);
if (qu.Deletes.RowsData.Count > 0)
{
Log.Warn("Non-insert during an insert-only server message!");
}
var (insertReader, insertRowCount) = ParseRowList(qu.Inserts);
for (var i = 0; i < insertRowCount; i++)
{
var obj = Decode(table, insertReader, out var pk);
delta.Add(pk, obj);
}
}
}
void ParseDeleteOnlyTable(IRemoteTableHandle table, TableUpdate update, ParsedDatabaseUpdate dbOps)
{
var delta = dbOps.DeltaForTable(table);
foreach (var cqu in update.Updates)
{
var qu = DecompressDecodeQueryUpdate(cqu);
if (qu.Inserts.RowsData.Count > 0)
{
Log.Warn("Non-delete during a delete-only operation!");
}
var (deleteReader, deleteRowCount) = ParseRowList(qu.Deletes);
for (var i = 0; i < deleteRowCount; i++)
{
var obj = Decode(table, deleteReader, out var pk);
delta.Remove(pk, obj);
}
}
}
void ParseTable(IRemoteTableHandle table, TableUpdate update, ParsedDatabaseUpdate dbOps)
{
var delta = dbOps.DeltaForTable(table);
foreach (var cqu in update.Updates)
{
var qu = DecompressDecodeQueryUpdate(cqu);
// Because we are accumulating into a MultiDictionaryDelta that will be applied all-at-once
// to the table, it doesn't matter that we call Add before Remove here.
var (insertReader, insertRowCount) = ParseRowList(qu.Inserts);
for (var i = 0; i < insertRowCount; i++)
{
var obj = Decode(table, insertReader, out var pk);
delta.Add(pk, obj);
}
var (deleteReader, deleteRowCount) = ParseRowList(qu.Deletes);
for (var i = 0; i < deleteRowCount; i++)
{
var obj = Decode(table, deleteReader, out var pk);
delta.Remove(pk, obj);
}
}
}
ParsedDatabaseUpdate ParseUnsubscribeMultiApplied(UnsubscribeMultiApplied unsubMultiApplied)
{
var dbOps = ParsedDatabaseUpdate.New();
foreach (var (table, update) in GetTables(unsubMultiApplied.Update))
{
ParseDeleteOnlyTable(table, update, dbOps);
}
return dbOps;
}
ParsedDatabaseUpdate ParseDatabaseUpdate(DatabaseUpdate updates)
{
var dbOps = ParsedDatabaseUpdate.New();
foreach (var (table, update) in GetTables(updates))
{
ParseTable(table, update, dbOps);
}
return dbOps;
}
void ParseOneOffQuery(OneOffQueryResponse resp)
{
/// This case does NOT produce a list of DBOps, because it should not modify the client cache state!
var messageId = new Guid(resp.MessageId.ToArray());
if (!waitingOneOffQueries.Remove(messageId, out var resultSource))
{
Log.Error($"Response to unknown one-off-query: {messageId}");
return;
}
resultSource.SetResult(resp);
}
ParsedMessage ParseMessage(UnparsedMessage unparsed)
{
var dbOps = ParsedDatabaseUpdate.New();
var message = DecompressDecodeMessage(unparsed.bytes);
var trackerMetadata = TrackerMetadataForMessage(message);
stats.ParseMessageQueueTracker.FinishTrackingRequest(unparsed.parseQueueTrackerId, trackerMetadata);
var parseStart = DateTime.UtcNow;
ReducerEvent<Reducer>? reducerEvent = default;
switch (message)
{
case ServerMessage.InitialSubscription(var initSub):
stats.SubscriptionRequestTracker.FinishTrackingRequest(initSub.RequestId, unparsed.timestamp);
dbOps = ParseLegacySubscription(initSub);
break;
case ServerMessage.SubscribeApplied(var subscribeApplied):
break;
case ServerMessage.SubscribeMultiApplied(var subscribeMultiApplied):
stats.SubscriptionRequestTracker.FinishTrackingRequest(subscribeMultiApplied.RequestId, unparsed.timestamp);
dbOps = ParseSubscribeMultiApplied(subscribeMultiApplied);
break;
case ServerMessage.SubscriptionError(var subscriptionError):
// do nothing; main thread will warn.
if (subscriptionError.RequestId.HasValue)
{
stats.SubscriptionRequestTracker.FinishTrackingRequest(subscriptionError.RequestId.Value, unparsed.timestamp);
}
break;
case ServerMessage.UnsubscribeApplied(var unsubscribeApplied):
// do nothing; main thread will warn.
break;
case ServerMessage.UnsubscribeMultiApplied(var unsubscribeMultiApplied):
stats.SubscriptionRequestTracker.FinishTrackingRequest(unsubscribeMultiApplied.RequestId, unparsed.timestamp);
dbOps = ParseUnsubscribeMultiApplied(unsubscribeMultiApplied);
break;
case ServerMessage.TransactionUpdate(var transactionUpdate):
// Convert the generic event arguments in to a domain specific event object
var hostDuration = (TimeSpan)transactionUpdate.TotalHostExecutionDuration;
stats.AllReducersTracker.InsertRequest(hostDuration, $"reducer={transactionUpdate.ReducerCall.ReducerName}");
try
{
reducerEvent = new(
(DateTimeOffset)transactionUpdate.Timestamp,
transactionUpdate.Status switch
{
UpdateStatus.Committed => Committed,
UpdateStatus.OutOfEnergy => OutOfEnergy,
UpdateStatus.Failed(var reason) => new Status.Failed(reason),
_ => throw new InvalidOperationException()
},
transactionUpdate.CallerIdentity,
transactionUpdate.CallerConnectionId,
transactionUpdate.EnergyQuantaUsed.Quanta,
ToReducer(transactionUpdate));
}
catch (Exception)
{
// Failing to parse the ReducerEvent is fine, it just means we should
// call downstream stuff with an UnknownTransaction.
// See ApplyMessage
}
var callerIdentity = transactionUpdate.CallerIdentity;
if (callerIdentity == Identity && transactionUpdate.CallerConnectionId == ConnectionId)
{
// This was a request that we initiated
var requestId = transactionUpdate.ReducerCall.RequestId;
// Make sure we mark the request as having finished when it came off the wire.
// That's why we use unparsed.timestamp, rather than DateTime.UtcNow.
// See ReducerRequestTracker's comment.
if (!stats.ReducerRequestTracker.FinishTrackingRequest(requestId, unparsed.timestamp))
{
Log.Warn($"Failed to finish tracking reducer request: {requestId}");
}
}
if (transactionUpdate.Status is UpdateStatus.Committed(var committed))
{
dbOps = ParseDatabaseUpdate(committed);
}
break;
case ServerMessage.TransactionUpdateLight(var update):
dbOps = ParseDatabaseUpdate(update.Update);
break;
case ServerMessage.IdentityToken(var identityToken):
break;
case ServerMessage.OneOffQueryResponse(var resp):
ParseOneOffQuery(resp);
break;
default:
throw new InvalidOperationException();
}
stats.ParseMessageTracker.InsertRequest(parseStart, trackerMetadata);
var applyTracker = stats.ApplyMessageQueueTracker.StartTrackingRequest(trackerMetadata);
return new ParsedMessage { message = message, dbOps = dbOps, receiveTimestamp = unparsed.timestamp, applyQueueTrackerId = applyTracker, reducerEvent = reducerEvent };
}
}
public void Disconnect()
{
isClosing = true;
connectionClosed = true;
// Only try to close if the connection is active
if (webSocket.IsConnected)
{
webSocket.Close();
}
_parseCancellationTokenSource.Cancel();
}
/// <summary>
/// Connect to a remote spacetime instance.
/// </summary>
/// <param name="uri"> URI of the SpacetimeDB server (ex: https://testnet.spacetimedb.com)
/// <param name="addressOrName">The name or address of the database to connect to</param>
void IDbConnection.Connect(string? token, string uri, string addressOrName, Compression compression, bool light)
{
isClosing = false;
uri = uri.Replace("http://", "ws://");
uri = uri.Replace("https://", "wss://");
if (!uri.StartsWith("ws://") && !uri.StartsWith("wss://"))
{
uri = $"ws://{uri}";
}
// Things fail surprisingly if we have a trailing slash, because we later manually append strings
// like `/foo` and then end up with `//` in the URI.
uri = uri.TrimEnd('/');
Log.Info($"SpacetimeDBClient: Connecting to {uri} {addressOrName}");
if (!IsTesting)
{
#if UNITY_WEBGL && !UNITY_EDITOR
async Task Function()
#else
Task.Run(async () =>
#endif
{
try
{
await webSocket.Connect(token, uri, addressOrName, ConnectionId, compression, light);
}
catch (Exception e)
{
if (connectionClosed)
{
Log.Info("Connection closed gracefully.");
return;
}
Log.Exception(e);
}
#if UNITY_WEBGL && !UNITY_EDITOR
}
_ = Function();
#else
});
#endif
}
}
private void ApplyUpdate(IEventContext eventContext, ParsedDatabaseUpdate dbOps)
{
// First trigger OnBeforeDelete
foreach (var (table, update) in dbOps.Updates)
{
table.PreApply(eventContext, update);
}
foreach (var (table, update) in dbOps.Updates)
{
table.Apply(eventContext, update);
}
foreach (var (table, _) in dbOps.Updates)
{
table.PostApply(eventContext);
}
}
protected abstract bool Dispatch(IReducerEventContext context, Reducer reducer);
private void ApplyMessage(ParsedMessage parsed)
{
var message = parsed.message;
var dbOps = parsed.dbOps;
var timestamp = parsed.receiveTimestamp;
stats.ApplyMessageQueueTracker.FinishTrackingRequest(parsed.applyQueueTrackerId);
var applyStart = DateTime.UtcNow;
switch (message)
{
case ServerMessage.InitialSubscription(var initialSubscription):
{
var eventContext = MakeSubscriptionEventContext();
var legacyEventContext = ToEventContext(new Event<Reducer>.SubscribeApplied());
ApplyUpdate(legacyEventContext, dbOps);
if (legacySubscriptions.TryGetValue(initialSubscription.RequestId, out var subscription))
{
try
{
subscription.OnApplied(eventContext, new SubscriptionAppliedType.LegacyActive(new()));
}
catch (Exception e)
{
Log.Exception(e);
}
}
break;
}
case ServerMessage.SubscribeApplied(var subscribeApplied):
Log.Warn($"Unexpected SubscribeApplied (we only expect to get SubscribeMultiApplied): {subscribeApplied}");
break;
case ServerMessage.SubscribeMultiApplied(var subscribeMultiApplied):
{
var eventContext = MakeSubscriptionEventContext();
var legacyEventContext = ToEventContext(new Event<Reducer>.SubscribeApplied());
ApplyUpdate(legacyEventContext, dbOps);
if (subscriptions.TryGetValue(subscribeMultiApplied.QueryId.Id, out var subscription))
{
try
{
subscription.OnApplied(eventContext, new SubscriptionAppliedType.Active(subscribeMultiApplied.QueryId));
}
catch (Exception e)
{
Log.Exception(e);
}
}
break;
}
case ServerMessage.SubscriptionError(var subscriptionError):
{
Log.Warn($"Subscription Error: ${subscriptionError.Error}");
// TODO: should I use a more specific exception type here?
var exception = new Exception(subscriptionError.Error);
var eventContext = ToErrorContext(exception);
var legacyEventContext = ToEventContext(new Event<Reducer>.SubscribeError(exception));
ApplyUpdate(legacyEventContext, dbOps);
if (subscriptionError.QueryId.HasValue)
{
if (subscriptions.TryGetValue(subscriptionError.QueryId.Value, out var subscription))
{
try
{
subscription.OnError(eventContext);
}
catch (Exception e)
{
Log.Exception(e);
}
}
}
else
{
Log.Warn("Received general subscription failure, disconnecting.");
Disconnect();
}
break;
}
case ServerMessage.UnsubscribeApplied(var unsubscribeApplied):
Log.Warn($"Unexpected UnsubscribeApplied (we only expect to get UnsubscribeMultiApplied): {unsubscribeApplied}");
break;
case ServerMessage.UnsubscribeMultiApplied(var unsubscribeMultiApplied):
{
var eventContext = MakeSubscriptionEventContext();
var legacyEventContext = ToEventContext(new Event<Reducer>.UnsubscribeApplied());
ApplyUpdate(legacyEventContext, dbOps);
if (subscriptions.TryGetValue(unsubscribeMultiApplied.QueryId.Id, out var subscription))
{
try
{
subscription.OnEnded(eventContext);
}
catch (Exception e)
{
Log.Exception(e);
}
}
}
break;
case ServerMessage.TransactionUpdateLight(var update):
{
var eventContext = ToEventContext(new Event<Reducer>.UnknownTransaction());
ApplyUpdate(eventContext, dbOps);
break;
}
case ServerMessage.TransactionUpdate(var transactionUpdate):
{
if (parsed.reducerEvent is { } reducerEvent)
{
var legacyEventContext = ToEventContext(new Event<Reducer>.Reducer(reducerEvent));
ApplyUpdate(legacyEventContext, dbOps);
var eventContext = ToReducerEventContext(reducerEvent);
Dispatch(eventContext, reducerEvent.Reducer);
// don't invoke OnUnhandledReducerError, that's [Obsolete].
}
else
{
var legacyEventContext = ToEventContext(new Event<Reducer>.UnknownTransaction());
ApplyUpdate(legacyEventContext, dbOps);
if (transactionUpdate.Status is UpdateStatus.Failed(string err))
{
var fiReducers = GetType().GetField("Reducers", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
var reducers = fiReducers.GetValue(this);
var fiEvent = reducers.GetType().GetField("InternalOnUnhandledReducerError", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var unhandledErrorEvent = fiEvent.GetValue(reducers) as Delegate;
if (unhandledErrorEvent != null)
{
unhandledErrorEvent.DynamicInvoke(null, new Exception(err));
}
}
}
break;
}
case ServerMessage.IdentityToken(var identityToken):
try
{
Identity = identityToken.Identity;
onConnect?.Invoke(identityToken.Identity, identityToken.Token);
}
catch (Exception e)
{
Log.Exception(e);
}
break;
case ServerMessage.OneOffQueryResponse:
/* OneOffQuery is async and handles its own responses */
break;
default:
throw new InvalidOperationException();
}
stats.ApplyMessageTracker.InsertRequest(applyStart, TrackerMetadataForMessage(message));
}
// Note: this method is called from unit tests.
internal void OnMessageReceived(byte[] bytes, DateTime timestamp)
{
_parseQueue.Add(new UnparsedMessage { bytes = bytes, timestamp = timestamp, parseQueueTrackerId = stats.ParseMessageQueueTracker.StartTrackingRequest() });
}
// TODO: this should become [Obsolete] but for now is used by autogenerated code.
void IDbConnection.InternalCallReducer<T>(T args, CallReducerFlags flags)
{
if (!webSocket.IsConnected)
{
Log.Error("Cannot call reducer, not connected to server!");
return;
}
webSocket.Send(new ClientMessage.CallReducer(new CallReducer(
args.ReducerName,
IStructuralReadWrite.ToBytes(args).ToList(),
stats.ReducerRequestTracker.StartTrackingRequest(args.ReducerName),
(byte)flags
)));
}
void IDbConnection.LegacySubscribe(ISubscriptionHandle handle, string[] querySqls)
{
if (!webSocket.IsConnected)
{
Log.Error("Cannot subscribe, not connected to server!");
return;
}
var id = stats.SubscriptionRequestTracker.StartTrackingRequest();
legacySubscriptions[id] = handle;
webSocket.Send(new ClientMessage.Subscribe(
new Subscribe
{
RequestId = id,
QueryStrings = querySqls.ToList()
}
));
}
QueryId? IDbConnection.Subscribe(ISubscriptionHandle handle, string[] querySqls)
{
if (!webSocket.IsConnected)
{
Log.Error("Cannot subscribe, not connected to server!");
return null;
}