Skip to content

Commit

Permalink
Add server implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
chkr1011 committed Mar 26, 2017
1 parent 18538eb commit ee25228
Show file tree
Hide file tree
Showing 110 changed files with 2,160 additions and 492 deletions.
43 changes: 18 additions & 25 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
[Xx]64/
[Xx]86/
[Bb]uild/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/

# Visual Studio 2015 cache/options directory
.vs/
Expand Down Expand Up @@ -81,7 +81,6 @@ ipch/
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb

# Visual Studio profiler
*.psess
Expand Down Expand Up @@ -140,15 +139,12 @@ publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# TODO: Comment the next line if you want to checkin your web deploy settings
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj

# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# TODO: Un-comment the next line if you do not want to checkin
# your web deploy settings because they may include unencrypted
# passwords
#*.pubxml
*.publishproj

# NuGet Packages
*.nupkg
Expand All @@ -170,11 +166,12 @@ csx/
ecf/
rcf/

# Windows Store app package directories and files
# Microsoft Azure ApplicationInsights config file
ApplicationInsights.config

# Windows Store app package directory
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt

# Visual Studio cache files
# files ending in .cache can be ignored
Expand All @@ -184,6 +181,7 @@ _pkginfo.txt

# Others
ClientBin/
[Ss]tyle[Cc]op.*
~$*
*~
*.dbmdl
Expand All @@ -193,10 +191,6 @@ ClientBin/
node_modules/
orleans.codegen.cs

# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/

# RIA/Silverlight projects
Generated_Code/

