2using System.Net.Sockets;
3using System.Threading.Tasks;
4using Microsoft.Extensions.Logging;
19 private readonly TcpListener _listener;
28 int portNumber,
int baudRate, ILoggerFactory loggerFactory =
null) : base(baudRate, loggerFactory)
30 _listener = TcpListener.Create(portNumber);
34 public override Task
Start(Func<IOsdpConnection, Task> newConnectionHandler)
41 Logger?.LogInformation(
"TCP listener started on {Endpoint} for incoming OSDP connections", _listener.LocalEndpoint.ToString());
49 var client = await _listener.AcceptTcpClientAsync();
50 Logger?.LogDebug(
"Accepted TCP connection from {RemoteEndpoint}", client.Client.RemoteEndPoint?.ToString());
53 var task = newConnectionHandler(connection);
56 catch (ObjectDisposedException)
63 Logger?.LogError(ex,
"Error accepting TCP connection");
68 return Task.CompletedTask;
72 public override Task
Stop()
76 Logger?.LogInformation(
"TCP listener stopped");
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.
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.
override Task Stop()
Stops the listener and terminates any active connections.A task representing the asynchronous operati...
override Task Start(Func< IOsdpConnection, Task > newConnectionHandler)
Starts listening for incoming connections from ACUs.A task representing the asynchronous operation.
TcpConnectionListener(int portNumber, int baudRate, ILoggerFactory loggerFactory=null)
Initializes a new instance of the TcpConnectionListener class.