[DW] Flex4Grid deleted

[NF] BosMon Mqtt Plugin created
This commit is contained in:
BlubbFish 2018-03-31 20:42:28 +00:00
commit 1075182069
41 changed files with 6414 additions and 0 deletions

25
mqtt/M2Mqtt.sln Normal file
View File

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27004.2010
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "M2Mqtt", "M2Mqtt\M2Mqtt.csproj", "{A11AEF5A-B246-4FE8-8330-06DB73CC8074}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A11AEF5A-B246-4FE8-8330-06DB73CC8074}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A11AEF5A-B246-4FE8-8330-06DB73CC8074}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A11AEF5A-B246-4FE8-8330-06DB73CC8074}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A11AEF5A-B246-4FE8-8330-06DB73CC8074}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E4CE8710-7B51-439F-A427-9AEFFE71E682}
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,105 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
using System;
namespace uPLibrary.Networking.M2Mqtt.Exceptions
{
/// <summary>
/// MQTT client exception
/// </summary>
public class MqttClientException : ApplicationException
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="code">Error code</param>
public MqttClientException(MqttClientErrorCode errorCode)
{
this.errorCode = errorCode;
}
// error code
private MqttClientErrorCode errorCode;
/// <summary>
/// Error code
/// </summary>
public MqttClientErrorCode ErrorCode
{
get { return this.errorCode; }
set { this.errorCode = value; }
}
}
/// <summary>
/// MQTT client erroro code
/// </summary>
public enum MqttClientErrorCode
{
/// <summary>
/// Will topic length error
/// </summary>
WillTopicWrong = 1,
/// <summary>
/// Keep alive period too large
/// </summary>
KeepAliveWrong,
/// <summary>
/// Topic contains wildcards
/// </summary>
TopicWildcard,
/// <summary>
/// Topic length wrong
/// </summary>
TopicLength,
/// <summary>
/// QoS level not allowed
/// </summary>
QosNotAllowed,
/// <summary>
/// Topics list empty for subscribe
/// </summary>
TopicsEmpty,
/// <summary>
/// Qos levels list empty for subscribe
/// </summary>
QosLevelsEmpty,
/// <summary>
/// Topics / Qos Levels not match in subscribe
/// </summary>
TopicsQosLevelsNotMatch,
/// <summary>
/// Wrong message from broker
/// </summary>
WrongBrokerMessage,
/// <summary>
/// Wrong Message Id
/// </summary>
WrongMessageId
}
}

View File

