diff options
author | Juan Manuel Tomás <jtomas1815@gmail.com> | 2020-07-11 21:37:42 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-11 21:37:42 -0300 |
commit | 13fdfe875efdfb33d392c5e634ceff494962e7dc (patch) | |
tree | 48e6d89b63f9af3a14d2ef4564f1c7cdceef77d5 | |
parent | 880047d09eb4b9a448a5817699422abc6f07b5d6 (diff) | |
parent | 291748a56824f77b137c66e9879ec0a4ecb14ced (diff) | |
download | jet-13fdfe875efdfb33d392c5e634ceff494962e7dc.tar.gz jet-13fdfe875efdfb33d392c5e634ceff494962e7dc.zip |
Merge pull request #10 from jmtomas/4
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | buffer.cpp | 2 | ||||
-rw-r--r-- | client.cpp | 80 | ||||
-rw-r--r-- | ipc.cpp | 37 | ||||
-rw-r--r-- | jet.cpp | 30 | ||||
-rw-r--r-- | jet2.cpp | 37 | ||||
-rw-r--r-- | page.cpp | 9 | ||||
-rw-r--r-- | point.cpp | 1 |
8 files changed, 111 insertions, 89 deletions
@@ -1,4 +1,4 @@ -FLAGS=-ggdb -O0 -lcurses $(shell ncursesw6-config --cflags --libs) +FLAGS=-ggdb -O0 _PHONY=all @@ -8,4 +8,4 @@ jet: Makefile *.cpp g++ $(FLAGS) jet.cpp -o jet jet2: Makefile *.cpp - g++ $(FLAGS) jet2.cpp -o jet2 + g++ $(FLAGS) jet2.cpp -o jet2 $(shell pkg-config --cflags --libs ncurses) @@ -1,5 +1,3 @@ -#include "page.cpp" - struct Buffer { const char *name; Page *storage; @@ -1,43 +1,52 @@ -#include "point.cpp" - -#define MAX_ARGS 16 +#define MAX_MSG_SIZE 128 #define pos(x, y) (x) + (y) * window_w -struct ArgList { - int len, cap; - int *args; - - ArgList(int cap) : len(0), cap(cap) { - args = new int[cap]; - for (int i = 0; i < cap; args[i++] = 0); - } - ~ArgList() { delete[] args; } - - int pop() { - return args[--len]; - } - - void push(int value) { - args[len++] = value; - } - -}; - struct Client { int sockfd; Point cursor; Point window_start; - ArgList args; - Client(const Buffer &b) : - cursor(b), - window_start(cursor), - args(MAX_ARGS) - {} + Client(const Buffer &b) : cursor(b), window_start(cursor) {} + + void parse_message() { + uint8_t message[MAX_MSG_SIZE] = {}; + read(sockfd, message, MAX_MSG_SIZE - 1); + uint8_t *iter = message; + while (*iter) { + switch (*iter) { + case OP_MOVE1: + move(iter[1]); + iter += 2; + break; + case OP_MOVE2: + move(decode2(iter, 1)); + iter += 3; + break; + case OP_MOVE4: + move(decode4(iter, 1)); + iter += 5; + break; + case OP_MOVE8: + move(decode8(iter, 1)); + iter += 9; + break; + case OP_INSERT: + push(iter[1]); + iter += 2; + break; + case OP_DELETE: + pop(); + iter += 1; + break; + case OP_SHOW: + show(decode2(iter, 1), decode2(iter, 3)); + iter += 6; + break; + } + } + } - void show() { - int window_h = args.pop(); - int window_w = args.pop(); + void show(int16_t window_w, int16_t window_h) { char *view = new char[window_w * window_h]; Point window_end(window_start); @@ -67,8 +76,7 @@ struct Client { delete[] view; } - void move() { - int target = args.pop(); + void move(int64_t target) { while (target > 0) { cursor++; target--; @@ -79,8 +87,8 @@ struct Client { } } - void push() { - cursor.push(args.pop()); + void push(int8_t input) { + cursor.push(input); } void pop() { @@ -0,0 +1,37 @@ +enum Operation { + OP_NULL = 0, + OP_MOVE1, + OP_MOVE2, + OP_MOVE4, + OP_MOVE8, + OP_INSERT, + OP_DELETE, + OP_SHOW +}; + +void encode2(int16_t data, uint8_t *message, size_t offset) { + message[offset] = data & 0x00ff; + message[offset + 1] = (data & 0xff00) >> 8; +} + +int16_t decode2(uint8_t *message, size_t offset) { + return message[offset] | ((int16_t)message[offset + 1] << 8); +} + +void encode4(int32_t data, uint8_t *message, size_t offset) { + encode2( data & 0x0000ffff, message, offset); + encode2((data & 0xffff0000) >> 16, message, offset + 2); +} + +int32_t decode4(uint8_t *message, size_t offset) { + return decode2(message, offset) | ((int32_t)decode2(message, offset + 2) << 16); +} + +void encode8(int64_t data, uint8_t *message, size_t offset) { + encode4( data & 0x00000000ffffffff, message, offset); + encode4((data & 0xffffffff00000000) >> 32, message, offset + 4); +} + +int64_t decode8(uint8_t *message, size_t offset) { + return decode4(message, offset) | ((int64_t)decode4(message, offset + 4) << 32); +} @@ -1,6 +1,7 @@ -#include <stdlib.h> #include <stdio.h> +#include <stdint.h> #include <string.h> +#include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> @@ -11,11 +12,13 @@ #include <unistd.h> #include <sys/epoll.h> +#include "ipc.cpp" +#include "page.cpp" #include "buffer.cpp" +#include "point.cpp" #include "client.cpp" #define PORT 6969 -#define MAX_COMMAND_SIZE 128 #define MAX_EVENTS 10 int create_listener() { @@ -28,24 +31,6 @@ int create_listener() { return s; } -void parse_command(char *command, Client *client) { - char *token = strtok(command, " \n"); - while (token) { - if (strcmp(token, "show") == 0) { - client->show(); - } else if (strcmp(token, "move") == 0) { - client->move(); - } else if (strcmp(token, "push") == 0) { - client->push(); - } else if (strcmp(token, "pop") == 0) { - client->pop(); - } else { - client->args.push(atoi(token)); - } - token = strtok(0, " \n"); - } -} - int main() { Buffer scratch("scratch"); scratch.read_file("LICENSE"); @@ -78,10 +63,7 @@ int main() { clients[clientfd] = c; epoll_ctl(epollfd, EPOLL_CTL_ADD, clientfd, &ev); } else { - char command[MAX_COMMAND_SIZE] = {}; - int clientfd = events[i].data.fd; - read(clientfd, command, MAX_COMMAND_SIZE - 1); - parse_command(command, clients[clientfd]); + clients[events[i].data.fd]->parse_message(); } } } @@ -1,7 +1,6 @@ #include <stdio.h> -#include <stdlib.h> #include <string.h> -#include <curses.h> +#include <cursesw.h> #include <sys/types.h> #include <sys/socket.h> @@ -9,6 +8,8 @@ #include <arpa/inet.h> #include <unistd.h> +#include "ipc.cpp" + #define NORMAL_MODE 0 #define INSERT_MODE 1 #define PORT 6969 @@ -27,7 +28,6 @@ int main(int argc, char *argv[]) { char *view = new char[window_width * window_height]; for (int i = 0; i < window_width * window_height; view[i++] = 0); - 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)); @@ -38,15 +38,19 @@ int main(int argc, char *argv[]) { while (!quit) { clear(); - char msg[32] = {}; - int len = sprintf(msg, " %d %d show ", window_width, window_height); - write(s, msg, len); + uint8_t msg[5]; + msg[0] = OP_SHOW; + encode2(window_width, msg, 1); + encode2(window_height, msg, 3); + write(s, msg, 5); read(s, view, window_width * window_height); for (int i = 0; i < window_width * window_height; i++) { printw("%c", view[i]); } - memset(msg, 0, 32); + uint8_t mov[2]; + uint8_t del[1]; + uint8_t ins[2]; int input = getch(); if (mode == NORMAL_MODE) { switch (input) { @@ -57,12 +61,14 @@ int main(int argc, char *argv[]) { mode = INSERT_MODE; break; case 'h': - len = sprintf(msg, " -1 move "); - write(s, msg, len); + mov[0] = OP_MOVE1; + mov[1] = -1; + write(s, mov, 2); break; case 'l': - len = sprintf(msg, " 1 move "); - write(s, msg, len); + mov[0] = OP_MOVE1; + mov[1] = 1; + write(s, mov, 2); break; } } else { @@ -71,12 +77,13 @@ int main(int argc, char *argv[]) { mode = NORMAL_MODE; break; case KEY_BACKSPACE: - len = sprintf(msg, " pop "); - write(s, msg, len); + del[0] = OP_DELETE; + write(s, del, 1); break; default: - len = sprintf(msg, " %d push ", input); - write(s, msg, len); + ins[0] = OP_INSERT; + ins[1] = input; + write(s, ins, 2); } } } @@ -1,8 +1,3 @@ -#include <stdint.h> -#include <string.h> -#include <stdlib.h> -#include <assert.h> - #ifndef PAGE_SIZE #define PAGE_SIZE 4096 #endif @@ -59,28 +54,24 @@ struct Page { } void move_gap_forward() { - assert(gap_end < PAGE_SIZE); elements[gap_start] = elements[gap_end]; gap_start++; gap_end++; } void move_gap_backward() { - assert(gap_start > 0); gap_end--; gap_start--; elements[gap_end] = elements[gap_start]; } void push(uint8_t c) { - assert(element_count < PAGE_SIZE); elements[gap_start] = c; gap_start++; element_count++; } void pop() { - assert(gap_start > 0); gap_start--; element_count--; } @@ -1,5 +1,4 @@ #include <stdint.h> -#include <assert.h> #include <stdbool.h> struct Point { |