refactor: NatsClient accepts Stream parameter for TLS support

This commit is contained in:
Joseph Doherty
2026-02-22 22:09:48 -05:00
parent 1a777e09c9
commit a26c1359de
3 changed files with 6 additions and 5 deletions

View File

@@ -25,7 +25,7 @@ public interface ISubListAccess
public sealed class NatsClient : IDisposable public sealed class NatsClient : IDisposable
{ {
private readonly Socket _socket; private readonly Socket _socket;
private readonly NetworkStream _stream; private readonly Stream _stream;
private readonly NatsOptions _options; private readonly NatsOptions _options;
private readonly ServerInfo _serverInfo; private readonly ServerInfo _serverInfo;
private readonly NatsParser _parser; private readonly NatsParser _parser;
@@ -57,12 +57,12 @@ public sealed class NatsClient : IDisposable
public IReadOnlyDictionary<string, Subscription> Subscriptions => _subs; public IReadOnlyDictionary<string, Subscription> Subscriptions => _subs;
public NatsClient(ulong id, Socket socket, NatsOptions options, ServerInfo serverInfo, public NatsClient(ulong id, Stream stream, Socket socket, NatsOptions options, ServerInfo serverInfo,
ILogger logger, ServerStats serverStats) ILogger logger, ServerStats serverStats)
{ {
Id = id; Id = id;
_socket = socket; _socket = socket;
_stream = new NetworkStream(socket, ownsSocket: false); _stream = stream;
_options = options; _options = options;
_serverInfo = serverInfo; _serverInfo = serverInfo;
_logger = logger; _logger = logger;

View File

@@ -98,7 +98,8 @@ public sealed class NatsServer : IMessageRouter, ISubListAccess, IDisposable
_logger.LogDebug("Client {ClientId} connected from {RemoteEndpoint}", clientId, socket.RemoteEndPoint); _logger.LogDebug("Client {ClientId} connected from {RemoteEndpoint}", clientId, socket.RemoteEndPoint);
var clientLogger = _loggerFactory.CreateLogger($"NATS.Server.NatsClient[{clientId}]"); var clientLogger = _loggerFactory.CreateLogger($"NATS.Server.NatsClient[{clientId}]");
var client = new NatsClient(clientId, socket, _options, _serverInfo, clientLogger, _stats); var networkStream = new NetworkStream(socket, ownsSocket: false);
var client = new NatsClient(clientId, networkStream, socket, _options, _serverInfo, clientLogger, _stats);
client.Router = this; client.Router = this;
_clients[clientId] = client; _clients[clientId] = client;

View File

@@ -39,7 +39,7 @@ public class ClientTests : IAsyncDisposable
Port = 4222, Port = 4222,
}; };
_natsClient = new NatsClient(1, _serverSocket, new NatsOptions(), serverInfo, NullLogger.Instance, new ServerStats()); _natsClient = new NatsClient(1, new NetworkStream(_serverSocket, ownsSocket: false), _serverSocket, new NatsOptions(), serverInfo, NullLogger.Instance, new ServerStats());
} }
public async ValueTask DisposeAsync() public async ValueTask DisposeAsync()