From c19d05c64ee05377bb6b92d0b7b0b7648a60ebe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Sun, 14 Jul 2019 20:26:29 +0300 Subject: [PATCH] Change handling of msgid output from backend --- ethermess-backend.c | 32 +++++++++++++++++--------------- ethermess.py | 10 ++++++++-- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/ethermess-backend.c b/ethermess-backend.c index 926c64d..930307e 100644 --- a/ethermess-backend.c +++ b/ethermess-backend.c @@ -425,6 +425,15 @@ void writeallx(int fd, const void *buf, size_t length) { } } +void writeallx_u16(int fd, uint16_t value) { + // Write these little-endian (native for x86) instead of network + // byte order + unsigned char buf[2]; + buf[0] = value & 0xff; + buf[1] = value >> 8; + writeallx(fd, buf, sizeof(buf)); +} + bool check_utf8(const unsigned char *data, size_t data_length, bool newline_allowed); void read_status(void) { @@ -485,10 +494,6 @@ void read_command(void) { send_status_request(mac); } else if (cmd == 'S') { read_status(); - } else if (cmd == 'i') { - unsigned char mac[6]; - readallx(0, mac, sizeof(mac)); - send_msgid_request(mac); } else if (cmd == 'm') { read_message(); } else { @@ -704,17 +709,6 @@ void handle_msgid(const unsigned char source_mac[6], const unsigned char *data, msgid_cache[cache_index].next_send = msgid; msgid_cache[cache_index].know_send = true; } - - char mac[18]; - format_mac(source_mac, mac); - - if (printf("%s awaits message ID %" PRIu16 "\n", mac, msgid_cache[cache_index].next_send) == 0) { - err(1, "printf"); - } - - if (fflush(stdout) == EOF) { - err(1, "fflush"); - } } void handle_message(const unsigned char source_mac[6], const unsigned char *data, size_t data_length) { @@ -977,7 +971,15 @@ void eventloop(void) { } if (own_message_send_state == SENDING) { + // Type of event: msgid for a message + writeallx(1, "i", 1); + + // Msgid + writeallx_u16(1, own_message_msgid); + + // Send message send_message(); + // Wait around 1 to 1.5 before sending again next_retransmission = ms_in_future(EM_RETRANSMIT_TIME); retransmission_count = 0; diff --git a/ethermess.py b/ethermess.py index 9e6fd69..d056bb4 100644 --- a/ethermess.py +++ b/ethermess.py @@ -59,7 +59,7 @@ def handle_user_command(backend, command): writeall(backend, b'm' + mac + bytes([len(message) >> 8, len(message) & 0xff]) + message) else: - print('s - request status, i - request msgid, m - send message, ^D - quit') + print('s - request status, m - send message, ^D - quit') def eventloop(proc): # Create unbuffered version of stdin @@ -86,6 +86,12 @@ def eventloop(proc): print('%s (%s) ~%s' % (format_mac(source_mac), format_status(status), nick.decode('utf-8'))) + elif event_type == b'i': + # Msgid for message + msgid_bytes = readall(proc.stdout, 2) + msgid = (msgid_bytes[1] << 8) | msgid_bytes[0] + print('Msgid: %i' % msgid) + else: # Not sth we handle yet data = proc.stdout.read(1023) @@ -134,7 +140,7 @@ def main(): print('Own mac: %s' % format_mac(mac)) - print('s - request status, i - request msgid, m - send message, ^D - quit') + print('s - request status, m - send message, ^D - quit') eventloop(proc)