Skip to content

Commit

Permalink
Connect to ActiveMQ Artemis
Browse files Browse the repository at this point in the history
  • Loading branch information
Havret committed Mar 12, 2024
1 parent 52ca0fd commit 5249557
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/ArtemisNetCoreClient/Endpoint.cs
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

View workflow job for this annotation

GitHub Actions / linux

Non-nullable property 'Host' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 11 in src/ArtemisNetCoreClient/Endpoint.cs

View workflow job for this annotation

GitHub Actions / linux

Non-nullable property 'Host' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

/// <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; }
}
14 changes: 14 additions & 0 deletions src/ArtemisNetCoreClient/ISession.cs
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;
}
}
47 changes: 47 additions & 0 deletions src/ArtemisNetCoreClient/SessionFactory.cs
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);
}
}

0 comments on commit 5249557

Please sign in to comment.