@ -0,0 +1,44 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
using System;
namespace uPLibrary.Networking.M2Mqtt.Exceptions
{
/// <summary>
/// Exception due to error communication with broker on socket
/// </summary>
public class MqttCommunicationException : ApplicationException
{
/// <summary>
/// Default constructor
/// </summary>
public MqttCommunicationException()
{
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="e">Inner Exception</param>
public MqttCommunicationException(Exception e)
: base(String.Empty, e)
{
}
}
}

View File

@ -0,0 +1,33 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
using System;
namespace uPLibrary.Networking.M2Mqtt.Exceptions
{
/// <summary>
/// Connection to the broker exception
/// </summary>
public class MqttConnectionException : ApplicationException
{
public MqttConnectionException(string message, Exception innerException)
: base(message, innerException)
{
}
}
}

View File

@ -0,0 +1,29 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
using System;
namespace uPLibrary.Networking.M2Mqtt.Exceptions
{
/// <summary>
/// Timeout on receiving from broker exception
/// </summary>
public class MqttTimeoutException : ApplicationException
{
}
}

View File

@ -0,0 +1,58 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
using System;
using System.Text;
namespace uPLibrary.Networking.M2Mqtt
{
/// <summary>
/// Interface for channel under MQTT library
/// </summary>
public interface IMqttNetworkChannel
{
/// <summary>
/// Data available on channel
/// </summary>
bool DataAvailable { get; }
/// <summary>
/// Receive data from the network channel
/// </summary>
/// <param name="buffer">Data buffer for receiving data</param>
/// <returns>Number of bytes received</returns>
int Receive(byte[] buffer);
/// <summary>
/// Send data on the network channel to the broker
/// </summary>
/// <param name="buffer">Data buffer to send</param>
/// <returns>Number of byte sent</returns>
int Send(byte[] buffer);
/// <summary>
/// Close the network channel
/// </summary>
void Close();
/// <summary>
/// Connect to remote server
/// </summary>
void Connect();
}
}

82
mqtt/M2Mqtt/M2Mqtt.csproj Normal file
View File

@ -0,0 +1,82 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{A11AEF5A-B246-4FE8-8330-06DB73CC8074}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>uPLibrary.Networking.M2Mqtt</RootNamespace>
<AssemblyName>M2Mqtt</AssemblyName>
<TargetFrameworkVersion>v2.0</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>TRACE;DEBUG;SSL</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;SSL</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Exceptions\MqttClientException.cs" />
<Compile Include="Exceptions\MqttCommunicationException.cs" />
<Compile Include="Exceptions\MqttConnectionException.cs" />
<Compile Include="Exceptions\MqttTimeoutException.cs" />
<Compile Include="IMqttNetworkChannel.cs" />
<Compile Include="Messages\MqttMsgBase.cs" />
<Compile Include="Messages\MqttMsgConnack.cs" />
<Compile Include="Messages\MqttMsgConnect.cs" />
<Compile Include="Messages\MqttMsgConnectEventArgs.cs" />
<Compile Include="Messages\MqttMsgContext.cs" />
<Compile Include="Messages\MqttMsgDisconnect.cs" />
<Compile Include="Messages\MqttMsgPingReq.cs" />
<Compile Include="Messages\MqttMsgPingResp.cs" />
<Compile Include="Messages\MqttMsgPuback.cs" />
<Compile Include="Messages\MqttMsgPubcomp.cs" />
<Compile Include="Messages\MqttMsgPublish.cs" />
<Compile Include="Messages\MqttMsgPublishedEventArgs.cs" />
<Compile Include="Messages\MqttMsgPublishEventArgs.cs" />
<Compile Include="Messages\MqttMsgPubrec.cs" />
<Compile Include="Messages\MqttMsgPubrel.cs" />
<Compile Include="Messages\MqttMsgSuback.cs" />
<Compile Include="Messages\MqttMsgSubscribe.cs" />
<Compile Include="Messages\MqttMsgSubscribedEventArgs.cs" />
<Compile Include="Messages\MqttMsgSubscribeEventArgs.cs" />
<Compile Include="Messages\MqttMsgUnsuback.cs" />
<Compile Include="Messages\MqttMsgUnsubscribe.cs" />
<Compile Include="Messages\MqttMsgUnsubscribedEventArgs.cs" />
<Compile Include="Messages\MqttMsgUnsubscribeEventArgs.cs" />
<Compile Include="MqttClient.cs" />
<Compile Include="MqttNetworkChannel.cs" />
<Compile Include="MqttSettings.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utility\QueueExtension.cs" />
</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>

View File

@ -0,0 +1,71 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>10.0.0</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{9B706DEC-4CE7-4764-BDBE-8A5F855E5857}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>uPLibrary.Networking.M2Mqtt</RootNamespace>
<AssemblyName>M2Mqtt</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\Mono</OutputPath>
<DefineConstants>DEBUG;SSL</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Release\Mono</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<DefineConstants>SSL</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="Exceptions\MqttClientException.cs" />
<Compile Include="Exceptions\MqttCommunicationException.cs" />
<Compile Include="Exceptions\MqttConnectionException.cs" />
<Compile Include="Exceptions\MqttTimeoutException.cs" />
<Compile Include="Messages\MqttMsgBase.cs" />
<Compile Include="Messages\MqttMsgConnack.cs" />
<Compile Include="Messages\MqttMsgConnect.cs" />
<Compile Include="Messages\MqttMsgConnectEventArgs.cs" />
<Compile Include="Messages\MqttMsgContext.cs" />
<Compile Include="Messages\MqttMsgDisconnect.cs" />
<Compile Include="Messages\MqttMsgPingReq.cs" />
<Compile Include="Messages\MqttMsgPingResp.cs" />
<Compile Include="Messages\MqttMsgPuback.cs" />
<Compile Include="Messages\MqttMsgPubcomp.cs" />
<Compile Include="Messages\MqttMsgPublish.cs" />
<Compile Include="Messages\MqttMsgPublishEventArgs.cs" />
<Compile Include="Messages\MqttMsgPublishedEventArgs.cs" />
<Compile Include="Messages\MqttMsgPubrec.cs" />
<Compile Include="Messages\MqttMsgPubrel.cs" />
<Compile Include="Messages\MqttMsgSuback.cs" />
<Compile Include="Messages\MqttMsgSubscribe.cs" />
<Compile Include="Messages\MqttMsgSubscribedEventArgs.cs" />
<Compile Include="Messages\MqttMsgSubscribeEventArgs.cs" />
<Compile Include="Messages\MqttMsgUnsuback.cs" />
<Compile Include="Messages\MqttMsgUnsubscribe.cs" />
<Compile Include="Messages\MqttMsgUnsubscribedEventArgs.cs" />
<Compile Include="Messages\MqttMsgUnsubscribeEventArgs.cs" />
<Compile Include="MqttClient.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="MqttNetworkChannel.cs" />
<Compile Include="MqttSettings.cs" />
<Compile Include="Utility\QueueExtension.cs" />
<Compile Include="IMqttNetworkChannel.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -0,0 +1,106 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{194FEA1B-E67F-4FC0-AC47-CD71F7F060CC}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>uPLibrary.Networking.M2Mqtt</RootNamespace>
<AssemblyName>M2Mqtt</AssemblyName>
<ProjectTypeGuids>{4D628B5B-2FBC-4AA6-8C16-197242AEB884};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<PlatformFamilyName>WindowsCE</PlatformFamilyName>
<PlatformID>E2BECB1F-8C8C-41ba-B736-9BE7D946A398</PlatformID>
<OSVersion>5.0</OSVersion>
<DeployDirSuffix>M2MqttNetCf35</DeployDirSuffix>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<NativePlatformName>Windows CE</NativePlatformName>
<FormFactorID>
</FormFactorID>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\NetCf35\</OutputPath>
<DefineConstants>TRACE;DEBUG;WindowsCE,COMPACT_FRAMEWORK</DefineConstants>
<NoStdLib>true</NoStdLib>
<NoConfig>true</NoConfig>
<ErrorReport>prompt</ErrorReport>
<FileAlignment>512</FileAlignment>
<WarningLevel>4</WarningLevel>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\NetCf35\</OutputPath>
<DefineConstants>TRACE;WindowsCE,COMPACT_FRAMEWORK</DefineConstants>
<NoStdLib>true</NoStdLib>
<NoConfig>true</NoConfig>
<ErrorReport>prompt</ErrorReport>
<FileAlignment>512</FileAlignment>
<WarningLevel>4</WarningLevel>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
</PropertyGroup>
<ItemGroup>
<Reference Include="mscorlib" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Exceptions\MqttClientException.cs" />
<Compile Include="Exceptions\MqttCommunicationException.cs" />
<Compile Include="Exceptions\MqttConnectionException.cs" />
<Compile Include="Exceptions\MqttTimeoutException.cs" />
<Compile Include="IMqttNetworkChannel.cs" />
<Compile Include="Messages\MqttMsgBase.cs" />
<Compile Include="Messages\MqttMsgConnack.cs" />
<Compile Include="Messages\MqttMsgConnect.cs" />
<Compile Include="Messages\MqttMsgConnectEventArgs.cs" />
<Compile Include="Messages\MqttMsgContext.cs" />
<Compile Include="Messages\MqttMsgDisconnect.cs" />
<Compile Include="Messages\MqttMsgPingReq.cs" />
<Compile Include="Messages\MqttMsgPingResp.cs" />
<Compile Include="Messages\MqttMsgPuback.cs" />
<Compile Include="Messages\MqttMsgPubcomp.cs" />
<Compile Include="Messages\MqttMsgPublish.cs" />
<Compile Include="Messages\MqttMsgPublishedEventArgs.cs" />
<Compile Include="Messages\MqttMsgPublishEventArgs.cs" />
<Compile Include="Messages\MqttMsgPubrec.cs" />
<Compile Include="Messages\MqttMsgPubrel.cs" />
<Compile Include="Messages\MqttMsgSuback.cs" />
<Compile Include="Messages\MqttMsgSubscribe.cs" />
<Compile Include="Messages\MqttMsgSubscribedEventArgs.cs" />
<Compile Include="Messages\MqttMsgSubscribeEventArgs.cs" />
<Compile Include="Messages\MqttMsgUnsuback.cs" />
<Compile Include="Messages\MqttMsgUnsubscribe.cs" />
<Compile Include="Messages\MqttMsgUnsubscribedEventArgs.cs" />
<Compile Include="Messages\MqttMsgUnsubscribeEventArgs.cs" />
<Compile Include="MqttClient.cs" />
<Compile Include="MqttNetworkChannel.cs" />
<Compile Include="MqttSettings.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utility\QueueExtension.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CompactFramework.CSharp.targets" />
<ProjectExtensions>
<VisualStudio>
<FlavorProperties GUID="{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}">
<HostingProcess disable="1" />
</FlavorProperties>
</VisualStudio>
</ProjectExtensions>
<!-- 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>

View File

@ -0,0 +1,87 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.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>{BB9B7FF4-6502-41AF-8851-5060B67645E8}</ProjectGuid>
<ProjectTypeGuids>{6AFDAB0D-95EF-424D-8A49-099ECD40B0FF};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>uPLibrary.Networking.M2Mqtt</RootNamespace>
<AssemblyName>M2Mqtt</AssemblyName>
<TargetFrameworkIdentifier>WindowsEmbeddedCompact</TargetFrameworkIdentifier>
<TargetFrameworkVersion>v3.9</TargetFrameworkVersion>
<TargetFrameworkTargetsVersion>v8.0</TargetFrameworkTargetsVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\NetCf39\</OutputPath>
<DefineConstants>TRACE;DEBUG;COMPACT_FRAMEWORK</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>ManagedMinimumRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\NetCf39\</OutputPath>
<DefineConstants>TRACE;COMPACT_FRAMEWORK</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Exceptions\MqttClientException.cs" />
<Compile Include="Exceptions\MqttCommunicationException.cs" />
<Compile Include="Exceptions\MqttConnectionException.cs" />
<Compile Include="Exceptions\MqttTimeoutException.cs" />
<Compile Include="IMqttNetworkChannel.cs" />
<Compile Include="Messages\MqttMsgBase.cs" />
<Compile Include="Messages\MqttMsgConnack.cs" />
<Compile Include="Messages\MqttMsgConnect.cs" />
<Compile Include="Messages\MqttMsgConnectEventArgs.cs" />
<Compile Include="Messages\MqttMsgContext.cs" />
<Compile Include="Messages\MqttMsgDisconnect.cs" />
<Compile Include="Messages\MqttMsgPingReq.cs" />
<Compile Include="Messages\MqttMsgPingResp.cs" />
<Compile Include="Messages\MqttMsgPuback.cs" />
<Compile Include="Messages\MqttMsgPubcomp.cs" />
<Compile Include="Messages\MqttMsgPublish.cs" />
<Compile Include="Messages\MqttMsgPublishedEventArgs.cs" />
<Compile Include="Messages\MqttMsgPublishEventArgs.cs" />
<Compile Include="Messages\MqttMsgPubrec.cs" />
<Compile Include="Messages\MqttMsgPubrel.cs" />
<Compile Include="Messages\MqttMsgSuback.cs" />
<Compile Include="Messages\MqttMsgSubscribe.cs" />
<Compile Include="Messages\MqttMsgSubscribedEventArgs.cs" />
<Compile Include="Messages\MqttMsgSubscribeEventArgs.cs" />
<Compile Include="Messages\MqttMsgUnsuback.cs" />
<Compile Include="Messages\MqttMsgUnsubscribe.cs" />
<Compile Include="Messages\MqttMsgUnsubscribedEventArgs.cs" />
<Compile Include="Messages\MqttMsgUnsubscribeEventArgs.cs" />
<Compile Include="MqttClient.cs" />
<Compile Include="MqttNetworkChannel.cs" />
<Compile Include="MqttSettings.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utility\QueueExtension.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\$(TargetFrameworkIdentifier)\$(TargetFrameworkTargetsVersion)\Microsoft.$(TargetFrameworkIdentifier).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>

View File

@ -0,0 +1,176 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
#if SSL && !WINDOWS_PHONE
#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3)
using Microsoft.SPOT.Net.Security;
#else
using System.Net.Security;
#endif
#endif
using System;
namespace uPLibrary.Networking.M2Mqtt.Messages
{
/// <summary>
/// Base class for all MQTT messages
/// </summary>
public abstract class MqttMsgBase
{
#region Constants...
// mask, offset and size for fixed header fields
internal const byte MSG_TYPE_MASK = 0xF0;
internal const byte MSG_TYPE_OFFSET = 0x04;
internal const byte MSG_TYPE_SIZE = 0x04;
internal const byte DUP_FLAG_MASK = 0x08;
internal const byte DUP_FLAG_OFFSET = 0x03;
internal const byte DUP_FLAG_SIZE = 0x01;
internal const byte QOS_LEVEL_MASK = 0x06;
internal const byte QOS_LEVEL_OFFSET = 0x01;
internal const byte QOS_LEVEL_SIZE = 0x02;
internal const byte RETAIN_FLAG_MASK = 0x01;
internal const byte RETAIN_FLAG_OFFSET = 0x00;
internal const byte RETAIN_FLAG_SIZE = 0x01;
// MQTT message types
internal const byte MQTT_MSG_CONNECT_TYPE = 0x01;
internal const byte MQTT_MSG_CONNACK_TYPE = 0x02;
internal const byte MQTT_MSG_PUBLISH_TYPE = 0x03;
internal const byte MQTT_MSG_PUBACK_TYPE = 0x04;
internal const byte MQTT_MSG_PUBREC_TYPE = 0x05;
internal const byte MQTT_MSG_PUBREL_TYPE = 0x06;
internal const byte MQTT_MSG_PUBCOMP_TYPE = 0x07;
internal const byte MQTT_MSG_SUBSCRIBE_TYPE = 0x08;
internal const byte MQTT_MSG_SUBACK_TYPE = 0x09;
internal const byte MQTT_MSG_UNSUBSCRIBE_TYPE = 0x0A;
internal const byte MQTT_MSG_UNSUBACK_TYPE = 0x0B;
internal const byte MQTT_MSG_PINGREQ_TYPE = 0x0C;
internal const byte MQTT_MSG_PINGRESP_TYPE = 0x0D;
internal const byte MQTT_MSG_DISCONNECT_TYPE = 0x0E;
// QOS levels
public const byte QOS_LEVEL_AT_MOST_ONCE = 0x00;
public const byte QOS_LEVEL_AT_LEAST_ONCE = 0x01;
public const byte QOS_LEVEL_EXACTLY_ONCE = 0x02;
internal const ushort MAX_TOPIC_LENGTH = 65535;
internal const ushort MIN_TOPIC_LENGTH = 1;
internal const byte MESSAGE_ID_SIZE = 2;
#endregion
#region Properties...
/// <summary>
/// Message type
/// </summary>
public byte Type
{
get { return this.type; }
set { this.type = value; }
}
/// <summary>
/// Duplicate message flag
/// </summary>
public bool DupFlag
{
get { return this.dupFlag; }
set { this.dupFlag = value; }
}
/// <summary>
/// Quality of Service level
/// </summary>
public byte QosLevel
{
get { return this.qosLevel; }
set { this.qosLevel = value; }
}
/// <summary>
/// Retain message flag
/// </summary>
public bool Retain
{
get { return this.retain; }
set { this.retain = value; }
}
#endregion
// message type
protected byte type;
// duplicate delivery
protected bool dupFlag;
// quality of service level
protected byte qosLevel;
// retain flag
protected bool retain;
/// <summary>
/// Returns message bytes rapresentation
/// </summary>
/// <returns>Bytes rapresentation</returns>
public abstract byte[] GetBytes();
/// <summary>
/// Encode remaining length and insert it into message buffer
/// </summary>
/// <param name="remainingLength">Remaining length value to encode</param>
/// <param name="buffer">Message buffer for inserting encoded value</param>
/// <param name="index">Index from which insert encoded value into buffer</param>
/// <returns>Index updated</returns>
protected int encodeRemainingLength(int remainingLength, byte[] buffer, int index)
{
int digit = 0;
do
{
digit = remainingLength % 128;
remainingLength /= 128;
if (remainingLength > 0)
digit = digit | 0x80;
buffer[index++] = (byte)digit;
} while (remainingLength > 0);
return index;
}
/// <summary>
/// Decode remaining length reading bytes from socket
/// </summary>
/// <param name="channel">Channel from reading bytes</param>
/// <returns>Decoded remaining length</returns>
protected static int decodeRemainingLength(IMqttNetworkChannel channel)
{
int multiplier = 1;
int value = 0;
int digit = 0;
byte[] nextByte = new byte[1];
do
{
// next digit from stream
channel.Receive(nextByte);
digit = nextByte[0];
value += ((digit & 127) * multiplier);
multiplier *= 128;
} while ((digit & 128) != 0);
return value;
}
}
}

View File

@ -0,0 +1,137 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
using System;
namespace uPLibrary.Networking.M2Mqtt.Messages
{
/// <summary>
/// Class for CONNACK message from broker to client
/// </summary>
public class MqttMsgConnack : MqttMsgBase
{
#region Constants...
// return codes for CONNACK message
public const byte CONN_ACCEPTED = 0x00;
public const byte CONN_REFUSED_PROT_VERS = 0x01;
public const byte CONN_REFUSED_IDENT_REJECTED = 0x02;
public const byte CONN_REFUSED_SERVER_UNAVAILABLE = 0x03;
public const byte CONN_REFUSED_USERNAME_PASSWORD = 0x04;
public const byte CONN_REFUSED_NOT_AUTHORIZED = 0x05;
private const byte TOPIC_NAME_COMP_RESP_BYTE_OFFSET = 0;
private const byte TOPIC_NAME_COMP_RESP_BYTE_SIZE = 1;
private const byte CONN_RETURN_CODE_BYTE_OFFSET = 1;
private const byte CONN_RETURN_CODE_BYTE_SIZE = 1;
#endregion
#region Properties...
/// <summary>
/// Return Code
/// </summary>
public byte ReturnCode
{
get { return this.returnCode; }
set { this.returnCode = value; }
}
#endregion
// return code for CONNACK message
private byte returnCode;
/// <summary>
/// Constructor
/// </summary>
public MqttMsgConnack()
{
this.type = MQTT_MSG_CONNACK_TYPE;
}
/// <summary>
/// Parse bytes for a CONNACK message
/// </summary>
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
/// <param name="channel">Channel connected to the broker</param>
/// <returns>CONNACK message instance</returns>
public static MqttMsgConnack Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
{
byte[] buffer;
MqttMsgConnack msg = new MqttMsgConnack();
// get remaining length and allocate buffer
int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
buffer = new byte[remainingLength];
// read bytes from socket...
channel.Receive(buffer);
// ...and set return code from broker
msg.returnCode = buffer[CONN_RETURN_CODE_BYTE_OFFSET];
return msg;
}
public override byte[] GetBytes()
{
int fixedHeaderSize = 0;
int varHeaderSize = 0;
int payloadSize = 0;
int remainingLength = 0;
byte[] buffer;
int index = 0;
// topic name compression response and connect return code
varHeaderSize += (TOPIC_NAME_COMP_RESP_BYTE_SIZE + CONN_RETURN_CODE_BYTE_SIZE);
remainingLength += (varHeaderSize + payloadSize);
// first byte of fixed header
fixedHeaderSize = 1;
int temp = remainingLength;
// increase fixed header size based on remaining length
// (each remaining length byte can encode until 128)
do
{
fixedHeaderSize++;
temp = temp / 128;
} while (temp > 0);
// allocate buffer for message
buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
// first fixed header byte
buffer[index] = (byte)(MQTT_MSG_CONNACK_TYPE << MSG_TYPE_OFFSET);
index++;
// encode remaining length
index = this.encodeRemainingLength(remainingLength, buffer, index);
// topic name compression response (reserved values. not used);
buffer[index++] = 0x00;
// connect return code
buffer[index++] = this.returnCode;
return buffer;
}
}
}

View File

@ -0,0 +1,509 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
using System;
using System.Text;
using uPLibrary.Networking.M2Mqtt.Exceptions;
namespace uPLibrary.Networking.M2Mqtt.Messages
{
/// <summary>
/// Class for CONNECT message from client to broker
/// </summary>
public class MqttMsgConnect : MqttMsgBase
{
#region Constants...
// protocol name supported
internal const string PROTOCOL_NAME = "MQIsdp";
// max length for client id
internal const int CLIENT_ID_MAX_LENGTH = 23;
// variable header fields
internal const byte PROTOCOL_NAME_LEN_SIZE = 2;
internal const byte PROTOCOL_NAME_SIZE = 6;
internal const byte PROTOCOL_VERSION_NUMBER_SIZE = 1;
internal const byte CONNECT_FLAGS_SIZE = 1;
internal const byte KEEP_ALIVE_TIME_SIZE = 2;
internal const byte PROTOCOL_VERSION = 0x03;
internal const ushort KEEP_ALIVE_PERIOD_DEFAULT = 60; // seconds
internal const ushort MAX_KEEP_ALIVE = 65535; // 16 bit
// connect flags
internal const byte USERNAME_FLAG_MASK = 0x80;
internal const byte USERNAME_FLAG_OFFSET = 0x07;
internal const byte USERNAME_FLAG_SIZE = 0x01;
internal const byte PASSWORD_FLAG_MASK = 0x40;
internal const byte PASSWORD_FLAG_OFFSET = 0x06;
internal const byte PASSWORD_FLAG_SIZE = 0x01;
internal const byte WILL_RETAIN_FLAG_MASK = 0x20;
internal const byte WILL_RETAIN_FLAG_OFFSET = 0x05;
internal const byte WILL_RETAIN_FLAG_SIZE = 0x01;
internal const byte WILL_QOS_FLAG_MASK = 0x18;
internal const byte WILL_QOS_FLAG_OFFSET = 0x03;
internal const byte WILL_QOS_FLAG_SIZE = 0x02;
internal const byte WILL_FLAG_MASK = 0x04;
internal const byte WILL_FLAG_OFFSET = 0x02;
internal const byte WILL_FLAG_SIZE = 0x01;
internal const byte CLEAN_SESSION_FLAG_MASK = 0x02;
internal const byte CLEAN_SESSION_FLAG_OFFSET = 0x01;
internal const byte CLEAN_SESSION_FLAG_SIZE = 0x01;
#endregion
#region Properties...
/// <summary>
/// Protocol name
/// </summary>
public string ProtocolName
{
get { return this.protocolName; }
set { this.protocolName = value; }
}
/// <summary>
/// Protocol version
/// </summary>
public byte ProtocolVersion
{
get { return this.protocolVersion; }
set { this.protocolVersion = value; }
}
/// <summary>
/// Client identifier
/// </summary>
public string ClientId
{
get { return this.clientId; }
set { this.clientId = value; }
}
/// <summary>
/// Will retain flag
/// </summary>
public bool WillRetain
{
get { return this.willRetain; }
set { this.willRetain = value; }
}
/// <summary>
/// Will QOS level
/// </summary>
public byte WillQosLevel
{
get { return this.willQosLevel; }
set { this.willQosLevel = value; }
}
/// <summary>
/// Will flag
/// </summary>
public bool WillFlag
{
get { return this.willFlag; }
set { this.willFlag = value; }
}
/// <summary>
/// Will topic
/// </summary>
public string WillTopic
{
get { return this.willTopic; }
set { this.willTopic = value; }
}
/// <summary>
/// Will message
/// </summary>
public string WillMessage
{
get { return this.willMessage; }
set { this.willMessage = value; }
}
/// <summary>
/// Username
/// </summary>
public string Username
{
get { return this.username; }
set { this.username = value; }
}
/// <summary>
/// Password
/// </summary>
public string Password
{
get { return this.password; }
set { this.password = value; }
}
/// <summary>
/// Clean session flag
/// </summary>
public bool CleanSession
{
get { return this.cleanSession; }
set { this.cleanSession = value; }
}
/// <summary>
/// Keep alive period
/// </summary>
public ushort KeepAlivePeriod
{
get { return this.keepAlivePeriod; }
set { this.keepAlivePeriod = value; }
}
#endregion
// protocol name
private string protocolName;
// protocol version
private byte protocolVersion;
// client identifier
private string clientId;
// will retain flag
protected bool willRetain;
// will quality of service level
protected byte willQosLevel;
// will flag
private bool willFlag;
// will topic
private string willTopic;
// will message
private string willMessage;
// username
private string username;
// password
private string password;
// clean session flag
private bool cleanSession;
// keep alive period (in sec)
private ushort keepAlivePeriod;
/// <summary>
/// Constructor
/// </summary>
public MqttMsgConnect()
{
this.type = MQTT_MSG_CONNECT_TYPE;
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="clientId">Client identifier</param>
public MqttMsgConnect(string clientId) :
this(clientId, null, null, false, QOS_LEVEL_AT_LEAST_ONCE, false, null, null, true, KEEP_ALIVE_PERIOD_DEFAULT)
{
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="clientId">Client identifier</param>
/// <param name="username">Username</param>
/// <param name="password">Password</param>
/// <param name="willRetain">Will retain flag</param>
/// <param name="willQosLevel">Will QOS level</param>
/// <param name="willFlag">Will flag</param>
/// <param name="willTopic">Will topic</param>
/// <param name="willMessage">Will message</param>
/// <param name="cleanSession">Clean sessione flag</param>
/// <param name="keepAlivePeriod">Keep alive period</param>
public MqttMsgConnect(string clientId,
string username,
string password,
bool willRetain,
byte willQosLevel,
bool willFlag,
string willTopic,
string willMessage,
bool cleanSession,
ushort keepAlivePeriod
)
{
this.type = MQTT_MSG_CONNECT_TYPE;
this.clientId = clientId;
this.username = username;
this.password = password;
this.willRetain = willRetain;
this.willQosLevel = willQosLevel;
this.willFlag = willFlag;
this.willTopic = willTopic;
this.willMessage = willMessage;
this.cleanSession = cleanSession;
this.keepAlivePeriod = keepAlivePeriod;
}
/// <summary>
/// Parse bytes for a CONNECT message
/// </summary>
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
/// <param name="channel">Channel connected to the broker</param>
/// <returns>CONNECT message instance</returns>
public static MqttMsgConnect Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
{
byte[] buffer;
int index = 0;
int protNameUtf8Length;
byte[] protNameUtf8;
bool isUsernameFlag;
bool isPasswordFlag;
int clientIdUtf8Length;
byte[] clientIdUtf8;
int willTopicUtf8Length;
byte[] willTopicUtf8;
int willMessageUtf8Length;
byte[] willMessageUtf8;
int usernameUtf8Length;
byte[] usernameUtf8;
int passwordUtf8Length;
byte[] passwordUtf8;
MqttMsgConnect msg = new MqttMsgConnect();
// get remaining length and allocate buffer
int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
buffer = new byte[remainingLength];
// read bytes from socket...
channel.Receive(buffer);
// protocol name
protNameUtf8Length = ((buffer[index++] << 8) & 0xFF00);
protNameUtf8Length |= buffer[index++];
protNameUtf8 = new byte[protNameUtf8Length];
Array.Copy(buffer, index, protNameUtf8, 0, protNameUtf8Length);
index += protNameUtf8Length;
msg.protocolName = new String(Encoding.UTF8.GetChars(protNameUtf8));
// protocol version
msg.protocolVersion = buffer[index];
index += PROTOCOL_VERSION_NUMBER_SIZE;
// connect flags
isUsernameFlag = (buffer[index] & USERNAME_FLAG_MASK) != 0x00;
isPasswordFlag = (buffer[index] & PASSWORD_FLAG_MASK) != 0x00;
msg.willRetain = (buffer[index] & WILL_RETAIN_FLAG_MASK) != 0x00;
msg.willQosLevel = (byte)((buffer[index] & WILL_QOS_FLAG_MASK) >> WILL_QOS_FLAG_OFFSET);
msg.willFlag = (buffer[index] & WILL_FLAG_MASK) != 0x00;
msg.cleanSession = (buffer[index] & CLEAN_SESSION_FLAG_MASK) != 0x00;
index += CONNECT_FLAGS_SIZE;
// keep alive timer
msg.keepAlivePeriod = (ushort)((buffer[index++] << 8) & 0xFF00);
msg.keepAlivePeriod |= buffer[index++];
// client identifier
clientIdUtf8Length = ((buffer[index++] << 8) & 0xFF00);
clientIdUtf8Length |= buffer[index++];
clientIdUtf8 = new byte[clientIdUtf8Length];
Array.Copy(buffer, index, clientIdUtf8, 0, clientIdUtf8Length);
index += clientIdUtf8Length;
msg.clientId = new String(Encoding.UTF8.GetChars(clientIdUtf8));
// will topic and will message
if (msg.willFlag)
{
willTopicUtf8Length = ((buffer[index++] << 8) & 0xFF00);
willTopicUtf8Length |= buffer[index++];
willTopicUtf8 = new byte[willTopicUtf8Length];
Array.Copy(buffer, index, willTopicUtf8, 0, willTopicUtf8Length);
index += willTopicUtf8Length;
msg.willTopic = new String(Encoding.UTF8.GetChars(willTopicUtf8));
willMessageUtf8Length = ((buffer[index++] << 8) & 0xFF00);
willMessageUtf8Length |= buffer[index++];
willMessageUtf8 = new byte[willMessageUtf8Length];
Array.Copy(buffer, index, willMessageUtf8, 0, willMessageUtf8Length);
index += willMessageUtf8Length;
msg.willMessage = new String(Encoding.UTF8.GetChars(willMessageUtf8));
}
// username
if (isUsernameFlag)
{
usernameUtf8Length = ((buffer[index++] << 8) & 0xFF00);
usernameUtf8Length |= buffer[index++];
usernameUtf8 = new byte[usernameUtf8Length];
Array.Copy(buffer, index, usernameUtf8, 0, usernameUtf8Length);
index += usernameUtf8Length;
msg.username = new String(Encoding.UTF8.GetChars(usernameUtf8));
}
// password
if (isPasswordFlag)
{
passwordUtf8Length = ((buffer[index++] << 8) & 0xFF00);
passwordUtf8Length |= buffer[index++];
passwordUtf8 = new byte[passwordUtf8Length];
Array.Copy(buffer, index, passwordUtf8, 0, passwordUtf8Length);
index += passwordUtf8Length;
msg.password = new String(Encoding.UTF8.GetChars(passwordUtf8));
}
return msg;
}
public override byte[] GetBytes()
{
int fixedHeaderSize = 0;
int varHeaderSize = 0;
int payloadSize = 0;
int remainingLength = 0;
byte[] buffer;
int index = 0;
byte[] clientIdUtf8 = Encoding.UTF8.GetBytes(this.clientId);
byte[] willTopicUtf8 = (this.willTopic != null) ? Encoding.UTF8.GetBytes(this.willTopic) : null;
byte[] willMessageUtf8 = (this.willMessage != null) ? Encoding.UTF8.GetBytes(this.willMessage) : null;
byte[] usernameUtf8 = (this.username != null) ? Encoding.UTF8.GetBytes(this.username) : null;
byte[] passwordUtf8 = (this.password != null) ? Encoding.UTF8.GetBytes(this.password) : null;
// will flag set but will topic wrong
if (this.willFlag && (willTopicUtf8.Length == 0))
throw new MqttClientException(MqttClientErrorCode.WillTopicWrong);
if (this.keepAlivePeriod > MAX_KEEP_ALIVE)
throw new MqttClientException(MqttClientErrorCode.KeepAliveWrong);
// protocol name field size
varHeaderSize += (PROTOCOL_NAME_LEN_SIZE + PROTOCOL_NAME_SIZE);
// protocol version number field size
varHeaderSize += PROTOCOL_VERSION_NUMBER_SIZE;
// connect flags field size
varHeaderSize += CONNECT_FLAGS_SIZE;
// keep alive timer field size
varHeaderSize += KEEP_ALIVE_TIME_SIZE;
// client identifier field size
payloadSize += clientIdUtf8.Length + 2;
// will topic field size
payloadSize += (willTopicUtf8 != null) ? (willTopicUtf8.Length + 2) : 0;
// will message field size
payloadSize += (willMessageUtf8 != null) ? (willMessageUtf8.Length + 2) : 0;
// username field size
payloadSize += (usernameUtf8 != null) ? (usernameUtf8.Length + 2) : 0;
// password field size
payloadSize += (passwordUtf8 != null) ? (passwordUtf8.Length + 2) : 0;
remainingLength += (varHeaderSize + payloadSize);
// first byte of fixed header
fixedHeaderSize = 1;
int temp = remainingLength;
// increase fixed header size based on remaining length
// (each remaining length byte can encode until 128)
do
{
fixedHeaderSize++;
temp = temp / 128;
} while (temp > 0);
// allocate buffer for message
buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
// first fixed header byte
buffer[index++] = (MQTT_MSG_CONNECT_TYPE << MSG_TYPE_OFFSET);
// encode remaining length
index = this.encodeRemainingLength(remainingLength, buffer, index);
// protocol name
buffer[index++] = 0; // MSB protocol name size
buffer[index++] = PROTOCOL_NAME_SIZE; // LSB protocol name size
buffer[index++] = (byte)'M';
buffer[index++] = (byte)'Q';
buffer[index++] = (byte)'I';
buffer[index++] = (byte)'s';
buffer[index++] = (byte)'d';
buffer[index++] = (byte)'p';
// protocol version
buffer[index++] = PROTOCOL_VERSION;
// connect flags
byte connectFlags = 0x00;
connectFlags |= (this.username != null) ? (byte)(1 << USERNAME_FLAG_OFFSET) : (byte)0x00;
connectFlags |= (this.password != null) ? (byte)(1 << PASSWORD_FLAG_OFFSET) : (byte)0x00;
connectFlags |= (this.willRetain) ? (byte)(1 << WILL_RETAIN_FLAG_OFFSET) : (byte)0x00;
connectFlags |= (byte)(this.willQosLevel << WILL_QOS_FLAG_OFFSET);
connectFlags |= (this.willFlag) ? (byte)(1 << WILL_FLAG_OFFSET) : (byte)0x00;
connectFlags |= (this.cleanSession) ? (byte)(1 << CLEAN_SESSION_FLAG_OFFSET) : (byte)0x00;
buffer[index++] = connectFlags;
// keep alive period
buffer[index++] = (byte)((this.keepAlivePeriod >> 8) & 0x00FF); // MSB
buffer[index++] = (byte)(this.keepAlivePeriod & 0x00FF); // LSB
// client identifier
buffer[index++] = (byte)((clientIdUtf8.Length >> 8) & 0x00FF); // MSB
buffer[index++] = (byte)(clientIdUtf8.Length & 0x00FF); // LSB
Array.Copy(clientIdUtf8, 0, buffer, index, clientIdUtf8.Length);
index += clientIdUtf8.Length;
// will topic
if (this.willFlag && (this.willTopic != null))
{
buffer[index++] = (byte)((willTopicUtf8.Length >> 8) & 0x00FF); // MSB
buffer[index++] = (byte)(willTopicUtf8.Length & 0x00FF); // LSB
Array.Copy(willTopicUtf8, 0, buffer, index, willTopicUtf8.Length);
index += willTopicUtf8.Length;
}
// will message
if (this.willFlag && (this.willMessage != null))
{
buffer[index++] = (byte)((willMessageUtf8.Length >> 8) & 0x00FF); // MSB
buffer[index++] = (byte)(willMessageUtf8.Length & 0x00FF); // LSB
Array.Copy(willMessageUtf8, 0, buffer, index, willMessageUtf8.Length);
index += willMessageUtf8.Length;
}
// username
if (this.username != null)
{
buffer[index++] = (byte)((usernameUtf8.Length >> 8) & 0x00FF); // MSB
buffer[index++] = (byte)(usernameUtf8.Length & 0x00FF); // LSB
Array.Copy(usernameUtf8, 0, buffer, index, usernameUtf8.Length);
index += usernameUtf8.Length;
}
// password
if (this.password != null)
{
buffer[index++] = (byte)((passwordUtf8.Length >> 8) & 0x00FF); // MSB
buffer[index++] = (byte)(passwordUtf8.Length & 0x00FF); // LSB
Array.Copy(passwordUtf8, 0, buffer, index, passwordUtf8.Length);
index += passwordUtf8.Length;
}
return buffer;
}
}
}

View File

@ -0,0 +1,46 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3)
using System;
#else
using Microsoft.SPOT;
#endif
namespace uPLibrary.Networking.M2Mqtt.Messages
{
/// <summary>
/// Event Args class for CONNECT message received from client
/// </summary>
public class MqttMsgConnectEventArgs : EventArgs
{
/// <summary>
/// Message received from client
/// </summary>
public MqttMsgConnect Message { get; private set; }
/// <summary>
/// Constructor
/// </summary>
/// <param name="msg">CONNECT message received from client</param>
public MqttMsgConnectEventArgs(MqttMsgConnect connect)
{
this.Message = connect;
}
}
}

View File

@ -0,0 +1,141 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
using System;
using System.Text;
namespace uPLibrary.Networking.M2Mqtt.Messages
{
/// <summary>
/// Context for MQTT message
/// </summary>
public class MqttMsgContext
{
/// <summary>
/// MQTT message
/// </summary>
public MqttMsgBase Message { get; set; }
/// <summary>
/// MQTT message state
/// </summary>
public MqttMsgState State { get; set; }
/// <summary>
/// Flow of the message
/// </summary>
public MqttMsgFlow Flow { get; set; }
/// <summary>
/// Timestamp in ticks (for retry)
/// </summary>
public int Timestamp { get; set; }
/// <summary>
/// Attempt (for retry)
/// </summary>
public int Attempt { get; set; }
}
/// <summary>
/// Flow of the message
/// </summary>
public enum MqttMsgFlow
{
/// <summary>
/// To publish to subscribers
/// </summary>
ToPublish,
/// <summary>
/// To acknowledge to publisher
/// </summary>
ToAcknowledge
}
/// <summary>
/// MQTT message state
/// </summary>
public enum MqttMsgState
{
/// <summary>
/// QOS = 0, Message queued
/// </summary>
QueuedQos0,
/// <summary>
/// QOS = 1, Message queued
/// </summary>
QueuedQos1,
/// <summary>
/// QOS = 2, Message queued
/// </summary>
QueuedQos2,
/// <summary>
/// QOS = 1, PUBLISH sent, wait for PUBACK
/// </summary>
WaitForPuback,
/// <summary>
/// QOS = 2, PUBLISH sent, wait for PUBREC
/// </summary>
WaitForPubrec,
/// <summary>
/// QOS = 2, PUBREC sent, wait for PUBREL
/// </summary>
WaitForPubrel,
/// <summary>
/// QOS = 2, PUBREL sent, wait for PUBCOMP
/// </summary>
WaitForPubcomp,
/// <summary>
/// QOS = 2, start first phase handshake send PUBREC
/// </summary>
SendPubrec,
/// <summary>
/// QOS = 2, start second phase handshake send PUBREL
/// </summary>
SendPubrel,
/// <summary>
/// QOS = 2, end second phase handshake send PUBCOMP
/// </summary>
SendPubcomp,
/// <summary>
/// QOS = 1, PUBLISH received, send PUBACK
/// </summary>
SendPuback,
/// <summary>
/// (QOS = 1), SUBSCRIBE sent, wait for SUBACK
/// </summary>
WaitForSuback,
/// <summary>
/// (QOS = 1), UNSUBSCRIBE sent, wait for UNSUBACK
/// </summary>
WaitForUnsuback
}
}

View File

@ -0,0 +1,64 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
namespace uPLibrary.Networking.M2Mqtt.Messages
{
/// <summary>
/// Class for DISCONNECT message from client to broker
/// </summary>
public class MqttMsgDisconnect : MqttMsgBase
{
/// <summary>
/// Constructor
/// </summary>
public MqttMsgDisconnect()
{
this.type = MQTT_MSG_DISCONNECT_TYPE;
}
/// <summary>
/// Parse bytes for a DISCONNECT message
/// </summary>
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
/// <param name="channel">Channel connected to the broker</param>
/// <returns>DISCONNECT message instance</returns>
public static MqttMsgDisconnect Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
{
MqttMsgDisconnect msg = new MqttMsgDisconnect();
// get remaining length and allocate buffer
int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
// NOTE : remainingLength must be 0
return msg;
}
public override byte[] GetBytes()
{
byte[] buffer = new byte[2];
int index = 0;
// first fixed header byte
buffer[index++] = (MQTT_MSG_DISCONNECT_TYPE << MSG_TYPE_OFFSET);
buffer[index++] = 0x00;
return buffer;
}
}
}

View File

@ -0,0 +1,64 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
namespace uPLibrary.Networking.M2Mqtt.Messages
{
/// <summary>
/// Class for PINGREQ message from client to broker
/// </summary>
public class MqttMsgPingReq : MqttMsgBase
{
/// <summary>
/// Constructor
/// </summary>
public MqttMsgPingReq()
{
this.type = MQTT_MSG_PINGREQ_TYPE;
}
public override byte[] GetBytes()
{
byte[] buffer = new byte[2];
int index = 0;
// first fixed header byte
buffer[index++] = (MQTT_MSG_PINGREQ_TYPE << MSG_TYPE_OFFSET);
buffer[index++] = 0x00;
return buffer;
}
/// <summary>
/// Parse bytes for a PINGREQ message
/// </summary>
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
/// <param name="channel">Channel connected to the broker</param>
/// <returns>PINGREQ message instance</returns>
public static MqttMsgPingReq Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
{
MqttMsgPingReq msg = new MqttMsgPingReq();
// already know remaininglength is zero (MQTT specification),
// so it isn't necessary to read other data from socket
int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
return msg;
}
}
}

View File

@ -0,0 +1,65 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
using System;
namespace uPLibrary.Networking.M2Mqtt.Messages
{
/// <summary>
/// Class for PINGRESP message from client to broker
/// </summary>
public class MqttMsgPingResp : MqttMsgBase
{
/// <summary>
/// Constructor
/// </summary>
public MqttMsgPingResp()
{
this.type = MQTT_MSG_PINGRESP_TYPE;
}
/// <summary>
/// Parse bytes for a PINGRESP message
/// </summary>
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
/// <param name="channel">Channel connected to the broker</param>
/// <returns>PINGRESP message instance</returns>
public static MqttMsgPingResp Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
{
MqttMsgPingResp msg = new MqttMsgPingResp();
// already know remaininglength is zero (MQTT specification),
// so it isn't necessary to read other data from socket
int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
return msg;
}
public override byte[] GetBytes()
{
byte[] buffer = new byte[2];
int index = 0;
// first fixed header byte
buffer[index++] = (MQTT_MSG_PINGRESP_TYPE << MSG_TYPE_OFFSET);
buffer[index++] = 0x00;
return buffer;
}
}
}

View File

@ -0,0 +1,119 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
namespace uPLibrary.Networking.M2Mqtt.Messages
{
/// <summary>
/// Class for PUBACK message from broker to client
/// </summary>
public class MqttMsgPuback : MqttMsgBase
{
#region Properties...
/// <summary>
/// Message identifier for the publish message
/// that is acknowledged
/// </summary>
public ushort MessageId
{
get { return this.messageId; }
set { this.messageId = value; }
}
#endregion
// message identifier
private ushort messageId;
/// <summary>
/// Constructor
/// </summary>
public MqttMsgPuback()
{
this.type = MQTT_MSG_PUBACK_TYPE;
}
public override byte[] GetBytes()
{
int fixedHeaderSize = 0;
int varHeaderSize = 0;
int payloadSize = 0;
int remainingLength = 0;
byte[] buffer;
int index = 0;
// message identifier
varHeaderSize += MESSAGE_ID_SIZE;
remainingLength += (varHeaderSize + payloadSize);
// first byte of fixed header
fixedHeaderSize = 1;
int temp = remainingLength;
// increase fixed header size based on remaining length
// (each remaining length byte can encode until 128)
do
{
fixedHeaderSize++;
temp = temp / 128;
} while (temp > 0);
// allocate buffer for message
buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
// first fixed header byte
buffer[index++] = (MQTT_MSG_PUBACK_TYPE << MSG_TYPE_OFFSET);
// encode remaining length
index = this.encodeRemainingLength(remainingLength, buffer, index);
// get message identifier
buffer[index++] = (byte)((this.messageId >> 8) & 0x00FF); // MSB
buffer[index++] = (byte)(this.messageId & 0x00FF); // LSB
return buffer;
}
/// <summary>
/// Parse bytes for a PUBACK message
/// </summary>
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
/// <param name="channel">Channel connected to the broker</param>
/// <returns>PUBACK message instance</returns>
public static MqttMsgPuback Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
{
byte[] buffer;
int index = 0;
MqttMsgPuback msg = new MqttMsgPuback();
// get remaining length and allocate buffer
int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
buffer = new byte[remainingLength];
// read bytes from socket...
channel.Receive(buffer);
// message id
msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00);
msg.messageId |= (buffer[index++]);
return msg;
}
}
}

View File

@ -0,0 +1,118 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
namespace uPLibrary.Networking.M2Mqtt.Messages
{
/// <summary>
/// Class for PUBCOMP message from broker to client
/// </summary>
public class MqttMsgPubcomp : MqttMsgBase
{
#region Properties...
/// <summary>
/// Message identifier for the acknowledged publish message
/// </summary>
public ushort MessageId
{
get { return this.messageId; }
set { this.messageId = value; }
}
#endregion
// message identifier
private ushort messageId;
/// <summary>
/// Constructor
/// </summary>
public MqttMsgPubcomp()
{
this.type = MQTT_MSG_PUBCOMP_TYPE;
}
public override byte[] GetBytes()
{
int fixedHeaderSize = 0;
int varHeaderSize = 0;
int payloadSize = 0;
int remainingLength = 0;
byte[] buffer;
int index = 0;
// message identifier
varHeaderSize += MESSAGE_ID_SIZE;
remainingLength += (varHeaderSize + payloadSize);
// first byte of fixed header
fixedHeaderSize = 1;
int temp = remainingLength;
// increase fixed header size based on remaining length
// (each remaining length byte can encode until 128)
do
{
fixedHeaderSize++;
temp = temp / 128;
} while (temp > 0);
// allocate buffer for message
buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
// first fixed header byte
buffer[index++] = (MQTT_MSG_PUBCOMP_TYPE << MSG_TYPE_OFFSET);
// encode remaining length
index = this.encodeRemainingLength(remainingLength, buffer, index);
// get message identifier
buffer[index++] = (byte)((this.messageId >> 8) & 0x00FF); // MSB
buffer[index++] = (byte)(this.messageId & 0x00FF); // LSB
return buffer;
}
/// <summary>
/// Parse bytes for a PUBCOMP message
/// </summary>
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
/// <param name="channel">Channel connected to the broker</param>
/// <returns>PUBCOMP message instance</returns>
public static MqttMsgPubcomp Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
{
byte[] buffer;
int index = 0;
MqttMsgPubcomp msg = new MqttMsgPubcomp();
// get remaining length and allocate buffer
int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
buffer = new byte[remainingLength];
// read bytes from socket...
channel.Receive(buffer);
// message id
msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00);
msg.messageId |= (buffer[index++]);
return msg;
}
}
}

