summary refs log tree commit diff
path: root/child.c
diff options
context:
space:
mode:
Diffstat (limited to 'child.c')
-rw-r--r--child.c106
1 files changed, 106 insertions, 0 deletions
diff --git a/child.c b/child.c
new file mode 100644
index 0000000..58ee999
--- /dev/null
+++ b/child.c
@@ -0,0 +1,106 @@
+#include <inttypes.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdio.h>
+
+#include "fdwatcher.h"
+#include "logging.h"
+
+struct ConnectionDataBuffer {
+	size_t capacity;
+	size_t length;
+	char buf[];
+};
+
+struct ConnectionData {
+	struct ConnectionDataBuffer *buffer;
+};
+
+static int
+aura_addbufferdata(struct ConnectionDataBuffer *buffer, char *data, size_t length)
+{
+	if (buffer->length + length > buffer->capacity) {
+		aura_printf("FUCK!\n");
+		aura_printf("buffer->length: %ld\n", buffer->length);
+		aura_printf("buffer->capacity: %ld\n", buffer->capacity);
+		aura_printf("length: %ld\n", length);
+
+		abort();
+	}
+
+	memcpy(buffer->buf, data, length);
+	buffer->length = buffer->length + length;
+
+	return 0;
+}
+
+static int
+meowiero(struct FDWatchHandle *handle, enum FDWatch_EventType type, int fd, void *data)
+{
+	struct ConnectionData *connectiondata = (struct ConnectionData *)data;
+	char c = 0;
+
+	switch (type) {
+	case FDWATCH_EVENT_INP:
+		aura_printf("INPUT event in fd:%d\n", fd);
+
+		read(fd, &c, 2);
+		aura_addbufferdata(connectiondata->buffer, &c, 2);
+
+		aura_printf("%c\n", c);
+		break;
+	case FDWATCH_EVENT_HUP:
+		aura_printf("HANGUP event in fd:%d, exiting\n", fd);
+		fdwatcher_remove(handle, fd);
+		close(fd);
+		return 0;
+	}
+
+	return 1;
+}
+
+/* init */
+int childProcessMain(int argc, char **argv)
+{
+	struct ConnectionData *connectiondata = { 0 };
+	struct FDWatchHandle fdhandle = { 0 };
+	int sockie = 0;
+	int ret = 0;
+
+	connectiondata = (struct ConnectionData *)malloc(sizeof(struct ConnectionData));
+	if (errno != 0) {
+		perror("malloc(3)");
+		return 1;
+	}
+
+	connectiondata->buffer = malloc(sizeof(struct ConnectionDataBuffer) + 65536);
+	if (errno != 0) {
+		perror("malloc(3)");
+		return 1;
+	}
+	connectiondata->buffer->capacity = 65536;
+
+	/* socket fd int in argv[2]! ^w^ */
+	sscanf(argv[2], "%d", &sockie);
+	aura_printf("received fd:%d\n", sockie);
+
+	ret = fdwatcher_initialise(&fdhandle, 1);
+	if (ret < 0) {
+		aura_fprintf(stderr, "fdwatcher_initialise failed\n");
+		return 1;
+	}
+
+	ret = fdwatcher_add(&fdhandle, sockie, (void *)connectiondata);
+	if (ret < 0) {
+		aura_fprintf(stderr, "fdwatcher_add failed\n");
+		return 1;
+	}
+
+	fdwatcher_watch(&fdhandle, meowiero);
+	aura_printf("finished\n");
+
+	return 0;
+}
+