diff --git a/src/SuperSocket.Connection/PipeConnection.cs b/src/SuperSocket.Connection/PipeConnection.cs index 10550697c..6054f8d16 100644 --- a/src/SuperSocket.Connection/PipeConnection.cs +++ b/src/SuperSocket.Connection/PipeConnection.cs @@ -167,7 +167,7 @@ protected async ValueTask ProcessOutputRead(PipeReader reader) } catch (Exception e) { - // Cancel all the work in the channel if encounter an error during sending + // Cancel all the work in the connection if encounter an error during sending Cancel(); if (!IsIgnorableException(e)) diff --git a/src/SuperSocket.Connection/PipeConnectionBase.cs b/src/SuperSocket.Connection/PipeConnectionBase.cs index c19410b57..faa0d6c4d 100644 --- a/src/SuperSocket.Connection/PipeConnectionBase.cs +++ b/src/SuperSocket.Connection/PipeConnectionBase.cs @@ -106,7 +106,7 @@ private async ValueTask HandleClosing() } catch (Exception e) { - OnError("Unhandled exception in the method PipeChannel.Run.", e); + OnError("Unhandled exception in the method PipeConnection.Run.", e); } finally { @@ -120,7 +120,7 @@ private async ValueTask HandleClosing() catch (Exception exc) { if (!IsIgnorableException(exc)) - OnError("Unhandled exception in the method PipeChannel.Close.", exc); + OnError("Unhandled exception in the method PipeConnection.Close.", exc); } } } @@ -160,7 +160,7 @@ private void CheckConnectionOpen() { if (this.IsClosed) { - throw new Exception("Channel is closed now, send is not allowed."); + throw new Exception("Connection is closed now, send is not allowed."); } } diff --git a/src/SuperSocket.Server/Connection/TcpConnectionListener.cs b/src/SuperSocket.Server/Connection/TcpConnectionListener.cs index 74a214663..934b649ba 100644 --- a/src/SuperSocket.Server/Connection/TcpConnectionListener.cs +++ b/src/SuperSocket.Server/Connection/TcpConnectionListener.cs @@ -116,7 +116,7 @@ private async void OnNewClientAccept(Socket socket) } catch (Exception e) { - _logger.LogError(e, $"Failed to create channel for {socket.RemoteEndPoint}."); + _logger.LogError(e, $"Failed to create connection for {socket.RemoteEndPoint}."); return; } diff --git a/src/SuperSocket.Server/SuperSocketService.cs b/src/SuperSocket.Server/SuperSocketService.cs index 3c0b0d3f7..547c0b93b 100644 --- a/src/SuperSocket.Server/SuperSocketService.cs +++ b/src/SuperSocket.Server/SuperSocketService.cs @@ -163,7 +163,7 @@ private Task StartListenAsync(CancellationToken cancellationToken) if (!AddConnectionListener(null, serverOptions)) { - _logger.LogError($"Failed to add the channel creator."); + _logger.LogError($"Failed to add the connection creator."); return Task.FromResult(false); } } @@ -288,7 +288,7 @@ private async ValueTask InitializeSession(IAppSession session, IConnection return false; } - connection.Closed += (s, e) => OnChannelClosed(session, e); + connection.Closed += (s, e) => OnConnectionClosed(session, e); return true; } @@ -303,7 +303,7 @@ protected virtual ValueTask OnSessionConnectedAsync(IAppSession session) return new ValueTask(); } - private void OnChannelClosed(IAppSession session, CloseEventArgs e) + private void OnConnectionClosed(IAppSession session, CloseEventArgs e) { FireSessionClosedEvent(session as AppSession, e.Reason).DoNotAwait(); } diff --git a/src/SuperSocket.Udp/UdpConnectionListener.cs b/src/SuperSocket.Udp/UdpConnectionListener.cs index 1a62fca49..90cd71f14 100644 --- a/src/SuperSocket.Udp/UdpConnectionListener.cs +++ b/src/SuperSocket.Udp/UdpConnectionListener.cs @@ -193,7 +193,7 @@ private async ValueTask CreateConnection(Socket socket, IPEndPoint } catch (Exception e) { - _logger.LogError(e, $"Failed to create channel for {socket.RemoteEndPoint}."); + _logger.LogError(e, $"Failed to create connection for {socket.RemoteEndPoint}."); return null; } } diff --git a/src/SuperSocket.WebSocket.Server/WebSocketPackageHandler.cs b/src/SuperSocket.WebSocket.Server/WebSocketPackageHandler.cs index 6f54012d5..9ce00e1cb 100644 --- a/src/SuperSocket.WebSocket.Server/WebSocketPackageHandler.cs +++ b/src/SuperSocket.WebSocket.Server/WebSocketPackageHandler.cs @@ -354,8 +354,8 @@ private async ValueTask HandleHandshake(IAppSession session, WebSocketPack if (selectedExtensionHeadItems != null && selectedExtensionHeadItems.Count > 0) { - var pipeChannel = session.Connection as IPipeConnection; - pipeChannel.PipelineFilter.Context = new WebSocketPipelineFilterContext + var pipeConnection = session.Connection as IPipeConnection; + pipeConnection.PipelineFilter.Context = new WebSocketPipelineFilterContext { Extensions = extensions }; diff --git a/src/SuperSocket.WebSocket.Server/WebSocketSession.cs b/src/SuperSocket.WebSocket.Server/WebSocketSession.cs index 21a741796..b697750c6 100644 --- a/src/SuperSocket.WebSocket.Server/WebSocketSession.cs +++ b/src/SuperSocket.WebSocket.Server/WebSocketSession.cs @@ -7,7 +7,7 @@ using SuperSocket.ProtoBase; using SuperSocket.Server; using SuperSocket.Server.Abstractions.Session; -using ChannelCloseReason = SuperSocket.Connection.CloseReason; +using ConnectionCloseReason = SuperSocket.Connection.CloseReason; namespace SuperSocket.WebSocket.Server { @@ -117,17 +117,17 @@ private void OnCloseHandshakeStarted() internal void CloseWithoutHandshake() { - base.CloseAsync(ChannelCloseReason.LocalClosing).DoNotAwait(); + base.CloseAsync(ConnectionCloseReason.LocalClosing).DoNotAwait(); } - public override async ValueTask CloseAsync(ChannelCloseReason closeReason) + public override async ValueTask CloseAsync(ConnectionCloseReason closeReason) { var closeStatus = CloseStatus; if (closeStatus != null) { var clientInitiated = closeStatus.RemoteInitiated; - await base.CloseAsync(clientInitiated ? ChannelCloseReason.RemoteClosing : ChannelCloseReason.LocalClosing); + await base.CloseAsync(clientInitiated ? ConnectionCloseReason.RemoteClosing : ConnectionCloseReason.LocalClosing); return; } diff --git a/test/SuperSocket.Benchmarks/TransparentPipeConnection.cs b/test/SuperSocket.Benchmarks/TransparentPipeConnection.cs index 23adf9f6c..c2f1b7b09 100644 --- a/test/SuperSocket.Benchmarks/TransparentPipeConnection.cs +++ b/test/SuperSocket.Benchmarks/TransparentPipeConnection.cs @@ -10,13 +10,13 @@ namespace SuperSocket.Benchmarks public class TransparentPipeConnection : PipeConnection { private TaskCompletionSource _tcs; - private Task _channelTask; + private Task _connectionTask; public TransparentPipeConnection(ConnectionOptions options) : base(options) { _tcs = new TaskCompletionSource(); - _channelTask = _tcs.Task; + _connectionTask = _tcs.Task; } public override ValueTask CloseAsync(CloseReason closeReason) @@ -32,7 +32,7 @@ protected override void Close() protected override async ValueTask FillPipeWithDataAsync(Memory memory, CancellationToken cancellationToken) { - await _channelTask; + await _connectionTask; return 0; } diff --git a/test/SuperSocket.Tests/ClientTest.cs b/test/SuperSocket.Tests/ClientTest.cs index b36cff6e2..c190f8174 100644 --- a/test/SuperSocket.Tests/ClientTest.cs +++ b/test/SuperSocket.Tests/ClientTest.cs @@ -257,30 +257,30 @@ public async Task TestCommandLine(Type hostConfiguratorType) } [Fact] - [Trait("Category", "TestDetachableChannel")] - public async Task TestDetachableChannel() + [Trait("Category", "TestDetachableConnection")] + public async Task TestDetachableConnection() { IHostConfigurator hostConfigurator = new RegularHostConfigurator(); - await TestDetachableChannelInternal(hostConfigurator, (_, socket) => + await TestDetachableConnectionInternal(hostConfigurator, (_, socket) => new StreamPipeConnection( hostConfigurator.GetClientStream(socket).Result, socket.RemoteEndPoint, socket.LocalEndPoint, new ConnectionOptions { - Logger = DefaultLoggerFactory.CreateLogger(nameof(TestDetachableChannel)), + Logger = DefaultLoggerFactory.CreateLogger(nameof(TestDetachableConnection)), ReadAsDemand = true }) ); /* KestrelPipeConnection doesn't support Detach right now. - await TestDetachableChannelInternal(new KestralConnectionHostConfigurator(), (server, socket) => + await TestDetachableConnectionInternal(new KestralConnectionHostConfigurator(), (server, socket) => new KestrelPipeConnection( server.ServiceProvider.GetService().Create(socket), new ConnectionOptions { - Logger = DefaultLoggerFactory.CreateLogger(nameof(TestDetachableChannel)), + Logger = DefaultLoggerFactory.CreateLogger(nameof(TestDetachableConnection)), ReadAsDemand = false } ) @@ -288,7 +288,7 @@ await TestDetachableChannelInternal(new KestralConnectionHostConfigurator(), (se */ } - async Task TestDetachableChannelInternal(IHostConfigurator hostConfigurator, Func connectionFactory) + async Task TestDetachableConnectionInternal(IHostConfigurator hostConfigurator, Func connectionFactory) { using (var server = CreateSocketServerBuilder(hostConfigurator) .UsePackageHandler(async (s, p) => diff --git a/test/SuperSocket.Tests/SessionTest.cs b/test/SuperSocket.Tests/SessionTest.cs index d6df161db..9dccaee12 100644 --- a/test/SuperSocket.Tests/SessionTest.cs +++ b/test/SuperSocket.Tests/SessionTest.cs @@ -216,6 +216,69 @@ public async Task TestCloseReason(Type hostConfiguratorType) } } + [Theory] + [InlineData(typeof(RegularHostConfigurator), 198)] + [InlineData(typeof(RegularHostConfigurator), 255)] + public async Task TestCustomCloseReason(Type hostConfiguratorType, int closeReasonCode) + { + var hostConfigurator = CreateObject(hostConfiguratorType); + + IAppSession session = null; + + CloseReason closeReason = CloseReason.Unknown; + + var resetEvent = new AutoResetEvent(true); + + using (var server = CreateSocketServerBuilder(hostConfigurator) + .ConfigureAppConfiguration((HostBuilder, configBuilder) => + { + configBuilder.AddInMemoryCollection(new Dictionary + { + { "serverOptions:maxPackageLength", "8" } + }); + }) + .UseSessionHandler( + onConnected: (s) => + { + session = s; + resetEvent.Set(); + return ValueTask.CompletedTask; + }, + onClosed: (s, e) => + { + closeReason = e.Reason; + resetEvent.Set(); + return ValueTask.CompletedTask; + }) + .BuildAsServer()) + { + Assert.Equal("TestServer", server.Name); + + Assert.True(await server.StartAsync()); + OutputHelper.WriteLine("Started."); + + using (var socket = CreateClient(hostConfigurator)) + using (var socketStream = await hostConfigurator.GetClientStream(socket)) + { + resetEvent.WaitOne(1000); + + // LocalClosing + closeReason = CloseReason.Unknown; + + var closeReasonRequested = (CloseReason)closeReasonCode; + + await session.CloseAsync(closeReasonRequested); + + resetEvent.WaitOne(1000); + + Assert.Equal(SessionState.Closed, session.State); + Assert.Equal(closeReasonRequested, closeReason); + } + + await server.StopAsync(); + } + } + [Fact] public async Task TestConsoleProtocol() { diff --git a/unlist.sh b/unlist.sh index 32e50aee5..0e1590cbd 100755 --- a/unlist.sh +++ b/unlist.sh @@ -6,6 +6,6 @@ nuget delete SuperSocket.WebSocket "$version" -source nuget.org -NonInteractive nuget delete SuperSocket.Server "$version" -source nuget.org -NonInteractive nuget delete SuperSocket.SessionContainer "$version" -source nuget.org -NonInteractive nuget delete SuperSocket.Command "$version" -source nuget.org -NonInteractive -nuget delete SuperSocket.Channel "$version" -source nuget.org -NonInteractive +nuget delete SuperSocket.Connection "$version" -source nuget.org -NonInteractive nuget delete SuperSocket.Primitives "$version" -source nuget.org -NonInteractive nuget delete SuperSocket.ProtoBase "$version" -source nuget.org -NonInteractive \ No newline at end of file