Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

7 роки тому
7 роки тому
7 роки тому
7 роки тому
7 роки тому
7 роки тому
7 роки тому
7 роки тому
7 роки тому
7 роки тому
7 роки тому
7 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 async Task ReadAsync(byte[] buffer)
  80. {
  81. if (buffer == null) throw new ArgumentNullException(nameof(buffer));
  82. try
  83. {
  84. await _socket.InputStream.ReadAsync(buffer.AsBuffer(), (uint)buffer.Length, InputStreamOptions.None);
  85. }
  86. catch (SocketException exception)
  87. {
  88. throw new MqttCommunicationException(exception);
  89. }
  90. }
  91. public void Dispose()
  92. {
  93. _socket?.Dispose();
  94. _socket = null;
  95. }
  96. private static Certificate LoadCertificate(MqttClientOptions options)
  97. {
  98. if (options.TlsOptions.Certificates == null || !options.TlsOptions.Certificates.Any())
  99. {
  100. return null;
  101. }
  102. if (options.TlsOptions.Certificates.Count > 1)
  103. {
  104. throw new NotSupportedException("Only one client certificate is supported for UWP.");
  105. }
  106. return new Certificate(options.TlsOptions.Certificates.First().AsBuffer());
  107. }
  108. }
  109. }