Expand Down Expand Up @@ -240,13 +234,12 @@ FakesAssemblies/
**/*.Server/ModelManifest.xml
_Pvt_Extensions

# LightSwitch generated files
GeneratedArtifacts/
ModelManifest.xml

# Paket dependency manager
.paket/paket.exe
paket-files/

# FAKE - F# Make
.fake/

# JetBrains Rider
.idea/
*.sln.iml
.fake/
23 changes: 23 additions & 0 deletions Frameworks/MQTTnet.NetCore/MQTTnet.NetCore.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<Authors>Christian Kratky</Authors>
<Company>Christian Kratky</Company>
<Product>MQTTnet</Product>
<Description>MQTTnet for .NET Core</Description>
<Copyright>Copyright © Christian Kratky 2016-2017</Copyright>
<Version>2.0.4.0</Version>
<AssemblyVersion>2.0.4.0</AssemblyVersion>
<FileVersion>2.0.4.0</FileVersion>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<AssemblyName>MQTTnet</AssemblyName>
<RootNamespace>MQTTnet</RootNamespace>
<PackageId>MQTTnet</PackageId>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\MQTTnet.Core\MQTTnet.Core.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
using MQTTnet.Core.Client;
using MQTTnet.Core.Serializer;

namespace MQTTnet.Universal
namespace MQTTnet
{
public class MqttClientFactory
{
public MqttClient CreateMqttClient(MqttClientOptions options)
{
if (options == null) throw new ArgumentNullException(nameof(options));

return new MqttClient(options, new MqttChannelAdapter(new MqttTcpChannel(), new DefaultMqttV311PacketSerializer()));
return new MqttClient(options, new MqttChannelCommunicationAdapter(new MqttTcpChannel(), new DefaultMqttV311PacketSerializer()));
}
}
}
56 changes: 56 additions & 0 deletions Frameworks/MQTTnet.NetCore/MqttServerAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using MQTTnet.Core.Adapter;
using MQTTnet.Core.Serializer;
using MQTTnet.Core.Server;

namespace MQTTnet
{
public sealed class MqttServerAdapter : IMqttServerAdapter, IDisposable
{
private CancellationTokenSource _cancellationTokenSource;
private Socket _socket;

public event EventHandler<MqttClientConnectedEventArgs> ClientConnected;

public void Start(MqttServerOptions options)
{
if (_socket != null) throw new InvalidOperationException("Server is already started.");

_cancellationTokenSource = new CancellationTokenSource();

_socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
_socket.Bind(new IPEndPoint(IPAddress.Any, options.Port));
_socket.Listen(options.ConnectionBacklog);

Task.Run(async () => await AcceptConnectionsAsync(_cancellationTokenSource.Token), _cancellationTokenSource.Token);
}

public void Stop()
{
_cancellationTokenSource?.Dispose();
_cancellationTokenSource = null;

_socket?.Dispose();
_socket = null;
}

public void Dispose()
{
Stop();
}

private async Task AcceptConnectionsAsync(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
var clientSocket = await _socket.AcceptAsync();
var clientAdapter = new MqttChannelCommunicationAdapter(new MqttTcpChannel(clientSocket), new DefaultMqttV311PacketSerializer());
ClientConnected?.Invoke(this, new MqttClientConnectedEventArgs(clientSocket.RemoteEndPoint.ToString(), clientAdapter));
}
}
}
}
15 changes: 15 additions & 0 deletions Frameworks/MQTTnet.NetCore/MqttServerFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using MQTTnet.Core.Server;

namespace MQTTnet
{
public class MqttServerFactory
{
public MqttServer CreateMqttServer(MqttServerOptions options)
{
if (options == null) throw new ArgumentNullException(nameof(options));

return new MqttServer(options, new MqttServerAdapter());
}
}
}
81 changes: 81 additions & 0 deletions Frameworks/MQTTnet.NetCore/MqttTcpChannel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Net.Sockets;
using System.Threading.Tasks;
using MQTTnet.Core.Channel;
using MQTTnet.Core.Client;
using MQTTnet.Core.Exceptions;

namespace MQTTnet
{
public class MqttTcpChannel : IMqttCommunicationChannel, IDisposable
{
private readonly Socket _socket;

public MqttTcpChannel()
{
_socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
}

public MqttTcpChannel(Socket socket)
{
_socket = socket ?? throw new ArgumentNullException(nameof(socket));
}

public async Task ConnectAsync(MqttClientOptions options)
{
try
{
await _socket.ConnectAsync(options.Server, options.Port);
}
catch (SocketException exception)
{
throw new MqttCommunicationException(exception);
}
}

public async Task DisconnectAsync()
{
try
{
_socket.Dispose();
await Task.FromResult(0);
}
catch (SocketException exception)
{
throw new MqttCommunicationException(exception);
}
}

public async Task WriteAsync(byte[] buffer)
{
if (buffer == null) throw new ArgumentNullException(nameof(buffer));

try
{
await _socket.SendAsync(new ArraySegment<byte>(buffer), SocketFlags.None);
}
catch (SocketException exception)
{
throw new MqttCommunicationException(exception);
}
}

public async Task ReadAsync(byte[] buffer)
{
try
{
var buffer2 = new ArraySegment<byte>(buffer);
await _socket.ReceiveAsync(buffer2, SocketFlags.None);
}
catch (SocketException exception)
{
throw new MqttCommunicationException(exception);
}
}

public void Dispose()
{
_socket?.Dispose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
<ProjectGuid>{A480EF90-0EAA-4D9A-B271-47A9C47F6F7D}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>MQTTnet.NETFramework</RootNamespace>
<AssemblyName>MQTTnet.NETFramework</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<RootNamespace>MQTTnet</RootNamespace>
<AssemblyName>MQTTnet</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -20,6 +21,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -28,19 +30,21 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="MqttClientFactory.cs" />
<Compile Include="MqttServerAdapter.cs" />
<Compile Include="MqttServerFactory.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="MqttTcpChannel.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MQTT.NET.Core\MQTTnet.Core.csproj">
<ProjectReference Include="..\..\MQTTnet.Core\MQTTnet.Core.csproj">
<Project>{99C884F3-B4B9-417D-AA92-DC7DD1C4CFEE}</Project>
<Name>MQTTnet.Core</Name>
</ProjectReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
using MQTTnet.Core.Client;
using MQTTnet.Core.Serializer;

namespace MQTTnet.NETFramework
namespace MQTTnet
{
public class MqttClientFactory
{
public MqttClient CreateMqttClient(MqttClientOptions options)
{
if (options == null) throw new ArgumentNullException(nameof(options));

return new MqttClient(options, new MqttChannelAdapter(new MqttTcpChannel(), new DefaultMqttV311PacketSerializer()));
return new MqttClient(options, new MqttChannelCommunicationAdapter(new MqttTcpChannel(), new DefaultMqttV311PacketSerializer()));
}
}
}
Loading

0 comments on commit ee25228

Please sign in to comment.