# HG changeset patch # User Matti Hamalainen # Date 1288440423 -10800 # Node ID 4235ff4ced04526e48d9645ded54863398d11296 # Parent 713879a7ca10e9a0315693e3adc84b93f4ea1a0f "Optimize" protocol handling. diff -r 713879a7ca10 -r 4235ff4ced04 nnchat.c --- 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[] = { - { "", handleUser }, - { "", handleDeleteUser }, - { "", handleAddUser }, - { "", handleFoo }, - { "", handleBoot }, +static protocmd_t protoCmds[] = { + { "", -1, handleUser }, + { "", -1, handleDeleteUser }, + { "", -1, handleAddUser }, + { "", -1, handleFoo }, + { "", -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;