commit dc5449b4f116f2adacbcf10077681cfa2af4a094
parent e42fce43b9acf783a820abf6620802286329ee9c
Author: William Casarin <jb55@jb55.com>
Date: Tue, 22 Sep 2020 19:12:53 -0700
fetch response packet
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat:
6 files changed, 68 insertions(+), 14 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,7 +1,7 @@
CFLAGS = -Wno-error=unused-function -O1 -g -std=c89 -Wall -Wextra -Werror -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes -Wmissing-declarations -Wdeclaration-after-statement
-OBJS = io.o parse.o cursor.o describe.o serve.o client.o net.o varint.o
+OBJS = io.o parse.o cursor.o describe.o serve.o client.o net.o varint.o util.o
all: protoverse libprotoverse.a
diff --git a/net.c b/net.c
@@ -1,6 +1,7 @@
#include "net.h"
#include "cursor.h"
+#include "util.h"
#include "varint.h"
#include <sys/types.h>
@@ -9,6 +10,27 @@
#include <stdio.h>
#include <string.h>
+static int push_fetch_response_packet(struct cursor *c, struct fetch_response_packet *resp)
+{
+ int ok;
+ ok = push_prefixed_str(c, resp->path);
+ if (!ok) return 0;
+ ok = push_varint(c, resp->data_len);
+ if (!ok) return 0;
+ return push_data(c, resp->data, resp->data_len);
+}
+
+static int pull_fetch_response_packet(struct cursor *c, struct cursor *buf,
+ struct fetch_response_packet *resp)
+{
+ int ok;
+ ok = pull_prefixed_str(c, buf, &resp->path);
+ if (!ok) return 0;
+ ok = pull_varint(c, &resp->data_len);
+ if (!ok) return 0;
+ return pull_data(c, resp->data, resp->data_len);
+}
+
static int push_fetch_packet(struct cursor *c, struct fetch_packet *fetch)
{
return push_prefixed_str(c, fetch->path);
@@ -112,10 +134,10 @@ static int push_packet_data(struct cursor *c, struct packet *packet)
switch (packet->type) {
case PKT_FETCH_DATA:
return push_fetch_packet(c, &packet->data.fetch);
- break;
+ case PKT_FETCH_DATA_RESPONSE:
+ return push_fetch_response_packet(c, &packet->data.fetch_response);
case PKT_CHAT:
return push_chat_packet(c, &packet->data.chat);
- break;
case PKT_NUM_TYPES:
return 0;
}
@@ -155,6 +177,9 @@ static int pull_packet_data(struct cursor *c, struct cursor *buf,
switch (packet->type) {
case PKT_FETCH_DATA:
return pull_fetch_packet(c, buf, &packet->data.fetch);
+ case PKT_FETCH_DATA_RESPONSE:
+ return pull_fetch_response_packet(c, buf,
+ &packet->data.fetch_response);
case PKT_CHAT:
return pull_chat_packet(c, buf, &packet->data.chat);
case PKT_NUM_TYPES:
@@ -192,8 +217,7 @@ static int packet_chat_eq(struct chat_packet *a, struct chat_packet *b)
return a->sender == b->sender && !strcmp(a->message, b->message);
}
-static int packet_fetch_eq(struct fetch_data_packet *a,
- struct fetch_data_packet *b)
+static int packet_fetch_eq(struct fetch_packet *a, struct fetch_packet *b)
{
if (!a->path ^ !b->path)
return 0;
@@ -201,6 +225,15 @@ static int packet_fetch_eq(struct fetch_data_packet *a,
return !strcmp(a->path, b->path);
}
+static int packet_fetch_resp_eq(struct fetch_response_packet *a,
+ struct fetch_response_packet *b)
+{
+ if (!a->path ^ !b->path)
+ return 0;
+
+ return memeq(a->data, a->data_len, b->data, b->data_len);
+}
+
int packet_eq(struct packet *a, struct packet *b)
{
if (a->type != b->type)
@@ -211,6 +244,9 @@ int packet_eq(struct packet *a, struct packet *b)
return packet_chat_eq(&a->data.chat, &b->data.chat);
case PKT_FETCH_DATA:
return packet_fetch_eq(&a->data.fetch, &b->data.fetch);
+ case PKT_FETCH_DATA_RESPONSE:
+ return packet_fetch_resp_eq(&a->data.fetch_response,
+ &b->data.fetch_response);
case PKT_NUM_TYPES:
return 0;
}
@@ -227,6 +263,11 @@ void print_packet(struct packet *packet)
packet->data.chat.sender,
packet->data.chat.message);
return;
+ case PKT_FETCH_DATA_RESPONSE:
+ printf("(fetch-resp (path \"%s\") (data %d))\n",
+ packet->data.fetch_response.path,
+ packet->data.fetch_response.data_len);
+ return;
case PKT_FETCH_DATA:
printf("(fetch (path \"%s\"))\n",
packet->data.fetch.path);
diff --git a/net.h b/net.h
@@ -20,7 +20,7 @@ struct fetch_packet {
struct fetch_response_packet {
const char *path;
int data_len;
- const unsigned char *data;
+ unsigned char *data;
};
struct chat_packet {
diff --git a/parse.c b/parse.c
@@ -1,5 +1,7 @@
+#include "util.h"
#include "parse.h"
+
#include <stdio.h>
#include <string.h>
#include <ctype.h>
@@ -772,14 +774,6 @@ struct cell *get_cell(struct cursor *cells, u16 index)
}
-static int memeq(void *buf, int buf_len, void *buf2, int buf2_len)
-{
- if (buf_len != buf2_len)
- return 0;
-
- return memcmp(buf, buf2, buf_len) == 0;
-}
-
static int symbol_eq(struct tok_str *a, const char *b, int b_len)
{
return memeq(a->data, a->len, (char*)b, b_len);
diff --git a/util.c b/util.c
@@ -0,0 +1,12 @@
+
+#include "util.h"
+
+#include <memory.h>
+
+int memeq(void *buf, int buf_len, void *buf2, int buf2_len)
+{
+ if (buf_len != buf2_len)
+ return 0;
+
+ return memcmp(buf, buf2, buf_len) == 0;
+}
diff --git a/util.h b/util.h
@@ -0,0 +1,7 @@
+
+#ifndef PROTOVERSE_UTIL_H
+#define PROTOVERSE_UTIL_H
+
+int memeq(void *buf, int buf_len, void *buf2, int buf2_len);
+
+#endif /* PROTOVERSE_UTIL_H */