diff rfcal/cmu200/session.c @ 195:db9ee7745cdd

fc-cmu200d: socket handling skeleton added
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 24 Apr 2017 01:43:02 +0000
parents
children 47d56330609d
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rfcal/cmu200/session.c	Mon Apr 24 01:43:02 2017 +0000
@@ -0,0 +1,51 @@
+/*
+ * This module contains the code that handles a single local socket
+ * connection session.
+ */
+
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+
+extern int activesock;
+
+handle_command()
+{
+	char readbuf[256], linebuf[256];
+	int cc, pos;
+
+	for (pos = 0; ; ) {
+		cc = read(activesock, readbuf, sizeof readbuf);
+		if (cc <= 0) {
+			printf("Client program closed connection\n");
+			return(1);
+		}
+		if (pos + cc > sizeof linebuf) {
+			send_socket_response("-Command too long\n");
+			return(1);
+		}
+		bcopy(readbuf, linebuf + pos, cc);
+		pos += cc;
+		if (linebuf[pos-1] == '\n')
+			break;
+	}
+	linebuf[pos-1] = '\0';
+	printf("Client command: %s\n", linebuf);
+	/* actual command handling will go here */
+	return(0);
+}
+
+handle_session()
+{
+	int rc;
+
+	send_socket_response("+CMU200 interface daemon ready\n");
+	for (;;) {
+		rc = handle_command();
+		if (rc)
+			break;
+	}
+	close(activesock);
+}