changeset 403:7bec02f382fb

Refactor the connection and protocol handling a bit.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 24 May 2012 05:28:44 +0300
parents 563a70e8a303
children d11a68f214eb
files libnnchat.c nnchat.c
diffstat 2 files changed, 51 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/libnnchat.c	Thu May 24 05:03:58 2012 +0300
+++ b/libnnchat.c	Thu May 24 05:28:44 2012 +0300
@@ -373,6 +373,16 @@
     if (conn == NULL)
         return -10;
 
+    /* Prod the input buffer */
+    if (conn->in_ptr > conn->buf && conn->in_ptr - conn->ptr > 0)
+    {
+        size_t delta = conn->in_ptr - conn->ptr;
+        memmove(conn->buf, conn->in_ptr, delta);
+        conn->ptr = conn->buf;
+        conn->in_ptr -= delta;
+        conn->total_bytes -= delta;
+    }
+
     /* Check for incoming data */
     socktv.tv_sec = 0;
     socktv.tv_usec = NN_DELAY_USEC;
--- a/nnchat.c	Thu May 24 05:03:58 2012 +0300
+++ b/nnchat.c	Thu May 24 05:28:44 2012 +0300
@@ -635,16 +635,15 @@
 }
 
 
-int handleUser(nn_conn_t *conn, const char *str)
+int nnproto_handle_user(nn_conn_t *conn)
 {
-    const char *msg = "</USER><MESSAGE>", *p = str;
+    static const char *msg = "</USER><MESSAGE>";
+    char *p = conn->ptr;
     BOOL isMine, isIgnored = FALSE;
     char *s, *t, *userName;
 
-    (void) conn;
-
     /* Find start of the message */
-    s = strstr(str, msg);
+    s = strstr(p, msg);
     if (!s) return 1;
     *s = 0;
     s += strlen(msg);
@@ -751,20 +750,18 @@
     return 0;
 }
 
-    str_get_timestamp(tmpStr, sizeof(tmpStr), "%c");
 
-int handleLogin(nn_conn_t *conn, const char *str)
+int nnproto_handle_login(nn_conn_t *conn)
 {
     char tmpStr[256];
+    str_get_timestamp(tmpStr, sizeof(tmpStr), "%c");
 
-    getTimeStamp(tmpStr, sizeof(tmpStr), "%c");
-
-    if (!strncmp(str, "FAILURE", 7))
+    if (!nn_conn_buf_strcmp(conn, "FAILURE>"))
     {
         printMsg(NULL, "½1½Login failure½0½ - ½3½%s½0½\n", tmpStr);
         return -2;
     }
-    else if (!strncmp(str, "SUCCESS", 7))
+    else if (!nn_conn_buf_strcmp(conn, "SUCCESS>"))
     {
         printMsg(NULL, "½2½Login success½0½ - ½3½%s½0½\n", tmpStr);
         nn_conn_send_msg(conn, optUserNameEnc, "%%2FRequestUserList");
@@ -775,13 +772,12 @@
 }
 
 
-int handleAddUser(nn_conn_t *conn, const char *str)
+int nnproto_handle_add_user(nn_conn_t *conn)
 {
-    char *p, *s = strstr(str, "</ADD_USER>");
+    char *p, *s, *str = conn->ptr;
     nn_window_t *win;
 
-    (void) conn;
-
+    s = nn_conn_buf_strstr(conn, "</ADD_USER>");
     if (!s) return 1;
     *s = 0;
 
@@ -800,13 +796,12 @@
 }
 
 
-int handleDeleteUser(nn_conn_t *conn, const char *str)
+int nnproto_handle_delete_user(nn_conn_t *conn)
 {
-    char *p, *s = strstr(str, "</DELETE_USER>");
+    char *p, *s, *str = conn->ptr;
     nn_window_t *win;
 
-    (void) conn;
-
+    s = nn_conn_buf_strstr(conn, "</DELETE_USER>");
     if (!s) return 1;
     *s = 0;
 
@@ -825,19 +820,16 @@
 }
 
 
-int handleFoo(nn_conn_t *conn, const char *str)
+int nnproto_handle_num_clients(nn_conn_t *conn)
 {
-    (void) conn;
-    (void) str;
-
+    nn_conn_buf_strstr(conn, "</NUMCLIENTS>");
     return 0;
 }
 
 
-int handleBoot(nn_conn_t *conn, const char *str)
+int nnproto_handle_boot(nn_conn_t *conn)
 {
     (void) conn;
-    (void) str;
     errorMsg("Booted by server.\n");
     return -1;
 }
@@ -847,24 +839,24 @@
 {
     char *cmd;
     ssize_t len;
-    int (*handler)(nn_conn_t *, const char *);
-} protocmd_t;
+    int (*handler)(nn_conn_t *);
+} nn_protocolcmd_t;
 
 
-static protocmd_t protoCmds[] =
+static nn_protocolcmd_t protoCmds[] =
 {
-    { "<USER>",         -1, handleUser },
-    { "<LOGIN_",        -1, handleLogin },
-    { "<DELETE_USER>",  -1, handleDeleteUser },
-    { "<ADD_USER>",     -1, handleAddUser },
-    { "<NUMCLIENTS>",   -1, handleFoo },
-    { "<BOOT />",       -1, handleBoot },
+    { "<USER>",         -1, nnproto_handle_user },
+    { "<LOGIN_",        -1, nnproto_handle_login },
+    { "<DELETE_USER>",  -1, nnproto_handle_delete_user },
+    { "<ADD_USER>",     -1, nnproto_handle_add_user },
+    { "<NUMCLIENTS>",   -1, nnproto_handle_num_clients },
+    { "<BOOT />",       -1, nnproto_handle_boot },
 };
 
 static const int nprotoCmds = sizeof(protoCmds) / sizeof(protoCmds[0]);
 
 
