summaryrefslogtreecommitdiff
path: root/src/common/socket.cpp
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2020-08-02 03:50:26 -0300
committerJuan Manuel Tomás <jtomas1815@gmail.com>2020-08-03 23:56:03 -0300
commit4bbbfb8e8139e690ea084b72ccc2de71fdca1e08 (patch)
tree0ea15129b752d00f96c6a91e88ec303fd90d7860 /src/common/socket.cpp
parent83ec165fddc5f9ee99a8e2aa75d188c45253a0fa (diff)
downloadjet-4bbbfb8e8139e690ea084b72ccc2de71fdca1e08.tar.gz
jet-4bbbfb8e8139e690ea084b72ccc2de71fdca1e08.zip
Simplify encoding and decoding
Diffstat (limited to 'src/common/socket.cpp')
-rw-r--r--src/common/socket.cpp17
1 files changed, 13 insertions, 4 deletions
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);
}