summaryrefslogtreecommitdiff
path: root/client.cpp
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2020-07-11 21:37:42 -0300
committerGitHub <noreply@github.com>2020-07-11 21:37:42 -0300
commit13fdfe875efdfb33d392c5e634ceff494962e7dc (patch)
tree48e6d89b63f9af3a14d2ef4564f1c7cdceef77d5 /client.cpp
parent880047d09eb4b9a448a5817699422abc6f07b5d6 (diff)
parent291748a56824f77b137c66e9879ec0a4ecb14ced (diff)
downloadjet-13fdfe875efdfb33d392c5e634ceff494962e7dc.tar.gz
jet-13fdfe875efdfb33d392c5e634ceff494962e7dc.zip
Merge pull request #10 from jmtomas/4
Diffstat (limited to 'client.cpp')
-rw-r--r--client.cpp80
1 files changed, 44 insertions, 36 deletions
diff --git a/client.cpp b/client.cpp
index 05d07a1..408c2e9 100644
--- a/client.cpp
+++ b/client.cpp
@@ -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() {