M2Mqtt source code

As already said, M2Mqtt library is available as open source project on GitHub. If you connect to the project web site at https://github.com/ppatierno/m2mqtt you can see all the source files inside the project itself.


M2Mqtt is also part of the Eclipse Paho project that contains all main MQTT client implementations in all main different languages (from C, C++ to Java, Python, C# and so on).


As you can see, there are two available solution files (.sln) : the main solution provides more projects files for the full .Net Framework, for .Net Micro Framework (4.2 and 4.3 version), one for .Net Compact Framework 3.9 that you can use with Windows Embedded Compact 2013 and finally for WinRT platforms like Windows 8.1, Windows Phone 8.1 and Windows 10. The Visual Studio 2008 solution is only for the .Net Compact Framework 3.5 project that you can use with Windows Embedded Compact 7. There is also a project file for Mono Project (http://www.mono-project.com) that provides support for .Net Framework on Linux using MonoDevelop (http://monodevelop.com/) as IDE.

The root folder contains following main C# source code files :

  • MqttClient.cs : it contains the class that implement the MQTT client;
  • MqttNetworkChannel.cs : it contains a wrapper class for the underling Socket or SslStream used to communicate with the broker without or with SSL/TLS protocol. It implements IMqttNetworkChannel interface;
  • IMqttNetworkChannel.cs : interface for communication channel over the network. The library has only one class (MqttNetworkChannel) that implements this interface to provide communication using TCP socket (standard for MQTT). You can develop your channel implementing this interface;
  • MqttSettings.cs : default and main settings for the client;
  • MqttSecurity.cs : some SSL/TLS related definitions;

The Messages folder contains all the classes that define the messages expected in the MQTT protocol each of which provides Parse() and GetBytes() methods for marshaling/unmarshaling messages between OO (Object Oriented) world and stream bytes on the network.

The Exceptions folder provides some files with specific exceptions related to MQTT client and problems that may occur during communication with the broker.

The Utility folder contains the QueueExtension class to provide a method to get an element from a queue not with FIFO pattern but based on a predicate. This method is needed for “inflight queue” handling and QoS levels.

Due to different assemblies needed for .Net Micro Framework 4.2 and 4.3 projects (ex. Microsoft.SPOT.*) and not present in the full .Net Framework and vice versa, the compiler uses some symbol for conditional compilation. This symbols are defined in the projects properties and are respectively MF_FRAMEWORK_VERSION_V4_2 and MF_FRAMEWORK_VERSION_V4_2; they aren’t defined for the full .Net Framework project.


There is another symbol, SSL, that is used to compile source code that uses SSL/TLS features (it is defined by default in all project files but the .Net Compact Framework because it doesn’t support SslStream class). It is useful because some .Net Micro Framework boards (like Netduino Plus) don’t have enough memory (Flash and RAM) to load assemblies related to security (cryptography and SSL/TLS support). In this case, you can delete this symbol from project properties and remove assembly references needed like Microsoft.SPOT.Net.Security and System.Net.Security for .Net Micro Framework.