Implements the modular networking layer for SOR Server: - SOR.Networking.Abstractions: pure interfaces (ITransport, IClientConnection, IMessageSerializer, IMessageDispatcher, IMessageHandler, IEventBus, IClientPush) - SOR.Networking.WebSocket: ITransport + IClientConnection over System.Net.WebSockets - SOR.Networking.Nats: IEventBus over NATS.Net (INatsConnection) - SOR.Networking.Redis: IClientPush over Redis Pub/Sub - SOR.Shared: ServiceResult<T>, RedisKeys, NatsSubjects, ServerMessage, JSON serializer - SOR.Shared.Proto: auth.proto with gRPC AuthService contract - SOR.Gateway: accept loop, auth handshake, dispatcher, Redis push subscriber - SOR.Auth: stub gRPC handler (placeholder responses) - SOR.Host: multi-service runner via ENABLED_SERVICES env var; wires all DI All concrete implementations are wired exclusively in SOR.Host — every transport, broker, and push implementation can be swapped by changing only Program.cs. dotnet build SOR.slnx → 0 errors, 0 warnings Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
72 lines
2.8 KiB
C#
72 lines
2.8 KiB
C#
namespace SOR.Networking.Redis;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
using StackExchange.Redis;
|
|
using SOR.Networking.Abstractions.Messaging;
|
|
using SOR.Networking.Abstractions.Push;
|
|
using SOR.Shared.Redis;
|
|
|
|
/// <summary>
|
|
/// IClientPush implementation using Redis Pub/Sub.
|
|
///
|
|
/// This is the WRITE side of the push delivery pipeline:
|
|
/// Service calls PushAsync(userId, message)
|
|
/// → Serializes message to bytes (via IMessageSerializer)
|
|
/// → Publishes to Redis channel "push:{userId}"
|
|
/// → Gateway (subscribed to that channel) receives the bytes
|
|
/// → Gateway forwards bytes to the user's WebSocket
|
|
///
|
|
/// The Gateway's READ side (RedisPushSubscriber) handles the subscription.
|
|
/// This class only handles publishing — it has no knowledge of Gateway internals.
|
|
///
|
|
/// Best-effort delivery: if no Gateway is subscribed to the userId channel
|
|
/// (user disconnected), the message is dropped silently. This is correct behavior.
|
|
/// Services must not assume push delivery is reliable — use persistent state and
|
|
/// client-initiated sync for any data that must survive disconnections.
|
|
///
|
|
/// Lifetime: Singleton (IConnectionMultiplexer is a Singleton from SOR.Host).
|
|
/// </summary>
|
|
public sealed class RedisClientPush : IClientPush
|
|
{
|
|
private readonly IConnectionMultiplexer _redis;
|
|
private readonly IMessageSerializer _serializer;
|
|
private readonly ILogger<RedisClientPush> _logger;
|
|
|
|
public RedisClientPush(
|
|
IConnectionMultiplexer redis,
|
|
IMessageSerializer serializer,
|
|
ILogger<RedisClientPush> logger)
|
|
{
|
|
_redis = redis;
|
|
_serializer = serializer;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public async Task PushAsync(
|
|
string userId,
|
|
IServerMessage message,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
// Serialize the message to bytes.
|
|
// The bytes are already in wire format (envelope + payload) — ready for
|
|
// the Gateway to forward directly to the WebSocket without re-serializing.
|
|
var bytes = _serializer.Serialize(message);
|
|
|
|
// Get the Redis channel key for this user.
|
|
// RedisKey.Literal wraps the string to avoid StackExchange.Redis interpreting
|
|
// pattern characters (* ? [) as wildcards.
|
|
var channel = RedisChannel.Literal(RedisKeys.ClientPushChannel(userId));
|
|
|
|
_logger.LogDebug(
|
|
"Pushing {MessageType} to user {UserId} via Redis Pub/Sub",
|
|
message.Type, userId);
|
|
|
|
// ISubscriber.PublishAsync sends to the channel.
|
|
// The return value is the number of subscribers that received the message.
|
|
// We don't check it — zero subscribers (user disconnected) is not an error.
|
|
var subscriber = _redis.GetSubscriber();
|
|
await subscriber.PublishAsync(channel, bytes.ToArray());
|
|
}
|
|
}
|