View File

@ -0,0 +1,270 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
using System;
using System.Text;
using uPLibrary.Networking.M2Mqtt.Exceptions;
namespace uPLibrary.Networking.M2Mqtt.Messages
{
/// <summary>
/// Class for PUBLISH message from client to broker
/// </summary>
public class MqttMsgPublish : MqttMsgBase
{
#region Properties...
/// <summary>
/// Message topic
/// </summary>
public string Topic
{
get { return this.topic; }
set { this.topic = value; }
}
/// <summary>
/// Message data
/// </summary>
public byte[] Message
{
get { return this.message; }
set { this.message = value; }
}
/// <summary>
/// Message identifier
/// </summary>
public ushort MessageId
{
get { return this.messageId; }
set { this.messageId = value; }
}
#endregion
// message topic
private string topic;
// message data
private byte[] message;
// message identifier
ushort messageId;
/// <summary>
/// Constructor
/// </summary>
public MqttMsgPublish()
{
this.type = MQTT_MSG_PUBLISH_TYPE;
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="topic">Message topic</param>
/// <param name="message">Message data</param>
public MqttMsgPublish(string topic, byte[] message) :
this(topic, message, false, QOS_LEVEL_AT_MOST_ONCE, false)
{
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="topic">Message topic</param>
/// <param name="message">Message data</param>
/// <param name="dupFlag">Duplicate flag</param>
/// <param name="qosLevel">Quality of Service level</param>
/// <param name="retain">Retain flag</param>
public MqttMsgPublish(string topic,
byte[] message,
bool dupFlag,
byte qosLevel,
bool retain) : base()
{
this.type = MQTT_MSG_PUBLISH_TYPE;
this.topic = topic;
this.message = message;
this.dupFlag = dupFlag;
this.qosLevel = qosLevel;
this.retain = retain;
this.messageId = 0;
}
public override byte[] GetBytes()
{
int fixedHeaderSize = 0;
int varHeaderSize = 0;
int payloadSize = 0;
int remainingLength = 0;
byte[] buffer;
int index = 0;
// topic can't contain wildcards
if ((this.topic.IndexOf('#') != -1) || (this.topic.IndexOf('+') != -1))
throw new MqttClientException(MqttClientErrorCode.TopicWildcard);
// check topic length
if ((this.topic.Length < MIN_TOPIC_LENGTH) || (this.topic.Length > MAX_TOPIC_LENGTH))
throw new MqttClientException(MqttClientErrorCode.TopicLength);
byte[] topicUtf8 = Encoding.UTF8.GetBytes(this.topic);
// topic name
varHeaderSize += topicUtf8.Length + 2;
// message id is valid only with QOS level 1 or QOS level 2
if ((this.qosLevel == QOS_LEVEL_AT_LEAST_ONCE) ||
(this.qosLevel == QOS_LEVEL_EXACTLY_ONCE))
{
varHeaderSize += MESSAGE_ID_SIZE;
}
// check on message with zero length
if (this.message != null)
// message data
payloadSize += this.message.Length;
remainingLength += (varHeaderSize + payloadSize);
// first byte of fixed header
fixedHeaderSize = 1;
int temp = remainingLength;
// increase fixed header size based on remaining length
// (each remaining length byte can encode until 128)
do
{
fixedHeaderSize++;
temp = temp / 128;
} while (temp > 0);
// allocate buffer for message
buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
// first fixed header byte
buffer[index] = (byte)((MQTT_MSG_PUBLISH_TYPE << MSG_TYPE_OFFSET) |
(this.qosLevel << QOS_LEVEL_OFFSET));
buffer[index] |= this.dupFlag ? (byte)(1 << DUP_FLAG_OFFSET) : (byte)0x00;
buffer[index] |= this.retain ? (byte)(1 << RETAIN_FLAG_OFFSET) : (byte)0x00;
index++;
// encode remaining length
index = this.encodeRemainingLength(remainingLength, buffer, index);
// topic name
buffer[index++] = (byte)((topicUtf8.Length >> 8) & 0x00FF); // MSB
buffer[index++] = (byte)(topicUtf8.Length & 0x00FF); // LSB
Array.Copy(topicUtf8, 0, buffer, index, topicUtf8.Length);
index += topicUtf8.Length;
// message id is valid only with QOS level 1 or QOS level 2
if ((this.qosLevel == QOS_LEVEL_AT_LEAST_ONCE) ||
(this.qosLevel == QOS_LEVEL_EXACTLY_ONCE))
{
// check message identifier assigned
if (this.messageId == 0)
throw new MqttClientException(MqttClientErrorCode.WrongMessageId);
buffer[index++] = (byte)((this.messageId >> 8) & 0x00FF); // MSB
buffer[index++] = (byte)(this.messageId & 0x00FF); // LSB
}
// check on message with zero length
if (this.message != null)
{
// message data
Array.Copy(this.message, 0, buffer, index, this.message.Length);
index += this.message.Length;
}
return buffer;
}
/// <summary>
/// Parse bytes for a PUBLISH message
/// </summary>
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
/// <param name="channel">Channel connected to the broker</param>
/// <returns>PUBLISH message instance</returns>
public static MqttMsgPublish Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
{
byte[] buffer;
int index = 0;
byte[] topicUtf8;
int topicUtf8Length;
MqttMsgPublish msg = new MqttMsgPublish();
// get remaining length and allocate buffer
int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
buffer = new byte[remainingLength];
// read bytes from socket...
int received = channel.Receive(buffer);
// topic name
topicUtf8Length = ((buffer[index++] << 8) & 0xFF00);
topicUtf8Length |= buffer[index++];
topicUtf8 = new byte[topicUtf8Length];
Array.Copy(buffer, index, topicUtf8, 0, topicUtf8Length);
index += topicUtf8Length;
msg.topic = new String(Encoding.UTF8.GetChars(topicUtf8));
// read QoS level from fixed header
msg.qosLevel = (byte)((fixedHeaderFirstByte & QOS_LEVEL_MASK) >> QOS_LEVEL_OFFSET);
// read DUP flag from fixed header
msg.dupFlag = (((fixedHeaderFirstByte & DUP_FLAG_MASK) >> DUP_FLAG_OFFSET) == 0x01);
// read retain flag from fixed header
msg.retain = (((fixedHeaderFirstByte & RETAIN_FLAG_MASK) >> RETAIN_FLAG_OFFSET) == 0x01);
// message id is valid only with QOS level 1 or QOS level 2
if ((msg.qosLevel == QOS_LEVEL_AT_LEAST_ONCE) ||
(msg.qosLevel == QOS_LEVEL_EXACTLY_ONCE))
{
// message id
msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00);
msg.messageId |= (buffer[index++]);
}
// get payload with message data
int messageSize = remainingLength - index;
int remaining = messageSize;
int messageOffset = 0;
msg.message = new byte[messageSize];
// BUG FIX 26/07/2013 : receiving large payload
// copy first part of payload data received
Array.Copy(buffer, index, msg.message, messageOffset, received - index);
remaining -= (received - index);
messageOffset += (received - index);
// if payload isn't finished
while (remaining > 0)
{
// receive other payload data
received = channel.Receive(buffer);
Array.Copy(buffer, 0, msg.message, messageOffset, received);
remaining -= received;
messageOffset += received;
}
return msg;
}
}
}

View File

@ -0,0 +1,113 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3)
using System;
#else
using Microsoft.SPOT;
#endif
namespace uPLibrary.Networking.M2Mqtt.Messages
{
/// <summary>
/// Event Args class for PUBLISH message received from broker
/// </summary>
public class MqttMsgPublishEventArgs : EventArgs
{
#region Properties...
/// <summary>
/// Message topic
/// </summary>
public string Topic
{
get { return this.topic; }
internal set { this.topic = value; }
}
/// <summary>
/// Message data
/// </summary>
public byte[] Message
{
get { return this.message; }
internal set { this.message = value; }
}
/// <summary>
/// Duplicate message flag
/// </summary>
public bool DupFlag
{
get { return this.dupFlag; }
set { this.dupFlag = value; }
}
/// <summary>
/// Quality of Service level
/// </summary>
public byte QosLevel
{
get { return this.qosLevel; }
internal set { this.qosLevel = value; }
}
/// <summary>
/// Retain message flag
/// </summary>
public bool Retain
{
get { return this.retain; }
internal set { this.retain = value; }
}
#endregion
// message topic
private string topic;
// message data
private byte[] message;
// duplicate delivery
private bool dupFlag;
// quality of service level
private byte qosLevel;
// retain flag
private bool retain;
/// <summary>
/// Constructor
/// </summary>
/// <param name="topic">Message topic</param>
/// <param name="message">Message data</param>
/// <param name="dupFlag">Duplicate delivery flag</param>
/// <param name="qosLevel">Quality of Service level</param>
/// <param name="retain">Retain flag</param>
public MqttMsgPublishEventArgs(string topic,
byte[] message,
bool dupFlag,
byte qosLevel,
bool retain)
{
this.topic = topic;
this.message = message;
this.dupFlag = dupFlag;
this.qosLevel = qosLevel;
this.retain = retain;
}
}
}

View File

@ -0,0 +1,57 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3)
using System;
#else
using Microsoft.SPOT;
#endif
namespace uPLibrary.Networking.M2Mqtt.Messages
{
/// <summary>
/// Event Args class for published message
/// </summary>
public class MqttMsgPublishedEventArgs : EventArgs
{
#region Properties...
/// <summary>
/// Message identifier
/// </summary>
public ushort MessageId
{
get { return this.messageId; }
internal set { this.messageId = value; }
}
#endregion
// message identifier
ushort messageId;
/// <summary>
/// Constructor
/// </summary>
/// <param name="messageId">Message identifier published</param>
public MqttMsgPublishedEventArgs(ushort messageId)
{
this.messageId = messageId;
}
}
}

View File

@ -0,0 +1,118 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
namespace uPLibrary.Networking.M2Mqtt.Messages
{
/// <summary>
/// Class for PUBREC message from broker to client
/// </summary>
public class MqttMsgPubrec : MqttMsgBase
{
#region Properties...
/// <summary>
/// Message identifier for the acknowledged publish message
/// </summary>
public ushort MessageId
{
get { return this.messageId; }
set { this.messageId = value; }
}
#endregion
// message identifier
private ushort messageId;
/// <summary>
/// Constructor
/// </summary>
public MqttMsgPubrec()
{
this.type = MQTT_MSG_PUBREC_TYPE;
}
public override byte[] GetBytes()
{
int fixedHeaderSize = 0;
int varHeaderSize = 0;
int payloadSize = 0;
int remainingLength = 0;
byte[] buffer;
int index = 0;
// message identifier
varHeaderSize += MESSAGE_ID_SIZE;
remainingLength += (varHeaderSize + payloadSize);
// first byte of fixed header
fixedHeaderSize = 1;
int temp = remainingLength;
// increase fixed header size based on remaining length
// (each remaining length byte can encode until 128)
do
{
fixedHeaderSize++;
temp = temp / 128;
} while (temp > 0);
// allocate buffer for message
buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
// first fixed header byte
buffer[index++] = (MQTT_MSG_PUBREC_TYPE << MSG_TYPE_OFFSET);
// encode remaining length
index = this.encodeRemainingLength(remainingLength, buffer, index);
// get message identifier
buffer[index++] = (byte)((this.messageId >> 8) & 0x00FF); // MSB
buffer[index++] = (byte)(this.messageId & 0x00FF); // LSB
return buffer;
}
/// <summary>
/// Parse bytes for a PUBREC message
/// </summary>
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
/// <param name="channel">Channel connected to the broker</param>
/// <returns>PUBREC message instance</returns>
public static MqttMsgPubrec Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
{
byte[] buffer;
int index = 0;
MqttMsgPubrec msg = new MqttMsgPubrec();
// get remaining length and allocate buffer
int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
buffer = new byte[remainingLength];
// read bytes from socket...
channel.Receive(buffer);
// message id
msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00);
msg.messageId |= (buffer[index++]);
return msg;
}
}
}

View File

@ -0,0 +1,128 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
namespace uPLibrary.Networking.M2Mqtt.Messages
{
/// <summary>
/// Class for PUBREL message from client top broker
/// </summary>
public class MqttMsgPubrel : MqttMsgBase
{
#region Properties...
/// <summary>
/// Message identifier for the acknowledged publish message
/// </summary>
public ushort MessageId
{
get { return this.messageId; }
set { this.messageId = value; }
}
#endregion
// message identifier
private ushort messageId;
/// <summary>
/// Constructor
/// </summary>
public MqttMsgPubrel()
{
this.type = MQTT_MSG_PUBREL_TYPE;
// PUBREL message use QoS Level 1
this.qosLevel = QOS_LEVEL_AT_LEAST_ONCE;
}
public override byte[] GetBytes()
{
int fixedHeaderSize = 0;
int varHeaderSize = 0;
int payloadSize = 0;
int remainingLength = 0;
byte[] buffer;
int index = 0;
// message identifier
varHeaderSize += MESSAGE_ID_SIZE;
remainingLength += (varHeaderSize + payloadSize);
// first byte of fixed header
fixedHeaderSize = 1;
int temp = remainingLength;
// increase fixed header size based on remaining length
// (each remaining length byte can encode until 128)
do
{
fixedHeaderSize++;
temp = temp / 128;
} while (temp > 0);
// allocate buffer for message
buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
// first fixed header byte
buffer[index] = (byte)((MQTT_MSG_PUBREL_TYPE << MSG_TYPE_OFFSET) |
(this.qosLevel << QOS_LEVEL_OFFSET));
buffer[index] |= this.dupFlag ? (byte)(1 << DUP_FLAG_OFFSET) : (byte)0x00;
index++;
// encode remaining length
index = this.encodeRemainingLength(remainingLength, buffer, index);
// get next message identifier
buffer[index++] = (byte)((this.messageId >> 8) & 0x00FF); // MSB
buffer[index++] = (byte)(this.messageId & 0x00FF); // LSB
return buffer;
}
/// <summary>
/// Parse bytes for a PUBREL message
/// </summary>
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
/// <param name="channel">Channel connected to the broker</param>
/// <returns>PUBREL message instance</returns>
public static MqttMsgPubrel Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
{
byte[] buffer;
int index = 0;
MqttMsgPubrel msg = new MqttMsgPubrel();
// get remaining length and allocate buffer
int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
buffer = new byte[remainingLength];
// read bytes from socket...
channel.Receive(buffer);
// read QoS level from fixed header (would be QoS Level 1)
msg.qosLevel = (byte)((fixedHeaderFirstByte & QOS_LEVEL_MASK) >> QOS_LEVEL_OFFSET);
// read DUP flag from fixed header
msg.dupFlag = (((fixedHeaderFirstByte & DUP_FLAG_MASK) >> DUP_FLAG_OFFSET) == 0x01);
// message id
msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00);
msg.messageId |= (buffer[index++]);
return msg;
}
}
}

View File

@ -0,0 +1,153 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
using System;
namespace uPLibrary.Networking.M2Mqtt.Messages
{
/// <summary>
/// Class for SUBACK message from broker to client
/// </summary>
public class MqttMsgSuback : MqttMsgBase
{
#region Properties...
/// <summary>
/// Message identifier for the subscribe message
/// that is acknowledged
/// </summary>
public ushort MessageId
{
get { return this.messageId; }
set { this.messageId = value; }
}
/// <summary>
/// List of granted QOS Levels
/// </summary>
public byte[] GrantedQoSLevels
{
get { return this.grantedQosLevels; }
set { this.grantedQosLevels = value; }
}
#endregion
// message identifier
private ushort messageId;
// granted QOS levels
byte[] grantedQosLevels;
/// <summary>
/// Constructor
/// </summary>
public MqttMsgSuback()
{
this.type = MQTT_MSG_SUBACK_TYPE;
}
/// <summary>
/// Parse bytes for a SUBACK message
/// </summary>
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
/// <param name="channel">Channel connected to the broker</param>
/// <returns>SUBACK message instance</returns>
public static MqttMsgSuback Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
{
byte[] buffer;
int index = 0;
MqttMsgSuback msg = new MqttMsgSuback();
// get remaining length and allocate buffer
int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
buffer = new byte[remainingLength];
// read bytes from socket...
channel.Receive(buffer);
// message id
msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00);
msg.messageId |= (buffer[index++]);
// payload contains QoS levels granted
msg.grantedQosLevels = new byte[remainingLength - MESSAGE_ID_SIZE];
int qosIdx = 0;
do
{
msg.grantedQosLevels[qosIdx++] = buffer[index++];
} while (index < remainingLength);
return msg;
}
public override byte[] GetBytes()
{
int fixedHeaderSize = 0;
int varHeaderSize = 0;
int payloadSize = 0;
int remainingLength = 0;
byte[] buffer;
int index = 0;
// message identifier
varHeaderSize += MESSAGE_ID_SIZE;
int grantedQosIdx = 0;
for (grantedQosIdx = 0; grantedQosIdx < this.grantedQosLevels.Length; grantedQosIdx++)
{
payloadSize++;
}
remainingLength += (varHeaderSize + payloadSize);
// first byte of fixed header
fixedHeaderSize = 1;
int temp = remainingLength;
// increase fixed header size based on remaining length
// (each remaining length byte can encode until 128)
do
{
fixedHeaderSize++;
temp = temp / 128;
} while (temp > 0);
// allocate buffer for message
buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
// first fixed header byte
buffer[index] = (byte)(MQTT_MSG_SUBACK_TYPE << MSG_TYPE_OFFSET);
index++;
// encode remaining length
index = this.encodeRemainingLength(remainingLength, buffer, index);
// message id
buffer[index++] = (byte)((this.messageId >> 8) & 0x00FF); // MSB
buffer[index++] = (byte)(this.messageId & 0x00FF); // LSB
// payload contains QoS levels granted
for (grantedQosIdx = 0; grantedQosIdx < this.grantedQosLevels.Length; grantedQosIdx++)
{
buffer[index++] = this.grantedQosLevels[grantedQosIdx];
}
return buffer;
}
}
}

View File

@ -0,0 +1,255 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
using System;
// if NOT .Net Micro Framework
#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3)
using System.Collections.Generic;
#endif
using System.Collections;
using System.Text;
using uPLibrary.Networking.M2Mqtt.Exceptions;
namespace uPLibrary.Networking.M2Mqtt.Messages
{
/// <summary>
/// Class for SUBSCRIBE message from client to broker
/// </summary>
public class MqttMsgSubscribe : MqttMsgBase
{
#region Properties...
/// <summary>
/// List of topics to subscribe
/// </summary>
public string[] Topics
{
get { return this.topics; }
set { this.topics = value; }
}
/// <summary>
/// List of QOS Levels related to topics
/// </summary>
public byte[] QoSLevels
{
get { return this.qosLevels; }
set { this.qosLevels = value; }
}
/// <summary>
/// Message identifier
/// </summary>
public ushort MessageId
{
get { return this.messageId; }
set { this.messageId = value; }
}
#endregion
// topics to subscribe
string[] topics;
// QOS levels related to topics
byte[] qosLevels;
// message identifier
ushort messageId;
/// <summary>
/// Constructor
/// </summary>
public MqttMsgSubscribe()
{
this.type = MQTT_MSG_SUBSCRIBE_TYPE;
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="topics">List of topics to subscribe</param>
/// <param name="qosLevels">List of QOS Levels related to topics</param>
public MqttMsgSubscribe(string[] topics, byte[] qosLevels)
{
this.type = MQTT_MSG_SUBSCRIBE_TYPE;
this.topics = topics;
this.qosLevels = qosLevels;
// SUBSCRIBE message uses QoS Level 1
this.qosLevel = QOS_LEVEL_AT_LEAST_ONCE;
}
/// <summary>
/// Parse bytes for a SUBSCRIBE message
/// </summary>
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
/// <param name="channel">Channel connected to the broker</param>
/// <returns>SUBSCRIBE message instance</returns>
public static MqttMsgSubscribe Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
{
byte[] buffer;
int index = 0;
byte[] topicUtf8;
int topicUtf8Length;
MqttMsgSubscribe msg = new MqttMsgSubscribe();
// get remaining length and allocate buffer
int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
buffer = new byte[remainingLength];
// read bytes from socket...
int received = channel.Receive(buffer);
// read QoS level from fixed header
msg.qosLevel = (byte)((fixedHeaderFirstByte & QOS_LEVEL_MASK) >> QOS_LEVEL_OFFSET);
// read DUP flag from fixed header
msg.dupFlag = (((fixedHeaderFirstByte & DUP_FLAG_MASK) >> DUP_FLAG_OFFSET) == 0x01);
// retain flag not used
msg.retain = false;
// message id
msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00);
msg.messageId |= (buffer[index++]);
// payload contains topics and QoS levels
// NOTE : before, I don't know how many topics will be in the payload (so use List)
// if .Net Micro Framework
#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3)
IList tmpTopics = new ArrayList();
IList tmpQosLevels = new ArrayList();
// else other frameworks (.Net, .Net Compact, Mono, Windows Phone)
#else
IList<String> tmpTopics = new List<String>();
IList<byte> tmpQosLevels = new List<byte>();
#endif
do
{
// topic name
topicUtf8Length = ((buffer[index++] << 8) & 0xFF00);
topicUtf8Length |= buffer[index++];
topicUtf8 = new byte[topicUtf8Length];
Array.Copy(buffer, index, topicUtf8, 0, topicUtf8Length);
index += topicUtf8Length;
tmpTopics.Add(new String(Encoding.UTF8.GetChars(topicUtf8)));
// QoS level
tmpQosLevels.Add(buffer[index++]);
} while (index < remainingLength);
// copy from list to array
msg.topics = new string[tmpTopics.Count];
msg.qosLevels = new byte[tmpQosLevels.Count];
for (int i = 0; i < tmpTopics.Count; i++)
{
msg.topics[i] = (string)tmpTopics[i];
msg.qosLevels[i] = (byte)tmpQosLevels[i];
}
return msg;
}
public override byte[] GetBytes()
{
int fixedHeaderSize = 0;
int varHeaderSize = 0;
int payloadSize = 0;
int remainingLength = 0;
byte[] buffer;
int index = 0;
// topics list empty
if ((this.topics == null) || (this.topics.Length == 0))
throw new MqttClientException(MqttClientErrorCode.TopicsEmpty);
// qos levels list empty
if ((this.qosLevels == null) || (this.qosLevels.Length == 0))
throw new MqttClientException(MqttClientErrorCode.QosLevelsEmpty);
// topics and qos levels lists length don't match
if (this.topics.Length != this.qosLevels.Length)
throw new MqttClientException(MqttClientErrorCode.TopicsQosLevelsNotMatch);
// message identifier
varHeaderSize += MESSAGE_ID_SIZE;
int topicIdx = 0;
byte[][] topicsUtf8 = new byte[this.topics.Length][];
for (topicIdx = 0; topicIdx < this.topics.Length; topicIdx++)
{
// check topic length
if ((this.topics[topicIdx].Length < MIN_TOPIC_LENGTH) || (this.topics[topicIdx].Length > MAX_TOPIC_LENGTH))
throw new MqttClientException(MqttClientErrorCode.TopicLength);
topicsUtf8[topicIdx] = Encoding.UTF8.GetBytes(this.topics[topicIdx]);
payloadSize += 2; // topic size (MSB, LSB)
payloadSize += topicsUtf8[topicIdx].Length;
payloadSize++; // byte for QoS
}
remainingLength += (varHeaderSize + payloadSize);
// first byte of fixed header
fixedHeaderSize = 1;
int temp = remainingLength;
// increase fixed header size based on remaining length
// (each remaining length byte can encode until 128)
do
{
fixedHeaderSize++;
temp = temp / 128;
} while (temp > 0);
// allocate buffer for message
buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
// first fixed header byte
buffer[index] = (byte)((MQTT_MSG_SUBSCRIBE_TYPE << MSG_TYPE_OFFSET) |
(this.qosLevel << QOS_LEVEL_OFFSET));
buffer[index] |= this.dupFlag ? (byte)(1 << DUP_FLAG_OFFSET) : (byte)0x00;
index++;
// encode remaining length
index = this.encodeRemainingLength(remainingLength, buffer, index);
// check message identifier assigned (SUBSCRIBE uses QoS Level 1, so message id is mandatory)
if (this.messageId == 0)
throw new MqttClientException(MqttClientErrorCode.WrongMessageId);
buffer[index++] = (byte)((messageId >> 8) & 0x00FF); // MSB
buffer[index++] = (byte)(messageId & 0x00FF); // LSB
topicIdx = 0;
for (topicIdx = 0; topicIdx < this.topics.Length; topicIdx++)
{
// topic name
buffer[index++] = (byte)((topicsUtf8[topicIdx].Length >> 8) & 0x00FF); // MSB
buffer[index++] = (byte)(topicsUtf8[topicIdx].Length & 0x00FF); // LSB
Array.Copy(topicsUtf8[topicIdx], 0, buffer, index, topicsUtf8[topicIdx].Length);
index += topicsUtf8[topicIdx].Length;
// requested QoS
buffer[index++] = this.qosLevels[topicIdx];
}
return buffer;
}
}
}

View File

@ -0,0 +1,83 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3)
using System;
#else
using Microsoft.SPOT;
#endif
namespace uPLibrary.Networking.M2Mqtt.Messages
{
/// <summary>
/// Event Args class for subscribe request on topics
/// </summary>
public class MqttMsgSubscribeEventArgs : EventArgs
{
#region Properties...
/// <summary>
/// Message identifier
/// </summary>
public ushort MessageId
{
get { return this.messageId; }
internal set { this.messageId = value; }
}
/// <summary>
/// Topics requested to subscribe
/// </summary>
public string[] Topics
{
get { return this.topics; }
internal set { this.topics = value; }
}
/// <summary>
/// List of QOS Levels requested
/// </summary>
public byte[] QoSLevels
{
get { return this.qosLevels; }
internal set { this.qosLevels = value; }
}
#endregion
// message identifier
ushort messageId;
// topics requested to subscribe
string[] topics;
// QoS levels requested
byte[] qosLevels;
/// <summary>
/// Constructor
/// </summary>
/// <param name="messageId">Message identifier for subscribe topics request</param>
/// <param name="topics">Topics requested to subscribe</param>
/// <param name="qosLevels">List of QOS Levels requested</param>
public MqttMsgSubscribeEventArgs(ushort messageId, string[] topics, byte[] qosLevels)
{
this.messageId = messageId;
this.topics = topics;
this.qosLevels = qosLevels;
}
}
}

View File

@ -0,0 +1,70 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3)
using System;
#else
using Microsoft.SPOT;
#endif
namespace uPLibrary.Networking.M2Mqtt.Messages
{
/// <summary>
/// Event Args class for subscribed topics
/// </summary>
public class MqttMsgSubscribedEventArgs : EventArgs
{
#region Properties...
/// <summary>
/// Message identifier
/// </summary>
public ushort MessageId
{
get { return this.messageId; }
internal set { this.messageId = value; }
}
/// <summary>
/// List of granted QOS Levels
/// </summary>
public byte[] GrantedQoSLevels
{
get { return this.grantedQosLevels; }
internal set { this.grantedQosLevels = value; }
}
#endregion
// message identifier
ushort messageId;
// granted QOS levels
byte[] grantedQosLevels;
/// <summary>
/// Constructor
/// </summary>
/// <param name="messageId">Message identifier for subscribed topics</param>
/// <param name="grantedQosLevels">List of granted QOS Levels</param>
public MqttMsgSubscribedEventArgs(ushort messageId, byte[] grantedQosLevels)
{
this.messageId = messageId;
this.grantedQosLevels = grantedQosLevels;
}
}
}

View File

@ -0,0 +1,122 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
using System;
namespace uPLibrary.Networking.M2Mqtt.Messages
{
/// <summary>
/// Class for UNSUBACK message from broker to client
/// </summary>
public class MqttMsgUnsuback : MqttMsgBase
{
#region Properties...
/// <summary>
/// Message identifier for the unsubscribe message
/// that is acknowledged
/// </summary>
public ushort MessageId
{
get { return this.messageId; }
set { this.messageId = value; }
}
#endregion
// message identifier
private ushort messageId;
/// <summary>
/// Constructor
/// </summary>
public MqttMsgUnsuback()
{
this.type = MQTT_MSG_UNSUBACK_TYPE;
}
/// <summary>
/// Parse bytes for a UNSUBACK message
/// </summary>
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
/// <param name="channel">Channel connected to the broker</param>
/// <returns>UNSUBACK message instance</returns>
public static MqttMsgUnsuback Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
{
byte[] buffer;
int index = 0;
MqttMsgUnsuback msg = new MqttMsgUnsuback();
// get remaining length and allocate buffer
int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
buffer = new byte[remainingLength];
// read bytes from socket...
channel.Receive(buffer);
// message id
msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00);
msg.messageId |= (buffer[index++]);
return msg;
}
public override byte[] GetBytes()
{
int fixedHeaderSize = 0;
int varHeaderSize = 0;
int payloadSize = 0;
int remainingLength = 0;
byte[] buffer;
int index = 0;
// message identifier
varHeaderSize += MESSAGE_ID_SIZE;
remainingLength += (varHeaderSize + payloadSize);
// first byte of fixed header
fixedHeaderSize = 1;
int temp = remainingLength;
// increase fixed header size based on remaining length
// (each remaining length byte can encode until 128)
do
{
fixedHeaderSize++;
temp = temp / 128;
} while (temp > 0);
// allocate buffer for message
buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
// first fixed header byte
buffer[index] = (byte)(MQTT_MSG_UNSUBACK_TYPE << MSG_TYPE_OFFSET);
index++;
// encode remaining length
index = this.encodeRemainingLength(remainingLength, buffer, index);
// message id
buffer[index++] = (byte)((this.messageId >> 8) & 0x00FF); // MSB
buffer[index++] = (byte)(this.messageId & 0x00FF); // LSB
return buffer;
}
}
}

View File

@ -0,0 +1,222 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
using System;
// if NOT .Net Micro Framework
#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3)
using System.Collections.Generic;
#endif
using System.Collections;
using System.Text;
using uPLibrary.Networking.M2Mqtt.Exceptions;
namespace uPLibrary.Networking.M2Mqtt.Messages
{
/// <summary>
/// Class for UNSUBSCRIBE message from client to broker
/// </summary>
public class MqttMsgUnsubscribe : MqttMsgBase
{
#region Properties...
/// <summary>
/// List of topics to unsubscribe
/// </summary>
public string[] Topics
{
get { return this.topics; }
set { this.topics = value; }
}
/// <summary>
/// Message identifier
/// </summary>
public ushort MessageId
{
get { return this.messageId; }
set { this.messageId = value; }
}
#endregion
// topics to unsubscribe
string[] topics;
// message identifier
ushort messageId;
/// <summary>
/// Constructor
/// </summary>
public MqttMsgUnsubscribe()
{
this.type = MQTT_MSG_UNSUBSCRIBE_TYPE;
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="topics">List of topics to unsubscribe</param>
public MqttMsgUnsubscribe(string[] topics)
{
this.type = MQTT_MSG_UNSUBSCRIBE_TYPE;
this.topics = topics;
// UNSUBSCRIBE message uses QoS Level 1
this.qosLevel = QOS_LEVEL_AT_LEAST_ONCE;
}
/// <summary>
/// Parse bytes for a UNSUBSCRIBE message
/// </summary>
/// <param name="fixedHeaderFirstByte">First fixed header byte</param>
/// <param name="channel">Channel connected to the broker</param>
/// <returns>UNSUBSCRIBE message instance</returns>
public static MqttMsgUnsubscribe Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel)
{
byte[] buffer;
int index = 0;
byte[] topicUtf8;
int topicUtf8Length;
MqttMsgUnsubscribe msg = new MqttMsgUnsubscribe();
// get remaining length and allocate buffer
int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
buffer = new byte[remainingLength];
// read bytes from socket...
int received = channel.Receive(buffer);
// read QoS level from fixed header
msg.qosLevel = (byte)((fixedHeaderFirstByte & QOS_LEVEL_MASK) >> QOS_LEVEL_OFFSET);
// read DUP flag from fixed header
msg.dupFlag = (((fixedHeaderFirstByte & DUP_FLAG_MASK) >> DUP_FLAG_OFFSET) == 0x01);
// retain flag not used
msg.retain = false;
// message id
msg.messageId = (ushort)((buffer[index++] << 8) & 0xFF00);
msg.messageId |= (buffer[index++]);
// payload contains topics
// NOTE : before, I don't know how many topics will be in the payload (so use List)
// if .Net Micro Framework
#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3)
IList tmpTopics = new ArrayList();
// else other frameworks (.Net, .Net Compact, Mono, Windows Phone)
#else
IList<String> tmpTopics = new List<String>();
#endif
do
{
// topic name
topicUtf8Length = ((buffer[index++] << 8) & 0xFF00);
topicUtf8Length |= buffer[index++];
topicUtf8 = new byte[topicUtf8Length];
Array.Copy(buffer, index, topicUtf8, 0, topicUtf8Length);
index += topicUtf8Length;
tmpTopics.Add(new String(Encoding.UTF8.GetChars(topicUtf8)));
} while (index < remainingLength);
// copy from list to array
msg.topics = new string[tmpTopics.Count];
for (int i = 0; i < tmpTopics.Count; i++)
{
msg.topics[i] = (string)tmpTopics[i];
}
return msg;
}
public override byte[] GetBytes()
{
int fixedHeaderSize = 0;
int varHeaderSize = 0;
int payloadSize = 0;
int remainingLength = 0;
byte[] buffer;
int index = 0;
// topics list empty
if ((this.topics == null) || (this.topics.Length == 0))
throw new MqttClientException(MqttClientErrorCode.TopicsEmpty);
// message identifier
varHeaderSize += MESSAGE_ID_SIZE;
int topicIdx = 0;
byte[][] topicsUtf8 = new byte[this.topics.Length][];
for (topicIdx = 0; topicIdx < this.topics.Length; topicIdx++)
{
// check topic length
if ((this.topics[topicIdx].Length < MIN_TOPIC_LENGTH) || (this.topics[topicIdx].Length > MAX_TOPIC_LENGTH))
throw new MqttClientException(MqttClientErrorCode.TopicLength);
topicsUtf8[topicIdx] = Encoding.UTF8.GetBytes(this.topics[topicIdx]);
payloadSize += 2; // topic size (MSB, LSB)
payloadSize += topicsUtf8[topicIdx].Length;
}
remainingLength += (varHeaderSize + payloadSize);
// first byte of fixed header
fixedHeaderSize = 1;
int temp = remainingLength;
// increase fixed header size based on remaining length
// (each remaining length byte can encode until 128)
do
{
fixedHeaderSize++;
temp = temp / 128;
} while (temp > 0);
// allocate buffer for message
buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
// first fixed header byte
buffer[index] = (byte)((MQTT_MSG_UNSUBSCRIBE_TYPE << MSG_TYPE_OFFSET) |
(this.qosLevel << QOS_LEVEL_OFFSET));
buffer[index] |= this.dupFlag ? (byte)(1 << DUP_FLAG_OFFSET) : (byte)0x00;
index++;
// encode remaining length
index = this.encodeRemainingLength(remainingLength, buffer, index);
// check message identifier assigned
if (this.messageId == 0)
throw new MqttClientException(MqttClientErrorCode.WrongMessageId);
buffer[index++] = (byte)((messageId >> 8) & 0x00FF); // MSB
buffer[index++] = (byte)(messageId & 0x00FF); // LSB
topicIdx = 0;
for (topicIdx = 0; topicIdx < this.topics.Length; topicIdx++)
{
// topic name
buffer[index++] = (byte)((topicsUtf8[topicIdx].Length >> 8) & 0x00FF); // MSB
buffer[index++] = (byte)(topicsUtf8[topicIdx].Length & 0x00FF); // LSB
Array.Copy(topicsUtf8[topicIdx], 0, buffer, index, topicsUtf8[topicIdx].Length);
index += topicsUtf8[topicIdx].Length;
}
return buffer;
}
}
}

View File

@ -0,0 +1,70 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3)
using System;
#else
using Microsoft.SPOT;
#endif
namespace uPLibrary.Networking.M2Mqtt.Messages
{
/// <summary>
/// Event Args class for unsubscribe request on topics
/// </summary>
public class MqttMsgUnsubscribeEventArgs : EventArgs
{
#region Properties...
/// <summary>
/// Message identifier
/// </summary>
public ushort MessageId
{
get { return this.messageId; }
internal set { this.messageId = value; }
}
/// <summary>
/// Topics requested to subscribe
/// </summary>
public string[] Topics
{
get { return this.topics; }
internal set { this.topics = value; }
}
#endregion
// message identifier
ushort messageId;
// topics requested to unsubscribe
string[] topics;
/// <summary>
/// Constructor
/// </summary>
/// <param name="messageId">Message identifier for subscribed topics</param>
/// <param name="topics">Topics requested to subscribe</param>
public MqttMsgUnsubscribeEventArgs(ushort messageId, string[] topics)
{
this.messageId = messageId;
this.topics = topics;
}
}
}

View File

@ -0,0 +1,57 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3)
using System;
#else
using Microsoft.SPOT;
#endif
namespace uPLibrary.Networking.M2Mqtt.Messages
{
/// <summary>
/// Event Args class for unsubscribed topic
/// </summary>
public class MqttMsgUnsubscribedEventArgs : EventArgs
{
#region Properties...
/// <summary>
/// Message identifier
/// </summary>
public ushort MessageId
{
get { return this.messageId; }
internal set { this.messageId = value; }
}
#endregion
// message identifier
ushort messageId;
/// <summary>
/// Constructor
/// </summary>
/// <param name="messageId">Message identifier for unsubscribed topic</param>
public MqttMsgUnsubscribedEventArgs(ushort messageId)
{
this.messageId = messageId;
}
}
}

