-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1267 from Badgerati/Issue-1251
Add DualMode support for Endpoints to listen on IPv4 and IPv6 at the same time
- Loading branch information
Showing
30 changed files
with
422 additions
and
189 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.5.002.0 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{41F81369-8680-4BC5-BA16-C7891D245717}" | ||
EndProject | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pode", "src\Listener\Pode.csproj", "{772D5C9F-1B25-46A7-8977-412A5F7F77D1}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{772D5C9F-1B25-46A7-8977-412A5F7F77D1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{772D5C9F-1B25-46A7-8977-412A5F7F77D1}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{772D5C9F-1B25-46A7-8977-412A5F7F77D1}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{772D5C9F-1B25-46A7-8977-412A5F7F77D1}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(NestedProjects) = preSolution | ||
{772D5C9F-1B25-46A7-8977-412A5F7F77D1} = {41F81369-8680-4BC5-BA16-C7891D245717} | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {F24001DC-2986-4305-B1B5-8E73BCDF1A77} | ||
EndGlobalSection | ||
EndGlobal |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Sockets; | ||
|
||
namespace Pode | ||
{ | ||
public class PodeEndpoint : IDisposable | ||
{ | ||
public IPAddress IPAddress { get; private set; } | ||
public int Port { get; private set; } | ||
public IPEndPoint Endpoint { get; private set; } | ||
public Socket Socket { get; private set; } | ||
public PodeSocket PodeSocket { get; private set; } | ||
public bool DualMode { get; private set; } | ||
public bool IsDisposed { get; private set; } | ||
|
||
public int ReceiveTimeout | ||
{ | ||
get => Socket.ReceiveTimeout; | ||
set => Socket.ReceiveTimeout = value; | ||
} | ||
|
||
public PodeEndpoint(PodeSocket socket, IPAddress ipAddress, int port, bool dualMode) | ||
{ | ||
IsDisposed = false; | ||
PodeSocket = socket; | ||
IPAddress = ipAddress; | ||
Port = port; | ||
Endpoint = new IPEndPoint(ipAddress, port); | ||
|
||
Socket = new Socket(Endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp) | ||
{ | ||
ReceiveTimeout = 100, | ||
NoDelay = true | ||
}; | ||
|
||
if (dualMode && Endpoint.AddressFamily == AddressFamily.InterNetworkV6) | ||
{ | ||
DualMode = true; | ||
Socket.DualMode = true; | ||
Socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false); | ||
} | ||
else | ||
{ | ||
DualMode = false; | ||
} | ||
} | ||
|
||
public void Listen() | ||
{ | ||
Socket.Bind(Endpoint); | ||
Socket.Listen(int.MaxValue); | ||
} | ||
|
||
public bool AcceptAsync(SocketAsyncEventArgs args) | ||
{ | ||
if (IsDisposed) | ||
{ | ||
throw new ObjectDisposedException("PodeEndpoint disposed"); | ||
} | ||
|
||
return Socket.AcceptAsync(args); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
IsDisposed = true; | ||
PodeSocket.CloseSocket(Socket); | ||
Socket = default(Socket); | ||
} | ||
|
||
public new bool Equals(object obj) | ||
{ | ||
var _endpoint = (PodeEndpoint)obj; | ||
return Endpoint.ToString() == _endpoint.Endpoint.ToString() && Port == _endpoint.Port; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.