2using System.Collections.Concurrent;
4using System.Threading.Tasks;
5using Microsoft.Extensions.Logging;
20 private bool _disposedValue;
21 private readonly ConcurrentDictionary<Task, OsdpConnection> _connections =
new();
55 public abstract Task
Start(Func<IOsdpConnection, Task> newConnectionHandler);
58 public virtual async Task
Stop()
62 Logger?.LogDebug(
"Stopping OSDP connection listener...");
66 var entries = _connections.ToArray();
67 if (entries.Length == 0)
break;
69 await Task.WhenAll(entries.Select(item => item.Value.Close()));
70 await Task.WhenAll(entries.Select(x => x.Key));
73 Logger?.LogDebug(
"OSDP connection listener stopped");
80 GC.SuppressFinalize(
this);
98 _connections.TryAdd(task, connection);
100 Logger?.LogDebug(
"New OSDP connection opened - total connections: {ConnectionCount}", _connections.Count);
102 _connections.TryRemove(task, out _);
103 Logger?.LogDebug(
"OSDP connection terminated - remaining connections: {ConnectionCount}", _connections.Count);
111 protected virtual void Dispose(
bool disposing)
120 _disposedValue =
true;
int ConnectionCount
Gets the number of active connections currently being managed by this listener.
void RegisterConnection(OsdpConnection connection, Task task)
Registers a new connection and its associated handling task with the listener.
OsdpConnectionListener(int baudRate, ILoggerFactory loggerFactory=null)
Initializes a new instance of the OsdpConnectionListener class.
Task Start(Func< IOsdpConnection, Task > newConnectionHandler)
Starts listening for incoming connections from ACUs.A task representing the asynchronous operation.
virtual async Task Stop()
Stops the listener and terminates any active connections.A task representing the asynchronous operati...
ILogger Logger
Gets the logger instance for this listener if a logger factory was provided.
bool IsRunning
Gets a value indicating whether the listener is currently running and accepting connections.
int BaudRate
Gets the baud rate for serial connections.For TCP connections, this value may not be applicable.
ILoggerFactory LoggerFactory
Gets the logger factory instance if one was provided during instantiation.
virtual void Dispose(bool disposing)
Releases the resources used by the OsdpConnectionListener instance.
Base OSDP connection class.
Task Close()
Close the connection for communications.
Defines a connection listener for OSDP Peripheral Devices (PDs) that need to accept incoming connecti...