Files
Aperio_Control_Centre/Aperio_Control_Centre.UnitTest/AperioServer/Setups/TestingAperioConnectionContext.cs
2025-10-17 08:48:13 +02:00

93 lines
3.1 KiB
C#

using Aperio_Control_Centre.Aadp.Commands;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.Options;
using System.Diagnostics;
using System.IO.Pipelines;
using System.Net;
namespace Aperio_Control_Centre.AperioServer
{
public class TestingAperioConnectionOptions
{
//public string Error { get; set; } = string.Empty;
public Exception? Exception { get; set; }
}
public class TestingAperioConnectionContext : IAperioConnectionContext
{
private ConnectionContext _connection;
private readonly TestingAperioConnectionOptions? _error;
public string ConnectionId { get => _connection.ConnectionId; set => _connection.ConnectionId = value; }
public IFeatureCollection Features => _connection.Features;
public IDuplexPipe Transport { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public IPAddress Address => ((IPEndPoint)_connection.RemoteEndPoint)?.Address;
public List<string> DeviceWrites { get; private set; } = [];
public TestingAperioConnectionContext() { }
public TestingAperioConnectionContext(IOptions<TestingAperioConnectionOptions> options)
{
//Debug.WriteLine($"init with {options.Value.Error}");
_error = options?.Value;
}
public void Abort(ConnectionAbortedException abortReason)
{
Debug.WriteLine($"TestingAperioConnectionContext Abort {abortReason}");
}
public Task WriteToDevice(IPduCommand command, CancellationToken token)
{
DeviceWrites.Add($"{command.PDUType} - {command.LogString()}");
return Task.CompletedTask;
}
public void SetConnection(ConnectionContext connection)
{
Debug.WriteLine("TestingAperioConnectionContext SetConnection");
_connection = connection;
}
public void RemoveConnection()
{
Debug.WriteLine("TestingAperioConnectionContext RemoveConnection");
}
public Task Receive(CancellationTokenSource linkedCts)
{
Debug.WriteLine("TestingAperioConnectionContext Receive");
if (_error != null)
{
Debug.WriteLine($"Throw Exception {_error.Exception}");
//switch (_error.Error)
//{
// case "ConnectionResetException":
// //Debug.WriteLine($"Throw Exception {_error.Error}");
// throw new ConnectionResetException("Test");
// default:
// break;
//}
if(_error.Exception != null)
{
throw _error.Exception;
}
}
return Task.CompletedTask;
}
public ValueTask DisposeAsync()
{
Debug.WriteLine("TestingAperioConnectionContext DisposeAsync");
return ValueTask.CompletedTask;
}
}
}