Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

MqttTcpChannel.cs 4.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Net.Security;
  3. using System.Net.Sockets;
  4. using System.Security.Authentication;
  5. using System.Security.Cryptography.X509Certificates;
  6. using System.Threading.Tasks;
  7. using MQTTnet.Core.Channel;
  8. using MQTTnet.Core.Client;
  9. using MQTTnet.Core.Exceptions;
  10. namespace MQTTnet.Implementations
  11. {
  12. public sealed class MqttTcpChannel : IMqttCommunicationChannel, IDisposable
  13. {
  14. private Socket _socket;
  15. private SslStream _sslStream;
  16. public MqttTcpChannel()
  17. {
  18. }
  19. public MqttTcpChannel(Socket socket, SslStream sslStream)
  20. {
  21. _socket = socket ?? throw new ArgumentNullException(nameof(socket));
  22. _sslStream = sslStream;
  23. }
  24. public async Task ConnectAsync(MqttClientOptions options)
  25. {
  26. if (options == null) throw new ArgumentNullException(nameof(options));
  27. try
  28. {
  29. if (_socket == null)
  30. {
  31. _socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
  32. }
  33. await Task.Factory.FromAsync(_socket.BeginConnect, _socket.EndConnect, options.Server, options.GetPort(), null);
  34. if (options.TlsOptions.UseTls)
  35. {
  36. _sslStream = new SslStream(new NetworkStream(_socket, true));
  37. await _sslStream.AuthenticateAsClientAsync(options.Server, LoadCertificates(options), SslProtocols.Tls12, options.TlsOptions.CheckCertificateRevocation);
  38. }
  39. }
  40. catch (SocketException exception)
  41. {
  42. throw new MqttCommunicationException(exception);
  43. }
  44. }
  45. public Task DisconnectAsync()
  46. {
  47. try
  48. {
  49. Dispose();
  50. return Task.FromResult(0);
  51. }
  52. catch (SocketException exception)
  53. {
  54. throw new MqttCommunicationException(exception);
  55. }
  56. }
  57. public Task WriteAsync(byte[] buffer)
  58. {
  59. if (buffer == null) throw new ArgumentNullException(nameof(buffer));
  60. try
  61. {
  62. if (_sslStream != null)
  63. {
  64. return _sslStream.WriteAsync(buffer, 0, buffer.Length);
  65. }
  66. return Task.Factory.FromAsync(
  67. // ReSharper disable once AssignNullToNotNullAttribute
  68. _socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, null, null),
  69. _socket.EndSend);
  70. }
  71. catch (SocketException exception)
  72. {
  73. throw new MqttCommunicationException(exception);
  74. }
  75. }
  76. public Task ReadAsync(byte[] buffer)
  77. {
  78. if (buffer == null) throw new ArgumentNullException(nameof(buffer));
  79. try
  80. {
  81. if (_sslStream != null)
  82. {
  83. return _sslStream.ReadAsync(buffer, 0, buffer.Length);
  84. }
  85. return Task.Factory.FromAsync(
  86. // ReSharper disable once AssignNullToNotNullAttribute
  87. _socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, null, null),
  88. _socket.EndReceive);
  89. }
  90. catch (SocketException exception)
  91. {
  92. throw new MqttCommunicationException(exception);
  93. }
  94. }
  95. public void Dispose()
  96. {
  97. _socket?.Dispose();
  98. _sslStream?.Dispose();
  99. _socket = null;
  100. _sslStream = null;
  101. }
  102. private static X509CertificateCollection LoadCertificates(MqttClientOptions options)
  103. {
  104. var certificates = new X509CertificateCollection();
  105. if (options.TlsOptions.Certificates == null)
  106. {
  107. return certificates;
  108. }
  109. foreach (var certificate in options.TlsOptions.Certificates)
  110. {
  111. certificates.Add(new X509Certificate(certificate));
  112. }
  113. return certificates;
  114. }
  115. }
  116. }