Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//HintName: FFI.cs
//HintName: FFI.cs
// <auto-generated />
#nullable enable
// The runtime already defines SpacetimeDB.Internal.LocalReadOnly in Runtime\Internal\Module.cs as an empty partial type.
Expand All @@ -17,7 +17,6 @@ namespace SpacetimeDB
{
public sealed record ReducerContext : DbContext<Local>, Internal.IReducerContext
{
public readonly Identity Sender;
public readonly ConnectionId? ConnectionId;
public readonly Random Rng;
public readonly Timestamp Timestamp;
Expand All @@ -29,6 +28,8 @@ public sealed record ReducerContext : DbContext<Local>, Internal.IReducerContext
// We need this property to be non-static for parity with client SDK.
public Identity Identity => Internal.IReducerContext.GetIdentity();

private readonly Identity _sender;

internal ReducerContext(
Identity identity,
ConnectionId? connectionId,
Expand All @@ -37,14 +38,19 @@ internal ReducerContext(
AuthCtx? senderAuth = null
)
{
Sender = identity;
_sender = identity;
ConnectionId = connectionId;
Rng = random;
Timestamp = time;
SenderAuth = senderAuth ?? AuthCtx.BuildFromSystemTables(connectionId, identity);
CounterUuid = 0;
}

/// <summary>
/// The identity of the client that invoked the reducer.
/// </summary>
public Identity Sender() => _sender;

/// <summary>
/// Create a new random <see cref="Uuid"/> `v4` using the built-in RNG.
/// </summary>
Expand Down Expand Up @@ -199,13 +205,18 @@ public sealed class Local : global::SpacetimeDB.LocalBase

public sealed record ViewContext : DbContext<Internal.LocalReadOnly>, Internal.IViewContext
{
public Identity Sender { get; }
private readonly Identity _sender;

internal ViewContext(Identity sender, Internal.LocalReadOnly db)
: base(db)
{
Sender = sender;
_sender = sender;
}

/// <summary>
/// The identity of the client that invoked the view.
/// </summary>
public Identity Sender() => _sender;
}

public sealed record AnonymousViewContext
Expand Down
20 changes: 16 additions & 4 deletions crates/bindings-csharp/Codegen/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1737,7 +1737,6 @@ public void Initialize(IncrementalGeneratorInitializationContext context)

namespace SpacetimeDB {
public sealed record ReducerContext : DbContext<Local>, Internal.IReducerContext {
public readonly Identity Sender;
public readonly ConnectionId? ConnectionId;
public readonly Random Rng;
public readonly Timestamp Timestamp;
Expand All @@ -1747,16 +1746,24 @@ public sealed record ReducerContext : DbContext<Local>, Internal.IReducerContext
// We need this property to be non-static for parity with client SDK.
public Identity Identity => Internal.IReducerContext.GetIdentity();

private readonly Identity _sender;

internal ReducerContext(Identity identity, ConnectionId? connectionId, Random random,
Timestamp time, AuthCtx? senderAuth = null)
{
Sender = identity;
_sender = identity;
ConnectionId = connectionId;
Rng = random;
Timestamp = time;
SenderAuth = senderAuth ?? AuthCtx.BuildFromSystemTables(connectionId, identity);
CounterUuid = 0;
}

/// <summary>
/// The identity of the client that invoked the reducer.
/// </summary>
public Identity Sender() => _sender;

/// <summary>
/// Create a new random <see cref="Uuid"/> `v4` using the built-in RNG.
/// </summary>
Expand Down Expand Up @@ -1891,13 +1898,18 @@ public sealed class Local : global::SpacetimeDB.LocalBase {

public sealed record ViewContext : DbContext<Internal.LocalReadOnly>, Internal.IViewContext
{
public Identity Sender { get; }
private readonly Identity _sender;

internal ViewContext(Identity sender, Internal.LocalReadOnly db)
: base(db)
{
Sender = sender;
_sender = sender;
}

/// <summary>
/// The identity of the client that invoked the view.
/// </summary>
public Identity Sender() => _sender;
}

public sealed record AnonymousViewContext : DbContext<Internal.LocalReadOnly>, Internal.IAnonymousViewContext
Expand Down
7 changes: 5 additions & 2 deletions crates/bindings-csharp/Runtime/Internal/TxContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ public sealed class TxContext(
Random rng
)
{
private readonly Identity _sender = sender;

public Local Db { get; } = db;
public Identity Sender { get; } = sender;
public ConnectionId? ConnectionId { get; } = connectionId;
public Timestamp Timestamp { get; } = timestamp;
public AuthCtx SenderAuth { get; } = senderAuth;
public Random Rng { get; } = rng;

public Identity Sender() => _sender;

public TxContext WithTimestamp(Timestamp ts) =>
new(Db, Sender, ConnectionId, ts, SenderAuth, Rng);
new(Db, _sender, ConnectionId, ts, SenderAuth, Rng);
}
10 changes: 7 additions & 3 deletions crates/bindings-csharp/Runtime/ProcedureContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
Timestamp time
) : Internal.IInternalProcedureContext
{
private readonly Identity _sender = sender;

public Identity Sender() => _sender;

public static Identity Identity => Internal.IProcedureContext.GetIdentity();
public Identity Sender { get; } = sender;
public ConnectionId? ConnectionId { get; } = connectionId;
public Random Rng { get; } = random;
public Timestamp Timestamp { get; private set; } = time;
Expand Down Expand Up @@ -47,7 +50,7 @@
txContext?.WithTimestamp(timestamp)
?? new Internal.TxContext(
CreateLocal(),
Sender,
_sender,
ConnectionId,
timestamp,
SenderAuth,
Expand Down Expand Up @@ -108,14 +111,14 @@
}

// Private transaction management methods (Rust-like encapsulation)
private long StartMutTx()

Check warning on line 114 in crates/bindings-csharp/Runtime/ProcedureContext.cs

View workflow job for this annotation

GitHub Actions / csharp-testsuite

Member 'StartMutTx' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 114 in crates/bindings-csharp/Runtime/ProcedureContext.cs

View workflow job for this annotation

GitHub Actions / unity-testsuite

Member 'StartMutTx' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 114 in crates/bindings-csharp/Runtime/ProcedureContext.cs

View workflow job for this annotation

GitHub Actions / Test Suite

Member 'StartMutTx' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 114 in crates/bindings-csharp/Runtime/ProcedureContext.cs

View workflow job for this annotation

GitHub Actions / Smoketests (windows-latest)

Member 'StartMutTx' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 114 in crates/bindings-csharp/Runtime/ProcedureContext.cs

View workflow job for this annotation

GitHub Actions / Smoketests (spacetimedb-new-runner)

Member 'StartMutTx' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)
{
var status = Internal.FFI.procedure_start_mut_tx(out var micros);
Internal.FFI.ErrnoHelpers.ThrowIfError(status);
return micros;
}

private void CommitMutTx()

Check warning on line 121 in crates/bindings-csharp/Runtime/ProcedureContext.cs

View workflow job for this annotation

GitHub Actions / csharp-testsuite

Member 'CommitMutTx' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 121 in crates/bindings-csharp/Runtime/ProcedureContext.cs

View workflow job for this annotation

GitHub Actions / unity-testsuite

Member 'CommitMutTx' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 121 in crates/bindings-csharp/Runtime/ProcedureContext.cs

View workflow job for this annotation

GitHub Actions / Test Suite

Member 'CommitMutTx' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 121 in crates/bindings-csharp/Runtime/ProcedureContext.cs

View workflow job for this annotation

GitHub Actions / Smoketests (windows-latest)

Member 'CommitMutTx' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)

Check warning on line 121 in crates/bindings-csharp/Runtime/ProcedureContext.cs

View workflow job for this annotation

GitHub Actions / Smoketests (spacetimedb-new-runner)

Member 'CommitMutTx' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)
{
var status = Internal.FFI.procedure_commit_mut_tx();
Internal.FFI.ErrnoHelpers.ThrowIfError(status);
Expand Down Expand Up @@ -229,8 +232,9 @@

internal void Refresh(Internal.TxContext inner) => Inner = inner;

public Identity Sender() => Inner.Sender();

public LocalBase Db => (LocalBase)Inner.Db;
public Identity Sender => Inner.Sender;
public ConnectionId? ConnectionId => Inner.ConnectionId;
public Timestamp Timestamp => Inner.Timestamp;
public AuthCtx SenderAuth => Inner.SenderAuth;
Expand Down
16 changes: 8 additions & 8 deletions demo/Blackholio/server-csharp/Lib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public static void Init(ReducerContext ctx)
[Reducer(ReducerKind.ClientConnected)]
public static void Connect(ReducerContext ctx)
{
var player = ctx.Db.logged_out_player.identity.Find(ctx.Sender);
var player = ctx.Db.logged_out_player.identity.Find(ctx.Sender());
if (player != null)
{
ctx.Db.player.Insert(player.Value);
Expand All @@ -157,7 +157,7 @@ public static void Connect(ReducerContext ctx)
{
ctx.Db.player.Insert(new Player
{
identity = ctx.Sender,
identity = ctx.Sender(),
name = "",
});
}
Expand All @@ -166,7 +166,7 @@ public static void Connect(ReducerContext ctx)
[Reducer(ReducerKind.ClientDisconnected)]
public static void Disconnect(ReducerContext ctx)
{
var player = ctx.Db.player.identity.Find(ctx.Sender) ?? throw new Exception("Player not found");
var player = ctx.Db.player.identity.Find(ctx.Sender()) ?? throw new Exception("Player not found");
foreach (var circle in ctx.Db.circle.player_id.Filter(player.player_id))
{
var entity = ctx.Db.entity.entity_id.Find(circle.entity_id) ?? throw new Exception("Could not find circle");
Expand All @@ -183,7 +183,7 @@ public static void Disconnect(ReducerContext ctx)
public static void EnterGame(ReducerContext ctx, string name)
{
Log.Info($"Creating player with name {name}");
var player = ctx.Db.player.identity.Find(ctx.Sender) ?? throw new Exception("Player not found");
var player = ctx.Db.player.identity.Find(ctx.Sender()) ?? throw new Exception("Player not found");
player.name = name;
ctx.Db.player.identity.Update(player);
SpawnPlayerInitialCircle(ctx, player.player_id);
Expand All @@ -192,15 +192,15 @@ public static void EnterGame(ReducerContext ctx, string name)
[Reducer]
public static void Respawn(ReducerContext ctx)
{
var player = ctx.Db.player.identity.Find(ctx.Sender) ?? throw new Exception("No such player found");
var player = ctx.Db.player.identity.Find(ctx.Sender()) ?? throw new Exception("No such player found");

SpawnPlayerInitialCircle(ctx, player.player_id);
}

[Reducer]
public static void Suicide(ReducerContext ctx)
{
var player = ctx.Db.player.identity.Find(ctx.Sender) ?? throw new Exception("No such player found");
var player = ctx.Db.player.identity.Find(ctx.Sender()) ?? throw new Exception("No such player found");

foreach (var circle in ctx.Db.circle.player_id.Filter(player.player_id))
{
Expand Down Expand Up @@ -246,7 +246,7 @@ public static Entity SpawnCircleAt(ReducerContext ctx, int player_id, int mass,
[Reducer]
public static void UpdatePlayerInput(ReducerContext ctx, DbVector2 direction)
{
var player = ctx.Db.player.identity.Find(ctx.Sender) ?? throw new Exception("Player not found");
var player = ctx.Db.player.identity.Find(ctx.Sender()) ?? throw new Exception("Player not found");
foreach (var c in ctx.Db.circle.player_id.Filter(player.player_id))
{
var circle = c;
Expand Down Expand Up @@ -449,7 +449,7 @@ public static void DestroyEntity(ReducerContext ctx, int entityId)
[Reducer]
public static void PlayerSplit(ReducerContext ctx)
{
var player = ctx.Db.player.identity.Find(ctx.Sender) ?? throw new Exception("Sender has no player");
var player = ctx.Db.player.identity.Find(ctx.Sender()) ?? throw new Exception("Sender has no player");
List<Circle> circles = ctx.Db.circle.player_id.Filter(player.player_id).ToList();
var circle_count = circles.Count;
if (circle_count >= MAX_CIRCLES_PER_PLAYER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ A view can be written in C# like so:
[SpacetimeDB.View(Name = "MyPlayer", Public = true)]
public static Player? MyPlayer(ViewContext ctx)
{
return ctx.Db.Player.Identity.Find(ctx.Sender) as Player;
return ctx.Db.Player.Identity.Find(ctx.Sender()) as Player;
}
```

Expand Down
12 changes: 6 additions & 6 deletions docs/docs/00100-intro/00300-tutorials/00100-chat-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public static void SetName(ReducerContext ctx, string name)
{
name = ValidateName(name);

if (ctx.Db.User.Identity.Find(ctx.Sender) is User user)
if (ctx.Db.User.Identity.Find(ctx.Sender()) is User user)
{
user.Name = name;
ctx.Db.User.Identity.Update(user);
Expand Down Expand Up @@ -411,7 +411,7 @@ public static void SendMessage(ReducerContext ctx, string text)
ctx.Db.Message.Insert(
new Message
{
Sender = ctx.Sender,
Sender = ctx.Sender(),
Text = text,
Sent = ctx.Timestamp,
}
Expand Down Expand Up @@ -504,9 +504,9 @@ In `spacetimedb/Lib.cs`, add to the `Module` class:
[Reducer(ReducerKind.ClientConnected)]
public static void ClientConnected(ReducerContext ctx)
{
Log.Info($"Connect {ctx.Sender}");
Log.Info($"Connect {ctx.Sender()}");

if (ctx.Db.User.Identity.Find(ctx.Sender) is User user)
if (ctx.Db.User.Identity.Find(ctx.Sender()) is User user)
{
user.Online = true;
ctx.Db.User.Identity.Update(user);
Expand All @@ -517,7 +517,7 @@ public static void ClientConnected(ReducerContext ctx)
new User
{
Name = null,
Identity = ctx.Sender,
Identity = ctx.Sender(),
Online = true,
}
);
Expand All @@ -527,7 +527,7 @@ public static void ClientConnected(ReducerContext ctx)
[Reducer(ReducerKind.ClientDisconnected)]
public static void ClientDisconnected(ReducerContext ctx)
{
if (ctx.Db.User.Identity.Find(ctx.Sender) is User user)
if (ctx.Db.User.Identity.Find(ctx.Sender()) is User user)
{
user.Online = false;
ctx.Db.User.Identity.Update(user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ Add this function to the `Module` class in `Lib.cs`:
[Reducer]
public static void Debug(ReducerContext ctx)
{
Log.Info($"This reducer was called by {ctx.Sender}");
Log.Info($"This reducer was called by {ctx.Sender()}");
}
```

Expand Down Expand Up @@ -444,7 +444,7 @@ Next let's connect our client to our database. Let's start by modifying our `Deb
[Reducer(ReducerKind.ClientConnected)]
public static void Connect(ReducerContext ctx)
{
Log.Info($"{ctx.Sender} just connected.");
Log.Info($"{ctx.Sender()} just connected.");
}
```

Expand Down
Loading
Loading