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/common/socket.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'src/common/socket.cpp') 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); } -- cgit v1.2.3