diff --git a/src/SuperSocket.Server/Connection/IConnectionStreamInitializer.cs b/src/SuperSocket.Server.Abstractions/Connections/IConnectionStreamInitializer.cs similarity index 80% rename from src/SuperSocket.Server/Connection/IConnectionStreamInitializer.cs rename to src/SuperSocket.Server.Abstractions/Connections/IConnectionStreamInitializer.cs index 9a8c0e3f6..78c7dc2ab 100644 --- a/src/SuperSocket.Server/Connection/IConnectionStreamInitializer.cs +++ b/src/SuperSocket.Server.Abstractions/Connections/IConnectionStreamInitializer.cs @@ -2,9 +2,8 @@ using System.Net.Sockets; using System.Threading; using System.Threading.Tasks; -using SuperSocket.Server.Abstractions; -namespace SuperSocket.Server.Connection +namespace SuperSocket.Server.Abstractions.Connections { public interface IConnectionStreamInitializer { diff --git a/src/SuperSocket.Server/Connection/IConnectionStreamInitializersFactory.cs b/src/SuperSocket.Server.Abstractions/Connections/IConnectionStreamInitializersFactory.cs similarity index 80% rename from src/SuperSocket.Server/Connection/IConnectionStreamInitializersFactory.cs rename to src/SuperSocket.Server.Abstractions/Connections/IConnectionStreamInitializersFactory.cs index 925cfa59a..bcee01494 100644 --- a/src/SuperSocket.Server/Connection/IConnectionStreamInitializersFactory.cs +++ b/src/SuperSocket.Server.Abstractions/Connections/IConnectionStreamInitializersFactory.cs @@ -4,9 +4,8 @@ using System.Net.Sockets; using System.Threading; using System.Threading.Tasks; -using SuperSocket.Server.Abstractions; -namespace SuperSocket.Server.Connection +namespace SuperSocket.Server.Abstractions.Connections { public interface IConnectionStreamInitializersFactory { diff --git a/src/SuperSocket.Server.Abstractions/Connections/TcpConnectionFactoryBase.cs b/src/SuperSocket.Server.Abstractions/Connections/TcpConnectionFactoryBase.cs new file mode 100644 index 000000000..026236e40 --- /dev/null +++ b/src/SuperSocket.Server.Abstractions/Connections/TcpConnectionFactoryBase.cs @@ -0,0 +1,112 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.IO; +using System.Linq; +using System.Net.Security; +using System.Net.Sockets; +using System.Security.Authentication; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using SuperSocket.Connection; +using SuperSocket.ProtoBase; + +namespace SuperSocket.Server.Abstractions.Connections +{ + public abstract class TcpConnectionFactoryBase : IConnectionFactory + { + protected ListenOptions ListenOptions { get; } + + protected ConnectionOptions ConnectionOptions { get; } + + protected Action SocketOptionsSetter { get; } + + protected IPipelineFilterFactory PipelineFilterFactory; + + protected ILogger Logger { get; } + + protected IEnumerable ConnectionStreamInitializers { get; } + + public TcpConnectionFactoryBase( + ListenOptions listenOptions, + ConnectionOptions connectionOptions, + Action socketOptionsSetter, + IPipelineFilterFactory pipelineFilterFactory, + IConnectionStreamInitializersFactory connectionStreamInitializersFactory) + { + ListenOptions = listenOptions; + ConnectionOptions = connectionOptions; + SocketOptionsSetter = socketOptionsSetter; + PipelineFilterFactory = pipelineFilterFactory; + Logger = connectionOptions.Logger; + + ConnectionStreamInitializers = connectionStreamInitializersFactory.Create(listenOptions); + } + + public abstract Task CreateConnection(object connection, CancellationToken cancellationToken); + + protected virtual void ApplySocketOptions(Socket socket) + { + try + { + if (ListenOptions.NoDelay) + socket.NoDelay = true; + } + catch (Exception e) + { + Logger.LogWarning(e, "Failed to set NoDelay for the socket."); + } + + try + { + if (ConnectionOptions.ReceiveBufferSize > 0) + socket.ReceiveBufferSize = ConnectionOptions.ReceiveBufferSize; + } + catch (Exception e) + { + Logger.LogWarning(e, "Failed to set ReceiveBufferSize for the socket."); + } + + try + { + if (ConnectionOptions.SendBufferSize > 0) + socket.SendBufferSize = ConnectionOptions.SendBufferSize; + } + catch (Exception e) + { + Logger.LogWarning(e, "Failed to set SendBufferSize for the socket."); + } + + try + { + if (ConnectionOptions.ReceiveTimeout > 0) + socket.ReceiveTimeout = ConnectionOptions.ReceiveTimeout; + } + catch (Exception e) + { + Logger.LogWarning(e, "Failed to set ReceiveTimeout for the socket."); + } + + try + { + if (ConnectionOptions.SendTimeout > 0) + socket.SendTimeout = ConnectionOptions.SendTimeout; + } + catch (Exception e) + { + Logger.LogWarning(e, "Failed to set SendTimeout for the socket."); + } + + try + { + SocketOptionsSetter?.Invoke(socket); + } + catch (Exception e) + { + Logger.LogWarning(e, "Failed to run socketOptionSetter for the socket."); + } + } + } +} \ No newline at end of file diff --git a/src/SuperSocket.Server/Connection/DefaultConnectionStreamInitializersFactory.cs b/src/SuperSocket.Server/Connection/DefaultConnectionStreamInitializersFactory.cs index a195eebdf..77f54d7de 100644 --- a/src/SuperSocket.Server/Connection/DefaultConnectionStreamInitializersFactory.cs +++ b/src/SuperSocket.Server/Connection/DefaultConnectionStreamInitializersFactory.cs @@ -3,11 +3,9 @@ using System.IO; using System.IO.Compression; using System.Linq; -using System.Net.Sockets; using System.Security.Authentication; -using System.Threading; -using System.Threading.Tasks; using SuperSocket.Server.Abstractions; +using SuperSocket.Server.Abstractions.Connections; namespace SuperSocket.Server.Connection { diff --git a/src/SuperSocket.Server/Connection/GZipStreamInitializer.cs b/src/SuperSocket.Server/Connection/GZipStreamInitializer.cs index 6576cff81..0990bf847 100644 --- a/src/SuperSocket.Server/Connection/GZipStreamInitializer.cs +++ b/src/SuperSocket.Server/Connection/GZipStreamInitializer.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using SuperSocket.Connection; using SuperSocket.Server.Abstractions; +using SuperSocket.Server.Abstractions.Connections; namespace SuperSocket.Server.Connection { diff --git a/src/SuperSocket.Server/Connection/NetworkStreamInitializer.cs b/src/SuperSocket.Server/Connection/NetworkStreamInitializer.cs index e407d1110..cfc2531f0 100644 --- a/src/SuperSocket.Server/Connection/NetworkStreamInitializer.cs +++ b/src/SuperSocket.Server/Connection/NetworkStreamInitializer.cs @@ -3,6 +3,7 @@ using System.Threading; using System.Threading.Tasks; using SuperSocket.Server.Abstractions; +using SuperSocket.Server.Abstractions.Connections; namespace SuperSocket.Server.Connection { diff --git a/src/SuperSocket.Server/Connection/SslStreamInitializer.cs b/src/SuperSocket.Server/Connection/SslStreamInitializer.cs index 37068fd49..68715f04e 100644 --- a/src/SuperSocket.Server/Connection/SslStreamInitializer.cs +++ b/src/SuperSocket.Server/Connection/SslStreamInitializer.cs @@ -1,10 +1,10 @@ using System.IO; using System.Net.Security; using System.Net.Sockets; -using System.Runtime.Serialization.Formatters; using System.Threading; using System.Threading.Tasks; using SuperSocket.Server.Abstractions; +using SuperSocket.Server.Abstractions.Connections; namespace SuperSocket.Server.Connection { diff --git a/src/SuperSocket.Server/Connection/TcpConnectionFactory.cs b/src/SuperSocket.Server/Connection/TcpConnectionFactory.cs index 2e359c095..38056c48e 100644 --- a/src/SuperSocket.Server/Connection/TcpConnectionFactory.cs +++ b/src/SuperSocket.Server/Connection/TcpConnectionFactory.cs @@ -1,15 +1,10 @@ using System; -using System.Collections; using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; using System.IO; using System.Linq; -using System.Net.Security; using System.Net.Sockets; -using System.Security.Authentication; using System.Threading; using System.Threading.Tasks; -using Microsoft.Extensions.Logging; using SuperSocket.Connection; using SuperSocket.ProtoBase; using SuperSocket.Server.Abstractions; @@ -17,43 +12,26 @@ namespace SuperSocket.Server.Connection { - public class TcpConnectionFactory : IConnectionFactory + public class TcpConnectionFactory : TcpConnectionFactoryBase { - protected ListenOptions ListenOptions { get; } - - protected ConnectionOptions ConnectionOptions { get; } - - protected Action SocketOptionsSetter { get; } - - protected IPipelineFilterFactory PipelineFilterFactory; - - protected ILogger Logger { get; } - - private IEnumerable _connectionStreamInitializers; - public TcpConnectionFactory( ListenOptions listenOptions, ConnectionOptions connectionOptions, Action socketOptionsSetter, IPipelineFilterFactory pipelineFilterFactory, IConnectionStreamInitializersFactory connectionStreamInitializersFactory) + : base(listenOptions, connectionOptions, socketOptionsSetter, pipelineFilterFactory, connectionStreamInitializersFactory) { - ListenOptions = listenOptions; - ConnectionOptions = connectionOptions; - SocketOptionsSetter = socketOptionsSetter; - PipelineFilterFactory = pipelineFilterFactory; - Logger = connectionOptions.Logger; - - _connectionStreamInitializers = connectionStreamInitializersFactory.Create(listenOptions); + } - public virtual async Task CreateConnection(object connection, CancellationToken cancellationToken) + public override async Task CreateConnection(object connection, CancellationToken cancellationToken) { var socket = connection as Socket; ApplySocketOptions(socket); - if (_connectionStreamInitializers is IEnumerable connectionStreamInitializers + if (ConnectionStreamInitializers is IEnumerable connectionStreamInitializers && connectionStreamInitializers.Any()) { var stream = default(Stream); @@ -68,67 +46,5 @@ public virtual async Task CreateConnection(object connection, Cance return new TcpPipeConnection(socket, PipelineFilterFactory.Create(socket), ConnectionOptions); } - - protected virtual void ApplySocketOptions(Socket socket) - { - try - { - if (ListenOptions.NoDelay) - socket.NoDelay = true; - } - catch (Exception e) - { - Logger.LogWarning(e, "Failed to set NoDelay for the socket."); - } - - try - { - if (ConnectionOptions.ReceiveBufferSize > 0) - socket.ReceiveBufferSize = ConnectionOptions.ReceiveBufferSize; - } - catch (Exception e) - { - Logger.LogWarning(e, "Failed to set ReceiveBufferSize for the socket."); - } - - try - { - if (ConnectionOptions.SendBufferSize > 0) - socket.SendBufferSize = ConnectionOptions.SendBufferSize; - } - catch (Exception e) - { - Logger.LogWarning(e, "Failed to set SendBufferSize for the socket."); - } - - try - { - if (ConnectionOptions.ReceiveTimeout > 0) - socket.ReceiveTimeout = ConnectionOptions.ReceiveTimeout; - } - catch (Exception e) - { - Logger.LogWarning(e, "Failed to set ReceiveTimeout for the socket."); - } - - try - { - if (ConnectionOptions.SendTimeout > 0) - socket.SendTimeout = ConnectionOptions.SendTimeout; - } - catch (Exception e) - { - Logger.LogWarning(e, "Failed to set SendTimeout for the socket."); - } - - try - { - SocketOptionsSetter?.Invoke(socket); - } - catch (Exception e) - { - Logger.LogWarning(e, "Failed to run socketOptionSetter for the socket."); - } - } } } \ No newline at end of file