Skip to content

Commit

Permalink
moved around classes
Browse files Browse the repository at this point in the history
  • Loading branch information
kerryjiang committed Mar 18, 2024
1 parent ef49457 commit fbcd404
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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<TPackageInfo> : IConnectionFactory
{
protected ListenOptions ListenOptions { get; }

protected ConnectionOptions ConnectionOptions { get; }

protected Action<Socket> SocketOptionsSetter { get; }

protected IPipelineFilterFactory<TPackageInfo> PipelineFilterFactory;

protected ILogger Logger { get; }

protected IEnumerable<IConnectionStreamInitializer> ConnectionStreamInitializers { get; }

public TcpConnectionFactoryBase(
ListenOptions listenOptions,
ConnectionOptions connectionOptions,
Action<Socket> socketOptionsSetter,
IPipelineFilterFactory<TPackageInfo> pipelineFilterFactory,
IConnectionStreamInitializersFactory connectionStreamInitializersFactory)
{
ListenOptions = listenOptions;
ConnectionOptions = connectionOptions;
SocketOptionsSetter = socketOptionsSetter;
PipelineFilterFactory = pipelineFilterFactory;
Logger = connectionOptions.Logger;

ConnectionStreamInitializers = connectionStreamInitializersFactory.Create(listenOptions);
}

public abstract Task<IConnection> 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.");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
1 change: 1 addition & 0 deletions src/SuperSocket.Server/Connection/GZipStreamInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using SuperSocket.Connection;
using SuperSocket.Server.Abstractions;
using SuperSocket.Server.Abstractions.Connections;

namespace SuperSocket.Server.Connection
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading;
using System.Threading.Tasks;
using SuperSocket.Server.Abstractions;
using SuperSocket.Server.Abstractions.Connections;

namespace SuperSocket.Server.Connection
{
Expand Down
2 changes: 1 addition & 1 deletion src/SuperSocket.Server/Connection/SslStreamInitializer.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
94 changes: 5 additions & 89 deletions src/SuperSocket.Server/Connection/TcpConnectionFactory.cs
Original file line number Diff line number Diff line change
@@ -1,59 +1,37 @@
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;
using SuperSocket.Server.Abstractions.Connections;

namespace SuperSocket.Server.Connection
{
public class TcpConnectionFactory<TPackageInfo> : IConnectionFactory
public class TcpConnectionFactory<TPackageInfo> : TcpConnectionFactoryBase<TPackageInfo>
{
protected ListenOptions ListenOptions { get; }

protected ConnectionOptions ConnectionOptions { get; }

protected Action<Socket> SocketOptionsSetter { get; }

protected IPipelineFilterFactory<TPackageInfo> PipelineFilterFactory;

protected ILogger Logger { get; }

private IEnumerable<IConnectionStreamInitializer> _connectionStreamInitializers;

public TcpConnectionFactory(
ListenOptions listenOptions,
ConnectionOptions connectionOptions,
Action<Socket> socketOptionsSetter,
IPipelineFilterFactory<TPackageInfo> 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<IConnection> CreateConnection(object connection, CancellationToken cancellationToken)
public override async Task<IConnection> CreateConnection(object connection, CancellationToken cancellationToken)
{
var socket = connection as Socket;

ApplySocketOptions(socket);

if (_connectionStreamInitializers is IEnumerable<IConnectionStreamInitializer> connectionStreamInitializers
if (ConnectionStreamInitializers is IEnumerable<IConnectionStreamInitializer> connectionStreamInitializers
&& connectionStreamInitializers.Any())
{
var stream = default(Stream);
Expand All @@ -68,67 +46,5 @@ public virtual async Task<IConnection> CreateConnection(object connection, Cance

return new TcpPipeConnection<TPackageInfo>(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.");
}
}
}
}

0 comments on commit fbcd404

Please sign in to comment.