From e9122394f97d54a6b28b4dd52db24e22cb1ab340 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Fri, 17 Jul 2020 23:31:57 -0300 Subject: Move main files into subdirectories --- src/client/jetc.cpp | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/jet.cpp | 72 --------------------------------------- src/jetc.cpp | 98 ----------------------------------------------------- src/server/jet.cpp | 72 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 170 insertions(+), 170 deletions(-) create mode 100644 src/client/jetc.cpp delete mode 100644 src/jet.cpp delete mode 100644 src/jetc.cpp create mode 100644 src/server/jet.cpp (limited to 'src') diff --git a/src/client/jetc.cpp b/src/client/jetc.cpp new file mode 100644 index 0000000..8d31d72 --- /dev/null +++ b/src/client/jetc.cpp @@ -0,0 +1,98 @@ +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include + +#define NORMAL_MODE 0 +#define INSERT_MODE 1 +#define PORT 6969 + +int main(int argc, char *argv[]) { + initscr(); + cbreak(); + noecho(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + + Window window; + getmaxyx(stdscr, window.height, window.width); + window.init(); + + int s = socket(AF_INET, SOCK_STREAM, 0); + sockaddr_in addr = { AF_INET, htons(PORT), htonl(INADDR_LOOPBACK) }; + connect(s, (sockaddr *) &addr, sizeof(sockaddr_in)); + + int mode = NORMAL_MODE; + Cursor cursor = { 0, 0, &window }; + + int quit = 0; + while (!quit) { + clear(); + + int8_t msg[5]; + msg[0] = OP_SHOW; + encode2(window.width, msg, 1); + encode2(window.height, msg, 3); + write(s, msg, 5); + read(s, window.view, window.width * window.height); + for (int i = 0; i < window.width * window.height; i++) { + printw("%c", window.view[i]); + } + move(cursor.y, cursor.x); + + int8_t mov[2]; + int8_t del[1]; + int8_t ins[2]; + int input = getch(); + if (mode == NORMAL_MODE) { + switch (input) { + case '': + quit = 1; + break; + case 'i': + mode = INSERT_MODE; + break; + case 'h': + mov[0] = OP_MOVE1; + mov[1] = -1; + write(s, mov, 2); + cursor.move_left(); + break; + case 'l': + mov[0] = OP_MOVE1; + mov[1] = 1; + write(s, mov, 2); + cursor.move_right(); + break; + } + } else { + switch (input) { + case '': + mode = NORMAL_MODE; + break; + case KEY_BACKSPACE: + del[0] = OP_DELETE; + write(s, del, 1); + break; + default: + ins[0] = OP_INSERT; + ins[1] = input; + write(s, ins, 2); + } + } + } + + close(s); + + endwin(); + return 0; +} diff --git a/src/jet.cpp b/src/jet.cpp deleted file mode 100644 index 6cc4470..0000000 --- a/src/jet.cpp +++ /dev/null @@ -1,72 +0,0 @@ -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "common/ipc.cpp" -#include "server/page.cpp" -#include "server/buffer.cpp" -#include "server/point.cpp" -#include "server/client.cpp" - -#define PORT 6969 -#define MAX_EVENTS 10 - -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; -} - -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); -} diff --git a/src/jetc.cpp b/src/jetc.cpp deleted file mode 100644 index 9650eb6..0000000 --- a/src/jetc.cpp +++ /dev/null @@ -1,98 +0,0 @@ -#include -#include -#include - -#include -#include -#include -#include -#include - -#include "common/ipc.cpp" -#include "client/window.cpp" -#include "client/cursor.cpp" - -#define NORMAL_MODE 0 -#define INSERT_MODE 1 -#define PORT 6969 - -int main(int argc, char *argv[]) { - initscr(); - cbreak(); - noecho(); - intrflush(stdscr, FALSE); - keypad(stdscr, TRUE); - - Window window; - getmaxyx(stdscr, window.height, window.width); - window.init(); - - int s = socket(AF_INET, SOCK_STREAM, 0); - sockaddr_in addr = { AF_INET, htons(PORT), htonl(INADDR_LOOPBACK) }; - connect(s, (sockaddr *) &addr, sizeof(sockaddr_in)); - - int mode = NORMAL_MODE; - Cursor cursor = { 0, 0, &window }; - - int quit = 0; - while (!quit) { - clear(); - - int8_t msg[5]; - msg[0] = OP_SHOW; - encode2(window.width, msg, 1); - encode2(window.height, msg, 3); - write(s, msg, 5); - read(s, window.view, window.width * window.height); - for (int i = 0; i < window.width * window.height; i++) { - printw("%c", window.view[i]); - } - move(cursor.y, cursor.x); - - int8_t mov[2]; - int8_t del[1]; - int8_t ins[2]; - int input = getch(); - if (mode == NORMAL_MODE) { - switch (input) { - case '': - quit = 1; - break; - case 'i': - mode = INSERT_MODE; - break; - case 'h': - mov[0] = OP_MOVE1; - mov[1] = -1; - write(s, mov, 2); - cursor.move_left(); - break; - case 'l': - mov[0] = OP_MOVE1; - mov[1] = 1; - write(s, mov, 2); - cursor.move_right(); - break; - } - } else { - switch (input) { - case '': - mode = NORMAL_MODE; - break; - case KEY_BACKSPACE: - del[0] = OP_DELETE; - write(s, del, 1); - break; - default: - ins[0] = OP_INSERT; - ins[1] = input; - write(s, ins, 2); - } - } - } - - close(s); - - endwin(); - return 0; -} diff --git a/src/server/jet.cpp b/src/server/jet.cpp new file mode 100644 index 0000000..5cc3ee4 --- /dev/null +++ b/src/server/jet.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#define PORT 6969 +#define MAX_EVENTS 10 + +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; +} + +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); +} -- cgit v1.2.3