@@ -1861,39 +1861,42 @@ channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
18611861 return OK ;
18621862}
18631863
1864+ /*
1865+ * Try to fill the buffer of "reader".
1866+ * Returns FALSE when nothing was added.
1867+ */
18641868 static int
18651869channel_fill (js_read_T * reader )
18661870{
18671871 channel_T * channel = (channel_T * )reader -> js_cookie ;
18681872 ch_part_T part = reader -> js_cookie_arg ;
18691873 char_u * next = channel_get (channel , part );
1870- int unused ;
1871- int len ;
1874+ int keeplen ;
1875+ int addlen ;
18721876 char_u * p ;
18731877
18741878 if (next == NULL )
18751879 return FALSE;
18761880
1877- unused = reader -> js_end - reader -> js_buf - reader -> js_used ;
1878- if (unused > 0 )
1881+ keeplen = reader -> js_end - reader -> js_buf ;
1882+ if (keeplen > 0 )
18791883 {
18801884 /* Prepend unused text. */
1881- len = (int )STRLEN (next );
1882- p = alloc (unused + len + 1 );
1885+ addlen = (int )STRLEN (next );
1886+ p = alloc (keeplen + addlen + 1 );
18831887 if (p == NULL )
18841888 {
18851889 vim_free (next );
18861890 return FALSE;
18871891 }
1888- mch_memmove (p , reader -> js_buf + reader -> js_used , unused );
1889- mch_memmove (p + unused , next , len + 1 );
1892+ mch_memmove (p , reader -> js_buf , keeplen );
1893+ mch_memmove (p + keeplen , next , addlen + 1 );
18901894 vim_free (next );
18911895 next = p ;
18921896 }
18931897
18941898 vim_free (reader -> js_buf );
18951899 reader -> js_buf = next ;
1896- reader -> js_used = 0 ;
18971900 return TRUE;
18981901}
18991902
@@ -1973,16 +1976,20 @@ channel_parse_json(channel_T *channel, ch_part_T part)
19731976 }
19741977
19751978 if (status == OK )
1976- chanpart -> ch_waiting = FALSE ;
1979+ chanpart -> ch_wait_len = 0 ;
19771980 else if (status == MAYBE )
19781981 {
1979- if (!chanpart -> ch_waiting )
1982+ size_t buflen = STRLEN (reader .js_buf );
1983+
1984+ if (chanpart -> ch_wait_len < buflen )
19801985 {
1981- /* First time encountering incomplete message, set a deadline of
1982- * 100 msec. */
1983- ch_log (channel , "Incomplete message - wait for more" );
1986+ /* First time encountering incomplete message or after receiving
1987+ * more (but still incomplete): set a deadline of 100 msec. */
1988+ ch_logn (channel ,
1989+ "Incomplete message (%d bytes) - wait 100 msec for more" ,
1990+ buflen );
19841991 reader .js_used = 0 ;
1985- chanpart -> ch_waiting = TRUE ;
1992+ chanpart -> ch_wait_len = buflen ;
19861993#ifdef WIN32
19871994 chanpart -> ch_deadline = GetTickCount () + 100L ;
19881995#else
@@ -2013,7 +2020,8 @@ channel_parse_json(channel_T *channel, ch_part_T part)
20132020 if (timeout )
20142021 {
20152022 status = FAIL ;
2016- chanpart -> ch_waiting = FALSE;
2023+ chanpart -> ch_wait_len = 0 ;
2024+ ch_log (channel , "timed out" );
20172025 }
20182026 else
20192027 {
@@ -2027,7 +2035,7 @@ channel_parse_json(channel_T *channel, ch_part_T part)
20272035 {
20282036 ch_error (channel , "Decoding failed - discarding input" );
20292037 ret = FALSE;
2030- chanpart -> ch_waiting = FALSE ;
2038+ chanpart -> ch_wait_len = 0 ;
20312039 }
20322040 else if (reader .js_buf [reader .js_used ] != NUL )
20332041 {
@@ -3394,7 +3402,7 @@ channel_read_json_block(
33943402 /* Wait for up to the timeout. If there was an incomplete message
33953403 * use the deadline for that. */
33963404 timeout = timeout_arg ;
3397- if (chanpart -> ch_waiting )
3405+ if (chanpart -> ch_wait_len > 0 )
33983406 {
33993407#ifdef WIN32
34003408 timeout = chanpart -> ch_deadline - GetTickCount () + 1 ;
@@ -3414,7 +3422,7 @@ channel_read_json_block(
34143422 {
34153423 /* Something went wrong, channel_parse_json() didn't
34163424 * discard message. Cancel waiting. */
3417- chanpart -> ch_waiting = FALSE ;
3425+ chanpart -> ch_wait_len = 0 ;
34183426 timeout = timeout_arg ;
34193427 }
34203428 else if (timeout > timeout_arg )
0 commit comments