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.
 
 
 
 

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