Skip to content

Commit

Permalink
Cleanup target frameworks and remove obsolete code
Browse files Browse the repository at this point in the history
  • Loading branch information
chkr1011 committed May 19, 2024
1 parent dc472d7 commit 1b449ef
Show file tree
Hide file tree
Showing 31 changed files with 699 additions and 836 deletions.
2 changes: 1 addition & 1 deletion Source/MQTTnet.AspTestApp/MQTTnet.AspTestApp.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net8.0</TargetFrameworks>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
Expand Down
2 changes: 1 addition & 1 deletion Source/MQTTnet.AspTestApp/Pages/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
clientId: 'mqttnet_web_client'
}
const client = mqtt.connect('ws://localhost:5049/mqtt')
const client = mqtt.connect('ws://localhost:5000/mqtt')
client.on('connect', function () {
console.log('Connected')
Expand Down
5 changes: 1 addition & 4 deletions Source/MQTTnet.AspTestApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@
app.MapRazorPages();

// Setup MQTT stuff.
app.UseEndpoints(endpoints =>
{
endpoints.MapMqtt("/mqtt");
});
app.MapMqtt("/mqtt");

app.UseMqttServer(server =>
{
Expand Down
19 changes: 7 additions & 12 deletions Source/MQTTnet.AspnetCore/Client/Tcp/BufferExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,17 @@
using System;
using System.Runtime.InteropServices;

namespace MQTTnet.AspNetCore.Client.Tcp
namespace MQTTnet.AspNetCore.Client.Tcp;

public static class BufferExtensions
{
public static class BufferExtensions
public static ArraySegment<byte> GetArray(this ReadOnlyMemory<byte> memory)
{
public static ArraySegment<byte> GetArray(this Memory<byte> memory)
if (!MemoryMarshal.TryGetArray(memory, out var result))
{
return ((ReadOnlyMemory<byte>)memory).GetArray();
throw new InvalidOperationException("Buffer backed by array was expected");
}

public static ArraySegment<byte> GetArray(this ReadOnlyMemory<byte> memory)
{
if (!MemoryMarshal.TryGetArray(memory, out var result))
{
throw new InvalidOperationException("Buffer backed by array was expected");
}
return result;
}
return result;
}
}
53 changes: 26 additions & 27 deletions Source/MQTTnet.AspnetCore/Client/Tcp/DuplexPipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,41 @@

using System.IO.Pipelines;

namespace MQTTnet.AspNetCore.Client.Tcp
namespace MQTTnet.AspNetCore.Client.Tcp;

public class DuplexPipe : IDuplexPipe
{
public class DuplexPipe : IDuplexPipe
public DuplexPipe(PipeReader reader, PipeWriter writer)
{
public DuplexPipe(PipeReader reader, PipeWriter writer)
{
Input = reader;
Output = writer;
}
Input = reader;
Output = writer;
}

public PipeReader Input { get; }
public PipeReader Input { get; }

public PipeWriter Output { get; }
public PipeWriter Output { get; }

public static DuplexPipePair CreateConnectionPair(PipeOptions inputOptions, PipeOptions outputOptions)
{
var input = new Pipe(inputOptions);
var output = new Pipe(outputOptions);
public static DuplexPipePair CreateConnectionPair(PipeOptions inputOptions, PipeOptions outputOptions)
{
var input = new Pipe(inputOptions);
var output = new Pipe(outputOptions);

var transportToApplication = new DuplexPipe(output.Reader, input.Writer);
var applicationToTransport = new DuplexPipe(input.Reader, output.Writer);
var transportToApplication = new DuplexPipe(output.Reader, input.Writer);
var applicationToTransport = new DuplexPipe(input.Reader, output.Writer);

return new DuplexPipePair(applicationToTransport, transportToApplication);
}
return new DuplexPipePair(applicationToTransport, transportToApplication);
}

// This class exists to work around issues with value tuple on .NET Framework
public readonly struct DuplexPipePair
{
public IDuplexPipe Transport { get; }
public IDuplexPipe Application { get; }

// This class exists to work around issues with value tuple on .NET Framework
public readonly struct DuplexPipePair
public DuplexPipePair(IDuplexPipe transport, IDuplexPipe application)
{
public IDuplexPipe Transport { get; }
public IDuplexPipe Application { get; }

public DuplexPipePair(IDuplexPipe transport, IDuplexPipe application)
{
Transport = transport;
Application = application;
}
Transport = transport;
Application = application;
}
}
}
85 changes: 44 additions & 41 deletions Source/MQTTnet.AspnetCore/Client/Tcp/SocketAwaitable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,65 +10,68 @@
using System.Threading;
using System.Threading.Tasks;

namespace MQTTnet.AspNetCore.Client.Tcp
namespace MQTTnet.AspNetCore.Client.Tcp;

public class SocketAwaitable : ICriticalNotifyCompletion
{
public class SocketAwaitable : ICriticalNotifyCompletion
static readonly Action _callbackCompleted = () =>
{
private static readonly Action _callbackCompleted = () => { };
};

private readonly PipeScheduler _ioScheduler;
readonly PipeScheduler _ioScheduler;
int _bytesTransferred;

private Action _callback;
private int _bytesTransferred;
private SocketError _error;
Action _callback;
SocketError _error;

public SocketAwaitable(PipeScheduler ioScheduler)
{
_ioScheduler = ioScheduler;
}
public SocketAwaitable(PipeScheduler ioScheduler)
{
_ioScheduler = ioScheduler;
}

public bool IsCompleted => ReferenceEquals(_callback, _callbackCompleted);
public bool IsCompleted => ReferenceEquals(_callback, _callbackCompleted);

public SocketAwaitable GetAwaiter() => this;
public void Complete(int bytesTransferred, SocketError socketError)
{
_error = socketError;
_bytesTransferred = bytesTransferred;
var continuation = Interlocked.Exchange(ref _callback, _callbackCompleted);

public int GetResult()
if (continuation != null)
{
Debug.Assert(ReferenceEquals(_callback, _callbackCompleted));
_ioScheduler.Schedule(state => ((Action)state)(), continuation);
}
}

_callback = null;
public SocketAwaitable GetAwaiter()
{
return this;
}

if (_error != SocketError.Success)
{
throw new SocketException((int)_error);
}
public int GetResult()
{
Debug.Assert(ReferenceEquals(_callback, _callbackCompleted));

return _bytesTransferred;
}
_callback = null;

public void OnCompleted(Action continuation)
if (_error != SocketError.Success)
{
if (ReferenceEquals(_callback, _callbackCompleted) ||
ReferenceEquals(Interlocked.CompareExchange(ref _callback, continuation, null), _callbackCompleted))
{
Task.Run(continuation);
}
throw new SocketException((int)_error);
}

public void UnsafeOnCompleted(Action continuation)
{
OnCompleted(continuation);
}
return _bytesTransferred;
}

public void Complete(int bytesTransferred, SocketError socketError)
public void OnCompleted(Action continuation)
{
if (ReferenceEquals(_callback, _callbackCompleted) || ReferenceEquals(Interlocked.CompareExchange(ref _callback, continuation, null), _callbackCompleted))
{
_error = socketError;
_bytesTransferred = bytesTransferred;
var continuation = Interlocked.Exchange(ref _callback, _callbackCompleted);

if (continuation != null)
{
_ioScheduler.Schedule(state => ((Action)state)(), continuation);
}
Task.Run(continuation);
}
}

public void UnsafeOnCompleted(Action continuation)
{
OnCompleted(continuation);
}
}
Loading

0 comments on commit 1b449ef

Please sign in to comment.