You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

128 lines
3.7 KiB

  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 _socket.ConnectAsync(options.Server, options.GetPort());
  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 _socket.SendAsync(new ArraySegment<byte>(buffer), SocketFlags.None);
  67. }
  68. catch (SocketException exception)
  69. {
  70. throw new MqttCommunicationException(exception);
  71. }
  72. }
  73. public Task ReadAsync(byte[] buffer)
  74. {
  75. if (buffer == null) throw new ArgumentNullException(nameof(buffer));
  76. try
  77. {
  78. if (_sslStream != null)
  79. {
  80. return _sslStream.ReadAsync(buffer, 0, buffer.Length);
  81. }
  82. return _socket.ReceiveAsync(new ArraySegment<byte>(buffer), SocketFlags.None);
  83. }
  84. catch (SocketException exception)
  85. {
  86. throw new MqttCommunicationException(exception);
  87. }
  88. }
  89. public void Dispose()
  90. {
  91. _socket?.Dispose();
  92. _sslStream?.Dispose();
  93. _socket = null;
  94. _sslStream = null;
  95. }
  96. private static X509CertificateCollection LoadCertificates(MqttClientOptions options)
  97. {
  98. var certificates = new X509CertificateCollection();
  99. if (options.TlsOptions.Certificates == null)
  100. {
  101. return certificates;
  102. }
  103. foreach (var certificate in options.TlsOptions.Certificates)
  104. {
  105. certificates.Add(new X509Certificate(certificate));
  106. }
  107. return certificates;
  108. }
  109. }
  110. }