Skip to content

Commit

Permalink
Allow Customizing Socket Creation in TCPClient #90
Browse files Browse the repository at this point in the history
  • Loading branch information
chronoxor committed Oct 29, 2020
1 parent fea66f9 commit 2312a1d
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Specify version format
version: "3.0.20.{build}"
version: "3.0.21.{build}"

# Image to use
image: Visual Studio 2019
Expand Down
2 changes: 1 addition & 1 deletion source/NetCoreServer/NetCoreServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Version>3.0.20</Version>
<Version>3.0.21</Version>
<Authors>Ivan Shynkarenka</Authors>
<Copyright>Copyright (c) 2019-2020 Ivan Shynkarenka</Copyright>
<RepositoryUrl>https://github.com/chronoxor/NetCoreServer</RepositoryUrl>
Expand Down
16 changes: 14 additions & 2 deletions source/NetCoreServer/SslClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,18 @@ public SslClient(SslContext context, IPEndPoint endpoint)
/// </summary>
public bool IsHandshaked { get; private set; }

/// <summary>
/// Create a new socket object
/// </summary>
/// <remarks>
/// Method may be override if you need to prepare some specific socket object in your implementation.
/// </remarks>
/// <returns>Socket object</returns>
protected virtual Socket CreateSocket()
{
return new Socket(Endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
}

/// <summary>
/// Connect the client (synchronous)
/// </summary>
Expand All @@ -160,7 +172,7 @@ public virtual bool Connect()
_connectEventArg.Completed += OnAsyncCompleted;

// Create a new client socket
Socket = new Socket(Endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
Socket = CreateSocket();

// Apply the option: dual mode (this option must be applied before connecting)
if (Socket.AddressFamily == AddressFamily.InterNetworkV6)
Expand Down Expand Up @@ -356,7 +368,7 @@ public virtual bool ConnectAsync()
_connectEventArg.Completed += OnAsyncCompleted;

// Create a new client socket
Socket = new Socket(Endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
Socket = CreateSocket();

// Apply the option: dual mode (this option must be applied before connecting)
if (Socket.AddressFamily == AddressFamily.InterNetworkV6)
Expand Down
14 changes: 13 additions & 1 deletion source/NetCoreServer/SslServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,18 @@ public SslServer(SslContext context, IPEndPoint endpoint)
/// </summary>
public bool IsAccepting { get; private set; }

/// <summary>
/// Create a new socket object
/// </summary>
/// <remarks>
/// Method may be override if you need to prepare some specific socket object in your implementation.
/// </remarks>
/// <returns>Socket object</returns>
protected virtual Socket CreateSocket()
{
return new Socket(Endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
}

/// <summary>
/// Start the server
/// </summary>
Expand All @@ -159,7 +171,7 @@ public virtual bool Start()
_acceptorEventArg.Completed += OnAsyncCompleted;

// Create a new acceptor socket
_acceptorSocket = new Socket(Endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
_acceptorSocket = CreateSocket();

// Update the acceptor socket disposed flag
IsSocketDisposed = false;
Expand Down
16 changes: 14 additions & 2 deletions source/NetCoreServer/TcpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ public TcpClient(IPEndPoint endpoint)
/// </summary>
public bool IsConnected { get; private set; }

/// <summary>
/// Create a new socket object
/// </summary>
/// <remarks>
/// Method may be override if you need to prepare some specific socket object in your implementation.
/// </remarks>
/// <returns>Socket object</returns>
protected virtual Socket CreateSocket()
{
return new Socket(Endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
}

/// <summary>
/// Connect the client (synchronous)
/// </summary>
Expand Down Expand Up @@ -138,7 +150,7 @@ public virtual bool Connect()
_sendEventArg.Completed += OnAsyncCompleted;

// Create a new client socket
Socket = new Socket(Endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
Socket = CreateSocket();

// Apply the option: dual mode (this option must be applied before connecting)
if (Socket.AddressFamily == AddressFamily.InterNetworkV6)
Expand Down Expand Up @@ -295,7 +307,7 @@ public virtual bool ConnectAsync()
_sendEventArg.Completed += OnAsyncCompleted;

// Create a new client socket
Socket = new Socket(Endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
Socket = CreateSocket();

// Apply the option: dual mode (this option must be applied before connecting)
if (Socket.AddressFamily == AddressFamily.InterNetworkV6)
Expand Down
14 changes: 13 additions & 1 deletion source/NetCoreServer/TcpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ public TcpServer(IPEndPoint endpoint)
/// </summary>
public bool IsAccepting { get; private set; }

/// <summary>
/// Create a new socket object
/// </summary>
/// <remarks>
/// Method may be override if you need to prepare some specific socket object in your implementation.
/// </remarks>
/// <returns>Socket object</returns>
protected virtual Socket CreateSocket()
{
return new Socket(Endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
}

/// <summary>
/// Start the server
/// </summary>
Expand All @@ -150,7 +162,7 @@ public virtual bool Start()
_acceptorEventArg.Completed += OnAsyncCompleted;

// Create a new acceptor socket
_acceptorSocket = new Socket(Endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
_acceptorSocket = CreateSocket();

// Update the acceptor socket disposed flag
IsSocketDisposed = false;
Expand Down
14 changes: 13 additions & 1 deletion source/NetCoreServer/UdpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ public UdpClient(IPEndPoint endpoint)
/// </summary>
public bool IsConnected { get; private set; }

/// <summary>
/// Create a new socket object
/// </summary>
/// <remarks>
/// Method may be override if you need to prepare some specific socket object in your implementation.
/// </remarks>
/// <returns>Socket object</returns>
protected virtual Socket CreateSocket()
{
return new Socket(Endpoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
}

/// <summary>
/// Connect the client (synchronous)
/// </summary>
Expand All @@ -135,7 +147,7 @@ public virtual bool Connect()
_sendEventArg.Completed += OnAsyncCompleted;

// Create a new client socket
Socket = new Socket(Endpoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
Socket = CreateSocket();

// Update the client socket disposed flag
IsSocketDisposed = false;
Expand Down
14 changes: 13 additions & 1 deletion source/NetCoreServer/UdpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ public UdpServer(IPEndPoint endpoint)
/// </summary>
public bool IsStarted { get; private set; }

/// <summary>
/// Create a new socket object
/// </summary>
/// <remarks>
/// Method may be override if you need to prepare some specific socket object in your implementation.
/// </remarks>
/// <returns>Socket object</returns>
protected virtual Socket CreateSocket()
{
return new Socket(Endpoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
}

/// <summary>
/// Start the server (synchronous)
/// </summary>
Expand All @@ -137,7 +149,7 @@ public virtual bool Start()
_sendEventArg.Completed += OnAsyncCompleted;

// Create a new server socket
Socket = new Socket(Endpoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
Socket = CreateSocket();

// Update the server socket disposed flag
IsSocketDisposed = false;
Expand Down

0 comments on commit 2312a1d

Please sign in to comment.