-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef49457
commit fbcd404
Showing
8 changed files
with
123 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
src/SuperSocket.Server.Abstractions/Connections/TcpConnectionFactoryBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters