--- mqtt/src/daemon.c 2012/04/25 07:37:16 1.2.2.10 +++ mqtt/src/daemon.c 2012/04/25 16:36:26 1.2.2.15 @@ -38,17 +38,9 @@ initSession(int sock, ait_val_t * __restrict v) else sess->sess_retry = strtol(str, NULL, 0); - if (!(sess->sess_root = schedBegin())) { - ioLIBERR(sched); - free(sess); - io_freeVar(v); - return NULL; - } - sess->sess_buf = mqtt_msgAlloc(USHRT_MAX); if (!sess->sess_buf) { ioLIBERR(mqtt); - schedEnd(&sess->sess_root); free(sess); io_freeVar(v); return NULL; @@ -59,7 +51,6 @@ initSession(int sock, ait_val_t * __restrict v) if (!sess->sess_srv) { ioDEBUG(3, "Error:: in srv_Init #%d - %s", mqtt_GetErrno(), mqtt_GetError()); mqtt_msgFree(&sess->sess_buf, 42); - schedEnd(&sess->sess_root); free(sess); io_freeVar(v); return NULL; @@ -107,8 +98,6 @@ finiSession(struct tagSession *sess) SESS_ELEM_UNLOCK(sess); pthread_mutex_destroy(&sess->sess_mtx); - schedEnd(&sess->sess_root); - if (sess->sess_will.msg) free(sess->sess_will.msg); if (sess->sess_will.topic) @@ -148,52 +137,6 @@ stopSession(struct tagSession *sess) sess->sess_addr, sess->sess_user); } -static int -KASession(struct tagSession *sess) -{ - mqtt_msg_t msg = { NULL, 0 }; - int ret; - struct pollfd pfd; - - ioTRACE(4); - - assert(sess); - - /* ping request */ - ret = mqtt_msgPINGREQ(&msg); - if ((ret = send(sess->sess_sock, msg.msg_base, ret, MSG_NOSIGNAL)) == -1) { - ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno)); - return -1; - } else { - ioDEBUG(5, "Sended %d bytes for ping request", ret); - free(msg.msg_base); - memset(&msg, 0, sizeof msg); - } - - pfd.fd = sess->sess_sock; - pfd.events = POLLIN | POLLPRI; - if ((ret = poll(&pfd, 1, sess->sess_ka * 1000)) == -1 || - pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) { - ioDEBUG(3, "Error:: poll(%d) #%d - %s", sess->sess_sock, errno, strerror(errno)); - return -1; - } else if (!ret) { - ioDEBUG(5, "Warning:: Session is abandoned ... must be disconnect!"); - return 1; - } - /* receive & decode packet */ - if (recv(sess->sess_sock, sess->sess_buf->msg_base, sess->sess_buf->msg_len, 0) == -1) { - ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno)); - return -1; - } - if (mqtt_readPINGRESP(sess->sess_buf)) { - ioDEBUG(5, "Warning:: Session is broken, not hear ping response ... must be disconnect!"); - return 2; - } - - /* Keep Alive is OK! */ - return 0; -} - static void * thrSession(struct tagSession *sess) { @@ -212,7 +155,7 @@ thrSession(struct tagSession *sess) pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) { ioDEBUG(3, "Error:: poll(%d) #%d - %s", sess->sess_sock, errno, strerror(errno)); break; - } else if (!ret && (ret = KASession(sess))) { + } else if (!ret && (ret = mqtt_KeepAlive(sess->sess_sock, sess->sess_ka, 1))) { call.LOG(logg, "Session %s keep-alive missing from %s for user %s ...\n", sess->sess_cid, sess->sess_addr, sess->sess_user); break; @@ -239,10 +182,19 @@ thrSession(struct tagSession *sess) io_freeVar(v); } else ioLIBERR(mqtt); + + SESS_LOCK; + TAILQ_REMOVE(&Sessions, sess, sess_node); + SESS_UNLOCK; + locKill ^= locKill; - continue; + break; case MQTT_TYPE_DISCONNECT: ioDEBUG(5, "Exec DISCONNECT session"); + SESS_LOCK; + TAILQ_REMOVE(&Sessions, sess, sess_node); + SESS_UNLOCK; + finiSession(sess); locKill ^= locKill; continue; @@ -262,6 +214,9 @@ thrSession(struct tagSession *sess) case MQTT_TYPE_PINGREQ: ioDEBUG(5, "Exec PINGREQ session"); break; + case MQTT_TYPE_PINGRESP: + ioDEBUG(5, "Exec PINGRESP session"); + break; default: ioDEBUG(5, "Error:: Session %s, wrong command %d - DISCARDED", sess->sess_cid, hdr->mqtt_msg.type); @@ -271,6 +226,7 @@ thrSession(struct tagSession *sess) pthread_cleanup_pop(locKill); pthread_exit(NULL); + return NULL; } static void * @@ -333,6 +289,7 @@ startSession(sched_task_t *task) ioDEBUG(1, "Login:: ALLOWED for username %s ...", sess->sess_user); ret = MQTT_RETCODE_ACCEPTED; } + if (call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%") > 0) { ioDEBUG(2, "Old session %s should be disconnect!", sess->sess_cid); TAILQ_FOREACH(s, &Sessions, sess_node) @@ -342,6 +299,7 @@ startSession(sched_task_t *task) break; } } + if (call.InitSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, sess->sess_addr, sess->sess_will.flag, sess->sess_will.topic, sess->sess_will.msg, sess->sess_will.qos, sess->sess_will.retain) == -1) { @@ -425,7 +383,8 @@ acceptClient(sched_task_t *task) ioDEBUG(1, "Terminated client with socket=%d", cli); } end: - schedRead(TASK_ROOT(task), acceptClient, NULL, TASK_FD(task), NULL, 0); + if (!schedRead(TASK_ROOT(task), acceptClient, NULL, TASK_FD(task), NULL, 0)) + ioLIBERR(sched); return NULL; }