Skip to content

Commit

Permalink
expose handle session connect to use with Socket Duplicate And Close.
Browse files Browse the repository at this point in the history
  • Loading branch information
bobaoapae committed Feb 27, 2024
1 parent 2ee8a31 commit 58ea6d5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion NetX/NetX.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<PackageProjectUrl>https://github.com/project-nevareth/NetX</PackageProjectUrl>
<RepositoryType>git</RepositoryType>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<Version>2.2.4</Version>
<Version>2.2.6</Version>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
Expand Down
36 changes: 26 additions & 10 deletions NetX/NetXServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Versioning;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -61,9 +62,9 @@ public void Listen(CancellationToken cancellationToken = default)
_logger?.LogInformation("{svrName}: TCP Server listening on {ip}:{port}", _serverName, _options.EndPoint.Address, _options.EndPoint.Port);

_ = Task.Factory.StartNew(
() => StartAcceptAsync(cancellationToken),
() => StartAcceptAsync(cancellationToken),
default,
TaskCreationOptions.LongRunning,
TaskCreationOptions.LongRunning,
TaskScheduler.Default);
}

Expand All @@ -78,13 +79,17 @@ private async Task StartAcceptAsync(CancellationToken listenCancellationToken)
var sessionSocket = await _socket.AcceptAsync(listenCancellationToken);

_ = Task.Factory.StartNew(
() => ProcessSessionConnection(sessionSocket, listenCancellationToken),
default,
TaskCreationOptions.LongRunning,
() => ProcessSessionConnection(sessionSocket, (IPEndPoint)sessionSocket.RemoteEndPoint, listenCancellationToken),
default,
TaskCreationOptions.LongRunning,
TaskScheduler.Default);
}
catch (TaskCanceledException) { }
catch (OperationCanceledException) { }
catch (TaskCanceledException)
{
}
catch (OperationCanceledException)
{
}
catch (SocketException e)
{
_logger?.LogError(e, "{svrName}: An exception was throw on accept new connections", _serverName);
Expand All @@ -99,11 +104,20 @@ private async Task StartAcceptAsync(CancellationToken listenCancellationToken)
}
}

private async Task ProcessSessionConnection(Socket sessionSocket, CancellationToken cancellationToken)
/**
* The unique purpose of expose this method it's to allow the library to be used in a proxy environment using Socket Duplicate And Close.
*/
[SupportedOSPlatform("windows")]
public async Task UnsafeProcessSessionConnection(Socket sessionSocket, IPEndPoint ipEndPoint, CancellationToken cancellationToken)
{
await ProcessSessionConnection(sessionSocket, ipEndPoint, cancellationToken);
}

private async Task ProcessSessionConnection(Socket sessionSocket, IPEndPoint ipEndPoint, CancellationToken cancellationToken)
{
try
{
var remoteAddress = ((IPEndPoint)sessionSocket.RemoteEndPoint).Address.MapToIPv4();
var remoteAddress = ipEndPoint.Address.MapToIPv4();
if (_options.UseProxy)
{
await using var stream = new NetworkStream(sessionSocket);
Expand Down Expand Up @@ -137,7 +151,9 @@ private async Task ProcessSessionConnection(Socket sessionSocket, CancellationTo
}
}
}
catch { }
catch
{
}
}

private async Task DispatchOnSessionConnect(INetXSession session, CancellationToken cancellationToken)
Expand Down

0 comments on commit 58ea6d5

Please sign in to comment.