Innovenergy_trunk/csharp/Lib/Channels/Framed/Channel.cs

66 lines
1.5 KiB
C#
Raw Normal View History

2023-02-16 12:57:06 +00:00
using InnovEnergy.Lib.Utils;
namespace InnovEnergy.Lib.Channels.Framed;
2023-08-18 13:57:00 +00:00
public class Channel<Tx, Rx> : Connection
2023-02-16 12:57:06 +00:00
{
private readonly AsyncAction<Tx> _Transmit;
private readonly Async<Rx> _Receive;
public Channel(AsyncAction<Tx> transmit,
Async<Rx> receive,
AsyncAction? open = null,
AsyncAction? close = null,
CloseAfter closeAfter = CloseAfter.Error)
:
base(open, close, closeAfter)
{
_Transmit = transmit;
_Receive = receive;
}
public async Task Transmit(Tx data)
{
try
{
await Open();
await _Transmit(data);
await CloseAfterTransmit();
}
catch
{
await CloseAfterError();
throw;
}
}
public async Task<Rx> Receive()
{
try
{
await Open();
var data = await _Receive();
await CloseAfterReceive();
return data;
}
catch
{
await CloseAfterError();
throw;
}
}
}
public class Channel<T> : Channel<T, T>
{
public Channel(AsyncAction<T> transmit,
Async<T> receive,
AsyncAction? open = null,
AsyncAction? close = null,
CloseAfter closeAfter = CloseAfter.Error)
:
base(transmit, receive, open, close, closeAfter)
{
}
}