version 1.1.2.29, 2012/01/05 10:01:20
|
version 1.1.2.30, 2012/01/24 10:18:45
|
Line 4
|
Line 4
|
#include "mqttd.h" |
#include "mqttd.h" |
|
|
|
|
|
static void *startSession(sched_task_t *task); |
|
|
|
|
static inline struct tagSession * |
static inline struct tagSession * |
initSession(int sock, ait_val_t * __restrict v) |
initSession(int sock, ait_val_t * __restrict v) |
{ |
{ |
Line 29 initSession(int sock, ait_val_t * __restrict v)
|
Line 32 initSession(int sock, ait_val_t * __restrict v)
|
else |
else |
sess->sess_retry = strtol(str, NULL, 0); |
sess->sess_retry = strtol(str, NULL, 0); |
|
|
sess->sess_buf = mqtt_msgAlloc(0); | sess->sess_buf = mqtt_msgAlloc(USHRT_MAX); |
if (!sess->sess_buf) { |
if (!sess->sess_buf) { |
ioDEBUG(3, "Error:: in msgAlloc #%d - %s", mqtt_GetErrno(), mqtt_GetError()); |
ioDEBUG(3, "Error:: in msgAlloc #%d - %s", mqtt_GetErrno(), mqtt_GetError()); |
free(sess); |
free(sess); |
Line 44 initSession(int sock, ait_val_t * __restrict v)
|
Line 47 initSession(int sock, ait_val_t * __restrict v)
|
} |
} |
|
|
static void |
static void |
finiSession(struct tagSession *sess) | finiSession(struct tagSession *sess, int preservSock) |
{ |
{ |
register int i; |
|
struct tagStore *store; |
struct tagStore *store; |
|
|
ioTRACE(5); |
ioTRACE(5); |
Line 54 finiSession(struct tagSession *sess)
|
Line 56 finiSession(struct tagSession *sess)
|
if (!sess) |
if (!sess) |
return; |
return; |
|
|
for (i = 0; i < MQTT_QOS_RESERVED; i++) | if (call.FiniSessPUB) |
while ((store = SLIST_FIRST(&sess->sess_store[i]))) { | call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%"); |
SLIST_REMOVE_HEAD(&sess->sess_store[i], st_node); | |
free(store); | |
} | |
|
|
|
while ((store = SLIST_FIRST(&sess->sess_sndqueue))) { |
|
SLIST_REMOVE_HEAD(&sess->sess_sndqueue, st_node); |
|
free(store); |
|
} |
|
|
if (sess->sess_will.msg) |
if (sess->sess_will.msg) |
free(sess->sess_will.msg); |
free(sess->sess_will.msg); |
if (sess->sess_will.topic) |
if (sess->sess_will.topic) |
free(sess->sess_will.topic); |
free(sess->sess_will.topic); |
|
|
if (sess->sess_sock > STDERR_FILENO) | if (sess->sess_sock > STDERR_FILENO && !preservSock) |
srv_Close(sess->sess_sock); |
srv_Close(sess->sess_sock); |
|
|
mqtt_msgFree(&sess->sess_buf, 42); |
mqtt_msgFree(&sess->sess_buf, 42); |
Line 97 stopSession(struct tagSession *sess)
|
Line 101 stopSession(struct tagSession *sess)
|
} |
} |
|
|
ioDEBUG(1, "Close socket=%d", sess->sess_sock); |
ioDEBUG(1, "Close socket=%d", sess->sess_sock); |
finiSession(sess); | finiSession(sess, 0); |
|
|
call.LOG(logg, "Session %s stopped from %s for user %s.\n", sess->sess_cid, |
call.LOG(logg, "Session %s stopped from %s for user %s.\n", sess->sess_cid, |
sess->sess_addr, sess->sess_user); |
sess->sess_addr, sess->sess_user); |
Line 106 stopSession(struct tagSession *sess)
|
Line 110 stopSession(struct tagSession *sess)
|
static int |
static int |
KASession(struct tagSession *sess) |
KASession(struct tagSession *sess) |
{ |
{ |
u_char basebuf[USHRT_MAX]; | mqtt_msg_t msg = { NULL, 0 }; |
mqtt_msg_t msg = { NULL, 0 }, buf = { basebuf, sizeof basebuf }; | |
int ret; |
int ret; |
struct pollfd pfd; |
struct pollfd pfd; |
|
|
Line 128 KASession(struct tagSession *sess)
|
Line 131 KASession(struct tagSession *sess)
|
|
|
pfd.fd = sess->sess_sock; |
pfd.fd = sess->sess_sock; |
pfd.events = POLLIN | POLLPRI; |
pfd.events = POLLIN | POLLPRI; |
if ((ret = poll(&pfd, 1, sess->sess_ka)) == -1 || | if ((ret = poll(&pfd, 1, sess->sess_ka * 1000)) == -1 || |
pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) { |
pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) { |
ioDEBUG(3, "Error:: poll(%d) #%d - %s", sess->sess_sock, errno, strerror(errno)); |
ioDEBUG(3, "Error:: poll(%d) #%d - %s", sess->sess_sock, errno, strerror(errno)); |
return -1; |
return -1; |
} else if (!ret) { |
} else if (!ret) { |
/* problem!!! session is abandoned ... must be disconnect! */ | ioDEBUG(5, "Warning:: Session is abandoned ... must be disconnect!"); |
return 1; |
return 1; |
} |
} |
/* receive & decode packet */ |
/* receive & decode packet */ |
if (recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0) == -1) { | 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)); |
ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno)); |
return -1; |
return -1; |
} |
} |
if (mqtt_readPINGRESP(&buf)) { | if (mqtt_readPINGRESP(sess->sess_buf)) { |
/* problem!!! session is broken, not hear ping response ... must be disconnect! */ | ioDEBUG(5, "Warning:: Session is broken, not hear ping response ... must be disconnect!"); |
return 2; |
return 2; |
} |
} |
|
|
Line 153 KASession(struct tagSession *sess)
|
Line 156 KASession(struct tagSession *sess)
|
static void * |
static void * |
thrSession(struct tagSession *sess) |
thrSession(struct tagSession *sess) |
{ |
{ |
u_char basebuf[USHRT_MAX]; | mqtt_msg_t msg = { NULL, 0 }; |
mqtt_msg_t msg = { NULL, 0 }, buf = { basebuf, sizeof basebuf }; | int ret, locKill = 42; |
int ret; | |
struct pollfd pfd; |
struct pollfd pfd; |
struct mqtthdr *hdr; |
struct mqtthdr *hdr; |
|
ait_val_t *v; |
|
struct tagStore *store; |
|
|
pthread_cleanup_push((void(*)(void*)) stopSession, sess); |
pthread_cleanup_push((void(*)(void*)) stopSession, sess); |
ioTRACE(2); |
ioTRACE(2); |
|
|
pfd.fd = sess->sess_sock; |
pfd.fd = sess->sess_sock; |
pfd.events = POLLIN | POLLPRI; |
pfd.events = POLLIN | POLLPRI; |
while (!Kill) { | while (!Kill && locKill) { |
if ((ret = poll(&pfd, 1, sess->sess_ka)) == -1 || | if ((ret = poll(&pfd, 1, sess->sess_ka * 1000)) == -1 || |
pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) { |
pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) { |
ioDEBUG(3, "Error:: poll(%d) #%d - %s", sess->sess_sock, errno, strerror(errno)); |
ioDEBUG(3, "Error:: poll(%d) #%d - %s", sess->sess_sock, errno, strerror(errno)); |
break; |
break; |
Line 175 thrSession(struct tagSession *sess)
|
Line 179 thrSession(struct tagSession *sess)
|
break; |
break; |
} |
} |
/* receive & decode packet */ |
/* receive & decode packet */ |
if ((ret = recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0)) == -1) { | if ((ret = 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)); |
ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno)); |
break; |
break; |
} else if (!ret) { |
} else if (!ret) { |
ioDEBUG(4, "Session %s EOF received.", sess->sess_cid); |
ioDEBUG(4, "Session %s EOF received.", sess->sess_cid); |
break; |
break; |
} else |
} else |
hdr = (struct mqtthdr*) buf.msg_base; | hdr = (struct mqtthdr*) sess->sess_buf->msg_base; |
|
|
/* dispatch message type */ |
/* dispatch message type */ |
switch (hdr->mqtt_msg.type) { |
switch (hdr->mqtt_msg.type) { |
|
case MQTT_TYPE_CONNECT: |
|
ioDEBUG(5, "Exec CONNECT session"); |
|
pthread_mutex_lock(&mtx_sess); |
|
TAILQ_REMOVE(&Sessions, sess, sess_node); |
|
pthread_mutex_unlock(&mtx_sess); |
|
|
|
if (call.FiniSessPUB) |
|
call.FiniSessPUB(&cfg, pub, sess->sess_cid, sess->sess_user, "%"); |
|
|
|
while ((store = SLIST_FIRST(&sess->sess_sndqueue))) { |
|
SLIST_REMOVE_HEAD(&sess->sess_sndqueue, st_node); |
|
free(store); |
|
} |
|
|
|
if (sess->sess_will.msg) |
|
free(sess->sess_will.msg); |
|
if (sess->sess_will.topic) |
|
free(sess->sess_will.topic); |
|
|
|
if ((v = io_allocVar())) { |
|
AIT_SET_STR(v, sess->sess_addr); |
|
if (!schedEvent(root, startSession, v, (u_long) sess->sess_sock, sess, ret)) |
|
io_freeVar(v); |
|
} else |
|
ioLIBERR(mqtt); |
|
|
|
locKill ^= locKill; |
|
continue; |
|
case MQTT_TYPE_DISCONNECT: |
|
ioDEBUG(5, "Exec DISCONNECT session"); |
|
pthread_mutex_lock(&mtx_sess); |
|
TAILQ_REMOVE(&Sessions, sess, sess_node); |
|
pthread_mutex_unlock(&mtx_sess); |
|
|
|
finiSession(sess, 0); |
|
locKill ^= locKill; |
|
continue; |
|
case MQTT_TYPE_PUBLISH: |
|
ioDEBUG(5, "Work in progress ..."); |
|
break; |
|
case MQTT_TYPE_PUBREL: |
|
break; |
|
case MQTT_TYPE_SUBSCRIBE: |
|
break; |
|
case MQTT_TYPE_UNSUBSCRIBE: |
|
break; |
|
case MQTT_TYPE_PINGREQ: |
|
break; |
default: |
default: |
ioDEBUG(5, "Error:: Session %s, wrong command %d - DISCARDED", |
ioDEBUG(5, "Error:: Session %s, wrong command %d - DISCARDED", |
sess->sess_cid, hdr->mqtt_msg.type); |
sess->sess_cid, hdr->mqtt_msg.type); |
Line 193 thrSession(struct tagSession *sess)
|
Line 245 thrSession(struct tagSession *sess)
|
} |
} |
} |
} |
|
|
pthread_cleanup_pop(42); | pthread_cleanup_pop(locKill); |
pthread_exit(NULL); |
pthread_exit(NULL); |
} |
} |
|
|
Line 212 startSession(sched_task_t *task)
|
Line 264 startSession(sched_task_t *task)
|
|
|
assert(task); |
assert(task); |
|
|
sess = initSession(TASK_FD(task), TASK_ARG(task)); | ioDEBUG(3, "task_Data=%p", TASK_DATA(task)); |
if (!sess) { | if (!TASK_DATA(task)) { |
io_freeVar(TASK_ARG(task)); | sess = initSession(TASK_FD(task), TASK_ARG(task)); |
close(TASK_FD(task)); | if (!sess) { |
return NULL; | io_freeVar(TASK_ARG(task)); |
} | close(TASK_FD(task)); |
| return NULL; |
| } |
|
|
/* receive & decode packet */ | /* receive & decode packet */ |
if (recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0) == -1) { | if (recv(sess->sess_sock, buf.msg_base, buf.msg_len, 0) == -1) { |
ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno)); | ioDEBUG(3, "Error:: recv(%d) #%d - %s", sess->sess_sock, errno, strerror(errno)); |
finiSession(sess); | finiSession(sess, 0); |
return NULL; | return NULL; |
| } |
| } else { |
| sess = TASK_DATA(task); |
| buf.msg_len = TASK_DATLEN(task) > sizeof basebuf ? sizeof basebuf : TASK_DATLEN(task); |
| ioDEBUG(3, "debug:: sock=%d s=%p sbuf=%p sbl=%d ret=%d\n", sess->sess_sock, sess->sess_buf, sess->sess_buf->msg_base, (int) sess->sess_buf->msg_len, (int) TASK_DATLEN(task)); |
| memcpy(buf.msg_base, sess->sess_buf->msg_base, buf.msg_len); |
} |
} |
|
|
cack = mqtt_readCONNECT(&buf, &sess->sess_ka, sess->sess_cid, sizeof sess->sess_cid, |
cack = mqtt_readCONNECT(&buf, &sess->sess_ka, sess->sess_cid, sizeof sess->sess_cid, |
sess->sess_user, sizeof sess->sess_user, sess->sess_pass, sizeof sess->sess_pass, |
sess->sess_user, sizeof sess->sess_user, sess->sess_pass, sizeof sess->sess_pass, |
&sess->sess_will.topic, &sess->sess_will.msg); |
&sess->sess_will.topic, &sess->sess_will.msg); |
Line 274 startSession(sched_task_t *task)
|
Line 335 startSession(sched_task_t *task)
|
ret = mqtt_msgCONNACK(&msg, ret); |
ret = mqtt_msgCONNACK(&msg, ret); |
if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) { |
if ((ret = send(sess->sess_sock, msg.msg_base, ret, 0)) == -1) { |
ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno)); |
ioDEBUG(3, "Error:: send(%d) #%d - %s", sess->sess_sock, errno, strerror(errno)); |
finiSession(sess); | finiSession(sess, 0); |
return NULL; |
return NULL; |
} else { |
} else { |
ioDEBUG(5, "Sended %d bytes", ret); |
ioDEBUG(5, "Sended %d bytes", ret); |
Line 291 startSession(sched_task_t *task)
|
Line 352 startSession(sched_task_t *task)
|
pthread_create(&sess->sess_tid, &attr, (void*(*)(void*)) thrSession, sess); |
pthread_create(&sess->sess_tid, &attr, (void*(*)(void*)) thrSession, sess); |
pthread_mutex_unlock(&mtx_sess); |
pthread_mutex_unlock(&mtx_sess); |
|
|
call.LOG(logg, "Session %s started from %s for user %s OK!\n", sess->sess_cid, | call.LOG(logg, "Session %s started from %s for user %s (timeout=%d) OK!\n", sess->sess_cid, |
sess->sess_addr, sess->sess_user); | sess->sess_addr, sess->sess_user, sess->sess_ka); |
|
|
pthread_attr_destroy(&attr); |
pthread_attr_destroy(&attr); |
return NULL; |
return NULL; |
Line 307 end: /* close client connection */
|
Line 368 end: /* close client connection */
|
} |
} |
|
|
ioDEBUG(1, "Close client %s with socket=%d", sess->sess_addr, sess->sess_sock); |
ioDEBUG(1, "Close client %s with socket=%d", sess->sess_addr, sess->sess_sock); |
finiSession(sess); | finiSession(sess, 0); |
return NULL; |
return NULL; |
} |
} |
|
|
Line 325 thrSched(void *arg __unused)
|
Line 386 thrSched(void *arg __unused)
|
TAILQ_FOREACH(sess, &Sessions, sess_node) |
TAILQ_FOREACH(sess, &Sessions, sess_node) |
if (sess->sess_tid) |
if (sess->sess_tid) |
pthread_cancel(sess->sess_tid); |
pthread_cancel(sess->sess_tid); |
|
ioDEBUG(5, "EXIT from Scheduler thread !!!"); |
pthread_exit(NULL); |
pthread_exit(NULL); |
} |
} |
|
|
Line 341 Run(int sock)
|
Line 403 Run(int sock)
|
ioTRACE(1); |
ioTRACE(1); |
|
|
if (pthread_create(&tid, NULL, thrSched, NULL)) { |
if (pthread_create(&tid, NULL, thrSched, NULL)) { |
ioLOGERR(0); | ioSYSERR(0); |
return -1; |
return -1; |
} else |
} else |
pthread_detach(tid); |
pthread_detach(tid); |
ioDEBUG(2, "Run scheduler management thread"); |
ioDEBUG(2, "Run scheduler management thread"); |
|
|
if (listen(sock, SOMAXCONN) == -1) { |
if (listen(sock, SOMAXCONN) == -1) { |
ioLOGERR(0); | ioSYSERR(0); |
return -1; |
return -1; |
} |
} |
|
|
Line 356 Run(int sock)
|
Line 418 Run(int sock)
|
if ((cli = accept(sock, &sa.sa, &sslen)) == -1) { |
if ((cli = accept(sock, &sa.sa, &sslen)) == -1) { |
if (!Kill) |
if (!Kill) |
continue; |
continue; |
ioLOGERR(0); | ioSYSERR(0); |
break; |
break; |
} |
} |
v = io_allocVar(); |
v = io_allocVar(); |
Line 370 Run(int sock)
|
Line 432 Run(int sock)
|
} |
} |
ioDEBUG(1, "Connected client with socket=%d from %s", cli, AIT_GET_STR(v)); |
ioDEBUG(1, "Connected client with socket=%d from %s", cli, AIT_GET_STR(v)); |
|
|
if (!schedRead(root, startSession, v, cli)) { | if (!schedRead(root, startSession, v, cli, NULL, 0)) { |
io_freeVar(v); |
io_freeVar(v); |
close(cli); |
close(cli); |
ioDEBUG(1, "Terminated client with socket=%d", cli); |
ioDEBUG(1, "Terminated client with socket=%d", cli); |