annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
195
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module contains the code that handles a single local socket
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * connection session.
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <ctype.h>
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdio.h>
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdlib.h>
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <string.h>
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <strings.h>
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 extern int activesock;
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 handle_command()
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 {
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 char readbuf[256], linebuf[256];
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 int cc, pos;
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 for (pos = 0; ; ) {
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 cc = read(activesock, readbuf, sizeof readbuf);
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 if (cc <= 0) {
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 printf("Client program closed connection\n");
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 return(1);
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 }
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 if (pos + cc > sizeof linebuf) {
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 send_socket_response("-Command too long\n");
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 return(1);
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 }
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 bcopy(readbuf, linebuf + pos, cc);
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 pos += cc;
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 if (linebuf[pos-1] == '\n')
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 break;
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 }
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 linebuf[pos-1] = '\0';
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 printf("Client command: %s\n", linebuf);
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 /* actual command handling will go here */
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 return(0);
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 }
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 handle_session()
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 {
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 int rc;
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 send_socket_response("+CMU200 interface daemon ready\n");
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 for (;;) {
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 rc = handle_command();
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 if (rc)
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 break;
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 }
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 close(activesock);
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 }