Skip to content

Commit

Permalink
implement websocket ids-no-names in c#
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Oct 30, 2024
1 parent b538f7c commit d83d981
Show file tree
Hide file tree
Showing 18 changed files with 302 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD.
// <auto-generated />

#nullable enable

using System;
using SpacetimeDB;

namespace SpacetimeDB.Types
{
[SpacetimeDB.Type]
public partial class IdentityConnected : IReducerArgs
{
uint IReducerArgs.ReducerIndex => 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD.
// <auto-generated />

#nullable enable

using System;
using SpacetimeDB;

namespace SpacetimeDB.Types
{
[SpacetimeDB.Type]
public partial class IdentityDisconnected : IReducerArgs
{
uint IReducerArgs.ReducerIndex => 1;
}
}
17 changes: 17 additions & 0 deletions examples~/quickstart/client/module_bindings/InitReducer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD.
// <auto-generated />

#nullable enable

using System;
using SpacetimeDB;

namespace SpacetimeDB.Types
{
[SpacetimeDB.Type]
public partial class Init : IReducerArgs
{
uint IReducerArgs.ReducerIndex => 2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace SpacetimeDB.Types
[SpacetimeDB.Type]
public partial class SendMessage : IReducerArgs
{
string IReducerArgs.ReducerName => "send_message";
uint IReducerArgs.ReducerIndex => 3;

public string Text = "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace SpacetimeDB.Types
[SpacetimeDB.Type]
public partial class SetName : IReducerArgs
{
string IReducerArgs.ReducerName => "set_name";
uint IReducerArgs.ReducerIndex => 4;

public string Name = "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,54 @@ public sealed class RemoteReducers : RemoteBase<DbConnection>
{
internal RemoteReducers(DbConnection conn, SetReducerFlags SetReducerFlags) : base(conn) { this.SetCallReducerFlags = SetReducerFlags; }
internal readonly SetReducerFlags SetCallReducerFlags;
public delegate void IdentityConnectedHandler(EventContext ctx);
public event IdentityConnectedHandler? OnIdentityConnected;

public void IdentityConnected()
{
conn.InternalCallReducer(new IdentityConnected { }, this.SetCallReducerFlags.IdentityConnectedFlags);
}

public bool InvokeIdentityConnected(EventContext ctx, IdentityConnected args)
{
if (OnIdentityConnected == null) return false;
OnIdentityConnected(
ctx
);
return true;
}
public delegate void IdentityDisconnectedHandler(EventContext ctx);
public event IdentityDisconnectedHandler? OnIdentityDisconnected;

public void IdentityDisconnected()
{
conn.InternalCallReducer(new IdentityDisconnected { }, this.SetCallReducerFlags.IdentityDisconnectedFlags);
}

public bool InvokeIdentityDisconnected(EventContext ctx, IdentityDisconnected args)
{
if (OnIdentityDisconnected == null) return false;
OnIdentityDisconnected(
ctx
);
return true;
}
public delegate void InitHandler(EventContext ctx);
public event InitHandler? OnInit;

public void Init()
{
conn.InternalCallReducer(new Init { }, this.SetCallReducerFlags.InitFlags);
}

public bool InvokeInit(EventContext ctx, Init args)
{
if (OnInit == null) return false;
OnInit(
ctx
);
return true;
}
public delegate void SendMessageHandler(EventContext ctx, string text);
public event SendMessageHandler? OnSendMessage;

Expand Down Expand Up @@ -104,6 +152,12 @@ public bool InvokeSetName(EventContext ctx, SetName args)
public sealed class SetReducerFlags
{
internal SetReducerFlags() { }
internal CallReducerFlags IdentityConnectedFlags;
public void IdentityConnected(CallReducerFlags flags) { this.IdentityConnectedFlags = flags; }
internal CallReducerFlags IdentityDisconnectedFlags;
public void IdentityDisconnected(CallReducerFlags flags) { this.IdentityDisconnectedFlags = flags; }
internal CallReducerFlags InitFlags;
public void Init(CallReducerFlags flags) { this.InitFlags = flags; }
internal CallReducerFlags SendMessageFlags;
public void SendMessage(CallReducerFlags flags) { this.SendMessageFlags = flags; }
internal CallReducerFlags SetNameFlags;
Expand All @@ -126,11 +180,12 @@ internal EventContext(DbConnection conn, Event<Reducer> reducerEvent) : base(con

[Type]
public partial record Reducer : TaggedEnum<(
IdentityConnected IdentityConnected,
IdentityDisconnected IdentityDisconnected,
Init Init,
SendMessage SendMessage,
SetName SetName,
Unit StdbNone,
Unit StdbIdentityConnected,
Unit StdbIdentityDisconnected
Unit StdbNone
)>;
public class DbConnection : DbConnectionBase<DbConnection, Reducer>
{
Expand All @@ -143,21 +198,20 @@ public DbConnection()
SetReducerFlags = new();
Reducers = new(this, this.SetReducerFlags);

clientDB.AddTable<Message>("message", Db.Message);
clientDB.AddTable<User>("user", Db.User);
clientDB.AddTable<Message>(0, Db.Message);
clientDB.AddTable<User>(1, Db.User);
}

protected override Reducer ToReducer(TransactionUpdate update)
protected override Reducer ToReducer(uint reducerIdx, TransactionUpdate update)
{
var encodedArgs = update.ReducerCall.Args;
return update.ReducerCall.ReducerName switch
{
"send_message" => new Reducer.SendMessage(BSATNHelpers.Decode<SendMessage>(encodedArgs)),
"set_name" => new Reducer.SetName(BSATNHelpers.Decode<SetName>(encodedArgs)),
"<none>" => new Reducer.StdbNone(default),
"__identity_connected__" => new Reducer.StdbIdentityConnected(default),
"__identity_disconnected__" => new Reducer.StdbIdentityDisconnected(default),
"" => new Reducer.StdbNone(default),
return reducerIdx switch {
0 => new Reducer.IdentityConnected(BSATNHelpers.Decode<IdentityConnected>(encodedArgs)),
1 => new Reducer.IdentityDisconnected(BSATNHelpers.Decode<IdentityDisconnected>(encodedArgs)),
2 => new Reducer.Init(BSATNHelpers.Decode<Init>(encodedArgs)),
3 => new Reducer.SendMessage(BSATNHelpers.Decode<SendMessage>(encodedArgs)),
4 => new Reducer.SetName(BSATNHelpers.Decode<SetName>(encodedArgs)),
4294967295 => new Reducer.StdbNone(default),
var reducer => throw new ArgumentOutOfRangeException("Reducer", $"Unknown reducer {reducer}")
};
}
Expand All @@ -168,13 +222,13 @@ protected override IEventContext ToEventContext(Event<Reducer> reducerEvent) =>
protected override bool Dispatch(IEventContext context, Reducer reducer)
{
var eventContext = (EventContext)context;
return reducer switch
{
return reducer switch {
Reducer.IdentityConnected(var args) => Reducers.InvokeIdentityConnected(eventContext, args),
Reducer.IdentityDisconnected(var args) => Reducers.InvokeIdentityDisconnected(eventContext, args),
Reducer.Init(var args) => Reducers.InvokeInit(eventContext, args),
Reducer.SendMessage(var args) => Reducers.InvokeSendMessage(eventContext, args),
Reducer.SetName(var args) => Reducers.InvokeSetName(eventContext, args),
Reducer.StdbNone or
Reducer.StdbIdentityConnected or
Reducer.StdbIdentityDisconnected => true,
Reducer.StdbNone => true,
_ => throw new ArgumentOutOfRangeException("Reducer", $"Unknown reducer {reducer}")
};
}
Expand Down
16 changes: 8 additions & 8 deletions src/ClientCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,29 @@ public class ClientCache
{
private readonly IDbConnection conn;

private readonly Dictionary<string, IRemoteTableHandle> tables = new();
private readonly Dictionary<uint, IRemoteTableHandle> tables = new();

public ClientCache(IDbConnection conn) => this.conn = conn;

public void AddTable<Row>(string name, IRemoteTableHandle table)
public void AddTable<Row>(uint tableIdx, IRemoteTableHandle table)
where Row : IDatabaseRow, new()
{
if (!tables.TryAdd(name, table))
if (!tables.TryAdd(tableIdx, table))
{
Log.Error($"Table with name already exists: {name}");
Log.Error($"Table with index already exists: {tableIdx}");
}

table.Initialize(name, conn);
table.Initialize(tableIdx, conn);
}

internal IRemoteTableHandle? GetTable(string name)
internal IRemoteTableHandle? GetTable(uint tableIdx)
{
if (tables.TryGetValue(name, out var table))
if (tables.TryGetValue(tableIdx, out var table))
{
return table;
}

Log.Error($"We don't know that this table is: {name}");
Log.Error($"We don't know that this table is: {tableIdx}");
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public interface IEventContext { }

public interface IReducerArgs : BSATN.IStructuralReadWrite
{
string ReducerName { get; }
uint ReducerIndex { get; }
}

[Type]
Expand Down
35 changes: 35 additions & 0 deletions src/SpacetimeDB/ClientApi/AfterConnecting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#nullable enable

using System;
using SpacetimeDB;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace SpacetimeDB.ClientApi
{
[SpacetimeDB.Type]
[DataContract]
public partial class AfterConnecting
{
[DataMember(Name = "identity_token")]
public SpacetimeDB.ClientApi.IdentityToken IdentityToken;
[DataMember(Name = "ids_to_names")]
public SpacetimeDB.ClientApi.IdsToNames IdsToNames;

public AfterConnecting(
SpacetimeDB.ClientApi.IdentityToken IdentityToken,
SpacetimeDB.ClientApi.IdsToNames IdsToNames
)
{
this.IdentityToken = IdentityToken;
this.IdsToNames = IdsToNames;
}

public AfterConnecting()
{
this.IdentityToken = new();
this.IdsToNames = new();
}

}
}
9 changes: 4 additions & 5 deletions src/SpacetimeDB/ClientApi/CallReducer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace SpacetimeDB.ClientApi
[DataContract]
public partial class CallReducer
{
[DataMember(Name = "reducer")]
public string Reducer;
[DataMember(Name = "reducer_id")]
public uint ReducerId;
[DataMember(Name = "args")]
public byte[] Args;
[DataMember(Name = "request_id")]
Expand All @@ -25,21 +25,20 @@ public partial class CallReducer
public byte Flags;

public CallReducer(
string Reducer,
uint ReducerId,
byte[] Args,
uint RequestId,
byte Flags
)
{
this.Reducer = Reducer;
this.ReducerId = ReducerId;
this.Args = Args;
this.RequestId = RequestId;
this.Flags = Flags;
}

public CallReducer()
{
this.Reducer = "";
this.Args = Array.Empty<byte>();
}

Expand Down
45 changes: 45 additions & 0 deletions src/SpacetimeDB/ClientApi/IdsToNames.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#nullable enable

using System;
using SpacetimeDB;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace SpacetimeDB.ClientApi
{
[SpacetimeDB.Type]
[DataContract]
public partial class IdsToNames
{
[DataMember(Name = "reducer_ids")]
public System.Collections.Generic.List<uint> ReducerIds;
[DataMember(Name = "reducer_names")]
public System.Collections.Generic.List<string> ReducerNames;
[DataMember(Name = "table_ids")]
public System.Collections.Generic.List<uint> TableIds;
[DataMember(Name = "table_names")]
public System.Collections.Generic.List<string> TableNames;

public IdsToNames(
System.Collections.Generic.List<uint> ReducerIds,
System.Collections.Generic.List<string> ReducerNames,
System.Collections.Generic.List<uint> TableIds,
System.Collections.Generic.List<string> TableNames
)
{
this.ReducerIds = ReducerIds;
this.ReducerNames = ReducerNames;
this.TableIds = TableIds;
this.TableNames = TableNames;
}

public IdsToNames()
{
ReducerIds = new();
ReducerNames = new();
TableIds = new();
TableNames = new();
}

}
}
9 changes: 4 additions & 5 deletions src/SpacetimeDB/ClientApi/OneOffTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,22 @@ namespace SpacetimeDB.ClientApi
[DataContract]
public partial class OneOffTable
{
[DataMember(Name = "table_name")]
public string TableName;
[DataMember(Name = "table_id")]
public uint TableId;
[DataMember(Name = "rows")]
public SpacetimeDB.ClientApi.BsatnRowList Rows;

public OneOffTable(
string TableName,
uint TableId,
SpacetimeDB.ClientApi.BsatnRowList Rows
)
{
this.TableName = TableName;
this.TableId = TableId;
this.Rows = Rows;
}

public OneOffTable()
{
this.TableName = "";
this.Rows = new();
}

Expand Down
Loading

0 comments on commit d83d981

Please sign in to comment.