1using System.Net.Sockets;
3using System.Threading.Tasks;
11 private readonly
int _portNumber;
12 private readonly
string _server;
13 private TcpClient _tcpClient;
23 _tcpClient =
new TcpClient();
25 _portNumber = portNumber;
33 var tcpClient = _tcpClient;
34 return tcpClient?.Connected ??
false;
39 public override async Task
Open()
43 _tcpClient =
new TcpClient {NoDelay =
true};
44 await _tcpClient.ConnectAsync(_server, _portNumber);
50 var tcpClient = _tcpClient;
52 if (_tcpClient?.Connected ??
false) tcpClient?.GetStream().Close();
54 return Task.CompletedTask;
62 var tcpClient = _tcpClient;
64 if (tcpClient !=
null)
66 await tcpClient.GetStream().WriteAsync(buffer, 0, buffer.Length).ConfigureAwait(
false);
71 public override async Task<int>
ReadAsync(
byte[] buffer, CancellationToken token)
73 var tcpClient = _tcpClient;
74 if (tcpClient !=
null)
76 return await tcpClient.GetStream().ReadAsync(buffer, 0, buffer.Length, token).ConfigureAwait(
false);
85 return $
"{_server}:{_portNumber}";
OsdpConnection(int baudRate)
Creates an instance of OsdpConnection.
override Task Close()
Close the connection for communications.
override string ToString()
override bool IsOpen
Is the connection open and ready to communicate.
override async Task WriteAsync(byte[] buffer)
Write to connection.
override async Task< int > ReadAsync(byte[] buffer, CancellationToken token)
Read from connection.Number of actual bytes read
override async Task Open()
Open the connection for communications.
TcpClientOsdpConnection(string server, int portNumber, int baudRate)
Initializes a new instance of the TcpClientOsdpConnection class.