summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2020-07-20 01:50:14 -0300
committerJuan Manuel Tomás <jtomas1815@gmail.com>2020-07-20 01:50:14 -0300
commit8693eef8febde4df90b7fbd1fb9ac813b132fa50 (patch)
tree088258d6333d54a6b90f331d3b723ae824110679 /src/common
parent7620a0b1c080f7b6315e796d29e324bdca98484b (diff)
downloadjet-8693eef8febde4df90b7fbd1fb9ac813b132fa50.tar.gz
jet-8693eef8febde4df90b7fbd1fb9ac813b132fa50.zip
Wrap external functionality into classes
Diffstat (limited to 'src/common')
-rw-r--r--src/common/file.cpp30
-rw-r--r--src/common/ipc.cpp2
-rw-r--r--src/common/socket.cpp74
3 files changed, 106 insertions, 0 deletions
diff --git a/src/common/file.cpp b/src/common/file.cpp
new file mode 100644
index 0000000..a9d8c2b
--- /dev/null
+++ b/src/common/file.cpp
@@ -0,0 +1,30 @@
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+class File {
+ int descriptor;
+
+ public:
+
+ File(const char *filepath, int flags) {
+ descriptor = open(filepath, flags);
+ }
+
+ File(const char *filepath, int flags, mode_t mode) {
+ descriptor = open(filepath, flags, mode);
+ }
+
+ size_t send(void *msg, size_t length) {
+ return write(descriptor, msg, length);
+ }
+
+ size_t recv(void *msg, size_t length) {
+ return read(descriptor, msg, length);
+ }
+
+ ~File() {
+ close(descriptor);
+ }
+};
diff --git a/src/common/ipc.cpp b/src/common/ipc.cpp
index 7d46307..d5a42e4 100644
--- a/src/common/ipc.cpp
+++ b/src/common/ipc.cpp
@@ -1,3 +1,5 @@
+#include <stdint.h>
+
enum Operation {
OP_NULL = 0,
OP_MOVE1,
diff --git a/src/common/socket.cpp b/src/common/socket.cpp
new file mode 100644
index 0000000..7eb4b46
--- /dev/null
+++ b/src/common/socket.cpp
@@ -0,0 +1,74 @@
+#include <unistd.h>
+#include <sys/socket.h>
+#include <netinet/ip.h>
+#include <arpa/inet.h>
+#include <sys/epoll.h>
+
+#define MAX_EVENTS 10
+
+#define PORT 6969
+
+class Socket {
+ int descriptor;
+
+ public:
+
+ Socket() {
+ descriptor = socket(AF_INET, SOCK_STREAM, 0);
+ }
+
+ Socket(int descriptor) : descriptor(descriptor) {}
+
+ typedef void (*EventAction) (int);
+
+ void listen(EventAction on_new_connection, EventAction on_existing_connection) {
+ sockaddr_in addr = { AF_INET, htons(PORT), htonl(INADDR_LOOPBACK)};
+ int opt = 1;
+ setsockopt(descriptor, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt));
+ bind(descriptor, (sockaddr *) &addr, sizeof(sockaddr_in));
+ ::listen(descriptor, MAX_EVENTS);
+
+ int epollfd = epoll_create1(0);
+
+ epoll_event ev;
+ ev.events = EPOLLIN;
+ ev.data.fd = descriptor;
+ epoll_ctl(epollfd, EPOLL_CTL_ADD, descriptor, &ev);
+
+ epoll_event events[MAX_EVENTS];
+
+ while (true) {
+ int nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1);
+
+ for (int i = 0; i < nfds; i++) {
+ if (events[i].data.fd == descriptor) {
+ int clientfd = accept(descriptor, 0, 0);
+ ev.events = EPOLLIN | EPOLLET;
+ ev.data.fd = clientfd;
+ epoll_ctl(epollfd, EPOLL_CTL_ADD, clientfd, &ev);
+ on_new_connection(clientfd);
+ } else {
+ on_existing_connection(events[i].data.fd);
+ }
+ }
+ }
+
+ }
+
+ void connect() {
+ sockaddr_in addr = { AF_INET, htons(PORT), htonl(INADDR_LOOPBACK) };
+ ::connect(descriptor, (sockaddr *) &addr, sizeof(sockaddr_in));
+ }
+
+ size_t send(void *msg, size_t length) {
+ return write(descriptor, msg, length);
+ }
+
+ size_t recv(void *msg, size_t length) {
+ return read(descriptor, msg, length);
+ }
+
+ ~Socket() {
+ close(descriptor);
+ }
+};