-int handleProtocol(nn_conn_t *conn, const char *buf, const ssize_t bufLen)
+int nn_parse_protocol(nn_conn_t *conn)
 {
     static BOOL protoCmdsInit = FALSE;
     int i;
@@ -873,19 +865,19 @@
     {
         for (i = 0; i < nprotoCmds; i++)
             protoCmds[i].len = strlen(protoCmds[i].cmd);
+
         protoCmdsInit = TRUE;
     }
 
     for (i = 0; i < nprotoCmds; i++)
     {
-        ssize_t cmdLen = protoCmds[i].len;
-        if (cmdLen < bufLen && !strncmp(buf, protoCmds[i].cmd, cmdLen))
-            return protoCmds[i].handler(conn, buf + cmdLen);
+        if (!nn_conn_buf_strncmp(conn, protoCmds[i].cmd, protoCmds[i].len))
+            return protoCmds[i].handler(conn);
     }
 
     if (optDebug)
     {
-        printMsg(NULL, "Unknown protocmd: \"%s\"\n", buf);
+        printMsg(NULL, "Unknown protocmd: \"%s\"\n", conn->ptr);
         return 0;
     }
     else
@@ -1704,33 +1696,27 @@
     nn_conn_reset(conn);
     while (!isError && !exitProg)
     {
-        int cres = nn_conn_pull(conn);
-        if (cres == 0 && *(conn->ptr - 1) == 0)
-        {
-            char *ptr = conn->buf;
-            do
+        nn_conn_reset(conn);
+        do {
+            int cres = nn_conn_pull(conn);
+            if (cres == 0 && *(conn->in_ptr - 1) == 0)
             {
-                size_t bufLen = strlen(ptr) + 1;
-                int result = handleProtocol(conn, ptr, bufLen);
-
+                int result = nn_parse_protocol(conn);
                 if (result > 0)
                 {
                     /* Couldn't handle the message for some reason */
-                    printMsg(currWin, "Could not handle: %s\n", ptr);
+                    printMsg(currWin, "Could not handle: %s\n", conn->ptr);
                 }
                 else if (result < 0)
                 {
                     /* Fatal error, quit */
-                    errorMsg("Fatal error with message: %s\n", ptr);
+                    errorMsg("Fatal error with message: %s\n", conn->ptr);
                     isError = TRUE;
                 }
+            }
+        }
+        while (conn->total_bytes > 0 && !isError);
 
-                conn->total -= bufLen;
-                ptr += bufLen;
-            }
-            while (conn->total > 0 && !isError);
-            nn_conn_reset(conn);
-        }
         if (!nn_conn_check(conn))
             isError = TRUE;