Repository: microsoft/garnet
File: libs/common/Networking/TcpNetworkHandlerBase.cs (C#)

Code before:

        void RecvEventArg_Completed(object sender, SocketAsyncEventArgs e)
        {
            // Complete receive event and release thread while we process data async
            _ = HandleReceiveAsync(sender, e);
        }

        private async ValueTask HandleReceiveAsync(object sender, SocketAsyncEventArgs e)
        {
            try
            {
                do
                {
                    if (e.BytesTransferred == 0 || e.SocketError != SocketError.Success || serverHook.Disposed)
                    {
                        // No more things to receive
                        Dispose(e);
                        return;
                    }
                    var receiveTask = OnNetworkReceiveAsync(e.BytesTransferred);
                    if (!receiveTask.IsCompleted)
                    {
                        await receiveTask;
                    }
                    NetworkBufferProcessed(sender, e);
                }
                while (!e.AcceptSocket.ReceiveAsync(e));
            }
            catch (Exception ex)
            {
                HandleReceiveFailure(ex, e);
            }
        }

        void NetworkBufferProcessed(object sender, SocketAsyncEventArgs e)
        {
            e.SetBuffer(networkReceiveBuffer, networkBytesRead, networkReceiveBuffer.Length - networkBytesRead);
        }

        void HandleReceiveFailure(Exception ex, SocketAsyncEventArgs e)
        {
            if (ex is ObjectDisposedException ex2 && ex2.ObjectName == "System.Net.Sockets.Socket")
                logger?.LogTrace("Accept socket was disposed at RecvEventArg_Completed");
            else
                logger?.LogError(ex, "An error occurred at RecvEventArg_Completed");
            Dispose(e);
        }

        unsafe void AllocateNetworkReceiveBuffer()
        {
            networkReceiveBufferEntry = networkPool.Get(networkBufferSettings.initialReceiveBufferSize);

Review comment (suggestion):
nit: `while` should be on same line as `}` to maintain parity with style on `main`:

```
  do
  {
  } while (!e.AcceptSocket.ReceiveAsync(e));
```

Code after:
            DisposeImpl();
            e.Dispose();
        }

        void RecvEventArgCompletedWithTLS(object sender, SocketAsyncEventArgs e) =>
            _ = HandleReceiveWithTLSAsync(sender, e);

        void RecvEventArgCompletedWithoutTLS(object sender, SocketAsyncEventArgs e) =>
            HandleReceiveWithoutTLS(sender, e);

        private void HandleReceiveWithoutTLS(object sender, SocketAsyncEventArgs e)
        {
            try
            {
                do
                {
                    if (e.BytesTransferred == 0 || e.SocketError != SocketError.Success || serverHook.Disposed)
                    {
                        // No more things to receive
                        Dispose(e);
                        break;
                    }
                    OnNetworkReceiveWithoutTLS(e.BytesTransferred);
                    e.SetBuffer(networkReceiveBuffer, networkBytesRead, networkReceiveBuffer.Length - networkBytesRead);
                } while (!e.AcceptSocket.ReceiveAsync(e));
            }
            catch (Exception ex)
            {
                HandleReceiveFailure(ex, e);
            }
        }

        private async ValueTask HandleReceiveWithTLSAsync(object sender, SocketAsyncEventArgs e)
        {
            try
            {
                do
                {
                    if (e.BytesTransferred == 0 || e.SocketError != SocketError.Success || serverHook.Disposed)
                    {
                        // No more things to receive
                        Dispose(e);
                        break;
                    }
                    var receiveTask = OnNetworkReceiveWithTLSAsync(e.BytesTransferred);
                    if (!receiveTask.IsCompletedSuccessfully)
                    {
                        await receiveTask;
                    }
                    e.SetBuffer(networkReceiveBuffer, networkBytesRead, networkReceiveBuffer.Length - networkBytesRead);
                } while (!e.AcceptSocket.ReceiveAsync(e));