25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

126 lines
5.1 KiB

  1. using System;
  2. using System.Net;
  3. using System.Net.Security;
  4. using System.Net.Sockets;
  5. using System.Security.Authentication;
  6. using System.Security.Cryptography.X509Certificates;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using MQTTnet.Core.Adapter;
  10. using MQTTnet.Core.Diagnostics;
  11. using MQTTnet.Core.Serializer;
  12. using MQTTnet.Core.Server;
  13. namespace MQTTnet.Implementations
  14. {
  15. public class MqttServerAdapter : IMqttServerAdapter, IDisposable
  16. {
  17. private CancellationTokenSource _cancellationTokenSource;
  18. private Socket _defaultEndpointSocket;
  19. private Socket _tlsEndpointSocket;
  20. private X509Certificate2 _tlsCertificate;
  21. private bool _isRunning;
  22. public event EventHandler<MqttClientConnectedEventArgs> ClientConnected;
  23. public void Start(MqttServerOptions options)
  24. {
  25. if (options == null) throw new ArgumentNullException(nameof(options));
  26. if (_isRunning) throw new InvalidOperationException("Server is already started.");
  27. _isRunning = true;
  28. _cancellationTokenSource = new CancellationTokenSource();
  29. if (options.DefaultEndpointOptions.IsEnabled)
  30. {
  31. _defaultEndpointSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
  32. _defaultEndpointSocket.Bind(new IPEndPoint(IPAddress.Any, options.GetDefaultEndpointPort()));
  33. _defaultEndpointSocket.Listen(options.ConnectionBacklog);
  34. Task.Run(() => AcceptDefaultEndpointConnectionsAsync(_cancellationTokenSource.Token), _cancellationTokenSource.Token);
  35. }
  36. if (options.TlsEndpointOptions.IsEnabled)
  37. {
  38. if (options.TlsEndpointOptions.Certificate == null)
  39. {
  40. throw new ArgumentException("TLS certificate is not set.");
  41. }
  42. _tlsCertificate = new X509Certificate2(options.TlsEndpointOptions.Certificate);
  43. _tlsEndpointSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
  44. _tlsEndpointSocket.Bind(new IPEndPoint(IPAddress.Any, options.GetTlsEndpointPort()));
  45. _tlsEndpointSocket.Listen(options.ConnectionBacklog);
  46. Task.Run(() => AcceptTlsEndpointConnectionsAsync(_cancellationTokenSource.Token), _cancellationTokenSource.Token);
  47. }
  48. }
  49. public void Stop()
  50. {
  51. _isRunning = false;
  52. _cancellationTokenSource?.Cancel(false);
  53. _cancellationTokenSource?.Dispose();
  54. _cancellationTokenSource = null;
  55. _defaultEndpointSocket?.Dispose();
  56. _defaultEndpointSocket = null;
  57. _tlsEndpointSocket?.Dispose();
  58. _tlsEndpointSocket = null;
  59. }
  60. public void Dispose()
  61. {
  62. Stop();
  63. }
  64. private async Task AcceptDefaultEndpointConnectionsAsync(CancellationToken cancellationToken)
  65. {
  66. while (!cancellationToken.IsCancellationRequested)
  67. {
  68. try
  69. {
  70. var clientSocket = await Task.Factory.FromAsync(_defaultEndpointSocket.BeginAccept, _defaultEndpointSocket.EndAccept, null);
  71. var clientAdapter = new MqttChannelCommunicationAdapter(new MqttTcpChannel(clientSocket, null), new MqttPacketSerializer());
  72. ClientConnected?.Invoke(this, new MqttClientConnectedEventArgs(clientSocket.RemoteEndPoint.ToString(), clientAdapter));
  73. }
  74. catch (Exception exception) when (!(exception is ObjectDisposedException))
  75. {
  76. MqttTrace.Error(nameof(MqttServerAdapter), exception, "Error while accepting connection at default endpoint.");
  77. //excessive CPU consumed if in endless loop of socket errors
  78. Thread.Sleep(TimeSpan.FromSeconds(1));
  79. }
  80. }
  81. }
  82. private async Task AcceptTlsEndpointConnectionsAsync(CancellationToken cancellationToken)
  83. {
  84. while (!cancellationToken.IsCancellationRequested)
  85. {
  86. try
  87. {
  88. var clientSocket = await Task.Factory.FromAsync(_tlsEndpointSocket.BeginAccept, _tlsEndpointSocket.EndAccept, null);
  89. var sslStream = new SslStream(new NetworkStream(clientSocket));
  90. await sslStream.AuthenticateAsServerAsync(_tlsCertificate, false, SslProtocols.Tls12, false);
  91. var clientAdapter = new MqttChannelCommunicationAdapter(new MqttTcpChannel(clientSocket, sslStream), new MqttPacketSerializer());
  92. ClientConnected?.Invoke(this, new MqttClientConnectedEventArgs(clientSocket.RemoteEndPoint.ToString(), clientAdapter));
  93. }
  94. catch (Exception exception)
  95. {
  96. MqttTrace.Error(nameof(MqttServerAdapter), exception, "Error while accepting connection at TLS endpoint.");
  97. //excessive CPU consumed if in endless loop of socket errors
  98. Thread.Sleep(TimeSpan.FromSeconds(1));
  99. }
  100. }
  101. }
  102. }
  103. }