|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- #if WINDOWS_UWP
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices.WindowsRuntime;
- using System.Threading;
- using System.Threading.Tasks;
- using Windows.Networking;
- using Windows.Networking.Sockets;
- using Windows.Security.Cryptography.Certificates;
- using MQTTnet.Channel;
- using MQTTnet.Client;
-
- namespace MQTTnet.Implementations
- {
- public sealed class MqttTcpChannel : IMqttChannel
- {
- // ReSharper disable once MemberCanBePrivate.Global
- // ReSharper disable once AutoPropertyCanBeMadeGetOnly.Global
- public static int BufferSize { get; set; } = 4096; // Can be changed for fine tuning by library user.
-
- private readonly int _bufferSize = BufferSize;
- private readonly MqttClientTcpOptions _options;
-
- private StreamSocket _socket;
- private Stream _readStream;
- private Stream _writeStream;
-
- public MqttTcpChannel(MqttClientTcpOptions options)
- {
- _options = options ?? throw new ArgumentNullException(nameof(options));
-
- _bufferSize = options.BufferSize;
- }
-
- public MqttTcpChannel(StreamSocket socket)
- {
- _socket = socket ?? throw new ArgumentNullException(nameof(socket));
-
- CreateStreams();
- }
-
- public static Func<MqttClientTcpOptions, IEnumerable<ChainValidationResult>> CustomIgnorableServerCertificateErrorsResolver { get; set; }
-
- public async Task ConnectAsync(CancellationToken cancellationToken)
- {
- if (_socket == null)
- {
- _socket = new StreamSocket();
- _socket.Control.NoDelay = true;
- _socket.Control.KeepAlive = true;
- }
-
- if (!_options.TlsOptions.UseTls)
- {
- await _socket.ConnectAsync(new HostName(_options.Server), _options.GetPort().ToString());
- }
- else
- {
- _socket.Control.ClientCertificate = LoadCertificate(_options);
-
- foreach (var ignorableChainValidationResult in ResolveIgnorableServerCertificateErrors())
- {
- _socket.Control.IgnorableServerCertificateErrors.Add(ignorableChainValidationResult);
- }
-
- await _socket.ConnectAsync(new HostName(_options.Server), _options.GetPort().ToString(), SocketProtectionLevel.Tls12);
- }
-
- CreateStreams();
- }
-
- public Task DisconnectAsync()
- {
- Dispose();
- return Task.FromResult(0);
- }
-
- public Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
- {
- return _readStream.ReadAsync(buffer, offset, count, cancellationToken);
- }
-
- public async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
- {
- await _writeStream.WriteAsync(buffer, offset, count, cancellationToken);
- await _writeStream.FlushAsync(cancellationToken);
- }
-
- public void Dispose()
- {
- try
- {
- _readStream?.Dispose();
- }
- catch (ObjectDisposedException)
- {
- }
- catch (NullReferenceException)
- {
- }
- finally
- {
- _readStream = null;
- }
-
- try
- {
- _writeStream?.Dispose();
- }
- catch (ObjectDisposedException)
- {
- }
- catch (NullReferenceException)
- {
- }
- finally
- {
- _writeStream = null;
- }
-
- try
- {
- _socket?.Dispose();
- }
- catch (ObjectDisposedException)
- {
- }
- catch (NullReferenceException)
- {
- }
- finally
- {
- _socket = null;
- }
- }
-
- private static Certificate LoadCertificate(MqttClientTcpOptions options)
- {
- if (options.TlsOptions.Certificates == null || !options.TlsOptions.Certificates.Any())
- {
- return null;
- }
-
- if (options.TlsOptions.Certificates.Count > 1)
- {
- throw new NotSupportedException("Only one client certificate is supported for UWP.");
- }
-
- return new Certificate(options.TlsOptions.Certificates.First().AsBuffer());
- }
-
- private IEnumerable<ChainValidationResult> ResolveIgnorableServerCertificateErrors()
- {
- if (CustomIgnorableServerCertificateErrorsResolver != null)
- {
- return CustomIgnorableServerCertificateErrorsResolver(_options);
- }
-
- var result = new List<ChainValidationResult>();
-
- if (_options.TlsOptions.IgnoreCertificateRevocationErrors)
- {
- result.Add(ChainValidationResult.RevocationInformationMissing);
- //_socket.Control.IgnorableServerCertificateErrors.Add(ChainValidationResult.Revoked); Not supported.
- result.Add(ChainValidationResult.RevocationFailure);
- }
-
- if (_options.TlsOptions.IgnoreCertificateChainErrors)
- {
- result.Add(ChainValidationResult.IncompleteChain);
- }
-
- if (_options.TlsOptions.AllowUntrustedCertificates)
- {
- result.Add(ChainValidationResult.Untrusted);
- }
-
- return result;
- }
-
- private void CreateStreams()
- {
- _readStream = _socket.InputStream.AsStreamForRead(_bufferSize);
- _writeStream = _socket.OutputStream.AsStreamForWrite(_bufferSize);
- }
- }
- }
- #endif
|