using InnovEnergy.Lib.Channels.V2.Connections; namespace InnovEnergy.Lib.Channels.V2; public abstract class GenericChannel2 : IChannel, IConnection { private readonly IConnection _Connection; protected GenericChannel2(IConnection connection) { _Connection = connection; } Task IChannel.Read() { throw new NotImplementedException(); } Task IChannel.Write(Tx tx) { throw new NotImplementedException(); } public abstract Task Read(); public abstract Task Write(Tx tx); Task IConnection.Open() => _Connection.Open(); public void Close() => _Connection.Close(); public Boolean IsOpen => _Connection.IsOpen; } public class GenericChannel : IChannel, IConnection { private readonly Func> _Read; private readonly Func _Write; private readonly IConnection _Connection; public GenericChannel(IConnection connection, Func> read, Func write) { _Connection = connection; _Read = read; _Write = write; } public async Task Read() { try { var connection = await _Connection.Open(); return await _Read(connection); } catch { _Connection.Close(); throw; } } public async Task Write(Tx tx) { try { var connection = await _Connection.Open(); await _Write(connection, tx); } catch { _Connection.Close(); throw; } } public Task Open() => _Connection.Open(); public void Close() => _Connection.Close(); public Boolean IsOpen => _Connection.IsOpen; }