-
Notifications
You must be signed in to change notification settings - Fork 1
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
6 changed files
with
91 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using EulynxLive.Point.Interfaces; | ||
using Google.Protobuf; | ||
using Grpc.Core; | ||
using Grpc.Net.Client; | ||
using Sci; | ||
using static Sci.Rasta; | ||
|
||
namespace EulynxLive.Point; | ||
|
||
class GrpcConnection : IConnection | ||
{ | ||
class GrpcConnectionException : Exception | ||
{ | ||
public GrpcConnectionException(string message) : base(message) { } | ||
} | ||
|
||
AsyncDuplexStreamingCall<SciPacket, SciPacket>? _connection; | ||
private CancellationToken _stoppingToken; | ||
|
||
public GrpcConnection(Metadata? metadata, string remoteEndpoint, CancellationToken stoppingToken) | ||
{ | ||
_stoppingToken = stoppingToken; | ||
|
||
var channel = GrpcChannel.ForAddress(remoteEndpoint); | ||
var client = new RastaClient(channel); | ||
_connection = client.Stream(metadata, cancellationToken: stoppingToken); | ||
} | ||
public void Dispose() | ||
{ | ||
_connection?.Dispose(); | ||
} | ||
|
||
public async Task<byte[]> ReceiveAsync(CancellationToken cancellationToken) | ||
{ | ||
if (_connection == null) throw new InvalidOperationException("Grpc connection not connected."); | ||
if (!await _connection.ResponseStream.MoveNext(cancellationToken)) throw new GrpcConnectionException("Could not receive grpc message."); | ||
return _connection.ResponseStream.Current.Message.ToByteArray(); | ||
} | ||
|
||
public async Task SendAsync(byte[] bytes) | ||
{ | ||
if (_connection == null) throw new InvalidOperationException("Grpc connection not connected."); | ||
await _connection.RequestStream.WriteAsync(new SciPacket() { Message = ByteString.CopyFrom(bytes) }); | ||
} | ||
} |
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,8 @@ | ||
namespace EulynxLive.Point.Interfaces | ||
{ | ||
interface IConnection : IDisposable | ||
{ | ||
Task<byte[]> ReceiveAsync(CancellationToken cancellationToken); | ||
Task SendAsync(byte[] bytes); | ||
} | ||
} |
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