diff --git a/ChangeLog.txt b/ChangeLog.txt index f0a3949db..12acfd342 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -2502,3 +2502,7 @@ Version 1.8.4, in progress builtin functions can now cause other kinds of aborts -- added caching regime for server options other than .protect_ + +Version X, in progress +**** Changes relevant to programmers / wizards: +-- Setting binary mode immediately flushes the connection's buffer, if any diff --git a/net_multi.c b/net_multi.c index f627198ff..bc3996645 100644 --- a/net_multi.c +++ b/net_multi.c @@ -272,16 +272,32 @@ pull_input(nhandle * h) for (ptr = buffer, end = buffer + count; ptr < end; ptr++) { unsigned char c = *ptr; - if (isgraph(c) || c == ' ' || c == '\t') - stream_add_char(s, c); + if (c == '\r' || (c == '\n' && !h->last_input_was_CR)) { + int fcount = stream_length(s); + char *fptr = reset_stream(s); + Stream *fs = new_stream(count + 1); + int i; + + h->last_input_was_CR = (c == '\r'); + for (i = 0; i < fcount; ++i) { + c = fptr[i]; + + if (isgraph(c) || c == ' ' || c == '\t') + stream_add_char(fs, c); #ifdef INPUT_APPLY_BACKSPACE - else if (c == 0x08 || c == 0x7F) - stream_delete_char(s); + else if (c == 0x08 || c == 0x7F) + stream_delete_char(fs); #endif - else if (c == '\r' || (c == '\n' && !h->last_input_was_CR)) - server_receive_line(h->shandle, reset_stream(s)); - - h->last_input_was_CR = (c == '\r'); + } + server_receive_line(h->shandle, stream_contents(fs)); + free_stream(fs); + } + else if (c == '\n') + h->last_input_was_CR = 0; + else { + stream_add_char(s, c); + h->last_input_was_CR = 0; + } } } return 1; @@ -620,6 +636,18 @@ network_set_connection_binary(network_handle nh, int do_binary) nhandle *h = nh.ptr; h->binary = do_binary; + + if (do_binary) { + int count = stream_length(h->input); + + if (count) { + Stream *s = new_stream(count + 1); + stream_add_raw_bytes_to_binary(s, reset_stream(h->input), count); + server_receive_line(h->shandle, stream_contents(s)); + h->last_input_was_CR = 0; + free_stream(s); + } + } } #if NETWORK_PROTOCOL == NP_LOCAL diff --git a/net_single.c b/net_single.c index e7424ba5d..088272b0a 100644 --- a/net_single.c +++ b/net_single.c @@ -33,7 +33,9 @@ static enum { } state = STATE_CLOSED; static int listening = 0; static server_listener slistener; +static server_handle sh; static int binary = 0; +static Stream *input = 0; const char * network_protocol_name(void) @@ -114,6 +116,17 @@ void network_set_connection_binary(network_handle nh, int do_binary) { binary = do_binary; + + if (do_binary) { + int count = stream_length(input); + + if (count) { + Stream *s = new_stream(count + 1); + stream_add_raw_bytes_to_binary(s, reset_stream(input), count); + server_receive_line(sh, stream_contents(s)); + free_stream(s); + } + } } #define NETWORK_CO_TABLE(DEFINE, nh, value, _) @@ -160,8 +173,7 @@ int network_process_io(int timeout) { network_handle nh; - static server_handle sh; - static Stream *s = 0; + Stream *s = input; char buffer[1024]; int count; char *ptr, *end; @@ -202,14 +214,27 @@ network_process_io(int timeout) ptr++) { unsigned char c = *ptr; - if (isgraph(c) || c == ' ' || c == '\t') - stream_add_char(s, c); + if (c == '\n') { + int fcount = stream_length(s); + char *fptr = reset_stream(s); + Stream *fs = new_stream(count + 1); + int i; + + for (i = 0; i < fcount; ++i) { + c = fptr[i]; + + if (isgraph(c) || c == ' ' || c == '\t') + stream_add_char(fs, c); #ifdef INPUT_APPLY_BACKSPACE - else if (c == 0x08 || c == 0x7F) - stream_delete_char(s); + else if (c == 0x08 || c == 0x7F) + stream_delete_char(fs); #endif - else if (c == '\n') - server_receive_line(sh, reset_stream(s)); + } + server_receive_line(sh, stream_contents(fs)); + free_stream(fs); + } + else + stream_add_char(s, c); } }