diff --git a/source/NetCoreServer/FileCache.cs b/source/NetCoreServer/FileCache.cs index c84943f..76736f1 100644 --- a/source/NetCoreServer/FileCache.cs +++ b/source/NetCoreServer/FileCache.cs @@ -95,7 +95,7 @@ public bool Remove(string key) /// 'true' if the cache path was setup, 'false' if failed to setup the cache path public bool InsertPath(string path, string prefix = "/", string filter = "*.*", TimeSpan timeout = new TimeSpan(), InsertHandler handler = null) { - handler ??= (FileCache cache, string key, byte[] value, TimeSpan timespan) => cache.Add(key, value, timespan); + handler = handler ?? ((FileCache cache, string key, byte[] value, TimeSpan timespan) => cache.Add(key, value, timespan)); // Try to find and remove the previous path RemovePathInternal(path); diff --git a/source/NetCoreServer/HttpClient.cs b/source/NetCoreServer/HttpClient.cs index 29e827a..98b69c4 100644 --- a/source/NetCoreServer/HttpClient.cs +++ b/source/NetCoreServer/HttpClient.cs @@ -259,7 +259,7 @@ public HttpClientEx(IPEndPoint endpoint) : base(endpoint) {} /// HTTP request Task public Task SendRequest(HttpRequest request, TimeSpan? timeout = null) { - timeout ??= TimeSpan.FromMinutes(1); + timeout = timeout ?? TimeSpan.FromMinutes(1); _tcs = new TaskCompletionSource(); Request = request; diff --git a/source/NetCoreServer/HttpServer.cs b/source/NetCoreServer/HttpServer.cs index 419f04c..363aea3 100644 --- a/source/NetCoreServer/HttpServer.cs +++ b/source/NetCoreServer/HttpServer.cs @@ -47,7 +47,7 @@ public class HttpServer : TcpServer /// Refresh cache timeout (default is 1 hour) public void AddStaticContent(string path, string prefix = "/", string filter = "*.*", TimeSpan? timeout = null) { - timeout ??= TimeSpan.FromHours(1); + timeout = timeout ?? TimeSpan.FromHours(1); bool Handler(FileCache cache, string key, byte[] value, TimeSpan timespan) { diff --git a/source/NetCoreServer/HttpsClient.cs b/source/NetCoreServer/HttpsClient.cs index 2ba4006..c8bb203 100644 --- a/source/NetCoreServer/HttpsClient.cs +++ b/source/NetCoreServer/HttpsClient.cs @@ -268,7 +268,7 @@ public HttpsClientEx(SslContext context, IPEndPoint endpoint) : base(context, en /// HTTP request Task public Task SendRequest(HttpRequest request, TimeSpan? timeout = null) { - timeout ??= TimeSpan.FromMinutes(1); + timeout = timeout ?? TimeSpan.FromMinutes(1); _tcs = new TaskCompletionSource(); Request = request; diff --git a/source/NetCoreServer/HttpsServer.cs b/source/NetCoreServer/HttpsServer.cs index c7eee72..020be81 100644 --- a/source/NetCoreServer/HttpsServer.cs +++ b/source/NetCoreServer/HttpsServer.cs @@ -51,7 +51,7 @@ public class HttpsServer : SslServer /// Refresh cache timeout (default is 1 hour) public void AddStaticContent(string path, string prefix = "/", string filter = "*.*", TimeSpan? timeout = null) { - timeout ??= TimeSpan.FromHours(1); + timeout = timeout ?? TimeSpan.FromHours(1); bool Handler(FileCache cache, string key, byte[] value, TimeSpan timespan) { diff --git a/source/NetCoreServer/NetCoreServer.csproj b/source/NetCoreServer/NetCoreServer.csproj index a712960..fbfc3ac 100644 --- a/source/NetCoreServer/NetCoreServer.csproj +++ b/source/NetCoreServer/NetCoreServer.csproj @@ -1,15 +1,18 @@  - net8.0 8.0.7.0 - Ivan Shynkarenka - Copyright (c) 2019-2023 Ivan Shynkarenka - https://github.com/chronoxor/NetCoreServer - Ultra fast and low latency asynchronous socket server & client C# .NET Core library with support TCP, SSL, UDP, HTTP, HTTPS, WebSocket protocols and 10K connections problem solution + GoPlay.NetCoreServer + Jennal + Copyright © What2d Ltd. 2023 + https://github.com/jennal/NetCoreServer + Fork from [https://github.com/chronoxor/NetCoreServer](https://github.com/chronoxor/NetCoreServer), make it support .NET Standard 2.1 + +Ultra fast and low latency asynchronous socket server & client C# .NET Core library with support TCP, SSL, UDP, HTTP, HTTPS, WebSocket protocols and 10K connections problem solution MIT - https://github.com/chronoxor/NetCoreServer + https://github.com/jennal/NetCoreServer async;client;server;tcp;udp;ssl;tls;http;https;websocket;low latency;performance + net6.0;net7.0;net8.0;net9.0;netstandard2.1 diff --git a/source/NetCoreServer/SslClient.cs b/source/NetCoreServer/SslClient.cs index 7f8a509..1e6d183 100644 --- a/source/NetCoreServer/SslClient.cs +++ b/source/NetCoreServer/SslClient.cs @@ -265,12 +265,14 @@ public virtual bool Connect() // Apply the option: keep alive if (OptionKeepAlive) Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); +#if NET6_0_OR_GREATER if (OptionTcpKeepAliveTime >= 0) Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, OptionTcpKeepAliveTime); if (OptionTcpKeepAliveInterval >= 0) Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, OptionTcpKeepAliveInterval); if (OptionTcpKeepAliveRetryCount >= 0) Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, OptionTcpKeepAliveRetryCount); +#endif // Apply the option: no delay if (OptionNoDelay) Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true); @@ -840,12 +842,14 @@ private void ProcessConnect(SocketAsyncEventArgs e) // Apply the option: keep alive if (OptionKeepAlive) Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); +#if NET6_0_OR_GREATER if (OptionTcpKeepAliveTime >= 0) Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, OptionTcpKeepAliveTime); if (OptionTcpKeepAliveInterval >= 0) Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, OptionTcpKeepAliveInterval); if (OptionTcpKeepAliveRetryCount >= 0) Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, OptionTcpKeepAliveRetryCount); +#endif // Apply the option: no delay if (OptionNoDelay) Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true); diff --git a/source/NetCoreServer/SslContext.cs b/source/NetCoreServer/SslContext.cs index d9450fb..a793b8f 100644 --- a/source/NetCoreServer/SslContext.cs +++ b/source/NetCoreServer/SslContext.cs @@ -12,7 +12,13 @@ public class SslContext /// /// Initialize SSL context with default protocols /// - public SslContext() : this(SslProtocols.Tls13) {} + public SslContext() : this( +#if NET6_0_OR_GREATER + SslProtocols.Tls13 +#else + SslProtocols.Tls12 +#endif + ) {} /// /// Initialize SSL context with given protocols /// diff --git a/source/NetCoreServer/SslSession.cs b/source/NetCoreServer/SslSession.cs index 4a22cb9..1305e35 100644 --- a/source/NetCoreServer/SslSession.cs +++ b/source/NetCoreServer/SslSession.cs @@ -106,12 +106,14 @@ internal void Connect(Socket socket) // Apply the option: keep alive if (Server.OptionKeepAlive) Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); +#if NET6_0_OR_GREATER if (Server.OptionTcpKeepAliveTime >= 0) Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, Server.OptionTcpKeepAliveTime); if (Server.OptionTcpKeepAliveInterval >= 0) Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, Server.OptionTcpKeepAliveInterval); if (Server.OptionTcpKeepAliveRetryCount >= 0) Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, Server.OptionTcpKeepAliveRetryCount); +#endif // Apply the option: no delay if (Server.OptionNoDelay) Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true); diff --git a/source/NetCoreServer/TcpClient.cs b/source/NetCoreServer/TcpClient.cs index feb0df8..24e140f 100644 --- a/source/NetCoreServer/TcpClient.cs +++ b/source/NetCoreServer/TcpClient.cs @@ -250,12 +250,14 @@ public virtual bool Connect() // Apply the option: keep alive if (OptionKeepAlive) Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); +#if NET6_0_OR_GREATER if (OptionTcpKeepAliveTime >= 0) Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, OptionTcpKeepAliveTime); if (OptionTcpKeepAliveInterval >= 0) Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, OptionTcpKeepAliveInterval); if (OptionTcpKeepAliveRetryCount >= 0) Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, OptionTcpKeepAliveRetryCount); +#endif // Apply the option: no delay if (OptionNoDelay) Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true); @@ -791,12 +793,14 @@ private void ProcessConnect(SocketAsyncEventArgs e) // Apply the option: keep alive if (OptionKeepAlive) Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); +#if NET6_0_OR_GREATER if (OptionTcpKeepAliveTime >= 0) Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, OptionTcpKeepAliveTime); if (OptionTcpKeepAliveInterval >= 0) Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, OptionTcpKeepAliveInterval); if (OptionTcpKeepAliveRetryCount >= 0) Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, OptionTcpKeepAliveRetryCount); +#endif // Apply the option: no delay if (OptionNoDelay) Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true); diff --git a/source/NetCoreServer/TcpSession.cs b/source/NetCoreServer/TcpSession.cs index 7ae9e1b..442ca36 100644 --- a/source/NetCoreServer/TcpSession.cs +++ b/source/NetCoreServer/TcpSession.cs @@ -103,12 +103,14 @@ internal void Connect(Socket socket) // Apply the option: keep alive if (Server.OptionKeepAlive) Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); +#if NET6_0_OR_GREATER if (Server.OptionTcpKeepAliveTime >= 0) Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, Server.OptionTcpKeepAliveTime); if (Server.OptionTcpKeepAliveInterval >= 0) Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, Server.OptionTcpKeepAliveInterval); if (Server.OptionTcpKeepAliveRetryCount >= 0) Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, Server.OptionTcpKeepAliveRetryCount); +#endif // Apply the option: no delay if (Server.OptionNoDelay) Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true); diff --git a/source/NetCoreServer/UdpClient.cs b/source/NetCoreServer/UdpClient.cs index dcfb339..92b182a 100644 --- a/source/NetCoreServer/UdpClient.cs +++ b/source/NetCoreServer/UdpClient.cs @@ -457,7 +457,11 @@ public virtual long Send(EndPoint endpoint, ReadOnlySpan buffer) try { // Sent datagram to the server +#if NET6_0_OR_GREATER long sent = Socket.SendTo(buffer, SocketFlags.None, endpoint); +#else + long sent = Socket.SendTo(buffer.ToArray(), SocketFlags.None, endpoint); +#endif if (sent > 0) { // Update statistic diff --git a/source/NetCoreServer/UdpServer.cs b/source/NetCoreServer/UdpServer.cs index 183b9ef..0c40ca9 100644 --- a/source/NetCoreServer/UdpServer.cs +++ b/source/NetCoreServer/UdpServer.cs @@ -473,7 +473,11 @@ public virtual long Send(EndPoint endpoint, ReadOnlySpan buffer) try { // Sent datagram to the client +#if NET6_0_OR_GREATER long sent = Socket.SendTo(buffer, SocketFlags.None, endpoint); +#else + long sent = Socket.SendTo(buffer.ToArray(), SocketFlags.None, endpoint); +#endif if (sent > 0) { // Update statistic