-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
110 changed files
with
2,160 additions
and
492 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.