-
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
4 changed files
with
101 additions
and
8 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 |
---|---|---|
@@ -1,14 +1,11 @@ | ||
using System.Net.Sockets; | ||
|
||
namespace ActiveMQ.Artemis.Core.Client; | ||
|
||
public interface ISession : IAsyncDisposable; | ||
|
||
internal class Session(Socket socket) : ISession | ||
internal class Session(Transport socket) : ISession | ||
{ | ||
public ValueTask DisposeAsync() | ||
public async ValueTask DisposeAsync() | ||
{ | ||
socket.Dispose(); | ||
return ValueTask.CompletedTask; | ||
await socket.DisposeAsync().ConfigureAwait(false); | ||
} | ||
} |
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,38 @@ | ||
using System.Net.Sockets; | ||
using ActiveMQ.Artemis.Core.Client.Framing; | ||
|
||
namespace ActiveMQ.Artemis.Core.Client; | ||
|
||
internal class Transport(Socket socket) : IAsyncDisposable | ||
{ | ||
public async Task SendAsync(Packet packet, CancellationToken cancellationToken) | ||
{ | ||
var byteBuffer = new ByteBuffer(); | ||
Codec.Encode(byteBuffer, packet, 1); | ||
await socket.SendAsync(byteBuffer.GetBuffer(), cancellationToken).ConfigureAwait(false); | ||
} | ||
|
||
public async Task<Packet> ReceiveAsync(CancellationToken cancellationToken) | ||
{ | ||
var receiveBuffer = new byte[sizeof(int)]; | ||
while (0 == await socket.ReceiveAsync(receiveBuffer, cancellationToken).ConfigureAwait(false)) | ||
{ | ||
} | ||
|
||
var size = new ByteBuffer(receiveBuffer).ReadInt(); | ||
|
||
var buffer = new byte[size]; | ||
_ = await socket.ReceiveAsync(buffer, cancellationToken).ConfigureAwait(false); | ||
|
||
var payloadBuffer = new ByteBuffer(buffer); | ||
|
||
var (packet, _) = Codec.Decode(payloadBuffer); | ||
return packet; | ||
} | ||
|
||
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,23 @@ | ||
namespace ActiveMQ.Artemis.Core.Client.Tests; | ||
|
||
public class SessionTests | ||
{ | ||
[Test] | ||
public async Task should_establish_session() | ||
{ | ||
// Arrange | ||
var connectionFactory = new SessionFactory(); | ||
|
||
// Act | ||
var session = await connectionFactory.CreateAsync(new Endpoint | ||
{ | ||
Host = "localhost", | ||
Port = 5445, | ||
User = "artemis", | ||
Password = "artemis" | ||
}); | ||
|
||
// Assert | ||
Assert.IsNotNull(session); | ||
} | ||
} |