-
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
9 changed files
with
98 additions
and
65 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
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,6 @@ | ||
namespace ActiveMQ.Artemis.Core.Client.Framing; | ||
|
||
internal class SessionCloseMessage : Packet | ||
{ | ||
public const byte Type = 69; | ||
} |
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 |
---|---|---|
@@ -1,42 +1,3 @@ | ||
using System.Collections.Concurrent; | ||
using ActiveMQ.Artemis.Core.Client.Framing; | ||
|
||
namespace ActiveMQ.Artemis.Core.Client; | ||
|
||
public interface ISession : IAsyncDisposable; | ||
|
||
internal class Session : ISession | ||
{ | ||
private readonly Transport _transport; | ||
|
||
private ConcurrentDictionary<long, TaskCompletionSource<Packet>> _completionSources = new(); | ||
|
||
public Session(Transport transport) | ||
{ | ||
_transport = transport; | ||
|
||
_ = Task.Run(async () => | ||
{ | ||
while (true) | ||
{ | ||
var packet = await _transport.ReceiveAsync(default); | ||
} | ||
}); | ||
} | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
|
||
await _transport.SendAsync(new SessionStop(), ChannelId, default); | ||
|
||
await _transport.DisposeAsync().ConfigureAwait(false); | ||
} | ||
|
||
public long ChannelId { get; init; } | ||
|
||
public async Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
await _transport.SendAsync(new SessionStart(), ChannelId, cancellationToken); | ||
} | ||
} | ||
public interface ISession : IAsyncDisposable; |
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,75 @@ | ||
using System.Collections.Concurrent; | ||
using ActiveMQ.Artemis.Core.Client.Framing; | ||
|
||
namespace ActiveMQ.Artemis.Core.Client; | ||
|
||
internal class Session : ISession | ||
{ | ||
private readonly Transport _transport; | ||
|
||
private readonly ConcurrentDictionary<long, TaskCompletionSource<Packet>> _completionSources = new(); | ||
|
||
public Session(Transport transport) | ||
{ | ||
_transport = transport; | ||
|
||
// TODO: Clean up while loop on close | ||
_ = Task.Run(async () => | ||
{ | ||
while (true) | ||
{ | ||
try | ||
{ | ||
var packet = await _transport.ReceiveAsync(default); | ||
if (packet.IsResponse && _completionSources.TryRemove(packet.CorrelationId, out var tcs)) | ||
{ | ||
tcs.TrySetResult(packet); | ||
} | ||
else | ||
{ | ||
// TODO: Handle | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
// TODO: Handle exception | ||
Console.WriteLine(e); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
_ = await SendBlockingAsync<SessionStop, NullResponse>(new SessionStop(), default); | ||
_ = await SendBlockingAsync<SessionCloseMessage, NullResponse>(new SessionCloseMessage(), default); | ||
await _transport.DisposeAsync().ConfigureAwait(false); | ||
} | ||
|
||
private async Task<TResponse> SendBlockingAsync<TRequest, TResponse>(TRequest request, CancellationToken cancellationToken) where TRequest : Packet | ||
{ | ||
var tcs = new TaskCompletionSource<Packet>(); | ||
|
||
// TODO: Handle scenario when we cannot CorrelationId | ||
_ = _completionSources.TryAdd(request.CorrelationId, tcs); | ||
|
||
await _transport.SendAsync(request, ChannelId, cancellationToken); | ||
var responsePacket = await tcs.Task; | ||
if (responsePacket is TResponse response) | ||
{ | ||
return response; | ||
} | ||
else | ||
{ | ||
// TODO: Handle gracefully | ||
throw new ArgumentException($"Expected response {typeof(TResponse).Name} but got {responsePacket.GetType().Name}"); | ||
} | ||
} | ||
|
||
public long ChannelId { get; init; } | ||
|
||
public async Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
await _transport.SendAsync(new SessionStart(), ChannelId, cancellationToken); | ||
} | ||
} |
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