Skip to content

Commit ae0a5e8

Browse files
committed
fix: improve send_window_update function for better flow control handling
Signed-off-by: Dengfeng Liu <liudf0716@gmail.com>
1 parent 3773f0a commit ae0a5e8

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

tcpmux.c

+17-4
Original file line numberDiff line numberDiff line change
@@ -488,16 +488,29 @@ static uint16_t get_send_flags(struct tmux_stream *stream) {
488488
*
489489
* @note Window size cannot exceed MAX_STREAM_WINDOW_SIZE
490490
*/
491+
/**
492+
* @brief Sends a window update message for stream flow control.
493+
*
494+
* Updates the receive window for a stream and sends a window update message
495+
* if the delta exceeds half of the maximum window size or if there are flags to send.
496+
*
497+
* @param bout Buffered output event for sending data.
498+
* @param stream Pointer to the tmux stream to update.
499+
* @param length Current receive buffer length.
500+
*/
491501
void send_window_update(struct bufferevent *bout, struct tmux_stream *stream, uint32_t length) {
492-
uint32_t max_window = MAX_STREAM_WINDOW_SIZE;
493-
uint32_t delta = max_window - length - stream->recv_window;
502+
const uint32_t max_window = MAX_STREAM_WINDOW_SIZE;
503+
const uint32_t half_max_window = max_window / 2;
504+
uint32_t delta = max_window > (length + stream->recv_window)
505+
? max_window - length - stream->recv_window
506+
: 0;
494507
uint16_t flags = get_send_flags(stream);
495508

496-
if (delta < max_window / 2 && flags == 0) {
509+
if (delta < half_max_window && flags == 0) {
497510
return;
498511
}
499512

500-
stream->recv_window += delta;
513+
stream->recv_window = MIN(stream->recv_window + delta, max_window);
501514
tcp_mux_send_win_update(bout, flags, stream->id, delta);
502515
}
503516

0 commit comments

Comments
 (0)