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.
 
 
 
 

111 rivejä
3.3 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. using System.IO;
  11. namespace MQTTnet.Implementations
  12. {
  13. public sealed class MqttTcpChannel : IMqttCommunicationChannel, IDisposable
  14. {
  15. private Stream _dataStream;
  16. private Socket _socket;
  17. private SslStream _sslStream;
  18. public Stream ReceiveStream => _dataStream;
  19. public Stream RawStream => _dataStream;
  20. public Stream SendStream => _dataStream;
  21. /// <summary>
  22. /// called on client sockets are created in connect
  23. /// </summary>
  24. public MqttTcpChannel()
  25. {
  26. }
  27. /// <summary>
  28. /// called on server, sockets are passed in
  29. /// connect will not be called
  30. /// </summary>
  31. public MqttTcpChannel(Socket socket, SslStream sslStream)
  32. {
  33. _socket = socket ?? throw new ArgumentNullException(nameof(socket));
  34. _sslStream = sslStream;
  35. _dataStream = (Stream)sslStream ?? new NetworkStream(socket);
  36. }
  37. public async Task ConnectAsync(MqttClientOptions options)
  38. {
  39. if (options == null) throw new ArgumentNullException(nameof(options));
  40. try
  41. {
  42. if (_socket == null)
  43. {
  44. _socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
  45. }
  46. await _socket.ConnectAsync(options.Server, options.GetPort()).ConfigureAwait(false);
  47. if (options.TlsOptions.UseTls)
  48. {
  49. _sslStream = new SslStream(new NetworkStream(_socket, true));
  50. _dataStream = _sslStream;
  51. await _sslStream.AuthenticateAsClientAsync(options.Server, LoadCertificates(options), SslProtocols.Tls12, options.TlsOptions.CheckCertificateRevocation).ConfigureAwait(false);
  52. }
  53. else
  54. {
  55. _dataStream = new NetworkStream(_socket);
  56. }
  57. }
  58. catch (SocketException exception)
  59. {
  60. throw new MqttCommunicationException(exception);
  61. }
  62. }
  63. public Task DisconnectAsync()
  64. {
  65. try
  66. {
  67. Dispose();
  68. return Task.FromResult(0);
  69. }
  70. catch (SocketException exception)
  71. {
  72. throw new MqttCommunicationException(exception);
  73. }
  74. }
  75. public void Dispose()
  76. {
  77. _socket?.Dispose();
  78. _sslStream?.Dispose();
  79. _socket = null;
  80. _sslStream = null;
  81. }
  82. private static X509CertificateCollection LoadCertificates(MqttClientOptions options)
  83. {
  84. var certificates = new X509CertificateCollection();
  85. if (options.TlsOptions.Certificates == null)
  86. {
  87. return certificates;
  88. }
  89. foreach (var certificate in options.TlsOptions.Certificates)
  90. {
  91. certificates.Add(new X509Certificate(certificate));
  92. }
  93. return certificates;
  94. }
  95. }
  96. }