@@ -1840,39 +1840,42 @@ channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
18401840 return OK ;
18411841}
18421842
1843+ /*
1844+ * Try to fill the buffer of "reader".
1845+ * Returns FALSE when nothing was added.
1846+ */
18431847 static int
18441848channel_fill (js_read_T * reader )
18451849{
18461850 channel_T * channel = (channel_T * )reader -> js_cookie ;
18471851 ch_part_T part = reader -> js_cookie_arg ;
18481852 char_u * next = channel_get (channel , part );
1849- int unused ;
1850- int len ;
1853+ int keeplen ;
1854+ int addlen ;
18511855 char_u * p ;
18521856
18531857 if (next == NULL )
18541858 return FALSE;
18551859
1856- unused = reader -> js_end - reader -> js_buf - reader -> js_used ;
1857- if (unused > 0 )
1860+ keeplen = reader -> js_end - reader -> js_buf ;
1861+ if (keeplen > 0 )
18581862 {
18591863 /* Prepend unused text. */
1860- len = (int )STRLEN (next );
1861- p = alloc (unused + len + 1 );
1864+ addlen = (int )STRLEN (next );
1865+ p = alloc (keeplen + addlen + 1 );
18621866 if (p == NULL )
18631867 {
18641868 vim_free (next );
18651869 return FALSE;
18661870 }
1867- mch_memmove (p , reader -> js_buf + reader -> js_used , unused );
1868- mch_memmove (p + unused , next , len + 1 );
1871+ mch_memmove (p , reader -> js_buf , keeplen );
1872+ mch_memmove (p + keeplen , next , addlen + 1 );
18691873 vim_free (next );
18701874 next = p ;
18711875 }
18721876
18731877 vim_free (reader -> js_buf );
18741878 reader -> js_buf = next ;
1875- reader -> js_used = 0 ;
18761879 return TRUE;
18771880}
18781881
@@ -1952,16 +1955,20 @@ channel_parse_json(channel_T *channel, ch_part_T part)
19521955 }
19531956
19541957 if (status == OK )
1955- chanpart -> ch_waiting = FALSE ;
1958+ chanpart -> ch_wait_len = 0 ;
19561959 else if (status == MAYBE )
19571960 {
1958- if (!chanpart -> ch_waiting )
1961+ size_t buflen = STRLEN (reader .js_buf );
1962+
1963+ if (chanpart -> ch_wait_len < buflen )
19591964 {
1960- /* First time encountering incomplete message, set a deadline of
1961- * 100 msec. */
1962- ch_log (channel , "Incomplete message - wait for more" );
1965+ /* First time encountering incomplete message or after receiving
1966+ * more (but still incomplete): set a deadline of 100 msec. */
1967+ ch_logn (channel ,
1968+ "Incomplete message (%d bytes) - wait 100 msec for more" ,
1969+ buflen );
19631970 reader .js_used = 0 ;
1964- chanpart -> ch_waiting = TRUE ;
1971+ chanpart -> ch_wait_len = buflen ;
19651972#ifdef WIN32
19661973 chanpart -> ch_deadline = GetTickCount () + 100L ;
19671974#else
@@ -1992,7 +1999,8 @@ channel_parse_json(channel_T *channel, ch_part_T part)
19921999 if (timeout )
19932000 {
19942001 status = FAIL ;
1995- chanpart -> ch_waiting = FALSE;
2002+ chanpart -> ch_wait_len = 0 ;
2003+ ch_log (channel , "timed out" );
19962004 }
19972005 else
19982006 {
@@ -2006,7 +2014,7 @@ channel_parse_json(channel_T *channel, ch_part_T part)
20062014 {
20072015 ch_error (channel , "Decoding failed - discarding input" );
20082016 ret = FALSE;
2009- chanpart -> ch_waiting = FALSE ;
2017+ chanpart -> ch_wait_len = 0 ;
20102018 }
20112019 else if (reader .js_buf [reader .js_used ] != NUL )
20122020 {
@@ -3369,7 +3377,7 @@ channel_read_json_block(
33693377 /* Wait for up to the timeout. If there was an incomplete message
33703378 * use the deadline for that. */
33713379 timeout = timeout_arg ;
3372- if (chanpart -> ch_waiting )
3380+ if (chanpart -> ch_wait_len > 0 )
33733381 {
33743382#ifdef WIN32
33753383 timeout = chanpart -> ch_deadline - GetTickCount () + 1 ;
@@ -3389,7 +3397,7 @@ channel_read_json_block(
33893397 {
33903398 /* Something went wrong, channel_parse_json() didn't
33913399 * discard message. Cancel waiting. */
3392- chanpart -> ch_waiting = FALSE ;
3400+ chanpart -> ch_wait_len = 0 ;
33933401 timeout = timeout_arg ;
33943402 }
33953403 else if (timeout > timeout_arg )
0 commit comments