-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
3 changed files
with
88 additions
and
0 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,27 @@ | ||
namespace ActiveMQ.Artemis.Core.Client; | ||
|
||
/// <summary> | ||
/// Defines the ActiveMQ Artemis endpoint. | ||
/// </summary> | ||
public class Endpoint | ||
{ | ||
/// <summary> | ||
/// Gets or sets the protocol scheme. | ||
/// </summary> | ||
public string Host { get; init; } | ||
Check warning on line 11 in src/ArtemisNetCoreClient/Endpoint.cs GitHub Actions / linux
|
||
|
||
/// <summary> | ||
/// Gets or sets the port number of the endpoint. | ||
/// </summary> | ||
public int Port { get; init; } | ||
|
||
/// <summary> | ||
/// Gets or sets the user name that is used for authentication. | ||
/// </summary> | ||
public string? User { get; init; } | ||
|
||
/// <summary> | ||
/// Gets or sets the password that is used for authentication. | ||
/// </summary> | ||
public string? Password { get; init; } | ||
} |
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,14 @@ | ||
using System.Net.Sockets; | ||
|
||
namespace ActiveMQ.Artemis.Core.Client; | ||
|
||
public interface ISession : IAsyncDisposable; | ||
|
||
internal class Session(Socket socket) : ISession | ||
{ | ||
public ValueTask DisposeAsync() | ||
{ | ||
socket.Dispose(); | ||
return ValueTask.CompletedTask; | ||
} | ||
} |
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,47 @@ | ||
using System.Net; | ||
using System.Net.Sockets; | ||
|
||
namespace ActiveMQ.Artemis.Core.Client; | ||
|
||
public class SessionFactory | ||
{ | ||
public async Task<ISession> CreateAsync(Endpoint endpoint, CancellationToken cancellationToken = default) | ||
{ | ||
var ipAddresses = IPAddress.TryParse(endpoint.Host, out var ip) | ||
? [ip] | ||
: await Dns.GetHostAddressesAsync(endpoint.Host, cancellationToken).ConfigureAwait(false); | ||
|
||
Socket? socket = null; | ||
Exception? exception = null; | ||
|
||
foreach (var ipAddress in ipAddresses) | ||
{ | ||
if ((ipAddress.AddressFamily == AddressFamily.InterNetwork && !Socket.OSSupportsIPv4) || | ||
(ipAddress.AddressFamily == AddressFamily.InterNetworkV6 && !Socket.OSSupportsIPv6)) | ||
{ | ||
continue; | ||
} | ||
|
||
socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); | ||
try | ||
{ | ||
await socket.ConnectAsync(ipAddress, endpoint.Port, cancellationToken).ConfigureAwait(false); | ||
exception = null; | ||
break; | ||
} | ||
catch (Exception e) | ||
{ | ||
exception = e; | ||
socket.Dispose(); | ||
socket = null; | ||
} | ||
} | ||
|
||
if (socket == null) | ||
{ | ||
throw exception ?? new SocketException((int)SocketError.AddressNotAvailable); | ||
} | ||
|
||
return new Session(socket); | ||
} | ||
} |