# HG changeset patch # User Matti Hamalainen # Date 1337826524 -10800 # Node ID 7bec02f382fb8f89783ee68df35b67008b349561 # Parent 563a70e8a30372a8398b922731676ae8c9d72950 Refactor the connection and protocol handling a bit. diff -r 563a70e8a303 -r 7bec02f382fb libnnchat.c --- 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; diff -r 563a70e8a303 -r 7bec02f382fb nnchat.c --- 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 = "", *p = str; + static const char *msg = ""; + 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, ""); + char *p, *s, *str = conn->ptr; nn_window_t *win; - (void) conn; - + s = nn_conn_buf_strstr(conn, ""); 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, ""); + char *p, *s, *str = conn->ptr; nn_window_t *win; - (void) conn; - + s = nn_conn_buf_strstr(conn, ""); 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, ""); 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[] = { - { "", -1, handleUser }, - { "", -1, handleDeleteUser }, - { "", -1, handleAddUser }, - { "", -1, handleFoo }, - { "", -1, handleBoot }, + { "", -1, nnproto_handle_user }, + { "", -1, nnproto_handle_delete_user }, + { "", -1, nnproto_handle_add_user }, + { "", -1, nnproto_handle_num_clients }, + { "", -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;