changeset 129:4235ff4ced04

"Optimize" protocol handling.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 30 Oct 2010 15:07:03 +0300
parents 713879a7ca10
children 352ec3c300e4
files nnchat.c
diffstat 1 files changed, 14 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/nnchat.c	Sat Oct 30 12:15:55 2010 +0300
+++ b/nnchat.c	Sat Oct 30 15:07:03 2010 +0300
@@ -515,31 +515,34 @@
 
 typedef struct {
     char *cmd;
+    ssize_t len;
     int (*handler)(int, const char *);
 } protocmd_t;
 
 
-static const protocmd_t protoCmds[] = {
-    { "<USER>", handleUser },
-    { "<LOGIN_", handleLogin },
-    { "<DELETE_USER>", handleDeleteUser },
-    { "<ADD_USER>", handleAddUser },
-    { "<NUMCLIENTS>", handleFoo },
-    { "<BOOT />", handleBoot },
+static protocmd_t protoCmds[] = {
+    { "<USER>",         -1, handleUser },
+    { "<LOGIN_",        -1, handleLogin },
+    { "<DELETE_USER>",  -1, handleDeleteUser },
+    { "<ADD_USER>",     -1, handleAddUser },
+    { "<NUMCLIENTS>",   -1, handleFoo },
+    { "<BOOT />",       -1, handleBoot },
 };
 
 static const int nprotoCmds = sizeof(protoCmds) / sizeof(protoCmds[0]);
 
 
-int handleProtocol(const int sock, const char *buf, const size_t bufLen)
+int handleProtocol(const int sock, const char *buf, const ssize_t bufLen)
 {
     int i;
     
     for (i = 0; i < nprotoCmds; i++) {
-        size_t cmdLen = strlen(protoCmds[i].cmd);
-        if (cmdLen < bufLen && !strncmp(buf, protoCmds[i].cmd, cmdLen)) {
+        ssize_t cmdLen = protoCmds[i].len;
+        if (cmdLen < 0)
+            cmdLen = protoCmds[i].len = strlen(protoCmds[i].cmd);
+
+        if (cmdLen < bufLen && !strncmp(buf, protoCmds[i].cmd, cmdLen))
             return protoCmds[i].handler(sock, buf + cmdLen);
-        }
     }
     
     return 1;