Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

190 linhas
5.8 KiB

  1. #if WINDOWS_UWP
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.InteropServices.WindowsRuntime;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using Windows.Networking;
  10. using Windows.Networking.Sockets;
  11. using Windows.Security.Cryptography.Certificates;
  12. using MQTTnet.Channel;
  13. using MQTTnet.Client;
  14. namespace MQTTnet.Implementations
  15. {
  16. public sealed class MqttTcpChannel : IMqttChannel
  17. {
  18. // ReSharper disable once MemberCanBePrivate.Global
  19. // ReSharper disable once AutoPropertyCanBeMadeGetOnly.Global
  20. public static int BufferSize { get; set; } = 4096; // Can be changed for fine tuning by library user.
  21. private readonly int _bufferSize = BufferSize;
  22. private readonly MqttClientTcpOptions _options;
  23. private StreamSocket _socket;
  24. private Stream _readStream;
  25. private Stream _writeStream;
  26. public MqttTcpChannel(MqttClientTcpOptions options)
  27. {
  28. _options = options ?? throw new ArgumentNullException(nameof(options));
  29. _bufferSize = options.BufferSize;
  30. }
  31. public MqttTcpChannel(StreamSocket socket)
  32. {
  33. _socket = socket ?? throw new ArgumentNullException(nameof(socket));
  34. CreateStreams();
  35. }
  36. public static Func<MqttClientTcpOptions, IEnumerable<ChainValidationResult>> CustomIgnorableServerCertificateErrorsResolver { get; set; }
  37. public async Task ConnectAsync(CancellationToken cancellationToken)
  38. {
  39. if (_socket == null)
  40. {
  41. _socket = new StreamSocket();
  42. _socket.Control.NoDelay = true;
  43. _socket.Control.KeepAlive = true;
  44. }
  45. if (!_options.TlsOptions.UseTls)
  46. {
  47. await _socket.ConnectAsync(new HostName(_options.Server), _options.GetPort().ToString());
  48. }
  49. else
  50. {
  51. _socket.Control.ClientCertificate = LoadCertificate(_options);
  52. foreach (var ignorableChainValidationResult in ResolveIgnorableServerCertificateErrors())
  53. {
  54. _socket.Control.IgnorableServerCertificateErrors.Add(ignorableChainValidationResult);
  55. }
  56. await _socket.ConnectAsync(new HostName(_options.Server), _options.GetPort().ToString(), SocketProtectionLevel.Tls12);
  57. }
  58. CreateStreams();
  59. }
  60. public Task DisconnectAsync()
  61. {
  62. Dispose();
  63. return Task.FromResult(0);
  64. }
  65. public Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
  66. {
  67. return _readStream.ReadAsync(buffer, offset, count, cancellationToken);
  68. }
  69. public async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
  70. {
  71. await _writeStream.WriteAsync(buffer, offset, count, cancellationToken);
  72. await _writeStream.FlushAsync(cancellationToken);
  73. }
  74. public void Dispose()
  75. {
  76. try
  77. {
  78. _readStream?.Dispose();
  79. }
  80. catch (ObjectDisposedException)
  81. {
  82. }
  83. catch (NullReferenceException)
  84. {
  85. }
  86. finally
  87. {
  88. _readStream = null;
  89. }
  90. try
  91. {
  92. _writeStream?.Dispose();
  93. }
  94. catch (ObjectDisposedException)
  95. {
  96. }
  97. catch (NullReferenceException)
  98. {
  99. }
  100. finally
  101. {
  102. _writeStream = null;
  103. }
  104. try
  105. {
  106. _socket?.Dispose();
  107. }
  108. catch (ObjectDisposedException)
  109. {
  110. }
  111. catch (NullReferenceException)
  112. {
  113. }
  114. finally
  115. {
  116. _socket = null;
  117. }
  118. }
  119. private static Certificate LoadCertificate(MqttClientTcpOptions options)
  120. {
  121. if (options.TlsOptions.Certificates == null || !options.TlsOptions.Certificates.Any())
  122. {
  123. return null;
  124. }
  125. if (options.TlsOptions.Certificates.Count > 1)
  126. {
  127. throw new NotSupportedException("Only one client certificate is supported for UWP.");
  128. }
  129. return new Certificate(options.TlsOptions.Certificates.First().AsBuffer());
  130. }
  131. private IEnumerable<ChainValidationResult> ResolveIgnorableServerCertificateErrors()
  132. {
  133. if (CustomIgnorableServerCertificateErrorsResolver != null)
  134. {
  135. return CustomIgnorableServerCertificateErrorsResolver(_options);
  136. }
  137. var result = new List<ChainValidationResult>();
  138. if (_options.TlsOptions.IgnoreCertificateRevocationErrors)
  139. {
  140. result.Add(ChainValidationResult.RevocationInformationMissing);
  141. //_socket.Control.IgnorableServerCertificateErrors.Add(ChainValidationResult.Revoked); Not supported.
  142. result.Add(ChainValidationResult.RevocationFailure);
  143. }
  144. if (_options.TlsOptions.IgnoreCertificateChainErrors)
  145. {
  146. result.Add(ChainValidationResult.IncompleteChain);
  147. }
  148. if (_options.TlsOptions.AllowUntrustedCertificates)
  149. {
  150. result.Add(ChainValidationResult.Untrusted);
  151. }
  152. return result;
  153. }
  154. private void CreateStreams()
  155. {
  156. _readStream = _socket.InputStream.AsStreamForRead(_bufferSize);
  157. _writeStream = _socket.OutputStream.AsStreamForWrite(_bufferSize);
  158. }
  159. }
  160. }
  161. #endif