Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

104 строки
3.1 KiB

  1. using System;
  2. using System.IO;
  3. using System.Linq;
  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 MQTTnet.Core.Channel;
  10. using MQTTnet.Core.Client;
  11. namespace MQTTnet.Implementations
  12. {
  13. public sealed class MqttTcpChannel : IMqttCommunicationChannel, IDisposable
  14. {
  15. private StreamSocket _socket;
  16. public MqttTcpChannel()
  17. {
  18. }
  19. public MqttTcpChannel(StreamSocket socket)
  20. {
  21. _socket = socket ?? throw new ArgumentNullException(nameof(socket));
  22. CreateStreams();
  23. }
  24. public Stream SendStream { get; private set; }
  25. public Stream ReceiveStream { get; private set; }
  26. public Stream RawReceiveStream { get; private set; }
  27. public async Task ConnectAsync(MqttClientOptions options)
  28. {
  29. if (options == null) throw new ArgumentNullException(nameof(options));
  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. CreateStreams();
  49. }
  50. public Task DisconnectAsync()
  51. {
  52. Dispose();
  53. return Task.FromResult(0);
  54. }
  55. public void Dispose()
  56. {
  57. RawReceiveStream?.Dispose();
  58. RawReceiveStream = null;
  59. SendStream?.Dispose();
  60. SendStream = null;
  61. ReceiveStream?.Dispose();
  62. ReceiveStream = null;
  63. _socket?.Dispose();
  64. _socket = null;
  65. }
  66. private void CreateStreams()
  67. {
  68. SendStream = _socket.OutputStream.AsStreamForWrite();
  69. ReceiveStream = _socket.InputStream.AsStreamForRead();
  70. RawReceiveStream = ReceiveStream;
  71. }
  72. private static Certificate LoadCertificate(MqttClientOptions options)
  73. {
  74. if (options.TlsOptions.Certificates == null || !options.TlsOptions.Certificates.Any())
  75. {
  76. return null;
  77. }
  78. if (options.TlsOptions.Certificates.Count > 1)
  79. {
  80. throw new NotSupportedException("Only one client certificate is supported for UWP.");
  81. }
  82. return new Certificate(options.TlsOptions.Certificates.First().AsBuffer());
  83. }
  84. }
  85. }