summaryrefslogtreecommitdiff
path: root/src/common/ipc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/ipc.cpp')
-rw-r--r--src/common/ipc.cpp92
1 files changed, 62 insertions, 30 deletions
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 <stdint.h>
+#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;
+ }
+};
+