diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index edd96213a..14ac778ce 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -11,15 +11,12 @@ jobs: - uses: actions/setup-dotnet@v3 with: dotnet-version: | - 3.1.x - 6.0.x + 7.0.x - run: dotnet restore src/NetMQ.sln - name: build run: dotnet build src/NetMQ.sln /p:Configuration=Release /verbosity:minimal - - name: test netcoreapp3.1 - run: dotnet test -v n -p:ParallelizeTestCollections=false --configuration Release --no-build -f netcoreapp3.1 src/NetMQ.Tests/NetMQ.Tests.csproj - - name: test net6.0 - run: dotnet test -v n -p:ParallelizeTestCollections=false --configuration Release --no-build -f net6.0 src/NetMQ.Tests/NetMQ.Tests.csproj + - name: test net7.0 + run: dotnet test -v n -p:ParallelizeTestCollections=false --configuration Release --no-build -f net7.0 src/NetMQ.Tests/NetMQ.Tests.csproj windows: runs-on: windows-latest env: @@ -29,8 +26,7 @@ jobs: - uses: actions/setup-dotnet@v3 with: dotnet-version: | - 3.1.x - 6.0.x + 7.0.x - name: Install codecov run: | choco install opencover.portable @@ -38,13 +34,11 @@ jobs: - run: dotnet restore src/NetMQ.sln - name: build run: dotnet build src/NetMQ.sln /p:Configuration=Release /verbosity:minimal - - name: test net6.0 - run: dotnet test -v n -p:ParallelizeTestCollections=false --configuration Release --no-build -f net6.0 src\NetMQ.Tests\NetMQ.Tests.csproj - - name: test netcoreapp3.1 - run: dotnet test -v n -p:ParallelizeTestCollections=false --configuration Release --no-build -f netcoreapp3.1 src\NetMQ.Tests\NetMQ.Tests.csproj - - name: test net47 - run: dotnet test -v n -p:ParallelizeTestCollections=false --configuration Release --no-build -f net47 src\NetMQ.Tests\NetMQ.Tests.csproj + - name: test net7.0 + run: dotnet test -v n -p:ParallelizeTestCollections=false --configuration Release --no-build -f net7.0 src\NetMQ.Tests\NetMQ.Tests.csproj + - name: test net48 + run: dotnet test -v n -p:ParallelizeTestCollections=false --configuration Release --no-build -f net48 src\NetMQ.Tests\NetMQ.Tests.csproj - name: coverage run: | - OpenCover.Console.exe -register:user -target:"C:\Program Files\dotnet\dotnet.exe" -targetargs:"test --no-build --configuration Release -f net6.0 --logger:trx;LogFileName=results.trx /p:DebugType=full src\NetMQ.Tests\NetMQ.Tests.csproj" -filter:"+[NetMQ*]* -[NetMQ.Tests*]*" -output:".\NetMQ_coverage.xml" -oldStyle + OpenCover.Console.exe -register:user -target:"C:\Program Files\dotnet\dotnet.exe" -targetargs:"test --no-build --configuration Release -f net7.0 --logger:trx;LogFileName=results.trx /p:DebugType=full src\NetMQ.Tests\NetMQ.Tests.csproj" -filter:"+[NetMQ*]* -[NetMQ.Tests*]*" -output:".\NetMQ_coverage.xml" -oldStyle codecov -f "NetMQ_coverage.xml" diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 3c107188a..4f5b4cd7f 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -8,7 +8,7 @@ - + diff --git a/src/NetMQ.Tests/ClientServer.cs b/src/NetMQ.Tests/ClientServer.cs index 8fad5621d..c292e4773 100644 --- a/src/NetMQ.Tests/ClientServer.cs +++ b/src/NetMQ.Tests/ClientServer.cs @@ -82,7 +82,7 @@ public async void AsyncWithCancellationToken() await Assert.ThrowsAnyAsync(async () => await server.ReceiveStringAsync(source.Token)); } -#if NETCOREAPP3_1 +#if NET [Fact(Timeout = 120)] public async void AsyncEnumerableCanceled() diff --git a/src/NetMQ.Tests/CurveTests.cs b/src/NetMQ.Tests/CurveTests.cs index 7d991abad..ed569cfb2 100644 --- a/src/NetMQ.Tests/CurveTests.cs +++ b/src/NetMQ.Tests/CurveTests.cs @@ -2,7 +2,7 @@ using NetMQ.Sockets; using Xunit; -#if NET47 +#if NETFRAMEWORK using ZeroMQ; #endif @@ -38,8 +38,8 @@ public void CurveTest() } - -#if NET47 + +#if NETFRAMEWORK [Fact] public void WithLibzmqClient() { diff --git a/src/NetMQ.Tests/MessageTests.cs b/src/NetMQ.Tests/MessageTests.cs index 2009f8f93..3ea05d479 100644 --- a/src/NetMQ.Tests/MessageTests.cs +++ b/src/NetMQ.Tests/MessageTests.cs @@ -128,7 +128,7 @@ public void Issue52_ReqToRouterBug() var msg = router.ReceiveMultipartMessage(); Assert.Equal(3, msg.FrameCount); - Assert.Equal(msg[2].ConvertToString(), testmessage); + Assert.Equal(testmessage, msg[2].ConvertToString()); } } diff --git a/src/NetMQ.Tests/NetMQ.Tests.csproj b/src/NetMQ.Tests/NetMQ.Tests.csproj index ac51221d6..c5f628dd3 100644 --- a/src/NetMQ.Tests/NetMQ.Tests.csproj +++ b/src/NetMQ.Tests/NetMQ.Tests.csproj @@ -7,7 +7,7 @@ true true true - net6.0;netcoreapp3.1;net47 + net7.0;net48 @@ -26,7 +26,7 @@ $(FrameworkPathOverride)/Facades;$(AssemblySearchPaths) - + @@ -39,13 +39,16 @@ - - - - + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + - + diff --git a/src/NetMQ.Tests/NetMQMonitorTests.cs b/src/NetMQ.Tests/NetMQMonitorTests.cs index e6665e0b4..158e83cd0 100644 --- a/src/NetMQ.Tests/NetMQMonitorTests.cs +++ b/src/NetMQ.Tests/NetMQMonitorTests.cs @@ -57,7 +57,6 @@ public void Monitoring() } } -#if !NET35 [Fact] public void StartAsync() { @@ -71,7 +70,6 @@ public void StartAsync() Assert.True(task.Wait(TimeSpan.FromMilliseconds(1000))); } } -#endif [Fact] public void NoHangWhenMonitoringUnboundInprocAddress() diff --git a/src/NetMQ.Tests/NetMQPollerTest.cs b/src/NetMQ.Tests/NetMQPollerTest.cs index f49030732..2208427ae 100644 --- a/src/NetMQ.Tests/NetMQPollerTest.cs +++ b/src/NetMQ.Tests/NetMQPollerTest.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Diagnostics; using System.Net; using System.Net.Sockets; @@ -8,10 +9,6 @@ using NetMQ.Sockets; using Xunit; -#if !NET35 -using System.Collections.Concurrent; -#endif - // ReSharper disable AccessToDisposedClosure namespace NetMQ.Tests @@ -863,7 +860,6 @@ public void NativeSocket() #region TaskScheduler tests -#if !NET35 [Fact] public void OneTask() { @@ -990,13 +986,11 @@ public void TwoThreads() Assert.Equal(100, count2); } } -#endif #endregion #region ISynchronizeInvoke tests -#if NET451 [Fact] public void ISynchronizeInvokeWorks() { @@ -1018,8 +1012,7 @@ public void ISynchronizeInvokeWorks() Assert.True(isCorrectThread); } } -#endif -#endregion + #endregion } } diff --git a/src/NetMQ.Tests/NetMQQueueTests.cs b/src/NetMQ.Tests/NetMQQueueTests.cs index 9a26870df..b5cb49796 100644 --- a/src/NetMQ.Tests/NetMQQueueTests.cs +++ b/src/NetMQ.Tests/NetMQQueueTests.cs @@ -1,5 +1,4 @@ -#if !NET35 -using System; +using System; using System.Net.Sockets; using System.Threading; using System.Threading.Tasks; @@ -81,4 +80,3 @@ public void WithPoller() } } } -#endif diff --git a/src/NetMQ.Tests/RequestWithRetryTests.cs b/src/NetMQ.Tests/RequestWithRetryTests.cs index 8b161a122..215cb97c3 100644 --- a/src/NetMQ.Tests/RequestWithRetryTests.cs +++ b/src/NetMQ.Tests/RequestWithRetryTests.cs @@ -1,5 +1,4 @@ -#if !NET35 -using System; +using System; using System.Diagnostics; using NetMQ.Sockets; using Xunit; @@ -205,4 +204,3 @@ public void RequestResponseStringWithRetrySucceedsNotOnFirstTry() } } } -#endif \ No newline at end of file diff --git a/src/NetMQ.Tests/RouterTests.cs b/src/NetMQ.Tests/RouterTests.cs index 0633b6a71..daacc230e 100644 --- a/src/NetMQ.Tests/RouterTests.cs +++ b/src/NetMQ.Tests/RouterTests.cs @@ -44,7 +44,7 @@ public void ReceiveReadyDot35Bug() using (var server = new RouterSocket()) { server.BindRandomPort("tcp://127.0.0.1"); - server.ReceiveReady += (s, e) => Assert.True(false, "Should not receive"); + server.ReceiveReady += (s, e) => Assert.Fail("Should not receive"); Assert.False(server.Poll(TimeSpan.FromMilliseconds(1500))); } diff --git a/src/NetMQ.Tests/XPubSubTests.cs b/src/NetMQ.Tests/XPubSubTests.cs index 14d730e7e..a0a136749 100644 --- a/src/NetMQ.Tests/XPubSubTests.cs +++ b/src/NetMQ.Tests/XPubSubTests.cs @@ -332,7 +332,7 @@ public void Manual() sub.SendFrame(new byte[] { 1, (byte)'A' }); var subscription = pub.ReceiveFrameBytes(); - Assert.Equal(subscription[1], (byte)'A'); + Assert.Equal((byte)'A', subscription[1]); pub.Subscribe("B"); pub.SendFrame("A"); @@ -356,7 +356,7 @@ public void WelcomeMessage() var subscription = pub.ReceiveFrameBytes(); - Assert.Equal(subscription[1], (byte)'W'); + Assert.Equal((byte)'W', subscription[1]); Assert.Equal("W", sub.ReceiveFrameString()); } @@ -377,7 +377,7 @@ public void ClearWelcomeMessage() var subscription = pub.ReceiveFrameBytes(); - Assert.Equal(subscription[1], (byte)'W'); + Assert.Equal((byte)'W', subscription[1]); Assert.False(sub.TrySkipFrame()); } diff --git a/src/NetMQ.Tests/ZMTPTests.cs b/src/NetMQ.Tests/ZMTPTests.cs index 5efda7474..3e444bf77 100644 --- a/src/NetMQ.Tests/ZMTPTests.cs +++ b/src/NetMQ.Tests/ZMTPTests.cs @@ -10,7 +10,7 @@ using NetMQ.Sockets; using Xunit; -#if NET47 +#if NETFRAMEWORK using ZeroMQ; #endif @@ -156,7 +156,7 @@ public void V3Test() } } -#if NET47 +#if NETFRAMEWORK [Fact] public void WithLibzmq() { @@ -176,7 +176,6 @@ public void WithLibzmq() Assert.Equal("Hello", frame.ReadString()); } } - #endif } } \ No newline at end of file diff --git a/src/NetMQ/AsyncReceiveExtensions.cs b/src/NetMQ/AsyncReceiveExtensions.cs index 10c122021..5c4999af8 100644 --- a/src/NetMQ/AsyncReceiveExtensions.cs +++ b/src/NetMQ/AsyncReceiveExtensions.cs @@ -1,4 +1,4 @@ -#if NETSTANDARD2_0 || NETSTANDARD2_1 || NET47 +#if NETSTANDARD2_0_OR_GREATER || NET47_OR_GREATER using System; using System.Collections.Generic; diff --git a/src/NetMQ/Core/Mechanisms/CurveClientMechanism.cs b/src/NetMQ/Core/Mechanisms/CurveClientMechanism.cs index 5715952b7..1c60c25fe 100644 --- a/src/NetMQ/Core/Mechanisms/CurveClientMechanism.cs +++ b/src/NetMQ/Core/Mechanisms/CurveClientMechanism.cs @@ -189,7 +189,7 @@ PullMsgResult ProduceInitiate(ref Msg msg) VouchNoncePrefix.CopyTo(vouchNonce); using var rng = RandomNumberGenerator.Create(); -#if NETSTANDARD2_1 +#if NETSTANDARD2_1_OR_GREATER rng.GetBytes(vouchNonce.Slice(8)); #else byte[] temp = new byte[16]; diff --git a/src/NetMQ/Core/Mechanisms/CurveServerMechanism.cs b/src/NetMQ/Core/Mechanisms/CurveServerMechanism.cs index 1a104867b..7d9f47e9b 100644 --- a/src/NetMQ/Core/Mechanisms/CurveServerMechanism.cs +++ b/src/NetMQ/Core/Mechanisms/CurveServerMechanism.cs @@ -157,7 +157,7 @@ PullMsgResult ProduceWelcome(ref Msg msg) // 8-byte prefix plus 16-byte random nonce CookieNoncePrefix.CopyTo(cookieNonce); using var rng = RandomNumberGenerator.Create(); -#if NETSTANDARD2_1 +#if NETSTANDARD2_1_OR_GREATER rng.GetBytes(cookieNonce.Slice(8)); #else byte[] temp = new byte[16]; @@ -184,7 +184,7 @@ PullMsgResult ProduceWelcome(ref Msg msg) // Create full nonce for encryption // 8-byte prefix plus 16-byte random nonce WelcomeNoncePrefix.CopyTo(welcomeNonce); -#if NETSTANDARD2_1 +#if NETSTANDARD2_1_OR_GREATER rng.GetBytes(welcomeNonce.Slice(8)); #else rng.GetBytes(temp); diff --git a/src/NetMQ/Core/Transports/Pgm/PgmSocket.cs b/src/NetMQ/Core/Transports/Pgm/PgmSocket.cs index e0a2713da..fa4acd8db 100644 --- a/src/NetMQ/Core/Transports/Pgm/PgmSocket.cs +++ b/src/NetMQ/Core/Transports/Pgm/PgmSocket.cs @@ -97,7 +97,7 @@ internal void Init() Debug.WriteLine(xMsg); // If running on Microsoft Windows, suggest to the developer that he may need to install MSMQ in order to get PGM socket support. -#if NETSTANDARD1_1_OR_GREATER +#if NETSTANDARD2_0_OR_GREATER bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); #else bool isWindows = true; diff --git a/src/NetMQ/Core/Transports/Tcp/TcpListener.cs b/src/NetMQ/Core/Transports/Tcp/TcpListener.cs index 98b8261a5..4dd7a8fe5 100644 --- a/src/NetMQ/Core/Transports/Tcp/TcpListener.cs +++ b/src/NetMQ/Core/Transports/Tcp/TcpListener.cs @@ -22,7 +22,7 @@ You should have received a copy of the GNU Lesser General Public License using System; using System.Diagnostics; using System.Net.Sockets; -#if NETSTANDARD2_0 || NETSTANDARD2_1 +#if NETSTANDARD2_0_OR_GREATER using System.Runtime.InteropServices; #endif using AsyncIO; @@ -123,7 +123,7 @@ public virtual void SetAddress(string addr) m_address.Resolve(addr, m_options.IPv4Only); Assumes.NotNull(m_address.Address); - Assumes.NotNull(m_handle); + Assumes.Null(m_handle); try { @@ -141,7 +141,7 @@ public virtual void SetAddress(string addr) } } -#if NETSTANDARD2_0 || NETSTANDARD2_1 +#if NETSTANDARD2_0_OR_GREATER // This command is failing on linux if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) m_handle.ExclusiveAddressUse = false; diff --git a/src/NetMQ/Core/Utils/OpCode.cs b/src/NetMQ/Core/Utils/OpCode.cs index 060064c60..cf85c4b62 100644 --- a/src/NetMQ/Core/Utils/OpCode.cs +++ b/src/NetMQ/Core/Utils/OpCode.cs @@ -20,7 +20,7 @@ public static bool Open() string val = Environment.GetEnvironmentVariable("NETQM_SUPPRESS_RDTSC"); if (!string.IsNullOrEmpty(val)) return false; -#if NETSTANDARD1_1_OR_GREATER || NET471_OR_GREATER +#if NETSTANDARD || NET471_OR_GREATER if (RuntimeInformation.ProcessArchitecture != Architecture.X86 && RuntimeInformation.ProcessArchitecture != Architecture.X64) { diff --git a/src/NetMQ/Core/Utils/Signaler.cs b/src/NetMQ/Core/Utils/Signaler.cs index 8183132f5..9f46f39fa 100644 --- a/src/NetMQ/Core/Utils/Signaler.cs +++ b/src/NetMQ/Core/Utils/Signaler.cs @@ -72,22 +72,14 @@ public void Close() try { -#if NET35 - m_writeSocket.Close(); -#else m_writeSocket.Dispose(); -#endif } catch (SocketException) {} try { -#if NET35 - m_readSocket.Close(); -#else m_readSocket.Dispose(); -#endif } catch (SocketException) {} diff --git a/src/NetMQ/Core/Utils/SocketUtility.cs b/src/NetMQ/Core/Utils/SocketUtility.cs index c2719ba6e..c0230a19b 100644 --- a/src/NetMQ/Core/Utils/SocketUtility.cs +++ b/src/NetMQ/Core/Utils/SocketUtility.cs @@ -47,12 +47,6 @@ internal static class SocketUtility /// an error occurred when attempting to access the socket. public static void Select(IList? checkRead, IList? checkWrite, IList? checkError, int microSeconds) { -#if NET35 - // .NET 3.5 has a bug, such that -1 is not blocking the select call - therefore we use here instead the maximum integer value. - if (microSeconds == -1) - microSeconds = int.MaxValue; -#endif - Socket.Select(checkRead, checkWrite, checkError, microSeconds); } } diff --git a/src/NetMQ/Core/Utils/SpanUtility.cs b/src/NetMQ/Core/Utils/SpanUtility.cs index a5c529d30..92ae3285b 100644 --- a/src/NetMQ/Core/Utils/SpanUtility.cs +++ b/src/NetMQ/Core/Utils/SpanUtility.cs @@ -7,7 +7,7 @@ internal static class SpanUtility { public static string ToAscii(Span bytes) { -#if NETSTANDARD2_1 +#if NETSTANDARD2_1_OR_GREATER return Encoding.ASCII.GetString(bytes); #else return Encoding.ASCII.GetString(bytes.ToArray()); diff --git a/src/NetMQ/DnsEndPoint.cs b/src/NetMQ/DnsEndPoint.cs deleted file mode 100644 index 784c35631..000000000 --- a/src/NetMQ/DnsEndPoint.cs +++ /dev/null @@ -1,16 +0,0 @@ -#if NET35 -using System.Net; - -namespace NetMQ -{ - /// - /// Placeholder for System.Net.DnsEndPoint, introduced in .NET 4.0. - /// This code is enabled for .NET 3.5 builds only, allowing compilation. - /// - internal class DnsEndPoint : EndPoint - { - public string Host { get; set; } - public int Port { get; set; } - } -} -#endif diff --git a/src/NetMQ/GroupSocketExtensions.cs b/src/NetMQ/GroupSocketExtensions.cs index f4c305502..899465ddd 100644 --- a/src/NetMQ/GroupSocketExtensions.cs +++ b/src/NetMQ/GroupSocketExtensions.cs @@ -376,7 +376,7 @@ public static bool TryReceiveBytes(this IGroupInSocket socket, TimeSpan timeout, #region AsyncEnumerable -#if NETSTANDARD2_1 +#if NETSTANDARD2_1_OR_GREATER /// /// Provides a consuming IAsyncEnumerable for receiving messages from the socket. /// @@ -575,7 +575,7 @@ public static bool TryReceiveString(this IGroupInSocket socket, TimeSpan timeout #region AsyncEnumerable -#if NETSTANDARD2_1 +#if NETSTANDARD2_1_OR_GREATER /// /// Provides a consuming IAsyncEnumerable for receiving messages from the socket. /// diff --git a/src/NetMQ/Monitoring/NetMQMonitor.cs b/src/NetMQ/Monitoring/NetMQMonitor.cs index a782ebd63..118595d64 100644 --- a/src/NetMQ/Monitoring/NetMQMonitor.cs +++ b/src/NetMQ/Monitoring/NetMQMonitor.cs @@ -1,8 +1,6 @@ using System; using System.Threading; -#if !NET35 using System.Threading.Tasks; -#endif using AsyncIO; using NetMQ.Core; using NetMQ.Sockets; @@ -289,7 +287,6 @@ public void Start() } } -#if !NET35 /// /// Start a background task for the monitoring operation. /// @@ -304,7 +301,6 @@ public Task StartAsync() return Task.Factory.StartNew(Start); } -#endif /// /// Stop monitoring. Blocks until monitoring completed. @@ -352,11 +348,7 @@ protected virtual void Dispose(bool disposing) m_monitoringSocket.ReceiveReady -= Handle; -#if NET35 - m_isStoppedEvent.Close(); -#else m_isStoppedEvent.Dispose(); -#endif if (m_ownsMonitoringSocket && !attachedToPoller) { diff --git a/src/NetMQ/NetMQBeacon.cs b/src/NetMQ/NetMQBeacon.cs index 6c224c053..03cfd7400 100644 --- a/src/NetMQ/NetMQBeacon.cs +++ b/src/NetMQ/NetMQBeacon.cs @@ -76,11 +76,7 @@ private void Configure(string interfaceName, int port) { m_poller.Remove(m_udpSocket); -#if NET35 - m_udpSocket.Close(); -#else m_udpSocket.Dispose(); -#endif } m_udpPort = port; @@ -120,24 +116,24 @@ private void Configure(string interfaceName, int port) { if (interfaceAddress == null || @interface.Address.Equals(interfaceAddress)) { - // because windows and unix differ in how they handle broadcast addressing this needs to be platform specific - // on windows any interface can receive broadcast by requesting to enable broadcast on the socket - // on linux to receive broadcast you must bind to the broadcast address specifically - //bindTo = @interface.Address; - sendTo = @interface.BroadcastAddress; -#if NET45 || NET47 - if (Environment.OSVersion.Platform==PlatformID.Unix) + // because windows and unix differ in how they handle broadcast addressing this needs to be platform specific + // on windows any interface can receive broadcast by requesting to enable broadcast on the socket + // on linux to receive broadcast you must bind to the broadcast address specifically + //bindTo = @interface.Address; + sendTo = @interface.BroadcastAddress; +#if NETFRAMEWORK + if (Environment.OSVersion.Platform == PlatformID.Unix) #else - if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) #endif - { - bindTo = @interface.BroadcastAddress; - } - else - { - bindTo = @interface.Address; - } - sendTo = @interface.BroadcastAddress; + { + bindTo = @interface.BroadcastAddress; + } + else + { + bindTo = @interface.Address; + } + sendTo = @interface.BroadcastAddress; break; } @@ -182,11 +178,7 @@ public void Run(PairSocket shim) } // the beacon might never been configured -#if NET35 - m_udpSocket?.Close(); -#else m_udpSocket?.Dispose(); -#endif } private void PingElapsed(object sender, NetMQTimerEventArgs e) @@ -201,7 +193,7 @@ private void OnUdpReady(Socket socket) Assumes.NotNull(m_pipe); if (!TryReceiveUdpFrame(out NetMQFrame? frame, out string? peerName)) - return; + return; // If filter is set, check that beacon matches it var isValid = frame.MessageSize >= m_filter?.MessageSize && Compare(frame, m_filter, m_filter.MessageSize); diff --git a/src/NetMQ/NetMQPoller.cs b/src/NetMQ/NetMQPoller.cs index 476d0c2c9..8bf98dc02 100644 --- a/src/NetMQ/NetMQPoller.cs +++ b/src/NetMQ/NetMQPoller.cs @@ -1,17 +1,13 @@ using System; using System.Collections; using System.Collections.Generic; +using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Net.Sockets; using System.Threading; -using NetMQ.Core.Utils; -#if !NET35 using System.Threading.Tasks; -#endif -#if NET40 -using System.ComponentModel; -#endif +using NetMQ.Core.Utils; using Switch = NetMQ.Core.Utils.Switch; @@ -21,12 +17,8 @@ namespace NetMQ /// Enable polling on multiple NetMQSockets /// public sealed class NetMQPoller : -#if !NET35 TaskScheduler, -#endif -#if NET40 ISynchronizeInvoke, -#endif #pragma warning disable 618 INetMQPoller, ISocketPollableCollection, IEnumerable, IDisposable #pragma warning restore 618 @@ -39,18 +31,13 @@ public sealed class NetMQPoller : private readonly StopSignaler m_stopSignaler = new StopSignaler(); private NetMQSelector.Item[]? m_pollSet; - private NetMQSocket[]? m_pollact; + private NetMQSocket[]? m_pollact; - private volatile bool m_isPollSetDirty = true; + private volatile bool m_isPollSetDirty = true; private int m_disposeState = (int)DisposeState.Undisposed; -#if NET35 - private Thread m_pollerThread; -#endif - #region Scheduling -#if !NET35 private readonly NetMQQueue m_tasksQueue = new NetMQQueue(); private readonly ThreadLocal m_isSchedulerThread = new ThreadLocal(() => false); @@ -130,12 +117,6 @@ public void Run(Action action) else new Task(action).Start(this); } -#else - private void Run(Action action) - { - action(); - } -#endif #endregion @@ -146,8 +127,6 @@ public NetMQPoller() { m_sockets.Add(((ISocketPollable)m_stopSignaler).Socket); -#if !NET35 - m_tasksQueue.ReceiveReady += delegate { Debug.Assert(m_disposeState != (int)DisposeState.Disposed); @@ -159,7 +138,6 @@ public NetMQPoller() }; m_sockets.Add(((ISocketPollable)m_tasksQueue).Socket); -#endif } /// @@ -172,11 +150,7 @@ public NetMQPoller() /// public bool IsDisposed => m_disposeState == (int)DisposeState.Disposed; -#if NET35 - private bool IsPollerThread => ReferenceEquals(m_pollerThread, Thread.CurrentThread); -#else private bool IsPollerThread => m_isSchedulerThread.Value; -#endif #region Add / Remove @@ -353,7 +327,6 @@ public void Remove(Socket socket) #endregion #region Contains -#if !NET35 /// /// Check if poller contains the socket asynchronously. @@ -404,7 +377,6 @@ public Task ContainsAsync(Socket socket) Run(() => tcs.SetResult(m_pollinSockets.ContainsKey(socket))); return tcs.Task; } -#endif #endregion #region Start / Stop @@ -451,31 +423,6 @@ public void RunAsync(string threadName, bool isBackgroundThread) m_switch.WaitForOn(); } -#if NET35 - /// - /// Runs the poller on the caller's thread. Only returns when or are called from another thread. - /// - public void Run() - { - CheckDisposed(); - if (IsRunning) - throw new InvalidOperationException("NetMQPoller is already running"); - - m_pollerThread = Thread.CurrentThread; - m_stopSignaler.Reset(); - m_switch.SwitchOn(); - - try - { - RunPoller(); - } - finally - { - m_pollerThread = null; - m_switch.SwitchOff(); - } - } -#else /// /// Runs the poller on the caller's thread. Only returns when or are called from another thread. /// @@ -516,7 +463,6 @@ public void Run(SynchronizationContext syncContext) } } -#endif /// /// Runs the poller on the caller's thread. Only returns when or are called from another thread. @@ -639,11 +585,9 @@ private void RunPoller() } } -#if !NET35 // Try to dequeue and execute all pending tasks before stopping poller while (m_tasksQueue.TryDequeue(out Task? task, TimeSpan.Zero)) TryExecuteTask(task); -#endif } finally { @@ -695,9 +639,7 @@ private void OnSocketEventsChanged(object sender, NetMQSocketEventArgs e) private void RebuildPollset() { -#if !NET35 Debug.Assert(m_isSchedulerThread.Value); -#endif // Recreate the m_pollSet and m_pollact arrays. m_pollSet = new NetMQSelector.Item[m_sockets.Count + m_pollinSockets.Count]; @@ -779,10 +721,8 @@ public void Dispose() m_sockets.Remove(((ISocketPollable)m_stopSignaler).Socket); m_stopSignaler.Dispose(); -#if !NET35 m_sockets.Remove(((ISocketPollable)m_tasksQueue).Socket); m_tasksQueue.Dispose(); -#endif foreach (var socket in m_sockets) { @@ -798,7 +738,6 @@ public void Dispose() #region ISynchronizeInvoke -#if NET40 IAsyncResult ISynchronizeInvoke.BeginInvoke(Delegate method, object[] args) { var task = new Task(() => method.DynamicInvoke(args)); @@ -823,7 +762,6 @@ object ISynchronizeInvoke.Invoke(Delegate method, object[] args) } bool ISynchronizeInvoke.InvokeRequired => !CanExecuteTaskInline; -#endif #endregion } diff --git a/src/NetMQ/NetMQQueue.cs b/src/NetMQ/NetMQQueue.cs index d6c207774..eee9730b4 100644 --- a/src/NetMQ/NetMQQueue.cs +++ b/src/NetMQ/NetMQQueue.cs @@ -1,4 +1,3 @@ -#if !NET35 using System; using System.Collections.Concurrent; using System.Collections.Generic; @@ -163,4 +162,3 @@ public void Dispose() } } } -#endif diff --git a/src/NetMQ/NetMQRuntime.cs b/src/NetMQ/NetMQRuntime.cs index 87748a31c..36675e1ff 100644 --- a/src/NetMQ/NetMQRuntime.cs +++ b/src/NetMQ/NetMQRuntime.cs @@ -1,4 +1,4 @@ -#if NETSTANDARD2_0 || NETSTANDARD2_1 || NET47 +#if NETSTANDARD2_0_OR_GREATER || NET47_OR_GREATER using System; using System.Threading; diff --git a/src/NetMQ/NetMQSocket.cs b/src/NetMQ/NetMQSocket.cs index 05a52ccbd..f1f2d1e2b 100644 --- a/src/NetMQ/NetMQSocket.cs +++ b/src/NetMQ/NetMQSocket.cs @@ -1,11 +1,7 @@ using System; -using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Threading; using NetMQ.Core; -#if NET40 -using NetMQ.Core.Utils; -#endif namespace NetMQ { @@ -25,9 +21,9 @@ public abstract class NetMQSocket : INetMQSocket private EventHandler? m_sendReady; private int m_isClosed; - #if NETSTANDARD2_0 || NETSTANDARD2_1 || NET47 +#if NETSTANDARD2_0_OR_GREATER || NET47_OR_GREATER private NetMQRuntime? m_runtime; - #endif +#endif internal enum DefaultAction { @@ -249,13 +245,13 @@ public void Unbind(string address) /// Closes this socket, rendering it unusable. Equivalent to calling . public void Close() { - #if NETSTANDARD2_0 || NETSTANDARD2_1 || NET47 +#if NETSTANDARD2_0_OR_GREATER || NET47_OR_GREATER if (m_runtime != null) { m_runtime.Remove(this); m_runtime = null; } - #endif +#endif if (Interlocked.CompareExchange(ref m_isClosed, 1, 0) != 0) return; @@ -391,7 +387,7 @@ public virtual bool TrySend(ref Msg msg, TimeSpan timeout, bool more) #endregion - #if NETSTANDARD2_0 || NETSTANDARD2_1 || NET47 +#if NETSTANDARD2_0_OR_GREATER || NET47_OR_GREATER internal void AttachToRuntime() { @@ -409,7 +405,7 @@ internal void DetachFromRuntime() m_runtime = null; } - #endif +#endif /// /// Listen to the given endpoint for SocketEvent events. diff --git a/src/NetMQ/NetMQSynchronizationContext.cs b/src/NetMQ/NetMQSynchronizationContext.cs index 54f863a83..757c8e8a3 100644 --- a/src/NetMQ/NetMQSynchronizationContext.cs +++ b/src/NetMQ/NetMQSynchronizationContext.cs @@ -1,7 +1,6 @@ using System.Threading; using System.Threading.Tasks; -#if !NET35 namespace NetMQ { internal sealed class NetMQSynchronizationContext : SynchronizationContext @@ -29,4 +28,3 @@ public override void Send(SendOrPostCallback d, object state) } } } -#endif \ No newline at end of file diff --git a/src/NetMQ/ReceiveThreadSafeSocketExtensions.cs b/src/NetMQ/ReceiveThreadSafeSocketExtensions.cs index d0a163e86..002a7e25b 100644 --- a/src/NetMQ/ReceiveThreadSafeSocketExtensions.cs +++ b/src/NetMQ/ReceiveThreadSafeSocketExtensions.cs @@ -119,7 +119,7 @@ public static ValueTask ReceiveBytesAsync(this IThreadSafeInSocket socke #region AsyncEnumerable -#if NETSTANDARD2_1 +#if NETSTANDARD2_1_OR_GREATER /// /// Provides a consuming IAsyncEnumerable for receiving messages from the socket. @@ -302,7 +302,7 @@ public static ValueTask ReceiveStringAsync(this IThreadSafeInSocket sock #region AsyncEnumerable -#if NETSTANDARD2_1 +#if NETSTANDARD2_1_OR_GREATER /// /// Provides a consuming IAsyncEnumerable for receiving messages from the socket. diff --git a/src/NetMQ/RoutingIdSocketExtensions.cs b/src/NetMQ/RoutingIdSocketExtensions.cs index 6aaac857a..116de79c6 100644 --- a/src/NetMQ/RoutingIdSocketExtensions.cs +++ b/src/NetMQ/RoutingIdSocketExtensions.cs @@ -372,7 +372,7 @@ public static bool TryReceiveBytes(this IRoutingIdSocket socket, TimeSpan timeou #region AsyncEnumerable -#if NETSTANDARD2_1 +#if NETSTANDARD2_1_OR_GREATER /// /// Provides a consuming IAsyncEnumerable for receiving messages from the socket. @@ -564,7 +564,7 @@ public static bool TryReceiveString(this IRoutingIdSocket socket, TimeSpan timeo #region AsyncEnumerable -#if NETSTANDARD2_1 +#if NETSTANDARD2_1_OR_GREATER /// /// Provides a consuming IAsyncEnumerable for receiving messages from the socket. diff --git a/src/NetMQ/TaskThread.cs b/src/NetMQ/TaskThread.cs deleted file mode 100644 index 3f7c4b964..000000000 --- a/src/NetMQ/TaskThread.cs +++ /dev/null @@ -1,109 +0,0 @@ -#if UAP -using System; -using System.Globalization; -using System.Threading.Tasks; - -namespace System.Threading -{ - internal delegate void ParameterizedThreadStart(object obj); - internal delegate void ThreadStart(); - - internal class ThreadStateException : Exception - { - public ThreadStateException(string message = "", Exception innerException = null) - : base(message, innerException) - { } - } - - internal class Thread - { - private Task _task; - private CancellationTokenSource _tokenSource = new CancellationTokenSource(); - - public string Name { get; set; } - public bool IsBackground { get; set; } - public bool IsAlive => _task != null && !(_task.IsCanceled || _task.IsCompleted || _task.IsFaulted); - public CultureInfo CurrentCulture => throw new NotImplementedException(); - private static SemaphoreSlim _unavailable = new SemaphoreSlim(0, 1); - - private enum StartType - { - Standard, - Parameterized - }; - - StartType _startType; - ParameterizedThreadStart _parameterizedStart; - ThreadStart _start; - - public Thread(ParameterizedThreadStart threadStart, int maxStackSize = 0) - { - _startType = StartType.Parameterized; - _parameterizedStart = threadStart; - } - - public Thread(ThreadStart threadStart, int maxStackSize = 0) - { - _startType = StartType.Standard; - _start = threadStart; - } - - public void Start() - { - if (_startType == StartType.Parameterized) - { - throw new InvalidOperationException("Must supply argument for ParameterizedThreadStart!"); - } - - if (_task != null) - { - throw new ThreadStateException("Thread already started!"); - } - - _task = new Task(() => _start(), _tokenSource.Token, TaskCreationOptions.LongRunning); - _task.Start(); - } - - public void Start(object obj) - { - if (_startType == StartType.Standard) - { - throw new InvalidOperationException("Must use parameterless Start() method instead!"); - } - - if (_task != null) - { - throw new ThreadStateException("Thread already started!"); - } - - _task = new Task(() => _parameterizedStart(obj), _tokenSource.Token, TaskCreationOptions.LongRunning); - _task.Start(); - } - - public void Join() - { - _task.Wait(); - } - - public bool Join(Int32 milliseconds) - { - return _task.Wait(milliseconds); - } - - public bool Join(TimeSpan timeout) - { - return _task.Wait(timeout); - } - - public static void Sleep(int milliseconds) - { - _unavailable.Wait(milliseconds); - } - - public static void Sleep(TimeSpan duration) - { - _unavailable.Wait(duration); - } - } -} -#endif diff --git a/src/NetMQ/ThreadSafeSocket.cs b/src/NetMQ/ThreadSafeSocket.cs index b0d5b7f19..546f74e88 100644 --- a/src/NetMQ/ThreadSafeSocket.cs +++ b/src/NetMQ/ThreadSafeSocket.cs @@ -2,9 +2,6 @@ using System.Diagnostics; using System.Threading; using NetMQ.Core; -#if NET40 -using NetMQ.Core.Utils; -#endif namespace NetMQ { @@ -200,7 +197,7 @@ public void Unbind(string address) /// Closes this socket, rendering it unusable. Equivalent to calling . public void Close() { - // #if NETSTANDARD2_0 || NETSTANDARD2_1 || NET47 + // #if NETSTANDARD2_0_OR_GREATER || NET47_OR_GREATER // if (m_runtime != null) // { // m_runtime.Remove(this); diff --git a/src/NetMQ/Utils/Assumes.cs b/src/NetMQ/Utils/Assumes.cs index 7fb3791e2..1902a26cd 100644 --- a/src/NetMQ/Utils/Assumes.cs +++ b/src/NetMQ/Utils/Assumes.cs @@ -9,8 +9,14 @@ internal static class Assumes [Conditional("DEBUG")] public static void NotNull([NotNull] T o) where T : class? { - Debug.Assert(o is object, $"Unexpected null of type {typeof(T).Name}"); + Debug.Assert(o is not null, $"Unexpected null value of type {typeof(T).Name}"); } #pragma warning restore CS8777 // Parameter must have a non-null value when exiting. + + [Conditional("DEBUG")] + public static void Null([MaybeNull] T o) where T : class? + { + Debug.Assert(o is null, $"Unexpected non-null value of type {typeof(T).Name}"); + } } }