diff --git a/source/NetCoreServer/NetCoreServer.csproj b/source/NetCoreServer/NetCoreServer.csproj index fdfe818..4e5923e 100644 --- a/source/NetCoreServer/NetCoreServer.csproj +++ b/source/NetCoreServer/NetCoreServer.csproj @@ -2,7 +2,7 @@ net5.0 - 5.0.18.0 + 5.1.0.0 Ivan Shynkarenka Copyright (c) 2019-2021 Ivan Shynkarenka https://github.com/chronoxor/NetCoreServer diff --git a/source/NetCoreServer/SslClient.cs b/source/NetCoreServer/SslClient.cs index 9bb8c0d..d4aa458 100644 --- a/source/NetCoreServer/SslClient.cs +++ b/source/NetCoreServer/SslClient.cs @@ -104,10 +104,18 @@ public SslClient(SslContext context, IPEndPoint endpoint) /// public bool OptionNoDelay { get; set; } /// + /// Option: receive buffer limit + /// + public int OptionReceiveBufferLimit { get; set; } = 0; + /// /// Option: receive buffer size /// public int OptionReceiveBufferSize { get; set; } = 8192; /// + /// Option: send buffer limit + /// + public int OptionSendBufferLimit { get; set; } = 0; + /// /// Option: send buffer size /// public int OptionSendBufferSize { get; set; } = 8192; @@ -519,6 +527,13 @@ public virtual bool SendAsync(byte[] buffer, long offset, long size) lock (_sendLock) { + // Check the send buffer limit + if (((_sendBufferMain.Size + size) > OptionSendBufferLimit) && (OptionSendBufferLimit > 0)) + { + SendError(SocketError.NoBufferSpaceAvailable); + return false; + } + // Fill the main send buffer _sendBufferMain.Append(buffer, offset, size); @@ -866,7 +881,17 @@ private void ProcessReceive(IAsyncResult result) // If the receive buffer is full increase its size if (_receiveBuffer.Capacity == size) + { + // Check the receive buffer limit + if (((2 * size) > OptionReceiveBufferLimit) && (OptionReceiveBufferLimit > 0)) + { + SendError(SocketError.NoBufferSpaceAvailable); + DisconnectAsync(); + return; + } + _receiveBuffer.Reserve(2 * size); + } } _receiving = false; diff --git a/source/NetCoreServer/SslSession.cs b/source/NetCoreServer/SslSession.cs index e94005e..98a8e96 100644 --- a/source/NetCoreServer/SslSession.cs +++ b/source/NetCoreServer/SslSession.cs @@ -56,11 +56,19 @@ public SslSession(SslServer server) /// public long BytesReceived { get; private set; } + /// + /// Option: receive buffer limit + /// + public int OptionReceiveBufferLimit { get; set; } = 0; /// /// Option: receive buffer size /// public int OptionReceiveBufferSize { get; set; } = 8192; /// + /// Option: send buffer limit + /// + public int OptionSendBufferLimit { get; set; } = 0; + /// /// Option: send buffer size /// public int OptionSendBufferSize { get; set; } = 8192; @@ -320,6 +328,13 @@ public virtual bool SendAsync(byte[] buffer, long offset, long size) lock (_sendLock) { + // Check the send buffer limit + if (((_sendBufferMain.Size + size) > OptionSendBufferLimit) && (OptionSendBufferLimit > 0)) + { + SendError(SocketError.NoBufferSpaceAvailable); + return false; + } + // Fill the main send buffer _sendBufferMain.Append(buffer, offset, size); @@ -589,7 +604,17 @@ private void ProcessReceive(IAsyncResult result) // If the receive buffer is full increase its size if (_receiveBuffer.Capacity == size) + { + // Check the receive buffer limit + if (((2 * size) > OptionReceiveBufferLimit) && (OptionReceiveBufferLimit > 0)) + { + SendError(SocketError.NoBufferSpaceAvailable); + Disconnect(); + return; + } + _receiveBuffer.Reserve(2 * size); + } } _receiving = false; diff --git a/source/NetCoreServer/TcpClient.cs b/source/NetCoreServer/TcpClient.cs index f41b60f..4255fa3 100644 --- a/source/NetCoreServer/TcpClient.cs +++ b/source/NetCoreServer/TcpClient.cs @@ -89,10 +89,18 @@ public TcpClient(IPEndPoint endpoint) /// public bool OptionNoDelay { get; set; } /// + /// Option: receive buffer limit + /// + public int OptionReceiveBufferLimit { get; set; } = 0; + /// /// Option: receive buffer size /// public int OptionReceiveBufferSize { get; set; } = 8192; /// + /// Option: send buffer limit + /// + public int OptionSendBufferLimit { get; set; } = 0; + /// /// Option: send buffer size /// public int OptionSendBufferSize { get; set; } = 8192; @@ -459,6 +467,13 @@ public virtual bool SendAsync(byte[] buffer, long offset, long size) lock (_sendLock) { + // Check the send buffer limit + if (((_sendBufferMain.Size + size) > OptionSendBufferLimit) && (OptionSendBufferLimit > 0)) + { + SendError(SocketError.NoBufferSpaceAvailable); + return false; + } + // Fill the main send buffer _sendBufferMain.Append(buffer, offset, size); @@ -761,7 +776,17 @@ private bool ProcessReceive(SocketAsyncEventArgs e) // If the receive buffer is full increase its size if (_receiveBuffer.Capacity == size) + { + // Check the receive buffer limit + if (((2 * size) > OptionReceiveBufferLimit) && (OptionReceiveBufferLimit > 0)) + { + SendError(SocketError.NoBufferSpaceAvailable); + DisconnectAsync(); + return false; + } + _receiveBuffer.Reserve(2 * size); + } } _receiving = false; diff --git a/source/NetCoreServer/TcpSession.cs b/source/NetCoreServer/TcpSession.cs index 027d7cc..76625e0 100644 --- a/source/NetCoreServer/TcpSession.cs +++ b/source/NetCoreServer/TcpSession.cs @@ -55,11 +55,19 @@ public TcpSession(TcpServer server) /// public long BytesReceived { get; private set; } + /// + /// Option: receive buffer limit + /// + public int OptionReceiveBufferLimit { get; set; } = 0; /// /// Option: receive buffer size /// public int OptionReceiveBufferSize { get; set; } = 8192; /// + /// Option: send buffer limit + /// + public int OptionSendBufferLimit { get; set; } = 0; + /// /// Option: send buffer size /// public int OptionSendBufferSize { get; set; } = 8192; @@ -294,6 +302,13 @@ public virtual bool SendAsync(byte[] buffer, long offset, long size) lock (_sendLock) { + // Check the send buffer limit + if (((_sendBufferMain.Size + size) > OptionSendBufferLimit) && (OptionSendBufferLimit > 0)) + { + SendError(SocketError.NoBufferSpaceAvailable); + return false; + } + // Fill the main send buffer _sendBufferMain.Append(buffer, offset, size); @@ -543,7 +558,17 @@ private bool ProcessReceive(SocketAsyncEventArgs e) // If the receive buffer is full increase its size if (_receiveBuffer.Capacity == size) + { + // Check the receive buffer limit + if (((2 * size) > OptionReceiveBufferLimit) && (OptionReceiveBufferLimit > 0)) + { + SendError(SocketError.NoBufferSpaceAvailable); + Disconnect(); + return false; + } + _receiveBuffer.Reserve(2 * size); + } } _receiving = false; diff --git a/source/NetCoreServer/UdpClient.cs b/source/NetCoreServer/UdpClient.cs index 63a4b8d..dc092fb 100644 --- a/source/NetCoreServer/UdpClient.cs +++ b/source/NetCoreServer/UdpClient.cs @@ -100,10 +100,18 @@ public UdpClient(IPEndPoint endpoint) /// public bool OptionMulticast { get; set; } /// + /// Option: receive buffer limit + /// + public int OptionReceiveBufferLimit { get; set; } = 0; + /// /// Option: receive buffer size /// public int OptionReceiveBufferSize { get; set; } = 8192; /// + /// Option: send buffer limit + /// + public int OptionSendBufferLimit { get; set; } = 0; + /// /// Option: send buffer size /// public int OptionSendBufferSize { get; set; } = 8192; @@ -486,6 +494,13 @@ public virtual bool SendAsync(EndPoint endpoint, byte[] buffer, long offset, lon if (size == 0) return true; + // Check the send buffer limit + if (((_sendBuffer.Size + size) > OptionSendBufferLimit) && (OptionSendBufferLimit > 0)) + { + SendError(SocketError.NoBufferSpaceAvailable); + return false; + } + // Fill the main send buffer _sendBuffer.Append(buffer, offset, size); @@ -694,7 +709,17 @@ private void ProcessReceiveFrom(SocketAsyncEventArgs e) // If the receive buffer is full increase its size if (_receiveBuffer.Capacity == size) + { + // Check the receive buffer limit + if (((2 * size) > OptionReceiveBufferLimit) && (OptionReceiveBufferLimit > 0)) + { + SendError(SocketError.NoBufferSpaceAvailable); + Disconnect(); + return; + } + _receiveBuffer.Reserve(2 * size); + } } /// diff --git a/source/NetCoreServer/UdpServer.cs b/source/NetCoreServer/UdpServer.cs index 96902ac..dd5e4fb 100644 --- a/source/NetCoreServer/UdpServer.cs +++ b/source/NetCoreServer/UdpServer.cs @@ -101,10 +101,18 @@ public UdpServer(IPEndPoint endpoint) /// public bool OptionExclusiveAddressUse { get; set; } /// + /// Option: receive buffer limit + /// + public int OptionReceiveBufferLimit { get; set; } = 0; + /// /// Option: receive buffer size /// public int OptionReceiveBufferSize { get; set; } = 8192; /// + /// Option: send buffer limit + /// + public int OptionSendBufferLimit { get; set; } = 0; + /// /// Option: send buffer size /// public int OptionSendBufferSize { get; set; } = 8192; @@ -450,6 +458,13 @@ public virtual bool SendAsync(EndPoint endpoint, byte[] buffer, long offset, lon if (size == 0) return true; + // Check the send buffer limit + if (((_sendBuffer.Size + size) > OptionSendBufferLimit) && (OptionSendBufferLimit > 0)) + { + SendError(SocketError.NoBufferSpaceAvailable); + return false; + } + // Fill the main send buffer _sendBuffer.Append(buffer, offset, size); @@ -660,7 +675,20 @@ private void ProcessReceiveFrom(SocketAsyncEventArgs e) // If the receive buffer is full increase its size if (_receiveBuffer.Capacity == size) + { + // Check the receive buffer limit + if (((2 * size) > OptionReceiveBufferLimit) && (OptionReceiveBufferLimit > 0)) + { + SendError(SocketError.NoBufferSpaceAvailable); + + // Call the datagram received zero handler + OnReceived(e.RemoteEndPoint, _receiveBuffer.Data, 0, 0); + + return; + } + _receiveBuffer.Reserve(2 * size); + } } ///