mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-16 09:13:02 +08:00
50 lines
1.5 KiB
C#
50 lines
1.5 KiB
C#
using MQTTnet.Adapter;
|
|
using MQTTnet.Client.Options;
|
|
using MQTTnet.Diagnostics;
|
|
using MQTTnet.Formatter;
|
|
using System;
|
|
using MQTTnet.Channel;
|
|
using MQTTnet.Diagnostics.Logger;
|
|
|
|
namespace MQTTnet.Implementations
|
|
{
|
|
public class MqttClientAdapterFactory : IMqttClientAdapterFactory
|
|
{
|
|
readonly IMqttNetLogger _logger;
|
|
|
|
public MqttClientAdapterFactory(IMqttNetLogger logger)
|
|
{
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
}
|
|
|
|
public IMqttChannelAdapter CreateClientAdapter(IMqttClientOptions options)
|
|
{
|
|
if (options == null) throw new ArgumentNullException(nameof(options));
|
|
|
|
IMqttChannel channel;
|
|
switch (options.ChannelOptions)
|
|
{
|
|
case MqttClientTcpOptions _:
|
|
{
|
|
channel = new MqttTcpChannel(options);
|
|
break;
|
|
}
|
|
|
|
case MqttClientWebSocketOptions webSocketOptions:
|
|
{
|
|
channel = new MqttWebSocketChannel(webSocketOptions);
|
|
break;
|
|
}
|
|
|
|
default:
|
|
{
|
|
throw new NotSupportedException();
|
|
}
|
|
}
|
|
|
|
var packetFormatterAdapter = new MqttPacketFormatterAdapter(options.ProtocolVersion, new MqttPacketWriter());
|
|
return new MqttChannelAdapter(channel, packetFormatterAdapter, options.PacketInspector, _logger);
|
|
}
|
|
}
|
|
}
|