mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-25 07:41:32 +08:00
Add server implementation
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MQTTnet.Core.Serializer;
|
||||
|
||||
namespace MQTTnet.Core.Tests
|
||||
{
|
||||
[TestClass]
|
||||
public class ByteReaderTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void ByteReader_ReadToEnd()
|
||||
{
|
||||
var reader = new ByteReader(85);
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.IsFalse(reader.Read());
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.IsFalse(reader.Read());
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.IsFalse(reader.Read());
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.IsFalse(reader.Read());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ByteReader_ReadPartial()
|
||||
{
|
||||
var reader = new ByteReader(15);
|
||||
Assert.AreEqual(3, reader.Read(2));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MQTTnet.Core.Serializer;
|
||||
|
||||
namespace MQTTnet.Core.Tests
|
||||
{
|
||||
[TestClass]
|
||||
public class ByteWriterTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void ByteWriter_WriteMultipleAll()
|
||||
{
|
||||
var b = new ByteWriter();
|
||||
Assert.AreEqual(0, b.Value);
|
||||
b.Write(3, 2);
|
||||
Assert.AreEqual(3, b.Value);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ByteWriter_WriteMultiplePartial()
|
||||
{
|
||||
var b = new ByteWriter();
|
||||
Assert.AreEqual(0, b.Value);
|
||||
b.Write(255, 2);
|
||||
Assert.AreEqual(3, b.Value);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ByteWriter_WriteTo0xFF()
|
||||
{
|
||||
var b = new ByteWriter();
|
||||
|
||||
Assert.AreEqual(0, b.Value);
|
||||
b.Write(true);
|
||||
Assert.AreEqual(1, b.Value);
|
||||
b.Write(true);
|
||||
Assert.AreEqual(3, b.Value);
|
||||
b.Write(true);
|
||||
Assert.AreEqual(7, b.Value);
|
||||
b.Write(true);
|
||||
Assert.AreEqual(15, b.Value);
|
||||
b.Write(true);
|
||||
Assert.AreEqual(31, b.Value);
|
||||
b.Write(true);
|
||||
Assert.AreEqual(63, b.Value);
|
||||
b.Write(true);
|
||||
Assert.AreEqual(127, b.Value);
|
||||
b.Write(true);
|
||||
Assert.AreEqual(255, b.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,434 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MQTTnet.Core.Channel;
|
||||
using MQTTnet.Core.Client;
|
||||
using MQTTnet.Core.Packets;
|
||||
using MQTTnet.Core.Protocol;
|
||||
using MQTTnet.Core.Serializer;
|
||||
|
||||
namespace MQTTnet.Core.Tests
|
||||
{
|
||||
[TestClass]
|
||||
public class DefaultMqttV311PacketSerializerTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void SerializeV311_MqttConnectPacket()
|
||||
{
|
||||
var p = new MqttConnectPacket
|
||||
{
|
||||
ClientId = "XYZ",
|
||||
Password = "PASS",
|
||||
Username = "USER",
|
||||
KeepAlivePeriod = 123,
|
||||
CleanSession = true
|
||||
};
|
||||
|
||||
SerializeAndCompare(p, "EBsABE1RVFQEwgB7AANYWVoABFVTRVIABFBBU1M=");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SerializeV311_MqttConnectPacketWithWillMessage()
|
||||
{
|
||||
var p = new MqttConnectPacket
|
||||
{
|
||||
ClientId = "XYZ",
|
||||
Password = "PASS",
|
||||
Username = "USER",
|
||||
KeepAlivePeriod = 123,
|
||||
CleanSession = true,
|
||||
WillMessage = new MqttApplicationMessage(
|
||||
"My/last/will",
|
||||
Encoding.UTF8.GetBytes("Good byte."),
|
||||
MqttQualityOfServiceLevel.AtLeastOnce,
|
||||
true)
|
||||
};
|
||||
|
||||
SerializeAndCompare(p, "EDUABE1RVFQE7gB7AANYWVoADE15L2xhc3Qvd2lsbAAKR29vZCBieXRlLgAEVVNFUgAEUEFTUw==");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DeserializeV311_MqttConnectPacket()
|
||||
{
|
||||
var p = new MqttConnectPacket
|
||||
{
|
||||
ClientId = "XYZ",
|
||||
Password = "PASS",
|
||||
Username = "USER",
|
||||
KeepAlivePeriod = 123,
|
||||
CleanSession = true
|
||||
};
|
||||
|
||||
DeserializeAndCompare(p, "EBsABE1RVFQEwgB7AANYWVoABFVTRVIABFBBU1M=");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DeserializeV311_MqttConnectPacketWithWillMessage()
|
||||
{
|
||||
var p = new MqttConnectPacket
|
||||
{
|
||||
ClientId = "XYZ",
|
||||
Password = "PASS",
|
||||
Username = "USER",
|
||||
KeepAlivePeriod = 123,
|
||||
CleanSession = true,
|
||||
WillMessage = new MqttApplicationMessage(
|
||||
"My/last/will",
|
||||
Encoding.UTF8.GetBytes("Good byte."),
|
||||
MqttQualityOfServiceLevel.AtLeastOnce,
|
||||
true)
|
||||
};
|
||||
|
||||
DeserializeAndCompare(p, "EDUABE1RVFQE7gB7AANYWVoADE15L2xhc3Qvd2lsbAAKR29vZCBieXRlLgAEVVNFUgAEUEFTUw==");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SerializeV311_MqttConnAckPacket()
|
||||
{
|
||||
var p = new MqttConnAckPacket
|
||||
{
|
||||
IsSessionPresent = true,
|
||||
ConnectReturnCode = MqttConnectReturnCode.ConnectionRefusedNotAuthorized
|
||||
};
|
||||
|
||||
SerializeAndCompare(p, "IAIBBQ==");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DeserializeV311_MqttConnAckPacket()
|
||||
{
|
||||
var p = new MqttConnAckPacket
|
||||
{
|
||||
IsSessionPresent = true,
|
||||
ConnectReturnCode = MqttConnectReturnCode.ConnectionRefusedNotAuthorized
|
||||
};
|
||||
|
||||
DeserializeAndCompare(p, "IAIBBQ==");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SerializeV311_MqttDisconnectPacket()
|
||||
{
|
||||
SerializeAndCompare(new MqttDisconnectPacket(), "4AA=");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SerializeV311_MqttPingReqPacket()
|
||||
{
|
||||
SerializeAndCompare(new MqttPingReqPacket(), "wAA=");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SerializeV311_MqttPingRespPacket()
|
||||
{
|
||||
SerializeAndCompare(new MqttPingRespPacket(), "0AA=");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SerializeV311_MqttPublishPacket()
|
||||
{
|
||||
var p = new MqttPublishPacket
|
||||
{
|
||||
PacketIdentifier = 123,
|
||||
Dup = true,
|
||||
Retain = true,
|
||||
Payload = Encoding.ASCII.GetBytes("HELLO"),
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce,
|
||||
Topic = "A/B/C"
|
||||
};
|
||||
|
||||
SerializeAndCompare(p, "Ow4ABUEvQi9DAHtIRUxMTw==");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DeserializeV311_MqttPublishPacket()
|
||||
{
|
||||
var p = new MqttPublishPacket
|
||||
{
|
||||
PacketIdentifier = 123,
|
||||
Dup = true,
|
||||
Retain = true,
|
||||
Payload = Encoding.ASCII.GetBytes("HELLO"),
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce,
|
||||
Topic = "A/B/C"
|
||||
};
|
||||
|
||||
DeserializeAndCompare(p, "Ow4ABUEvQi9DAHtIRUxMTw==");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SerializeV311_MqttPubAckPacket()
|
||||
{
|
||||
var p = new MqttPubAckPacket
|
||||
{
|
||||
PacketIdentifier = 123
|
||||
};
|
||||
|
||||
SerializeAndCompare(p, "QAIAew==");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DeserializeV311_MqttPubAckPacket()
|
||||
{
|
||||
var p = new MqttPubAckPacket
|
||||
{
|
||||
PacketIdentifier = 123
|
||||
};
|
||||
|
||||
DeserializeAndCompare(p, "QAIAew==");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SerializeV311_MqttPubRecPacket()
|
||||
{
|
||||
var p = new MqttPubRecPacket
|
||||
{
|
||||
PacketIdentifier = 123
|
||||
};
|
||||
|
||||
SerializeAndCompare(p, "UAIAew==");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DeserializeV311_MqttPubRecPacket()
|
||||
{
|
||||
var p = new MqttPubRecPacket
|
||||
{
|
||||
PacketIdentifier = 123
|
||||
};
|
||||
|
||||
DeserializeAndCompare(p, "UAIAew==");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SerializeV311_MqttPubRelPacket()
|
||||
{
|
||||
var p = new MqttPubRelPacket
|
||||
{
|
||||
PacketIdentifier = 123
|
||||
};
|
||||
|
||||
SerializeAndCompare(p, "YgIAew==");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DeserializeV311_MqttPubRelPacket()
|
||||
{
|
||||
var p = new MqttPubRelPacket
|
||||
{
|
||||
PacketIdentifier = 123
|
||||
};
|
||||
|
||||
DeserializeAndCompare(p, "YgIAew==");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SerializeV311_MqttPubCompPacket()
|
||||
{
|
||||
var p = new MqttPubCompPacket
|
||||
{
|
||||
PacketIdentifier = 123
|
||||
};
|
||||
|
||||
SerializeAndCompare(p, "cAIAew==");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DeserializeV311_MqttPubCompPacket()
|
||||
{
|
||||
var p = new MqttPubCompPacket
|
||||
{
|
||||
PacketIdentifier = 123
|
||||
};
|
||||
|
||||
DeserializeAndCompare(p, "cAIAew==");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SerializeV311_MqttSubscribePacket()
|
||||
{
|
||||
var p = new MqttSubscribePacket
|
||||
{
|
||||
PacketIdentifier = 123
|
||||
};
|
||||
|
||||
p.TopicFilters.Add(new TopicFilter("A/B/C", MqttQualityOfServiceLevel.ExactlyOnce));
|
||||
p.TopicFilters.Add(new TopicFilter("1/2/3", MqttQualityOfServiceLevel.AtLeastOnce));
|
||||
p.TopicFilters.Add(new TopicFilter("x/y/z", MqttQualityOfServiceLevel.AtMostOnce));
|
||||
|
||||
SerializeAndCompare(p, "ghoAewAFQS9CL0MCAAUxLzIvMwEABXgveS96AA==");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DeserializeV311_MqttSubscribePacket()
|
||||
{
|
||||
var p = new MqttSubscribePacket
|
||||
{
|
||||
PacketIdentifier = 123
|
||||
};
|
||||
|
||||
p.TopicFilters.Add(new TopicFilter("A/B/C", MqttQualityOfServiceLevel.ExactlyOnce));
|
||||
p.TopicFilters.Add(new TopicFilter("1/2/3", MqttQualityOfServiceLevel.AtLeastOnce));
|
||||
p.TopicFilters.Add(new TopicFilter("x/y/z", MqttQualityOfServiceLevel.AtMostOnce));
|
||||
|
||||
DeserializeAndCompare(p, "ghoAewAFQS9CL0MCAAUxLzIvMwEABXgveS96AA==");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SerializeV311_MqttSubAckPacket()
|
||||
{
|
||||
var p = new MqttSubAckPacket
|
||||
{
|
||||
PacketIdentifier = 123
|
||||
};
|
||||
|
||||
p.SubscribeReturnCodes.Add(MqttSubscribeReturnCode.SuccessMaximumQoS0);
|
||||
p.SubscribeReturnCodes.Add(MqttSubscribeReturnCode.SuccessMaximumQoS1);
|
||||
p.SubscribeReturnCodes.Add(MqttSubscribeReturnCode.SuccessMaximumQoS2);
|
||||
p.SubscribeReturnCodes.Add(MqttSubscribeReturnCode.Failure);
|
||||
|
||||
SerializeAndCompare(p, "kAYAewABAoA=");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DeserializeV311_MqttSubAckPacket()
|
||||
{
|
||||
var p = new MqttSubAckPacket
|
||||
{
|
||||
PacketIdentifier = 123
|
||||
};
|
||||
|
||||
p.SubscribeReturnCodes.Add(MqttSubscribeReturnCode.SuccessMaximumQoS0);
|
||||
p.SubscribeReturnCodes.Add(MqttSubscribeReturnCode.SuccessMaximumQoS1);
|
||||
p.SubscribeReturnCodes.Add(MqttSubscribeReturnCode.SuccessMaximumQoS2);
|
||||
p.SubscribeReturnCodes.Add(MqttSubscribeReturnCode.Failure);
|
||||
|
||||
DeserializeAndCompare(p, "kAYAewABAoA=");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SerializeV311_MqttUnsubscribePacket()
|
||||
{
|
||||
var p = new MqttUnsubscribePacket
|
||||
{
|
||||
PacketIdentifier = 123
|
||||
};
|
||||
|
||||
p.TopicFilters.Add("A/B/C");
|
||||
p.TopicFilters.Add("1/2/3");
|
||||
p.TopicFilters.Add("x/y/z");
|
||||
|
||||
SerializeAndCompare(p, "ohcAewAFQS9CL0MABTEvMi8zAAV4L3kveg==");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DeserializeV311_MqttUnsubscribePacket()
|
||||
{
|
||||
var p = new MqttUnsubscribePacket
|
||||
{
|
||||
PacketIdentifier = 123
|
||||
};
|
||||
|
||||
p.TopicFilters.Add("A/B/C");
|
||||
p.TopicFilters.Add("1/2/3");
|
||||
p.TopicFilters.Add("x/y/z");
|
||||
|
||||
DeserializeAndCompare(p, "ohcAewAFQS9CL0MABTEvMi8zAAV4L3kveg==");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SerializeV311_MqttUnsubAckPacket()
|
||||
{
|
||||
var p = new MqttUnsubAckPacket
|
||||
{
|
||||
PacketIdentifier = 123
|
||||
};
|
||||
|
||||
SerializeAndCompare(p, "sAIAew==");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DeserializeV311_MqttUnsubAckPacket()
|
||||
{
|
||||
var p = new MqttUnsubAckPacket
|
||||
{
|
||||
PacketIdentifier = 123
|
||||
};
|
||||
|
||||
DeserializeAndCompare(p, "sAIAew==");
|
||||
}
|
||||
|
||||
|
||||
public class TestChannel : IMqttCommunicationChannel
|
||||
{
|
||||
private readonly MemoryStream _stream = new MemoryStream();
|
||||
|
||||
public bool IsConnected { get; } = true;
|
||||
|
||||
public TestChannel()
|
||||
{
|
||||
}
|
||||
|
||||
public TestChannel(byte[] initialData)
|
||||
{
|
||||
_stream.Write(initialData, 0, initialData.Length);
|
||||
_stream.Position = 0;
|
||||
}
|
||||
|
||||
public async Task ConnectAsync(MqttClientOptions options)
|
||||
{
|
||||
await Task.FromResult(0);
|
||||
}
|
||||
|
||||
public async Task DisconnectAsync()
|
||||
{
|
||||
await Task.FromResult(0);
|
||||
}
|
||||
|
||||
public async Task WriteAsync(byte[] buffer)
|
||||
{
|
||||
await _stream.WriteAsync(buffer, 0, buffer.Length);
|
||||
}
|
||||
|
||||
public async Task ReadAsync(byte[] buffer)
|
||||
{
|
||||
await _stream.ReadAsync(buffer, 0, buffer.Length);
|
||||
}
|
||||
|
||||
public byte[] ToArray()
|
||||
{
|
||||
return _stream.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
private void SerializeAndCompare(MqttBasePacket packet, string expectedBase64Value)
|
||||
{
|
||||
var serializer = new DefaultMqttV311PacketSerializer();
|
||||
var channel = new TestChannel();
|
||||
serializer.SerializeAsync(packet, channel).Wait();
|
||||
var buffer = channel.ToArray();
|
||||
|
||||
Assert.AreEqual(expectedBase64Value, Convert.ToBase64String(buffer));
|
||||
}
|
||||
|
||||
private void DeserializeAndCompare(MqttBasePacket packet, string expectedBase64Value)
|
||||
{
|
||||
var serializer = new DefaultMqttV311PacketSerializer();
|
||||
|
||||
var channel1 = new TestChannel();
|
||||
serializer.SerializeAsync(packet, channel1).Wait();
|
||||
var buffer1 = channel1.ToArray();
|
||||
|
||||
var channel2 = new TestChannel(buffer1);
|
||||
var deserializedPacket = serializer.DeserializeAsync(channel2).Result;
|
||||
var buffer2 = channel2.ToArray();
|
||||
|
||||
var channel3 = new TestChannel(buffer2);
|
||||
serializer.SerializeAsync(deserializedPacket, channel3).Wait();
|
||||
|
||||
Assert.AreEqual(expectedBase64Value, Convert.ToBase64String(channel3.ToArray()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{A7FF0C91-25DE-4BA6-B39E-F54E8DADF1CC}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>MQTTnet.Core.Tests</RootNamespace>
|
||||
<AssemblyName>MQTTnet.Core.Tests</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="ByteReaderTests.cs" />
|
||||
<Compile Include="ByteWriterTests.cs" />
|
||||
<Compile Include="DefaultMqttV311PacketSerializerTests.cs" />
|
||||
<Compile Include="MqttServerTests.cs" />
|
||||
<Compile Include="MqttSubscriptionsManagerTests.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="TestMqttServerAdapter.cs" />
|
||||
<Compile Include="TopicFilterComparerTests.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\MQTTnet.Core\MQTTnet.Core.csproj">
|
||||
<Project>{99C884F3-B4B9-417D-AA92-DC7DD1C4CFEE}</Project>
|
||||
<Name>MQTTnet.Core</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
||||
@@ -0,0 +1,161 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MQTTnet.Core.Adapter;
|
||||
using MQTTnet.Core.Client;
|
||||
using MQTTnet.Core.Packets;
|
||||
using MQTTnet.Core.Protocol;
|
||||
using MQTTnet.Core.Server;
|
||||
|
||||
namespace MQTTnet.Core.Tests
|
||||
{
|
||||
[TestClass]
|
||||
public class MqttServerTests
|
||||
{
|
||||
[TestMethod]
|
||||
public async Task MqttServer_PublishSimple_AtMostOnce()
|
||||
{
|
||||
await TestPublishAsync(
|
||||
"A/B/C",
|
||||
MqttQualityOfServiceLevel.AtMostOnce,
|
||||
"A/B/C",
|
||||
MqttQualityOfServiceLevel.AtMostOnce,
|
||||
1);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task MqttServer_PublishSimple_AtLeastOnce()
|
||||
{
|
||||
await TestPublishAsync(
|
||||
"A/B/C",
|
||||
MqttQualityOfServiceLevel.AtLeastOnce,
|
||||
"A/B/C",
|
||||
MqttQualityOfServiceLevel.AtLeastOnce,
|
||||
1);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task MqttServer_PublishSimple_ExactlyOnce()
|
||||
{
|
||||
await TestPublishAsync(
|
||||
"A/B/C",
|
||||
MqttQualityOfServiceLevel.ExactlyOnce,
|
||||
"A/B/C",
|
||||
MqttQualityOfServiceLevel.ExactlyOnce,
|
||||
1);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task MqttServer_WillMessage()
|
||||
{
|
||||
var s = new MqttServer(new MqttServerOptions(), new TestMqttServerAdapter());
|
||||
s.Start();
|
||||
|
||||
var willMessage = new MqttApplicationMessage("My/last/will", new byte[0], MqttQualityOfServiceLevel.AtMostOnce, false);
|
||||
var c1 = ConnectTestClient("c1", null, s);
|
||||
var c2 = ConnectTestClient("c2", willMessage, s);
|
||||
|
||||
var receivedMessagesCount = 0;
|
||||
c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
|
||||
await c1.SubscribeAsync(new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce));
|
||||
|
||||
await c2.DisconnectAsync();
|
||||
|
||||
await Task.Delay(1000);
|
||||
|
||||
s.Stop();
|
||||
|
||||
Assert.AreEqual(1, receivedMessagesCount);
|
||||
}
|
||||
|
||||
private MqttClient ConnectTestClient(string clientId, MqttApplicationMessage willMessage, MqttServer server)
|
||||
{
|
||||
var adapterA = new TestMqttClientAdapter();
|
||||
var adapterB = new TestMqttClientAdapter();
|
||||
adapterA.Partner = adapterB;
|
||||
adapterB.Partner = adapterA;
|
||||
|
||||
var client = new MqttClient(new MqttClientOptions(), adapterA);
|
||||
server.InjectClient(clientId, adapterB);
|
||||
client.ConnectAsync(willMessage).Wait();
|
||||
return client;
|
||||
}
|
||||
|
||||
private async Task TestPublishAsync(
|
||||
string topic,
|
||||
MqttQualityOfServiceLevel qualityOfServiceLevel,
|
||||
string topicFilter,
|
||||
MqttQualityOfServiceLevel filterQualityOfServiceLevel,
|
||||
int expectedReceivedMessagesCount)
|
||||
{
|
||||
var s = new MqttServer(new MqttServerOptions(), new TestMqttServerAdapter());
|
||||
s.Start();
|
||||
|
||||
var c1 = ConnectTestClient("c1", null, s);
|
||||
var c2 = ConnectTestClient("c2", null, s);
|
||||
|
||||
var receivedMessagesCount = 0;
|
||||
c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
|
||||
|
||||
await c1.SubscribeAsync(new TopicFilter(topicFilter, filterQualityOfServiceLevel));
|
||||
await c2.PublishAsync(new MqttApplicationMessage(topic, new byte[0], qualityOfServiceLevel, false));
|
||||
|
||||
await Task.Delay(500);
|
||||
await c1.Unsubscribe(topicFilter);
|
||||
|
||||
await Task.Delay(500);
|
||||
|
||||
s.Stop();
|
||||
|
||||
Assert.AreEqual(expectedReceivedMessagesCount, receivedMessagesCount);
|
||||
}
|
||||
}
|
||||
|
||||
public class TestMqttClientAdapter : IMqttCommunicationAdapter
|
||||
{
|
||||
private readonly BlockingCollection<MqttBasePacket> _incomingPackets = new BlockingCollection<MqttBasePacket>();
|
||||
|
||||
public TestMqttClientAdapter Partner { get; set; }
|
||||
|
||||
public async Task ConnectAsync(MqttClientOptions options, TimeSpan timeout)
|
||||
{
|
||||
await Task.FromResult(0);
|
||||
}
|
||||
|
||||
public async Task DisconnectAsync()
|
||||
{
|
||||
await Task.FromResult(0);
|
||||
}
|
||||
|
||||
public async Task SendPacketAsync(MqttBasePacket packet, TimeSpan timeout)
|
||||
{
|
||||
ThrowIfPartnerIsNull();
|
||||
|
||||
Partner.SendPacketInternal(packet);
|
||||
await Task.FromResult(0);
|
||||
}
|
||||
|
||||
public async Task<MqttBasePacket> ReceivePacketAsync(TimeSpan timeout)
|
||||
{
|
||||
ThrowIfPartnerIsNull();
|
||||
|
||||
return await Task.Run(() => _incomingPackets.Take());
|
||||
}
|
||||
|
||||
private void SendPacketInternal(MqttBasePacket packet)
|
||||
{
|
||||
if (packet == null) throw new ArgumentNullException(nameof(packet));
|
||||
|
||||
_incomingPackets.Add(packet);
|
||||
}
|
||||
|
||||
private void ThrowIfPartnerIsNull()
|
||||
{
|
||||
if (Partner == null)
|
||||
{
|
||||
throw new InvalidOperationException("Partner is not set.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MQTTnet.Core.Packets;
|
||||
using MQTTnet.Core.Protocol;
|
||||
using MQTTnet.Core.Server;
|
||||
|
||||
namespace MQTTnet.Core.Tests
|
||||
{
|
||||
[TestClass]
|
||||
public class MqttSubscriptionsManagerTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void MqttSubscriptionsManager_SubscribeSingleSuccess()
|
||||
{
|
||||
var sm = new MqttClientSubscriptionsManager();
|
||||
|
||||
var sp = new MqttSubscribePacket();
|
||||
sp.TopicFilters.Add(new TopicFilter("A/B/C", MqttQualityOfServiceLevel.AtMostOnce));
|
||||
|
||||
sm.Subscribe(sp);
|
||||
|
||||
var pp = new MqttPublishPacket
|
||||
{
|
||||
Topic = "A/B/C",
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
|
||||
};
|
||||
|
||||
Assert.IsTrue(sm.IsTopicSubscribed(pp));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MqttSubscriptionsManager_SubscribeSingleNoSuccess()
|
||||
{
|
||||
var sm = new MqttClientSubscriptionsManager();
|
||||
|
||||
var sp = new MqttSubscribePacket();
|
||||
sp.TopicFilters.Add(new TopicFilter("A/B/C", MqttQualityOfServiceLevel.AtMostOnce));
|
||||
|
||||
sm.Subscribe(sp);
|
||||
|
||||
var pp = new MqttPublishPacket
|
||||
{
|
||||
Topic = "A/B/X",
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
|
||||
};
|
||||
|
||||
Assert.IsFalse(sm.IsTopicSubscribed(pp));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MqttSubscriptionsManager_SubscribeAndUnsubscribeSingle()
|
||||
{
|
||||
var sm = new MqttClientSubscriptionsManager();
|
||||
|
||||
var sp = new MqttSubscribePacket();
|
||||
sp.TopicFilters.Add(new TopicFilter("A/B/C", MqttQualityOfServiceLevel.AtMostOnce));
|
||||
|
||||
sm.Subscribe(sp);
|
||||
|
||||
var pp = new MqttPublishPacket
|
||||
{
|
||||
Topic = "A/B/C",
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
|
||||
};
|
||||
|
||||
Assert.IsTrue(sm.IsTopicSubscribed(pp));
|
||||
|
||||
var up = new MqttUnsubscribePacket();
|
||||
up.TopicFilters.Add("A/B/C");
|
||||
sm.Unsubscribe(up);
|
||||
|
||||
Assert.IsFalse(sm.IsTopicSubscribed(pp));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
[assembly: AssemblyTitle("MQTTnet.Core.Tests")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("Christian Kratky")]
|
||||
[assembly: AssemblyProduct("MQTTnet")]
|
||||
[assembly: AssemblyCopyright("Copyright © Christian Kratky 2016-2017")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
[assembly: ComVisible(false)]
|
||||
[assembly: Guid("a7ff0c91-25de-4ba6-b39e-f54e8dadf1cc")]
|
||||
[assembly: AssemblyVersion("2.1.0.4")]
|
||||
[assembly: AssemblyFileVersion("2.1.0.4")]
|
||||
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using MQTTnet.Core.Adapter;
|
||||
using MQTTnet.Core.Server;
|
||||
|
||||
namespace MQTTnet.Core.Tests
|
||||
{
|
||||
public class TestMqttServerAdapter : IMqttServerAdapter
|
||||
{
|
||||
public event EventHandler<MqttClientConnectedEventArgs> ClientConnected;
|
||||
|
||||
public void Start(MqttServerOptions options)
|
||||
{
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MQTTnet.Core.Server;
|
||||
|
||||
namespace MQTTnet.Core.Tests
|
||||
{
|
||||
[TestClass]
|
||||
public class TopicFilterComparerTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void TopicFilterComparer_DirectMatch()
|
||||
{
|
||||
CompareAndAssert("A/B/C", "A/B/C", true);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TopicFilterComparer_DirectNoMatch()
|
||||
{
|
||||
CompareAndAssert("A/B/X", "A/B/C", false);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TopicFilterComparer_MiddleOneLevelWildcardMatch()
|
||||
{
|
||||
CompareAndAssert("A/B/C", "A/+/C", true);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TopicFilterComparer_MiddleOneLevelWildcardNoMatch()
|
||||
{
|
||||
CompareAndAssert("A/B/C/D", "A/+/C", false);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TopicFilterComparer_BeginningOneLevelWildcardMatch()
|
||||
{
|
||||
CompareAndAssert("A/B/C", "+/B/C", true);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TopicFilterComparer_EndOneLevelWildcardMatch()
|
||||
{
|
||||
CompareAndAssert("A/B/C", "A/B/+", true);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TopicFilterComparer_EndMultipleLevelsWildcardMatch()
|
||||
{
|
||||
CompareAndAssert("A/B/C", "A/#", true);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TopicFilterComparer_EndMultipleLevelsWildcardNoMatch()
|
||||
{
|
||||
CompareAndAssert("A/B/C/D", "A/C/#", false);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TopicFilterComparer_AllLevelsWildcardMatch()
|
||||
{
|
||||
CompareAndAssert("A/B/C/D", "#", true);
|
||||
}
|
||||
|
||||
private void CompareAndAssert(string topic, string filter, bool expectedResult)
|
||||
{
|
||||
Assert.AreEqual(expectedResult, MqttTopicFilterComparer.IsMatch(topic, filter));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
|
||||
</startup>
|
||||
</configuration>
|
||||
@@ -0,0 +1,67 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{7B19B139-2E9D-4F1D-88B4-6180B4CF872A}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>MQTTnet.TestMqttClient</RootNamespace>
|
||||
<AssemblyName>MQTTnet.TestMqttClient</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\MQTTnet.Core\MQTTnet.Core.csproj">
|
||||
<Project>{99c884f3-b4b9-417d-aa92-dc7dd1c4cfee}</Project>
|
||||
<Name>MQTTnet.Core</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\..\Frameworks\MQTTnet.NetFramework\MQTTnet.NetFramework.csproj">
|
||||
<Project>{a480ef90-0eaa-4d9a-b271-47a9c47f6f7d}</Project>
|
||||
<Name>MQTTnet.NetFramework</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
||||
@@ -0,0 +1,108 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Core;
|
||||
using MQTTnet.Core.Client;
|
||||
using MQTTnet.Core.Diagnostics;
|
||||
using MQTTnet.Core.Packets;
|
||||
using MQTTnet.Core.Protocol;
|
||||
using MQTTnet.Core.Server;
|
||||
|
||||
namespace MQTTnet.TestMqttClient
|
||||
{
|
||||
public static class Program
|
||||
{
|
||||
public static void Main(string[] arguments)
|
||||
{
|
||||
Task.Run(() => Run(arguments)).Wait();
|
||||
}
|
||||
|
||||
private static async Task Run(string[] arguments)
|
||||
{
|
||||
MqttTrace.TraceMessagePublished += (s, e) =>
|
||||
{
|
||||
Console.WriteLine($">> [{e.ThreadId}] [{e.Source}] [{e.Level}]: {e.Message}");
|
||||
if (e.Exception != null)
|
||||
{
|
||||
Console.WriteLine(e.Exception);
|
||||
}
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var options = new MqttClientOptions
|
||||
{
|
||||
Server = "localhost"
|
||||
};
|
||||
|
||||
var client = new MqttClientFactory().CreateMqttClient(options);
|
||||
client.ApplicationMessageReceived += (s, e) =>
|
||||
{
|
||||
Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");
|
||||
Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");
|
||||
Console.WriteLine($"+ Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}");
|
||||
Console.WriteLine($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}");
|
||||
Console.WriteLine($"+ Retain = {e.ApplicationMessage.Retain}");
|
||||
Console.WriteLine();
|
||||
};
|
||||
|
||||
client.Connected += async (s, e) =>
|
||||
{
|
||||
Console.WriteLine("### CONNECTED WITH SERVER ###");
|
||||
|
||||
await client.SubscribeAsync(new List<TopicFilter>
|
||||
{
|
||||
new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce)
|
||||
});
|
||||
|
||||
Console.WriteLine("### SUBSCRIBED ###");
|
||||
};
|
||||
|
||||
client.Disconnected += async (s, e) =>
|
||||
{
|
||||
Console.WriteLine("### DISCONNECTED FROM SERVER ###");
|
||||
await Task.Delay(TimeSpan.FromSeconds(5));
|
||||
|
||||
try
|
||||
{
|
||||
await client.ConnectAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
Console.WriteLine("### RECONNECTING FAILED ###");
|
||||
}
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
await client.ConnectAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
Console.WriteLine("### CONNECTING FAILED ###");
|
||||
}
|
||||
|
||||
Console.WriteLine("### WAITING FOR APPLICATION MESSAGES ###");
|
||||
|
||||
while (true)
|
||||
{
|
||||
Console.ReadLine();
|
||||
|
||||
var applicationMessage = new MqttApplicationMessage(
|
||||
"A/B/C",
|
||||
Encoding.UTF8.GetBytes("Hello World"),
|
||||
MqttQualityOfServiceLevel.AtLeastOnce,
|
||||
false
|
||||
);
|
||||
|
||||
await client.PublishAsync(applicationMessage);
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Console.WriteLine(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
[assembly: AssemblyTitle("MQTTnet.TestMqttClient")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("Christian Kratky")]
|
||||
[assembly: AssemblyProduct("MQTTnet")]
|
||||
[assembly: AssemblyCopyright("Copyright © Christian Kratky 2016-2017")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
[assembly: ComVisible(false)]
|
||||
[assembly: Guid("7b19b139-2e9d-4f1d-88b4-6180b4cf872a")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<configuration>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
|
||||
</startup>
|
||||
</configuration>
|
||||
@@ -0,0 +1,56 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{6F8C0C0C-59EC-4921-9267-370AE113C34F}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
<RootNamespace>MQTTnet.TestMqttServer</RootNamespace>
|
||||
<AssemblyName>MQTTnet.TestMqttServer</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\MQTTnet.Core\MQTTnet.Core.csproj">
|
||||
<Project>{99C884F3-B4B9-417D-AA92-DC7DD1C4CFEE}</Project>
|
||||
<Name>MQTTnet.Core</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\..\Frameworks\MQTTnet.NetFramework\MQTTnet.NetFramework.csproj">
|
||||
<Project>{A480EF90-0EAA-4D9A-B271-47A9C47F6F7D}</Project>
|
||||
<Name>MQTTnet.NetFramework</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using MQTTnet.Core.Diagnostics;
|
||||
using MQTTnet.Core.Protocol;
|
||||
using MQTTnet.Core.Server;
|
||||
|
||||
namespace MQTTnet.TestMqttServer
|
||||
{
|
||||
public static class Program
|
||||
{
|
||||
public static void Main(string[] arguments)
|
||||
{
|
||||
MqttTrace.TraceMessagePublished += (s, e) =>
|
||||
{
|
||||
Console.WriteLine($">> [{e.ThreadId}] [{e.Source}] [{e.Level}]: {e.Message}");
|
||||
if (e.Exception != null)
|
||||
{
|
||||
Console.WriteLine(e.Exception);
|
||||
}
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var options = new MqttServerOptions
|
||||
{
|
||||
ConnectionValidator = p =>
|
||||
{
|
||||
if (p.ClientId == "SpecialClient")
|
||||
{
|
||||
if (p.Username != "USER" || p.Password != "PASS")
|
||||
{
|
||||
return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
|
||||
}
|
||||
}
|
||||
|
||||
return MqttConnectReturnCode.ConnectionAccepted;
|
||||
}
|
||||
};
|
||||
|
||||
var mqttServer = new MqttServerFactory().CreateMqttServer(options);
|
||||
mqttServer.Start();
|
||||
|
||||
Console.WriteLine("Press any key to exit.");
|
||||
Console.ReadLine();
|
||||
|
||||
mqttServer.Stop();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
|
||||
Console.ReadLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
[assembly: AssemblyTitle("MQTTnet.TestMqttServer")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("Christian Kratky")]
|
||||
[assembly: AssemblyProduct("MQTTnet")]
|
||||
[assembly: AssemblyCopyright("Copyright © Christian Kratky 2016-2017")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
[assembly: ComVisible(false)]
|
||||
[assembly: Guid("59c1090b-1734-4185-bc2f-53b9edd30079")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
Reference in New Issue
Block a user