Fix breaking changes.

This commit is contained in:
Christian Kratky
2017-06-03 22:48:22 +02:00
parent 1317fa3e2a
commit 929cdb971a
92 changed files with 1363 additions and 1309 deletions
@@ -0,0 +1,90 @@
using System;
using System.Security.Cryptography.X509Certificates;
using MQTTnet.Core.Adapter;
using MQTTnet.Core.Diagnostics;
using MQTTnet.Core.Serializer;
using MQTTnet.Core.Server;
using Windows.Networking.Sockets;
namespace MQTTnet.Implementations
{
public class MqttServerAdapter : IMqttServerAdapter, IDisposable
{
private StreamSocketListener _defaultEndpointSocket;
private StreamSocketListener _sslEndpointSocket;
private X509Certificate2 _sslCertificate;
private bool _isRunning;
public event EventHandler<MqttClientConnectedEventArgs> ClientConnected;
public void Start(MqttServerOptions options)
{
if (_isRunning) throw new InvalidOperationException("Server is already started.");
_isRunning = true;
if (options.DefaultEndpointOptions.IsEnabled)
{
_defaultEndpointSocket = new StreamSocketListener();
_defaultEndpointSocket.BindServiceNameAsync(options.GetDefaultEndpointPort().ToString(), SocketProtectionLevel.PlainSocket).GetAwaiter().GetResult();
_defaultEndpointSocket.ConnectionReceived += AcceptDefaultEndpointConnectionsAsync;
}
if (options.SslEndpointOptions.IsEnabled)
{
if (options.SslEndpointOptions.Certificate == null)
{
throw new ArgumentException("SSL certificate is not set.");
}
_sslCertificate = new X509Certificate2(options.SslEndpointOptions.Certificate);
_sslEndpointSocket = new StreamSocketListener();
_sslEndpointSocket.BindServiceNameAsync(options.GetSslEndpointPort().ToString(), SocketProtectionLevel.Tls12).GetAwaiter().GetResult();
_sslEndpointSocket.ConnectionReceived += AcceptSslEndpointConnectionsAsync;
}
}
public void Stop()
{
_isRunning = false;
_defaultEndpointSocket?.Dispose();
_defaultEndpointSocket = null;
_sslEndpointSocket?.Dispose();
_sslEndpointSocket = null;
}
public void Dispose()
{
Stop();
}
private void AcceptDefaultEndpointConnectionsAsync(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
{
try
{
var clientAdapter = new MqttChannelCommunicationAdapter(new MqttTcpChannel(args.Socket), new DefaultMqttV311PacketSerializer());
ClientConnected?.Invoke(this, new MqttClientConnectedEventArgs(args.Socket.Information.RemoteAddress.ToString(), clientAdapter));
}
catch (Exception exception)
{
MqttTrace.Error(nameof(MqttServerAdapter), exception, "Error while acceping connection at default endpoint.");
}
}
private void AcceptSslEndpointConnectionsAsync(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
{
try
{
var clientAdapter = new MqttChannelCommunicationAdapter(new MqttTcpChannel(args.Socket), new DefaultMqttV311PacketSerializer());
ClientConnected?.Invoke(this, new MqttClientConnectedEventArgs(args.Socket.Information.RemoteAddress.ToString(), clientAdapter));
}
catch (Exception exception)
{
MqttTrace.Error(nameof(MqttServerAdapter), exception, "Error while acceping connection at SSL endpoint.");
}
}
}
}
@@ -0,0 +1,121 @@
using System;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Networking;
using Windows.Networking.Sockets;
using Windows.Security.Cryptography.Certificates;
using Windows.Storage.Streams;
using MQTTnet.Core.Channel;
using MQTTnet.Core.Client;
using MQTTnet.Core.Exceptions;
namespace MQTTnet.Implementations
{
public class MqttTcpChannel : IMqttCommunicationChannel, IDisposable
{
private readonly StreamSocket _socket;
public MqttTcpChannel()
{
_socket = new StreamSocket();
}
public MqttTcpChannel(StreamSocket socket)
{
_socket = socket ?? throw new ArgumentNullException(nameof(socket));
}
public async Task ConnectAsync(MqttClientOptions options)
{
if (options == null) throw new ArgumentNullException(nameof(options));
try
{
if (!options.SslOptions.UseSsl)
{
await _socket.ConnectAsync(new HostName(options.Server), options.GetPort().ToString());
}
else
{
_socket.Control.ClientCertificate = LoadCertificate(options);
if (!options.SslOptions.CheckCertificateRevocation)
{
_socket.Control.IgnorableServerCertificateErrors.Add(ChainValidationResult.Revoked);
_socket.Control.IgnorableServerCertificateErrors.Add(ChainValidationResult.IncompleteChain);
_socket.Control.IgnorableServerCertificateErrors.Add(ChainValidationResult.RevocationInformationMissing);
}
await _socket.ConnectAsync(new HostName(options.Server), options.GetPort().ToString(), SocketProtectionLevel.Tls12);
}
}
catch (SocketException exception)
{
throw new MqttCommunicationException(exception);
}
}
public Task DisconnectAsync()
{
try
{
_socket.Dispose();
return Task.FromResult(0);
}
catch (SocketException exception)
{
throw new MqttCommunicationException(exception);
}
}
public async Task WriteAsync(byte[] buffer)
{
if (buffer == null) throw new ArgumentNullException(nameof(buffer));
try
{
await _socket.OutputStream.WriteAsync(buffer.AsBuffer());
await _socket.OutputStream.FlushAsync();
}
catch (SocketException exception)
{
throw new MqttCommunicationException(exception);
}
}
public async Task ReadAsync(byte[] buffer)
{
if (buffer == null) throw new ArgumentNullException(nameof(buffer));
try
{
await _socket.InputStream.ReadAsync(buffer.AsBuffer(), (uint)buffer.Length, InputStreamOptions.Partial);
}
catch (SocketException exception)
{
throw new MqttCommunicationException(exception);
}
}
public void Dispose()
{
_socket?.Dispose();
}
private static Certificate LoadCertificate(MqttClientOptions options)
{
if (options.SslOptions.Certificates == null || !options.SslOptions.Certificates.Any())
{
return null;
}
if (options.SslOptions.Certificates.Count > 1)
{
throw new NotSupportedException("Only one client certificate is supported for UWP.");
}
return new Certificate(options.SslOptions.Certificates.First().AsBuffer());
}
}
}
@@ -112,9 +112,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="MqttClientFactory.cs" />
<Compile Include="MqttServerAdapter.cs" />
<Compile Include="Implementations\MqttServerAdapter.cs" />
<Compile Include="MqttServerFactory.cs" />
<Compile Include="MqttTcpChannel.cs" />
<Compile Include="Implementations\MqttTcpChannel.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Properties\MQTTnet.Universal.rd.xml" />
</ItemGroup>
@@ -2,6 +2,7 @@
using MQTTnet.Core.Adapter;
using MQTTnet.Core.Client;
using MQTTnet.Core.Serializer;
using MQTTnet.Implementations;
namespace MQTTnet
{
@@ -14,4 +15,4 @@ namespace MQTTnet
return new MqttClient(options, new MqttChannelCommunicationAdapter(new MqttTcpChannel(), new DefaultMqttV311PacketSerializer()));
}
}
}
}
@@ -1,54 +0,0 @@
using System;
using System.Threading;
using Windows.Networking.Sockets;
using MQTTnet.Core.Adapter;
using MQTTnet.Core.Serializer;
using MQTTnet.Core.Server;
namespace MQTTnet
{
public sealed class MqttServerAdapter : IMqttServerAdapter, IDisposable
{
private CancellationTokenSource _cancellationTokenSource;
private StreamSocketListener _socket;
public event EventHandler<MqttClientConnectedEventArgs> ClientConnected;
public void Start(MqttServerOptions options)
{
if (_socket != null) throw new InvalidOperationException("Server is already started.");
_cancellationTokenSource = new CancellationTokenSource();
_socket = new StreamSocketListener();
_socket.BindServiceNameAsync(options.Port.ToString()).AsTask().Wait();
_socket.ConnectionReceived += ConnectionReceived;
}
private void ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
{
var clientAdapter = new MqttChannelCommunicationAdapter(new MqttTcpChannel(args.Socket), new DefaultMqttV311PacketSerializer());
var identifier = $"{args.Socket.Information.RemoteAddress}:{args.Socket.Information.RemotePort}";
ClientConnected?.Invoke(this, new MqttClientConnectedEventArgs(identifier, clientAdapter));
}
public void Stop()
{
_cancellationTokenSource?.Dispose();
if (_socket != null)
{
_socket.ConnectionReceived -= ConnectionReceived;
}
_socket?.Dispose();
_socket = null;
}
public void Dispose()
{
Stop();
}
}
}
@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using MQTTnet.Core.Adapter;
using MQTTnet.Core.Server;
using MQTTnet.Implementations;
namespace MQTTnet
{
@@ -9,7 +12,7 @@ namespace MQTTnet
{
if (options == null) throw new ArgumentNullException(nameof(options));
return new MqttServer(options, new MqttServerAdapter());
return new MqttServer(options, new List<IMqttServerAdapter> { new MqttServerAdapter() });
}
}
}
@@ -1,62 +0,0 @@
using System;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Networking;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
using MQTTnet.Core.Channel;
using MQTTnet.Core.Client;
using Buffer = Windows.Storage.Streams.Buffer;
namespace MQTTnet
{
public sealed class MqttTcpChannel : IMqttCommunicationChannel, IDisposable
{
private readonly StreamSocket _socket;
public MqttTcpChannel()
{
_socket = new StreamSocket();
}
public MqttTcpChannel(StreamSocket socket)
{
_socket = socket ?? throw new ArgumentNullException(nameof(socket));
}
public async Task ConnectAsync(MqttClientOptions options)
{
if (options == null) throw new ArgumentNullException(nameof(options));
await _socket.ConnectAsync(new HostName(options.Server), options.Port.ToString());
}
public async Task DisconnectAsync()
{
await _socket.CancelIOAsync();
_socket.Dispose();
}
public async Task WriteAsync(byte[] buffer)
{
if (buffer == null) throw new ArgumentNullException(nameof(buffer));
await _socket.OutputStream.WriteAsync(buffer.AsBuffer());
await _socket.OutputStream.FlushAsync();
}
public async Task ReadAsync(byte[] buffer)
{
var buffer2 = new Buffer((uint)buffer.Length);
await _socket.InputStream.ReadAsync(buffer2, (uint)buffer.Length, InputStreamOptions.None);
var array2 = buffer2.ToArray();
Array.Copy(array2, buffer, array2.Length);
}
public void Dispose()
{
_socket?.Dispose();
}
}
}