From 4bbbfb8e8139e690ea084b72ccc2de71fdca1e08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Sun, 2 Aug 2020 03:50:26 -0300 Subject: Simplify encoding and decoding --- src/client/jetc.cpp | 36 +++++++++----------- src/common/file.cpp | 4 +-- src/common/ipc.cpp | 92 ++++++++++++++++++++++++++++++++++----------------- src/common/socket.cpp | 17 +++++++--- src/server/client.cpp | 41 ++++++++++------------- 5 files changed, 109 insertions(+), 81 deletions(-) (limited to 'src') diff --git a/src/client/jetc.cpp b/src/client/jetc.cpp index cdb570e..cfe3db7 100644 --- a/src/client/jetc.cpp +++ b/src/client/jetc.cpp @@ -12,13 +12,13 @@ Socket io; Window window; Cursor cursor(window); +Message msg; void update() { - int8_t msg[5]; - msg[0] = OP_SHOW; - encode2(window.width, msg, 1); - encode2(window.height, msg, 3); - io.send(msg, 5); + msg.encode1(OP_SHOW); + msg.encode2(window.width); + msg.encode2(window.height); + io.send(msg); io.recv(window.view, window.width * window.height); io.recv(window.line_ends, window.height); @@ -27,33 +27,29 @@ void update() { } void move_left() { - int8_t mov[2]; - mov[0] = OP_MOVE1; - mov[1] = -1; - io.send(mov, 2); + msg.encode1(OP_MOVE1); + msg.encode1(-1); + io.send(msg); cursor.move_left(); } void move_right() { - int8_t mov[2]; - mov[0] = OP_MOVE1; - mov[1] = 1; - io.send(mov, 2); + msg.encode1(OP_MOVE1); + msg.encode1(1); + io.send(msg); cursor.move_right(); } void delete_element() { - int8_t del; - del = OP_DELETE; - io.send(&del, 1); + msg.encode1(OP_DELETE); + io.send(msg); cursor.move_left(); } void insert_element(int input) { - int8_t ins[2]; - ins[0] = OP_INSERT; - ins[1] = input; - io.send(ins, 2); + msg.encode1(OP_INSERT); + msg.encode1(input); + io.send(msg); cursor.move_right(); } diff --git a/src/common/file.cpp b/src/common/file.cpp index a9d8c2b..626f352 100644 --- a/src/common/file.cpp +++ b/src/common/file.cpp @@ -3,11 +3,9 @@ #include #include -class File { +struct File { int descriptor; - public: - File(const char *filepath, int flags) { descriptor = open(filepath, flags); } diff --git a/src/common/ipc.cpp b/src/common/ipc.cpp index d5a42e4..261b31f 100644 --- a/src/common/ipc.cpp +++ b/src/common/ipc.cpp @@ -1,7 +1,9 @@ #include +#define MESSAGE_SIZE 32 + enum Operation { - OP_NULL = 0, + OP_NULL, OP_MOVE1, OP_MOVE2, OP_MOVE4, @@ -11,32 +13,62 @@ enum Operation { OP_SHOW }; -void encode2(int16_t data, int8_t *message, size_t offset) { - message[offset] = data & 0x00ff; - message[offset + 1] = (data & 0xff00) >> 8; -} - -int16_t decode2(int8_t *message, size_t offset) { - return (int16_t)message[offset + 1] << 8 & 0xff00 - | (int16_t)message[offset] & 0x00ff; -} - -void encode4(int32_t data, int8_t *message, size_t offset) { - encode2( data & 0x0000ffff, message, offset); - encode2((data & 0xffff0000) >> 16, message, offset + 2); -} - -int32_t decode4(int8_t *message, size_t offset) { - return (int32_t)decode2(message, offset + 2) << 16 & 0xffff0000 - | (int32_t)decode2(message, offset) & 0x0000ffff; -} - -void encode8(int64_t data, int8_t *message, size_t offset) { - encode4( data & 0x00000000ffffffff, message, offset); - encode4((data & 0xffffffff00000000) >> 32, message, offset + 4); -} - -int64_t decode8(int8_t *message, size_t offset) { - return (int64_t)decode4(message, offset + 4) << 32 & 0xffffffff00000000 - | (int64_t)decode4(message, offset) & 0x00000000ffffffff; -} +struct Message { + uint8_t data[MESSAGE_SIZE] = {}; + int offset = 0; + + bool at_end() { + return data[offset] == 0; + } + + void encode1(int8_t input) { + data[offset++] = input; + } + + int8_t decode1() { + return data[offset++]; + } + + void encode2(int16_t input) { + data[offset++] = input >> 8; + data[offset++] = input; + } + + int16_t decode2() { + int16_t result = data[offset++]; + result <<= 8; + result |= data[offset++]; + return result; + } + + void encode4(int32_t input) { + for (int i = 3; i >= 0; i++) { + data[offset++] = input >> i * 8; + } + } + + int32_t decode4() { + int32_t result = data[offset++]; + for (int i = 0; i < 3; i++) { + result <<= 8; + result |= data[offset++]; + } + return result; + } + + void encode8(int64_t input) { + for (int i = 7; i >= 0; i++) { + data[offset++] = input >> i * 8; + } + } + + int64_t decode8() { + int64_t result = data[offset++]; + for (int i = 0; i < 7; i++) { + result <<= 8; + result |= data[offset++]; + } + return result; + } +}; + diff --git a/src/common/socket.cpp b/src/common/socket.cpp index 7eb4b46..7cd02e4 100644 --- a/src/common/socket.cpp +++ b/src/common/socket.cpp @@ -8,11 +8,9 @@ #define PORT 6969 -class Socket { +struct Socket { int descriptor; - public: - Socket() { descriptor = socket(AF_INET, SOCK_STREAM, 0); } @@ -60,14 +58,25 @@ class Socket { ::connect(descriptor, (sockaddr *) &addr, sizeof(sockaddr_in)); } - size_t send(void *msg, size_t length) { + size_t send(const void *msg, size_t length) { return write(descriptor, msg, length); } + size_t send(Message &msg) { + size_t result = write(descriptor, msg.data, msg.offset); + msg.offset = 0; + return result; + } + size_t recv(void *msg, size_t length) { return read(descriptor, msg, length); } + size_t recv(Message &msg) { + msg.offset = 0; + return read(descriptor, msg.data, MESSAGE_SIZE); + } + ~Socket() { close(descriptor); } diff --git a/src/server/client.cpp b/src/server/client.cpp index 75c1c38..7932436 100644 --- a/src/server/client.cpp +++ b/src/server/client.cpp @@ -1,58 +1,51 @@ -#define MAX_MSG_SIZE 8 #define pos(x, y) (x) + (y) * window_w struct Client { Socket io; Point cursor; Point window_start; + Message msg; Client(const Buffer &b, int fd) : io(fd), cursor(b), window_start(cursor) {} void parse_message() { - int8_t message[MAX_MSG_SIZE] = {}; - io.recv(message, MAX_MSG_SIZE - 1); - int8_t *iter = message; - while (iter[0]) { - switch (iter[0]) { + size_t length = io.recv(msg); + while (msg.offset < length) { + switch (msg.decode1()) { case OP_MOVE1: - move(iter[1]); - iter += 2; + move(msg.decode1()); break; case OP_MOVE2: - move(decode2(iter, 1)); - iter += 3; + move(msg.decode2()); break; case OP_MOVE4: - move(decode4(iter, 1)); - iter += 5; + move(msg.decode4()); break; case OP_MOVE8: - move(decode8(iter, 1)); - iter += 9; + move(msg.decode8()); break; case OP_INSERT: - push(iter[1]); - iter += 2; + push(msg.decode1()); break; case OP_DELETE: pop(); - iter += 1; break; case OP_SHOW: - show(decode2(iter, 1), decode2(iter, 3)); - iter += 5; + int width = msg.decode2(); + int height = msg.decode2(); + show(width, height); break; - default: - iter++; } } - for (int i = 0; i < MAX_MSG_SIZE; i++) { - if (message[i]) - printf("%02hhx ", message[i]); +#if 1 + for (int i = 0; i < length; i++) { + if (msg.data[i]) + printf("%02hhx ", msg.data[i]); else printf(".. "); } printf("\n"); +#endif } void show(size_t window_w, size_t window_h) { -- cgit v1.2.3