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.
 
 
 
 

131 linhas
4.0 KiB

  1. using System;
  2. using System.Linq;
  3. using System.Net.Sockets;
  4. using System.Runtime.InteropServices.WindowsRuntime;
  5. using System.Threading.Tasks;
  6. using Windows.Networking;
  7. using Windows.Networking.Sockets;
  8. using Windows.Security.Cryptography.Certificates;
  9. using Windows.Storage.Streams;
  10. using MQTTnet.Core.Channel;
  11. using MQTTnet.Core.Client;
  12. using MQTTnet.Core.Exceptions;
  13. namespace MQTTnet.Implementations
  14. {
  15. public sealed class MqttTcpChannel : IMqttCommunicationChannel, IDisposable
  16. {
  17. private StreamSocket _socket;
  18. public MqttTcpChannel()
  19. {
  20. }
  21. public MqttTcpChannel(StreamSocket socket)
  22. {
  23. _socket = socket ?? throw new ArgumentNullException(nameof(socket));
  24. }
  25. public async Task ConnectAsync(MqttClientOptions options)
  26. {
  27. if (options == null) throw new ArgumentNullException(nameof(options));
  28. try
  29. {
  30. if (_socket == null)
  31. {
  32. _socket = new StreamSocket();
  33. }
  34. if (!options.TlsOptions.UseTls)
  35. {
  36. await _socket.ConnectAsync(new HostName(options.Server), options.GetPort().ToString());
  37. }
  38. else
  39. {
  40. _socket.Control.ClientCertificate = LoadCertificate(options);
  41. if (!options.TlsOptions.CheckCertificateRevocation)
  42. {
  43. _socket.Control.IgnorableServerCertificateErrors.Add(ChainValidationResult.IncompleteChain);
  44. _socket.Control.IgnorableServerCertificateErrors.Add(ChainValidationResult.RevocationInformationMissing);
  45. }
  46. await _socket.ConnectAsync(new HostName(options.Server), options.GetPort().ToString(), SocketProtectionLevel.Tls12);
  47. }
  48. }
  49. catch (SocketException exception)
  50. {
  51. throw new MqttCommunicationException(exception);
  52. }
  53. }
  54. public Task DisconnectAsync()
  55. {
  56. try
  57. {
  58. Dispose();
  59. return Task.FromResult(0);
  60. }
  61. catch (SocketException exception)
  62. {
  63. throw new MqttCommunicationException(exception);
  64. }
  65. }
  66. public async Task WriteAsync(byte[] buffer)
  67. {
  68. if (buffer == null) throw new ArgumentNullException(nameof(buffer));
  69. try
  70. {
  71. await _socket.OutputStream.WriteAsync(buffer.AsBuffer());
  72. await _socket.OutputStream.FlushAsync();
  73. }
  74. catch (SocketException exception)
  75. {
  76. throw new MqttCommunicationException(exception);
  77. }
  78. }
  79. public int Peek()
  80. {
  81. return 0;
  82. }
  83. public async Task<ArraySegment<byte>> ReadAsync(int length, byte[] buffer)
  84. {
  85. if (buffer == null) throw new ArgumentNullException(nameof(buffer));
  86. try
  87. {
  88. var result = await _socket.InputStream.ReadAsync(buffer.AsBuffer(), (uint)buffer.Length, InputStreamOptions.None);
  89. return new ArraySegment<byte>(buffer, 0, (int)result.Length);
  90. }
  91. catch (SocketException exception)
  92. {
  93. throw new MqttCommunicationException(exception);
  94. }
  95. }
  96. public void Dispose()
  97. {
  98. _socket?.Dispose();
  99. _socket = null;
  100. }
  101. private static Certificate LoadCertificate(MqttClientOptions options)
  102. {
  103. if (options.TlsOptions.Certificates == null || !options.TlsOptions.Certificates.Any())
  104. {
  105. return null;
  106. }
  107. if (options.TlsOptions.Certificates.Count > 1)
  108. {
  109. throw new NotSupportedException("Only one client certificate is supported for UWP.");
  110. }
  111. return new Certificate(options.TlsOptions.Certificates.First().AsBuffer());
  112. }
  113. }
  114. }