Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TcpSession.SendAsync() returns true even if a message fails to send. BytesSent also increases when send fails. #299

Open
crav12345 opened this issue May 13, 2024 · 0 comments

Comments

@crav12345
Copy link

crav12345 commented May 13, 2024

In an instance where my server is told to send multiple TCP messages on the same clock tick, the lock statement in SendAsync() appears to prevent the second message from sending. However, the method still returns true when the second message fails to send. Spacing out the SendAsync() calls allows the second message to send, but is there a way to determine whether a message actually sends for cases where the lock blocks one? That way, I could just try to send the message until it successfully sends or until I say to stop trying.

I tried doing this with BytesSent, but the value appears to increase even in these cases where the message doesn't actually send. I don't know if this is a bug or me using the method incorrectly.

NetCoreServer.TcpSession

public virtual bool SendAsync(ReadOnlySpan<byte> buffer)
{
    if (!IsConnected)
    {
        return false;
    }

    if (buffer.IsEmpty)
    {
        return true;
    }

    // ----------------------------------------------------------------------------------------
    // Prevents second message from sending on same tick. Method still returns true.
    // ----------------------------------------------------------------------------------------
    lock (_sendLock)
    {
        if (_sendBufferMain.Size + buffer.Length > OptionSendBufferLimit && OptionSendBufferLimit > 0)
        {
            SendError(SocketError.NoBufferSpaceAvailable);
            return false;
        }

        _sendBufferMain.Append(buffer);
        BytesPending = _sendBufferMain.Size;
        if (_sending)
        {
            return true;
        }

        _sending = true;
        TrySend();
    }

    return true;
}
@crav12345 crav12345 changed the title TcpSession.SendAsync() returns true even if a message fails to send due to lock statement TcpSession.SendAsync() returns true even if a message fails to send. BytesSent also increases when send fails. May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant