Skip to content

Commit

Permalink
Add support of receive buffer limit & send buffer limit
Browse files Browse the repository at this point in the history
  • Loading branch information
chronoxor committed Jun 4, 2021
1 parent 4fe5800 commit cb854eb
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 1 deletion.
2 changes: 1 addition & 1 deletion source/NetCoreServer/NetCoreServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Version>5.0.18.0</Version>
<Version>5.1.0.0</Version>
<Authors>Ivan Shynkarenka</Authors>
<Copyright>Copyright (c) 2019-2021 Ivan Shynkarenka</Copyright>
<RepositoryUrl>https://github.com/chronoxor/NetCoreServer</RepositoryUrl>
Expand Down
25 changes: 25 additions & 0 deletions source/NetCoreServer/SslClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,18 @@ public SslClient(SslContext context, IPEndPoint endpoint)
/// </remarks>
public bool OptionNoDelay { get; set; }
/// <summary>
/// Option: receive buffer limit
/// </summary>
public int OptionReceiveBufferLimit { get; set; } = 0;
/// <summary>
/// Option: receive buffer size
/// </summary>
public int OptionReceiveBufferSize { get; set; } = 8192;
/// <summary>
/// Option: send buffer limit
/// </summary>
public int OptionSendBufferLimit { get; set; } = 0;
/// <summary>
/// Option: send buffer size
/// </summary>
public int OptionSendBufferSize { get; set; } = 8192;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions source/NetCoreServer/SslSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,19 @@ public SslSession(SslServer server)
/// </summary>
public long BytesReceived { get; private set; }

/// <summary>
/// Option: receive buffer limit
/// </summary>
public int OptionReceiveBufferLimit { get; set; } = 0;
/// <summary>
/// Option: receive buffer size
/// </summary>
public int OptionReceiveBufferSize { get; set; } = 8192;
/// <summary>
/// Option: send buffer limit
/// </summary>
public int OptionSendBufferLimit { get; set; } = 0;
/// <summary>
/// Option: send buffer size
/// </summary>
public int OptionSendBufferSize { get; set; } = 8192;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions source/NetCoreServer/TcpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,18 @@ public TcpClient(IPEndPoint endpoint)
/// </remarks>
public bool OptionNoDelay { get; set; }
/// <summary>
/// Option: receive buffer limit
/// </summary>
public int OptionReceiveBufferLimit { get; set; } = 0;
/// <summary>
/// Option: receive buffer size
/// </summary>
public int OptionReceiveBufferSize { get; set; } = 8192;
/// <summary>
/// Option: send buffer limit
/// </summary>
public int OptionSendBufferLimit { get; set; } = 0;
/// <summary>
/// Option: send buffer size
/// </summary>
public int OptionSendBufferSize { get; set; } = 8192;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions source/NetCoreServer/TcpSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,19 @@ public TcpSession(TcpServer server)
/// </summary>
public long BytesReceived { get; private set; }

/// <summary>
/// Option: receive buffer limit
/// </summary>
public int OptionReceiveBufferLimit { get; set; } = 0;
/// <summary>
/// Option: receive buffer size
/// </summary>
public int OptionReceiveBufferSize { get; set; } = 8192;
/// <summary>
/// Option: send buffer limit
/// </summary>
public int OptionSendBufferLimit { get; set; } = 0;
/// <summary>
/// Option: send buffer size
/// </summary>
public int OptionSendBufferSize { get; set; } = 8192;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions source/NetCoreServer/UdpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,18 @@ public UdpClient(IPEndPoint endpoint)
/// </summary>
public bool OptionMulticast { get; set; }
/// <summary>
/// Option: receive buffer limit
/// </summary>
public int OptionReceiveBufferLimit { get; set; } = 0;
/// <summary>
/// Option: receive buffer size
/// </summary>
public int OptionReceiveBufferSize { get; set; } = 8192;
/// <summary>
/// Option: send buffer limit
/// </summary>
public int OptionSendBufferLimit { get; set; } = 0;
/// <summary>
/// Option: send buffer size
/// </summary>
public int OptionSendBufferSize { get; set; } = 8192;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}
}

/// <summary>
Expand Down
28 changes: 28 additions & 0 deletions source/NetCoreServer/UdpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,18 @@ public UdpServer(IPEndPoint endpoint)
/// </remarks>
public bool OptionExclusiveAddressUse { get; set; }
/// <summary>
/// Option: receive buffer limit
/// </summary>
public int OptionReceiveBufferLimit { get; set; } = 0;
/// <summary>
/// Option: receive buffer size
/// </summary>
public int OptionReceiveBufferSize { get; set; } = 8192;
/// <summary>
/// Option: send buffer limit
/// </summary>
public int OptionSendBufferLimit { get; set; } = 0;
/// <summary>
/// Option: send buffer size
/// </summary>
public int OptionSendBufferSize { get; set; } = 8192;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}
}

/// <summary>
Expand Down

0 comments on commit cb854eb

Please sign in to comment.