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.

MqttTcpChannel.cs 3.6 KiB

пре 7 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Net.Security;
  3. using System.Net.Sockets;
  4. using System.Security.Authentication;
  5. using System.Security.Cryptography.X509Certificates;
  6. using System.Threading.Tasks;
  7. using MQTTnet.Core.Channel;
  8. using MQTTnet.Core.Client;
  9. using MQTTnet.Core.Exceptions;
  10. namespace MQTTnet.Implementations
  11. {
  12. public sealed class MqttTcpChannel : IMqttCommunicationChannel, IDisposable
  13. {
  14. private readonly Socket _socket;
  15. private SslStream _sslStream;
  16. public MqttTcpChannel()
  17. {
  18. _socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
  19. }
  20. public MqttTcpChannel(Socket socket, SslStream sslStream)
  21. {
  22. _socket = socket ?? throw new ArgumentNullException(nameof(socket));
  23. _sslStream = sslStream;
  24. }
  25. public async Task ConnectAsync(MqttClientOptions options)
  26. {
  27. if (options == null) throw new ArgumentNullException(nameof(options));
  28. try
  29. {
  30. await _socket.ConnectAsync(options.Server, options.GetPort());
  31. if (options.TlsOptions.UseTls)
  32. {
  33. _sslStream = new SslStream(new NetworkStream(_socket, true));
  34. await _sslStream.AuthenticateAsClientAsync(options.Server, LoadCertificates(options), SslProtocols.Tls12, options.TlsOptions.CheckCertificateRevocation);
  35. }
  36. }
  37. catch (SocketException exception)
  38. {
  39. throw new MqttCommunicationException(exception);
  40. }
  41. }
  42. public Task DisconnectAsync()
  43. {
  44. try
  45. {
  46. _sslStream.Dispose();
  47. _socket.Dispose();
  48. return Task.FromResult(0);
  49. }
  50. catch (SocketException exception)
  51. {
  52. throw new MqttCommunicationException(exception);
  53. }
  54. }
  55. public Task WriteAsync(byte[] buffer)
  56. {
  57. if (buffer == null) throw new ArgumentNullException(nameof(buffer));
  58. try
  59. {
  60. if (_sslStream != null)
  61. {
  62. return _sslStream.WriteAsync(buffer, 0, buffer.Length);
  63. }
  64. return _socket.SendAsync(new ArraySegment<byte>(buffer), SocketFlags.None);
  65. }
  66. catch (SocketException exception)
  67. {
  68. throw new MqttCommunicationException(exception);
  69. }
  70. }
  71. public Task ReadAsync(byte[] buffer)
  72. {
  73. if (buffer == null) throw new ArgumentNullException(nameof(buffer));
  74. try
  75. {
  76. if (_sslStream != null)
  77. {
  78. return _sslStream.ReadAsync(buffer, 0, buffer.Length);
  79. }
  80. return _socket.ReceiveAsync(new ArraySegment<byte>(buffer), SocketFlags.None);
  81. }
  82. catch (SocketException exception)
  83. {
  84. throw new MqttCommunicationException(exception);
  85. }
  86. }
  87. public void Dispose()
  88. {
  89. _socket?.Dispose();
  90. _sslStream?.Dispose();
  91. }
  92. private static X509CertificateCollection LoadCertificates(MqttClientOptions options)
  93. {
  94. var certificates = new X509CertificateCollection();
  95. if (options.TlsOptions.Certificates == null)
  96. {
  97. return certificates;
  98. }
  99. foreach (var certificate in options.TlsOptions.Certificates)
  100. {
  101. certificates.Add(new X509Certificate(certificate));
  102. }
  103. return certificates;
  104. }
  105. }
  106. }