- if(request_handlers[request] == NULL)
- syslog(LOG_ERR, "Unknown request %d.", request);
- else
- if(request_handlers[request](cl, tmp, lenin) < 0)
- return -1;
+ for(i = oldlen; i < cl->buflen; i++)
+ {
+ if(cl->buffer[i] == '\n')
+ {
+ cl->buffer[i] = 0; /* replace end-of-line by end-of-string so we can use sscanf */
+ cl->reqlen = i + 1;
+ break;
+ }
+ }
+
+ if(cl->reqlen)
+ {
+ if(sscanf(cl->buffer, "%d", &request) == 1)
+ {
+ if(request_handlers[request] == NULL)
+ {
+ syslog(LOG_ERR, "Unknown request: %s", cl->buffer);
+ return 0;
+ }
+
+ if(debug_lvl > 3)
+ syslog(LOG_DEBUG, "Got request: %s", cl->buffer);
+
+ request_handlers[request](cl);
+ }
+ else
+ {
+ syslog(LOG_ERR, "Bogus data received: %s", cl->buffer);
+ }
+
+ cl->buflen -= cl->reqlen;
+ memmove(cl->buffer, cl->buffer + cl->reqlen, cl->buflen);
+ oldlen = 0;
+ }
+ else
+ {
+ break;
+ }
+ }
+
+ cl->last_ping_time = time(NULL);
+ cl->want_ping = 0;