Skeleton server code (compiles, just doesn't do anything)
[tpg/opendispense2.git] / server / src / server.c
1 /*
2  * OpenDispense 2 
3  * UCC (University [of WA] Computer Club) Electronic Accounting System
4  *
5  * server.c - Client Server Code
6  *
7  * This file is licenced under the 3-clause BSD Licence. See the file
8  * COPYING for full details.
9  */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include "common.h"
13 #include <sys/socket.h>
14
15 #define MAX_CONNECTION_QUEUE    5
16 #define INPUT_BUFFER_SIZE       100
17
18 #define MSG_STR_TOO_LONG        "499 Malformed Command String"
19
20 // === GLOBALS ===
21  int    giServer_Port = 1020;
22
23 // === CODE ===
24 void Server_Start(void)
25 {
26         // Create Server
27 }
28
29 void Server_HandleClient(int Socket)
30 {
31         char    inbuf[INPUT_BUFFER_SIZE];
32         char    *buf = inbuf;
33          int    remspace = INPUT_BUFFER_SIZE-1;
34          int    bytes = -1;
35                 
36         // Read from client
37         while( (bytes = recv(Socket, buf, remspace, 0)) > 0 )
38         {
39                 char    *eol, *start;
40                 buf[bytes] = '\0';      // Allow us to use stdlib string functions on it
41                 
42                 // Split by lines
43                 start = inbuf;
44                 while( (eol = strchr(start, '\n')) )
45                 {
46                         *eol = '\0';
47                         Server_ParseClientCommand(Socket, start);
48                         start = eol + 1;
49                 }
50                 
51                 // Check if there was an incomplete line
52                 if( *start != '\0' ) {
53                          int    tailBytes = bytes - (start-buf);
54                         // Roll back in buffer
55                         memcpy(inbuf, start, tailBytes);
56                         remspace -= tailBytes;
57                         if(remspace == 0) {
58                                 send(Socket, MSG_STR_TOO_LONG, sizeof(MSG_STR_TOO_LONG));
59                         }
60                 }
61                 else {
62                         buf = inbuf;
63                         remspace = INPUT_BUFFER_SIZE - 1;
64                 }
65         }
66         
67         // Check for errors
68         if( bytes < 0 ) {
69                 fprintf(stderr, "ERROR: Unable to recieve from client on socket %i\n", Socket);
70                 return ;
71         }
72 }

UCC git Repository :: git.ucc.asn.au