AmendHub

Download:

jcs

/

subtext

/

amendments

/

494

session: Add session_get_chars to take multiple chars at once


jcs made amendment 494 12 months ago
--- session.c Mon Apr 10 22:29:54 2023 +++ session.c Thu Apr 27 09:08:50 2023 @@ -798,34 +798,47 @@ session_idled_out(struct session *session) short session_get_char(struct session *session, unsigned char *b) { - if (session->ibuflen == 0) { - if (session_idled_out(session)) - return 0; + return session_get_chars(session, b, 1); +} + +short +session_get_chars(struct session *session, unsigned char *b, size_t count) +{ + short ret = 0; + + while (ret < count) { + if (session->ibuflen == 0) { + if (session_idled_out(session)) + return 0; + + uthread_yield(); + session->node_funcs->input(session); - uthread_yield(); - session->node_funcs->input(session); + if (session->ending || session->abort_input) + return 0; + if (session->ibuflen == 0) + return 0; + } - if (session->ending || session->abort_input) - return 0; - if (session->ibuflen == 0) - return 0; + session->last_input_at = Time; + + *b = session->ibuf[session->ibufoff]; + b++; + + if (session->ibuflen == 1) { + session->ibuflen = 0; + session->ibufoff = 0; + } else { + session->ibuflen--; + session->ibufoff++; + } + + ret++; } - session->last_input_at = Time; - - *b = session->ibuf[session->ibufoff]; - - if (session->ibuflen == 1) { - session->ibuflen = 0; - session->ibufoff = 0; - } else { - session->ibuflen--; - session->ibufoff++; - } - session_check_buf_canaries(session); - return 1; + return ret; } bool --- session.h Wed Apr 26 10:02:46 2023 +++ session.h Thu Apr 27 09:09:23 2023 @@ -128,6 +128,8 @@ struct session * session_create(char *node, char *via, void session_close(struct session *session); void session_idle(struct session *session); short session_get_char(struct session *session, unsigned char *b); +short session_get_chars(struct session *session, unsigned char *b, + size_t count); bool session_wait_for_chars(struct session *session, unsigned short timeout_ms, unsigned short num_chars); unsigned short session_input_char(struct session *session);