1999
mqtt/M2Mqtt/MqttClient.cs Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,272 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
#if SSL
#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3)
using Microsoft.SPOT.Net.Security;
#else
using System.Net.Security;
using System.Security.Authentication;
#endif
#endif
using System.Net.Sockets;
using System.Net;
using System.Security.Cryptography.X509Certificates;
namespace uPLibrary.Networking.M2Mqtt
{
/// <summary>
/// Channel to communicate over the network
/// </summary>
public class MqttNetworkChannel : IMqttNetworkChannel
{
// remote host information
private string remoteHostName;
private IPAddress remoteIpAddress;
private int remotePort;
// socket for communication
private Socket socket;
// using SSL
private bool secure;
// CA certificate
private X509Certificate caCert;
/// <summary>
/// Remote host name
/// </summary>
public string RemoteHostName { get { return this.remoteHostName; } }
/// <summary>
/// Remote IP address
/// </summary>
public IPAddress RemoteIpAddress { get { return this.remoteIpAddress; } }
/// <summary>
/// Remote port
/// </summary>
public int RemotePort { get { return this.remotePort; } }
#if SSL
// SSL stream
private SslStream sslStream;
#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3)
private NetworkStream netStream;
#endif
#endif
/// <summary>
/// Data available on the channel
/// </summary>
public bool DataAvailable
{
get
{
#if SSL
#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3)
if (secure)
return this.sslStream.DataAvailable;
else
return (this.socket.Available > 0);
#else
if (secure)
return this.netStream.DataAvailable;
else
return (this.socket.Available > 0);
#endif
#else
return (this.socket.Available > 0);
#endif
}
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="socket">Socket opened with the client</param>
public MqttNetworkChannel(Socket socket)
{
this.socket = socket;
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="remoteHostName">Remote Host name</param>
/// <param name="remoteIpAddress">Remote IP address</param>
/// <param name="remotePort">Remote port</param>
public MqttNetworkChannel(string remoteHostName, IPAddress remoteIpAddress, int remotePort) :
this(remoteHostName, remoteIpAddress, remotePort, false, null)
{
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="remoteHostName">Remote Host name</param>
/// <param name="remoteIpAddress">Remote IP address</param>
/// <param name="remotePort">Remote port</param>
/// <param name="secure">Using SSL</param>
/// <param name="caCert">CA certificate</param>
public MqttNetworkChannel(string remoteHostName, IPAddress remoteIpAddress, int remotePort, bool secure, X509Certificate caCert)
{
this.remoteHostName = remoteHostName;
this.remoteIpAddress = remoteIpAddress;
this.remotePort = remotePort;
this.secure = secure;
this.caCert = caCert;
}
/// <summary>
/// Connect to remote server
/// </summary>
public void Connect()
{
this.socket = new Socket(IPAddressUtility.GetAddressFamily(this.remoteIpAddress), SocketType.Stream, ProtocolType.Tcp);
// try connection to the broker
this.socket.Connect(new IPEndPoint(this.remoteIpAddress, this.remotePort));
#if SSL
// secure channel requested
if (secure)
{
// create SSL stream
#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3)
this.sslStream = new SslStream(this.socket);
#else
this.netStream = new NetworkStream(this.socket);
this.sslStream = new SslStream(this.netStream);
#endif
// server authentication (SSL/TLS handshake)
#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3)
this.sslStream.AuthenticateAsClient(this.remoteHostName,
null,
new X509Certificate[] { this.caCert },
SslVerification.CertificateRequired,
SslProtocols.TLSv1);
#else
this.sslStream.AuthenticateAsClient(this.remoteHostName,
new X509CertificateCollection(new X509Certificate[] { this.caCert }),
SslProtocols.Tls, false);
#endif
}
#endif
}
/// <summary>
/// Send data on the network channel
/// </summary>
/// <param name="buffer">Data buffer to send</param>
/// <returns>Number of byte sent</returns>
public int Send(byte[] buffer)
{
#if SSL
if (this.secure)
{
this.sslStream.Write(buffer, 0, buffer.Length);
return buffer.Length;
}
else
return this.socket.Send(buffer, 0, buffer.Length, SocketFlags.None);
#else
return this.socket.Send(buffer, 0, buffer.Length, SocketFlags.None);
#endif
}
/// <summary>
/// Receive data from the network
/// </summary>
/// <param name="buffer">Data buffer for receiving data</param>
/// <returns>Number of bytes received</returns>
public int Receive(byte[] buffer)
{
#if SSL
if (this.secure)
{
// read all data needed (until fill buffer)
int idx = 0;
while (idx < buffer.Length)
{
idx += this.sslStream.Read(buffer, idx, buffer.Length - idx);
}
return buffer.Length;
}
else
{
// read all data needed (until fill buffer)
int idx = 0;
while (idx < buffer.Length)
{
idx += this.socket.Receive(buffer, idx, buffer.Length - idx, SocketFlags.None);
}
return buffer.Length;
}
#else
// read all data needed (until fill buffer)
int idx = 0;
while (idx < buffer.Length)
{
idx += this.socket.Receive(buffer, idx, buffer.Length - idx, SocketFlags.None);
}
return buffer.Length;
#endif
}
/// <summary>
/// Close the network channel
/// </summary>
public void Close()
{
#if SSL
if (this.secure)
{
#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3)
this.netStream.Close();
#endif
this.sslStream.Close();
}
this.socket.Close();
#else
this.socket.Close();
#endif
}
}
/// <summary>
/// IPAddress Utility class
/// </summary>
public static class IPAddressUtility
{
/// <summary>
/// Return AddressFamily for the IP address
/// </summary>
/// <param name="ipAddress">IP address to check</param>
/// <returns>Address family</returns>
public static AddressFamily GetAddressFamily(IPAddress ipAddress)
{
#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3)
return ipAddress.AddressFamily;
#else
return (ipAddress.ToString().IndexOf(':') != -1) ?
AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork;
#endif
}
}
}

View File

@ -0,0 +1,98 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
namespace uPLibrary.Networking.M2Mqtt
{
/// <summary>
/// Settings class for the MQTT broker
/// </summary>
public class MqttSettings
{
// default port for MQTT protocol
public const int MQTT_BROKER_DEFAULT_PORT = 1883;
public const int MQTT_BROKER_DEFAULT_SSL_PORT = 8883;
// default timeout on receiving from client
public const int MQTT_DEFAULT_TIMEOUT = 25000;
// max publish, subscribe and unsubscribe retry for QoS Level 1 or 2
public const int MQTT_ATTEMPTS_RETRY = 3;
// delay for retry publish, subscribe and unsubscribe for QoS Level 1 or 2
public const int MQTT_DELAY_RETRY = 10000;
// broker need to receive the first message (CONNECT)
// within a reasonable amount of time after TCP/IP connection
public const int MQTT_CONNECT_TIMEOUT = 25000;
/// <summary>
/// Listening connection port
/// </summary>
public int Port { get; internal set; }
/// <summary>
/// Listening connection SSL port
/// </summary>
public int SslPort { get; internal set; }
/// <summary>
/// Timeout on client connection (before receiving CONNECT message)
/// </summary>
public int TimeoutOnConnection { get; internal set; }
/// <summary>
/// Timeout on receiving
/// </summary>
public int TimeoutOnReceiving { get; internal set; }
/// <summary>
/// Attempts on retry
/// </summary>
public int AttemptsOnRetry { get; internal set; }
/// <summary>
/// Delay on retry
/// </summary>
public int DelayOnRetry { get; internal set; }
/// <summary>
/// Singleton instance of settings
/// </summary>
public static MqttSettings Instance
{
get
{
if (instance == null)
instance = new MqttSettings();
return instance;
}
}
// singleton instance
private static MqttSettings instance;
/// <summary>
/// Constructor
/// </summary>
private MqttSettings()
{
this.Port = MQTT_BROKER_DEFAULT_PORT;
this.SslPort = MQTT_BROKER_DEFAULT_SSL_PORT;
this.TimeoutOnReceiving = MQTT_DEFAULT_TIMEOUT;
this.AttemptsOnRetry = MQTT_ATTEMPTS_RETRY;
this.DelayOnRetry = MQTT_DELAY_RETRY;
this.TimeoutOnConnection = MQTT_CONNECT_TIMEOUT;
}
}
}

View File

@ -0,0 +1,46 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("M2Mqtt")]
[assembly: AssemblyDescription("MQTT Client Library for M2M communication")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Paolo Patierno")]
[assembly: AssemblyProduct("M2Mqtt")]
[assembly: AssemblyCopyright("Copyright © Paolo Patierno 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
[assembly: AssemblyVersion("3.3.0.0")]
// to avoid compilation error (AssemblyFileVersionAttribute doesn't exist) under .Net CF 3.5
#if !WindowsCE
[assembly: AssemblyFileVersion("3.3.0.0")]
#endif

View File

@ -0,0 +1,52 @@
/*
M2Mqtt - MQTT Client Library for .Net
Copyright (c) 2014, Paolo Patierno, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
using System;
using System.Collections;
namespace uPLibrary.Networking.M2Mqtt
{
/// <summary>
/// Extension class for a Queue
/// </summary>
internal static class QueueExtension
{
/// <summary>
/// Predicate for searching inside a queue
/// </summary>
/// <param name="item">Item of the queue</param>
/// <returns>Result of predicate</returns>
internal delegate bool QueuePredicate(object item);
/// <summary>
/// Get (without removing) an item from queue based on predicate
/// </summary>
/// <param name="queue">Queue in which to search</param>
/// <param name="predicate">Predicate to verify to get item</param>
/// <returns>Item matches the predicate</returns>
internal static object Get(Queue queue, QueuePredicate predicate)
{
foreach (var item in queue)
{
if (predicate(item))
return item;
}
return null;
}
}
}

Binary file not shown.

View File

@ -0,0 +1,75 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<AssemblyName>M2Mqtt</AssemblyName>
<OutputType>Library</OutputType>
<RootNamespace>uPLibrary.Networking.M2Mqtt</RootNamespace>
<ProjectTypeGuids>{b69e3092-b931-443c-abe7-7e7b65f2a37f};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{F733523A-F14E-4F5A-9E7C-085CA80F52B1}</ProjectGuid>
<TargetFrameworkVersion>v4.2</TargetFrameworkVersion>
<NetMfTargetsBaseDir Condition="'$(NetMfTargetsBaseDir)'==''">$(MSBuildExtensionsPath32)\Microsoft\.NET Micro Framework\</NetMfTargetsBaseDir>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\NetMf42\</OutputPath>
<DefineConstants>DEBUG;TRACE,MF_FRAMEWORK_VERSION_V4_2,SSL</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\NetMf42\</OutputPath>
<DefineConstants>TRACE,MF_FRAMEWORK_VERSION_V4_2,SSL</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<Import Project="$(NetMfTargetsBaseDir)$(TargetFrameworkVersion)\CSharp.Targets" />
<ItemGroup>
<Compile Include="Exceptions\MqttClientException.cs" />
<Compile Include="Exceptions\MqttCommunicationException.cs" />
<Compile Include="Exceptions\MqttConnectionException.cs" />
<Compile Include="Exceptions\MqttTimeoutException.cs" />
<Compile Include="IMqttNetworkChannel.cs" />
<Compile Include="Messages\MqttMsgBase.cs" />
<Compile Include="Messages\MqttMsgConnack.cs" />
<Compile Include="Messages\MqttMsgConnect.cs" />
<Compile Include="Messages\MqttMsgConnectEventArgs.cs" />
<Compile Include="Messages\MqttMsgContext.cs" />
<Compile Include="Messages\MqttMsgDisconnect.cs" />
<Compile Include="Messages\MqttMsgPingReq.cs" />
<Compile Include="Messages\MqttMsgPingResp.cs" />
<Compile Include="Messages\MqttMsgPuback.cs" />
<Compile Include="Messages\MqttMsgPubcomp.cs" />
<Compile Include="Messages\MqttMsgPublish.cs" />
<Compile Include="Messages\MqttMsgPublishedEventArgs.cs" />
<Compile Include="Messages\MqttMsgPublishEventArgs.cs" />
<Compile Include="Messages\MqttMsgPubrec.cs" />
<Compile Include="Messages\MqttMsgPubrel.cs" />
<Compile Include="Messages\MqttMsgSuback.cs" />
<Compile Include="Messages\MqttMsgSubscribe.cs" />
<Compile Include="Messages\MqttMsgSubscribedEventArgs.cs" />
<Compile Include="Messages\MqttMsgSubscribeEventArgs.cs" />
<Compile Include="Messages\MqttMsgUnsuback.cs" />
<Compile Include="Messages\MqttMsgUnsubscribe.cs" />
<Compile Include="Messages\MqttMsgUnsubscribedEventArgs.cs" />
<Compile Include="Messages\MqttMsgUnsubscribeEventArgs.cs" />
<Compile Include="MqttClient.cs" />
<Compile Include="MqttNetworkChannel.cs" />
<Compile Include="MqttSettings.cs" />
<Compile Include="Utility\QueueExtension.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.SPOT.Native">
</Reference>
<Reference Include="Microsoft.SPOT.Net.Security" />
<Reference Include="Microsoft.SPOT.Time" />
<Reference Include="System" />
<Reference Include="System.Net.Security" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,75 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<AssemblyName>M2Mqtt</AssemblyName>
<OutputType>Library</OutputType>
<RootNamespace>uPLibrary.Networking.M2Mqtt</RootNamespace>
<ProjectTypeGuids>{b69e3092-b931-443c-abe7-7e7b65f2a37f};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{6A6D540B-8554-4FFD-8884-8BEFCCD9AD41}</ProjectGuid>
<TargetFrameworkVersion>v4.3</TargetFrameworkVersion>
<NetMfTargetsBaseDir Condition="'$(NetMfTargetsBaseDir)'==''">$(MSBuildExtensionsPath32)\Microsoft\.NET Micro Framework\</NetMfTargetsBaseDir>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\NetMf43\</OutputPath>
<DefineConstants>DEBUG;TRACE,MF_FRAMEWORK_VERSION_V4_3,SSL</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\NetMf43\</OutputPath>
<DefineConstants>TRACE,MF_FRAMEWORK_VERSION_V4_3,SSL</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<Import Project="$(NetMfTargetsBaseDir)$(TargetFrameworkVersion)\CSharp.Targets" />
<ItemGroup>
<Compile Include="Exceptions\MqttClientException.cs" />
<Compile Include="Exceptions\MqttCommunicationException.cs" />
<Compile Include="Exceptions\MqttConnectionException.cs" />
<Compile Include="Exceptions\MqttTimeoutException.cs" />
<Compile Include="IMqttNetworkChannel.cs" />
<Compile Include="Messages\MqttMsgBase.cs" />
<Compile Include="Messages\MqttMsgConnack.cs" />
<Compile Include="Messages\MqttMsgConnect.cs" />
<Compile Include="Messages\MqttMsgConnectEventArgs.cs" />
<Compile Include="Messages\MqttMsgContext.cs" />
<Compile Include="Messages\MqttMsgDisconnect.cs" />
<Compile Include="Messages\MqttMsgPingReq.cs" />
<Compile Include="Messages\MqttMsgPingResp.cs" />
<Compile Include="Messages\MqttMsgPuback.cs" />
<Compile Include="Messages\MqttMsgPubcomp.cs" />
<Compile Include="Messages\MqttMsgPublish.cs" />
<Compile Include="Messages\MqttMsgPublishedEventArgs.cs" />
<Compile Include="Messages\MqttMsgPublishEventArgs.cs" />
<Compile Include="Messages\MqttMsgPubrec.cs" />
<Compile Include="Messages\MqttMsgPubrel.cs" />
<Compile Include="Messages\MqttMsgSuback.cs" />
<Compile Include="Messages\MqttMsgSubscribe.cs" />
<Compile Include="Messages\MqttMsgSubscribedEventArgs.cs" />
<Compile Include="Messages\MqttMsgSubscribeEventArgs.cs" />
<Compile Include="Messages\MqttMsgUnsuback.cs" />
<Compile Include="Messages\MqttMsgUnsubscribe.cs" />
<Compile Include="Messages\MqttMsgUnsubscribedEventArgs.cs" />
<Compile Include="Messages\MqttMsgUnsubscribeEventArgs.cs" />
<Compile Include="MqttClient.cs" />
<Compile Include="MqttNetworkChannel.cs" />
<Compile Include="MqttSettings.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utility\QueueExtension.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.SPOT.Native">
</Reference>
<Reference Include="Microsoft.SPOT.Net.Security" />
<Reference Include="Microsoft.SPOT.Time" />
<Reference Include="System" />
<Reference Include="System.Net.Security" />
</ItemGroup>
</Project>