diff options
author | Juan Manuel Tomás <jtomas1815@gmail.com> | 2020-07-20 01:50:14 -0300 |
---|---|---|
committer | Juan Manuel Tomás <jtomas1815@gmail.com> | 2020-07-20 01:50:14 -0300 |
commit | 8693eef8febde4df90b7fbd1fb9ac813b132fa50 (patch) | |
tree | 088258d6333d54a6b90f331d3b723ae824110679 /src/server | |
parent | 7620a0b1c080f7b6315e796d29e324bdca98484b (diff) | |
download | jet-8693eef8febde4df90b7fbd1fb9ac813b132fa50.tar.gz jet-8693eef8febde4df90b7fbd1fb9ac813b132fa50.zip |
Wrap external functionality into classes
Diffstat (limited to 'src/server')
-rw-r--r-- | src/server/buffer.cpp | 14 | ||||
-rw-r--r-- | src/server/client.cpp | 8 | ||||
-rw-r--r-- | src/server/jet.cpp | 71 | ||||
-rw-r--r-- | src/server/point.cpp | 2 |
4 files changed, 27 insertions, 68 deletions
diff --git a/src/server/buffer.cpp b/src/server/buffer.cpp index 7b4c92d..62c7ea9 100644 --- a/src/server/buffer.cpp +++ b/src/server/buffer.cpp @@ -14,9 +14,9 @@ struct Buffer { void read_file(const char *pathname) { free_storage(); - int file = open(pathname, O_RDONLY); + File file(pathname, O_RDONLY); storage = new Page(); - int bytes_read = read(file, storage->elements, PAGE_SIZE); + int bytes_read = file.recv(storage->elements, PAGE_SIZE); Page *iter = storage; while (bytes_read > 0) { iter->gap_start = bytes_read; @@ -24,22 +24,20 @@ struct Buffer { iter->next = new Page(); iter->next->prev = iter; iter = iter->next; - bytes_read = read(file, iter->elements, PAGE_SIZE); + bytes_read = file.recv(iter->elements, PAGE_SIZE); } if (iter->element_count == 0) { delete iter; } - close(file); } void write_file(const char *pathname) { - int file = open(pathname, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + File file(pathname, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); Page *iter = storage; while (iter) { - write(file, iter->elements, iter->gap_start); - write(file, iter->elements + iter->gap_end, PAGE_SIZE - iter->gap_end); + file.send(iter->elements, iter->gap_start); + file.send(iter->elements + iter->gap_end, PAGE_SIZE - iter->gap_end); iter = iter->next; } - close(file); } }; diff --git a/src/server/client.cpp b/src/server/client.cpp index d6fc0fc..45f7501 100644 --- a/src/server/client.cpp +++ b/src/server/client.cpp @@ -2,15 +2,15 @@ #define pos(x, y) (x) + (y) * window_w struct Client { - int sockfd; + Socket io; Point cursor; Point window_start; - Client(const Buffer &b) : cursor(b), window_start(cursor) {} + Client(const Buffer &b, int fd) : io(fd), cursor(b), window_start(cursor) {} void parse_message() { int8_t message[MAX_MSG_SIZE] = {}; - read(sockfd, message, MAX_MSG_SIZE - 1); + io.recv(message, MAX_MSG_SIZE - 1); switch (message[0]) { case OP_MOVE1: move(message[1]); @@ -58,7 +58,7 @@ struct Client { } } - write(sockfd, view, window_w * window_h); + io.send(view, window_w * window_h); delete[] view; } diff --git a/src/server/jet.cpp b/src/server/jet.cpp index 5cc3ee4..e29005a 100644 --- a/src/server/jet.cpp +++ b/src/server/jet.cpp @@ -1,72 +1,33 @@ #include <stdio.h> -#include <stdint.h> #include <string.h> #include <stdlib.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <sys/socket.h> -#include <netinet/ip.h> -#include <arpa/inet.h> -#include <unistd.h> -#include <sys/epoll.h> - #include <common/ipc.cpp> +#include <common/file.cpp> +#include <common/socket.cpp> + #include <server/page.cpp> #include <server/buffer.cpp> #include <server/point.cpp> #include <server/client.cpp> -#define PORT 6969 -#define MAX_EVENTS 10 +Client *clients[1024] = {}; +Buffer scratch("scratch"); + +void new_connection(int fd) { + if (clients[fd]) { + delete clients[fd]; + } + clients[fd] = new Client(scratch, fd); +} -int create_listener() { - int s = socket(AF_INET, SOCK_STREAM, 0); - sockaddr_in addr = { AF_INET, htons(PORT), htonl(INADDR_LOOPBACK)}; - int opt = 1; - setsockopt(s, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)); - bind(s, (sockaddr *) &addr, sizeof(sockaddr_in)); - listen(s, MAX_EVENTS); - return s; +void existing_connection(int fd) { + clients[fd]->parse_message(); } int main() { - Buffer scratch("scratch"); scratch.read_file("test.txt"); - int listener = create_listener(); - - int epollfd = epoll_create1(0); - - epoll_event ev; - ev.events = EPOLLIN; - ev.data.fd = listener; - epoll_ctl(epollfd, EPOLL_CTL_ADD, listener, &ev); - - epoll_event events[MAX_EVENTS]; - Client *clients[1024] = {}; - - while (true) { - int nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1); - - for (int i = 0; i < nfds; i++) { - if (events[i].data.fd == listener) { - int clientfd = accept(listener, 0, 0); - ev.events = EPOLLIN | EPOLLET; - ev.data.fd = clientfd; - if (clients[clientfd]) { - delete clients[clientfd]; - } - Client *c = new Client(scratch); - c->sockfd = clientfd; - clients[clientfd] = c; - epoll_ctl(epollfd, EPOLL_CTL_ADD, clientfd, &ev); - } else { - clients[events[i].data.fd]->parse_message(); - } - } - } - - close(listener); + Socket listener; + listener.listen(new_connection, existing_connection); } diff --git a/src/server/point.cpp b/src/server/point.cpp index ab9f70f..cf38bed 100644 --- a/src/server/point.cpp +++ b/src/server/point.cpp @@ -1,7 +1,7 @@ #include <stdint.h> #include <stdbool.h> -struct Point { +class Point { Page *page; uint16_t index; |