diff --git a/qse/cmd/http/httpd.c b/qse/cmd/http/httpd.c
index b31e7edf..3dbfc5e6 100644
--- a/qse/cmd/http/httpd.c
+++ b/qse/cmd/http/httpd.c
@@ -624,7 +624,7 @@ static int query_server (
(cgi->type == CGI_NAME && qse_mbscmp (xpath_base, cgi->spec) == 0))
{
scgi->cgi = 1;
- scgi->nph = cgi->nph;
+ scgi->nph = cgi->nph;
scgi->shebang = cgi->shebang;
return 0;
}
diff --git a/qse/include/qse/cmn/Makefile.am b/qse/include/qse/cmn/Makefile.am
index c85a061c..11243cac 100644
--- a/qse/include/qse/cmn/Makefile.am
+++ b/qse/include/qse/cmn/Makefile.am
@@ -41,6 +41,7 @@ pkginclude_HEADERS = \
task.h \
time.h \
tio.h \
+ tmr.h \
tre.h \
uni.h \
uri.h \
diff --git a/qse/include/qse/cmn/Makefile.in b/qse/include/qse/cmn/Makefile.in
index 1f1c55ed..3808010b 100644
--- a/qse/include/qse/cmn/Makefile.in
+++ b/qse/include/qse/cmn/Makefile.in
@@ -85,8 +85,8 @@ am__pkginclude_HEADERS_DIST = alg.h chr.h cp949.h cp950.h dir.h dll.h \
env.h fio.h fma.h fmt.h fs.h gdl.h glob.h htb.h hton.h ipad.h \
lda.h main.h map.h mb8.h mbwc.h mem.h mux.h nwad.h nwif.h \
nwio.h oht.h opt.h path.h pio.h pma.h rbt.h rex.h sio.h sll.h \
- slmb.h str.h task.h time.h tio.h tre.h uni.h uri.h utf8.h \
- xma.h Mmgr.hpp StdMmgr.hpp Mmged.hpp
+ slmb.h str.h task.h time.h tio.h tmr.h tre.h uni.h uri.h \
+ utf8.h xma.h Mmgr.hpp StdMmgr.hpp Mmged.hpp
am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`;
am__vpath_adj = case $$p in \
$(srcdir)/*) f=`echo "$$p" | sed "s|^$$srcdirstrip/||"`;; \
@@ -299,7 +299,7 @@ pkginclude_HEADERS = alg.h chr.h cp949.h cp950.h dir.h dll.h env.h \
fio.h fma.h fmt.h fs.h gdl.h glob.h htb.h hton.h ipad.h lda.h \
main.h map.h mb8.h mbwc.h mem.h mux.h nwad.h nwif.h nwio.h \
oht.h opt.h path.h pio.h pma.h rbt.h rex.h sio.h sll.h slmb.h \
- str.h task.h time.h tio.h tre.h uni.h uri.h utf8.h xma.h \
+ str.h task.h time.h tio.h tmr.h tre.h uni.h uri.h utf8.h xma.h \
$(am__append_1)
all: all-am
diff --git a/qse/include/qse/cmn/time.h b/qse/include/qse/cmn/time.h
index 64174fd4..a1cddd1a 100644
--- a/qse/include/qse/cmn/time.h
+++ b/qse/include/qse/cmn/time.h
@@ -110,7 +110,7 @@ struct qse_btime_t
/*int offset;*/
};
-#define qse_cleartime(x) ((x)->tv = (x)->nsec = 0);
+#define qse_cleartime(x) ((x)->sec = (x)->nsec = 0);
#define qse_cmptime(x,y) \
(((x)->sec == (y)->sec)? ((x)->nsec - (y)->nsec): \
((x)->sec - (y)->sec))
diff --git a/qse/include/qse/cmn/tmr.h b/qse/include/qse/cmn/tmr.h
new file mode 100644
index 00000000..7be152c5
--- /dev/null
+++ b/qse/include/qse/cmn/tmr.h
@@ -0,0 +1,133 @@
+/*
+ * $Id$
+ *
+ Copyright 2006-2014 Chung, Hyung-Hwan.
+ This file is part of QSE.
+
+ QSE is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation, either version 3 of
+ the License, or (at your option) any later version.
+
+ QSE is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with QSE. If not, see .
+ */
+
+#ifndef _QSE_CMN_TMR_H_
+#define _QSE_CMN_TMR_H_
+
+#include
+
+typedef struct qse_tmr_t qse_tmr_t;
+typedef struct qse_tmr_event_t qse_tmr_event_t;
+
+typedef void (*qse_tmr_handler_t) (
+ qse_tmr_t* tmr,
+ const qse_ntime_t* now,
+ void* ctx
+);
+
+typedef void (*qse_tmr_updater_t) (
+ qse_tmr_t* tmr,
+ qse_size_t old_index,
+ qse_size_t new_index,
+ void* ctx
+);
+
+struct qse_tmr_t
+{
+ qse_mmgr_t* mmgr;
+ qse_size_t capa;
+ qse_size_t size;
+ qse_tmr_event_t* event;
+};
+
+struct qse_tmr_event_t
+{
+ qse_ntime_t when;
+ void* ctx;
+ qse_tmr_handler_t handler;
+ qse_tmr_updater_t updater;
+};
+
+#define QSE_TMR_INVALID ((qse_size_t)-1)
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+QSE_EXPORT qse_tmr_t* qse_tmr_open (
+ qse_mmgr_t* mmgr,
+ qse_size_t xtnsize,
+ qse_size_t capa
+);
+
+QSE_EXPORT void qse_tmr_close (
+ qse_tmr_t* tmr
+);
+
+QSE_EXPORT int qse_tmr_init (
+ qse_tmr_t* tmr,
+ qse_mmgr_t* mmgr,
+ qse_size_t capa
+);
+
+QSE_EXPORT void qse_tmr_fini (
+ qse_tmr_t* tmr
+);
+
+QSE_EXPORT qse_mmgr_t* qse_tmr_getmmgr (
+ qse_tmr_t* tmr
+);
+
+QSE_EXPORT void* qse_tmr_getxtn (
+ qse_tmr_t* tmr
+);
+
+QSE_EXPORT void qse_tmr_clear (
+ qse_tmr_t* tmr
+);
+
+/**
+ * The qse_tmr_insert() function schedules a new event.
+ *
+ * \return #QSE_TMR_INVALID on failure, valid index on success.
+ */
+
+QSE_EXPORT qse_size_t qse_tmr_insert (
+ qse_tmr_t* tmr,
+ const qse_tmr_event_t* event
+);
+
+QSE_EXPORT qse_size_t qse_tmr_update (
+ qse_tmr_t* tmr,
+ qse_size_t index,
+ const qse_tmr_event_t* event
+);
+
+QSE_EXPORT void qse_tmr_remove (
+ qse_tmr_t* tmr,
+ qse_size_t index
+);
+
+QSE_EXPORT qse_size_t qse_tmr_fire (
+ qse_tmr_t* tmr,
+ const qse_ntime_t* tm
+);
+
+QSE_EXPORT int qse_tmr_gettmout (
+ qse_tmr_t* tmr,
+ const qse_ntime_t* tm,
+ qse_ntime_t* tmout
+);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/qse/include/qse/http/httpd.h b/qse/include/qse/http/httpd.h
index 6fe9b50b..ac1a1cc6 100644
--- a/qse/include/qse/http/httpd.h
+++ b/qse/include/qse/http/httpd.h
@@ -27,6 +27,7 @@
#include
#include
#include
+#include
typedef struct qse_httpd_t qse_httpd_t;
typedef struct qse_httpd_mate_t qse_httpd_mate_t;
@@ -421,6 +422,9 @@ struct qse_httpd_client_t
qse_httpd_task_trigger_t trigger;
qse_ntime_t last_active;
+ qse_size_t tmr_idle;
+ qse_size_t tmr_dns;
+
qse_httpd_client_t* prev;
qse_httpd_client_t* next;
@@ -468,6 +472,7 @@ struct qse_httpd_server_t
qse_ubi_t handle;
/* private */
+ qse_httpd_t* httpd;
qse_httpd_server_t* next;
qse_httpd_server_t* prev;
};
diff --git a/qse/lib/cmn/Makefile.am b/qse/lib/cmn/Makefile.am
index 1deafd5d..e9296c59 100644
--- a/qse/lib/cmn/Makefile.am
+++ b/qse/lib/cmn/Makefile.am
@@ -106,6 +106,7 @@ libqsecmn_la_SOURCES = \
task.c \
time.c \
tio.c \
+ tmr.c \
tre.c \
tre-ast.c \
tre-compile.c \
diff --git a/qse/lib/cmn/Makefile.in b/qse/lib/cmn/Makefile.in
index 844bc948..0a2ce091 100644
--- a/qse/lib/cmn/Makefile.in
+++ b/qse/lib/cmn/Makefile.in
@@ -115,7 +115,7 @@ am__libqsecmn_la_SOURCES_DIST = alg-base64.c alg-rand.c alg-search.c \
str-fcpy.c str-fmt.c str-fnmat.c str-incl.c str-join.c \
str-len.c str-pac.c str-pbrk.c str-put.c str-rev.c str-rot.c \
str-set.c str-spl.c str-spn.c str-str.c str-subst.c str-tok.c \
- str-trm.c str-word.c task.c time.c tio.c tre.c tre-ast.c \
+ str-trm.c str-word.c task.c time.c tio.c tmr.c tre.c tre-ast.c \
tre-compile.c tre-match-backtrack.c tre-match-parallel.c \
tre-parse.c tre-stack.c uri.c utf8.c xma.c uni.c cp949.c \
cp950.c
@@ -134,7 +134,7 @@ am_libqsecmn_la_OBJECTS = alg-base64.lo alg-rand.lo alg-search.lo \
str-join.lo str-len.lo str-pac.lo str-pbrk.lo str-put.lo \
str-rev.lo str-rot.lo str-set.lo str-spl.lo str-spn.lo \
str-str.lo str-subst.lo str-tok.lo str-trm.lo str-word.lo \
- task.lo time.lo tio.lo tre.lo tre-ast.lo tre-compile.lo \
+ task.lo time.lo tio.lo tmr.lo tre.lo tre-ast.lo tre-compile.lo \
tre-match-backtrack.lo tre-match-parallel.lo tre-parse.lo \
tre-stack.lo uri.lo utf8.lo xma.lo $(am__objects_1) \
$(am__objects_2)
@@ -422,7 +422,7 @@ libqsecmn_la_SOURCES = alg-base64.c alg-rand.c alg-search.c alg-sort.c \
str-fcpy.c str-fmt.c str-fnmat.c str-incl.c str-join.c \
str-len.c str-pac.c str-pbrk.c str-put.c str-rev.c str-rot.c \
str-set.c str-spl.c str-spn.c str-str.c str-subst.c str-tok.c \
- str-trm.c str-word.c task.c time.c tio.c tre.c tre-ast.c \
+ str-trm.c str-word.c task.c time.c tio.c tmr.c tre.c tre-ast.c \
tre-compile.c tre-match-backtrack.c tre-match-parallel.c \
tre-parse.c tre-stack.c uri.c utf8.c xma.c $(am__append_1) \
$(am__append_2)
@@ -591,6 +591,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/task.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/time.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tio.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tmr.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tre-ast.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tre-compile.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tre-match-backtrack.Plo@am__quote@
diff --git a/qse/lib/cmn/time.c b/qse/lib/cmn/time.c
index a26ece5e..631365b7 100644
--- a/qse/lib/cmn/time.c
+++ b/qse/lib/cmn/time.c
@@ -504,8 +504,8 @@ int qse_timelocal (const qse_btime_t* bt, qse_ntime_t* nt)
void qse_addtime (const qse_ntime_t* x, const qse_ntime_t* y, qse_ntime_t* z)
{
- QSE_ASSERT (x->nsec < QSE_NSECS_PER_SEC);
- QSE_ASSERT (y->nsec < QSE_NSECS_PER_SEC);
+ QSE_ASSERT (x->nsec >= 0 && x->nsec < QSE_NSECS_PER_SEC);
+ QSE_ASSERT (y->nsec >= 0 && y->nsec < QSE_NSECS_PER_SEC);
z->sec = x->sec + y->sec;
z->nsec = x->nsec + y->nsec;
@@ -519,8 +519,8 @@ void qse_addtime (const qse_ntime_t* x, const qse_ntime_t* y, qse_ntime_t* z)
void qse_subtime (const qse_ntime_t* x, const qse_ntime_t* y, qse_ntime_t* z)
{
- QSE_ASSERT (x->nsec < QSE_NSECS_PER_SEC);
- QSE_ASSERT (y->nsec < QSE_NSECS_PER_SEC);
+ QSE_ASSERT (x->nsec >= 0 && x->nsec < QSE_NSECS_PER_SEC);
+ QSE_ASSERT (y->nsec >= 0 && y->nsec < QSE_NSECS_PER_SEC);
z->sec = x->sec - y->sec;
z->nsec = x->nsec - y->nsec;
diff --git a/qse/lib/cmn/tmr.c b/qse/lib/cmn/tmr.c
new file mode 100644
index 00000000..12d6c266
--- /dev/null
+++ b/qse/lib/cmn/tmr.c
@@ -0,0 +1,249 @@
+/*
+ * $Id$
+ *
+ Copyright 2006-2014 Chung, Hyung-Hwan.
+ This file is part of QSE.
+
+ QSE is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundatmrn, either version 3 of
+ the License, or (at your optmrn) any later version.
+
+ QSE is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with QSE. If not, see .
+ */
+
+#include
+#include "mem.h"
+
+#define HEAP_PARENT(x) (((x) - 1) / 2)
+#define HEAP_LEFT(x) ((x) * 2 + 1)
+#define HEAP_RIGHT(x) ((x) * 2 + 2)
+
+#define YOUNGER_THAN(x,y) (qse_cmptime(&(x)->when, &(y)->when) < 0)
+
+qse_tmr_t* qse_tmr_open (qse_mmgr_t* mmgr, qse_size_t xtnsize, qse_size_t capa)
+{
+ qse_tmr_t* tmr;
+
+ tmr = QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(qse_tmr_t) + xtnsize);
+ if (tmr)
+ {
+ if (qse_tmr_init (tmr, mmgr, capa) <= -1)
+ {
+ QSE_MMGR_FREE (mmgr, tmr);
+ return QSE_NULL;
+ }
+
+ else QSE_MEMSET (QSE_XTN(tmr), 0, xtnsize);
+ }
+
+ return tmr;
+}
+
+
+void qse_tmr_close (qse_tmr_t* tmr)
+{
+ qse_tmr_fini (tmr);
+ QSE_MMGR_FREE (tmr->mmgr, tmr);
+}
+
+int qse_tmr_init (qse_tmr_t* tmr, qse_mmgr_t* mmgr, qse_size_t capa)
+{
+ QSE_MEMSET (tmr, 0, QSE_SIZEOF(*tmr));
+
+ if (capa <= 0) capa = 1;
+
+ tmr->event = QSE_MMGR_ALLOC (mmgr, capa * QSE_SIZEOF(*tmr->event));
+ if (tmr->event == QSE_NULL) return -1;
+
+ tmr->mmgr = mmgr;
+ tmr->capa = capa;
+ return 0;
+}
+
+void qse_tmr_fini (qse_tmr_t* tmr)
+{
+ if (tmr->event) QSE_MMGR_FREE (tmr->mmgr, tmr->event);
+}
+
+qse_mmgr_t* qse_tmr_getmmgr (qse_tmr_t* tmr)
+{
+ return tmr->mmgr;
+}
+
+void* qse_tmr_getxtn (qse_tmr_t* tmr)
+{
+ return QSE_XTN (tmr);
+}
+
+void qse_tmr_clear (qse_tmr_t* tmr)
+{
+ tmr->size = 0;
+}
+
+static qse_size_t sift_up (qse_tmr_t* tmr, qse_size_t index)
+{
+ qse_size_t parent;
+
+ parent = HEAP_PARENT(index);
+ if (index > 0 && YOUNGER_THAN(&tmr->event[index], &tmr->event[parent]))
+ {
+ qse_tmr_event_t item = tmr->event[index];
+ qse_size_t old_index = index;
+
+ do
+ {
+ /* move down the parent to my current position */
+ tmr->event[index] = tmr->event[parent];
+ tmr->event[index].updater (tmr, parent, index, tmr->event[index].ctx);
+
+ /* traverse up */
+ index = parent;
+ parent = HEAP_PARENT(parent);
+ }
+ while (index > 0 && YOUNGER_THAN(&item, &tmr->event[parent]));
+
+ tmr->event[index] = item;
+ if (index != old_index)
+ tmr->event[index].updater (tmr, old_index, index, tmr->event[index].ctx);
+ }
+
+ return index;
+}
+
+static qse_size_t sift_down (qse_tmr_t* tmr, qse_size_t index)
+{
+ qse_size_t base = tmr->size / 2;
+
+ if (index < base) /* at least 1 child is under the 'index' positmrn */
+ {
+ qse_tmr_event_t item = tmr->event[index];
+ qse_size_t old_index = index;
+
+ do
+ {
+ qse_size_t left, right, younger;
+
+ left= HEAP_LEFT(index);
+ right = HEAP_RIGHT(index);
+
+ if (right < tmr->size && YOUNGER_THAN(&tmr->event[right], &tmr->event[left]))
+ {
+ younger = right;
+ }
+ else
+ {
+ younger = left;
+ }
+
+ if (YOUNGER_THAN(&item, &tmr->event[younger])) break;
+
+ tmr->event[index] = tmr->event[younger];
+ tmr->event[index].updater (tmr, younger, index, tmr->event[index].ctx);
+
+ index = younger;
+ }
+ while (index < base);
+
+ tmr->event[index] = item;
+ if (index != old_index)
+ tmr->event[index].updater (tmr, old_index, index, tmr->event[index].ctx);
+ }
+
+ return index;
+}
+
+void qse_tmr_remove (qse_tmr_t* tmr, qse_size_t index)
+{
+ qse_tmr_event_t item;
+
+ QSE_ASSERT (index < tmr->size);
+
+ item = tmr->event[index];
+ tmr->event[index].updater (tmr, index, QSE_TMR_INVALID, tmr->event[index].ctx);
+ tmr->size = tmr->size - 1;
+ if (tmr->size > 0)
+ {
+ tmr->event[index] = tmr->event[tmr->size];
+ tmr->event[index].updater (tmr, tmr->size, index, tmr->event[index].ctx);
+ YOUNGER_THAN(&tmr->event[index], &item)? sift_up(tmr, index): sift_down(tmr, index);
+ }
+}
+
+
+qse_size_t qse_tmr_insert (qse_tmr_t* tmr, const qse_tmr_event_t* event)
+{
+ qse_size_t index = tmr->size;
+
+ if (index >= tmr->capa)
+ {
+ qse_tmr_event_t* tmp;
+ qse_size_t new_capa;
+
+ new_capa = tmr->capa * 2;
+ tmp = QSE_MMGR_REALLOC (tmr->mmgr, tmr->event, new_capa);
+ if (tmp == QSE_NULL) return QSE_TMR_INVALID;
+
+ tmr->event = tmp;
+ tmr->capa = new_capa;
+ }
+
+ tmr->size = tmr->size + 1;
+ tmr->event[index] = *event;
+ return sift_up (tmr, index);
+}
+
+qse_size_t qse_tmr_update (qse_tmr_t* tmr, qse_size_t index, const qse_tmr_event_t* event)
+{
+ qse_tmr_event_t item;
+
+ item = tmr->event[index];
+ tmr->event[index] = *event;
+ return YOUNGER_THAN(event, &item)? sift_up (tmr, index): sift_down (tmr, index);
+}
+
+qse_size_t qse_tmr_fire (qse_tmr_t* tmr, const qse_ntime_t* tm)
+{
+ qse_ntime_t now;
+ qse_tmr_event_t event;
+ qse_size_t fire_count = 0;
+
+ /* if the current time is not specified, get it from the system */
+ if (tm) now = *tm;
+ else if (qse_gettime (&now) <= -1) return -1;
+
+ while (tmr->size > 0)
+ {
+ if (qse_cmptime(&tmr->event[0].when, &now) > 0) break;
+
+ event = tmr->event[0];
+ qse_tmr_remove (tmr, 0);
+
+ fire_count++;
+ event.handler (tmr, &now, event.ctx);
+ }
+
+ return fire_count;
+}
+
+int qse_tmr_gettmout (qse_tmr_t* tmr, const qse_ntime_t* tm, qse_ntime_t* tmout)
+{
+ qse_ntime_t now;
+
+ /* time-out can't be calculated when there's no event scheduled */
+ if (tmr->size <= 0) return -1;
+
+ /* if the current time is not specified, get it from the system */
+ if (tm) now = *tm;
+ else if (qse_gettime (&now) <= -1) return -1;
+
+ qse_subtime (&tmr->event[0].when, &now, tmout);
+ if (tmout->sec < 0) qse_cleartime (tmout);
+}
+
diff --git a/qse/lib/http/httpd.c b/qse/lib/http/httpd.c
index 4f5db2c6..4681dcf9 100644
--- a/qse/lib/http/httpd.c
+++ b/qse/lib/http/httpd.c
@@ -26,6 +26,7 @@
#include
typedef struct htrd_xtn_t htrd_xtn_t;
+typedef struct tmr_xtn_t tmr_xtn_t;
struct htrd_xtn_t
{
@@ -46,7 +47,6 @@ qse_httpd_t* qse_httpd_open (qse_mmgr_t* mmgr, qse_size_t xtnsize)
httpd = (qse_httpd_t*) QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(qse_httpd_t) + xtnsize);
if (httpd)
{
-
if (qse_httpd_init (httpd, mmgr) <= -1)
{
QSE_MMGR_FREE (mmgr, httpd);
@@ -74,6 +74,8 @@ int qse_httpd_init (qse_httpd_t* httpd, qse_mmgr_t* mmgr)
QSE_MEMSET (httpd, 0, QSE_SIZEOF(*httpd));
httpd->mmgr = mmgr;
+ httpd->tmr = qse_tmr_open (mmgr, 0, 2048);
+ if (httpd->tmr == QSE_NULL) return -1;
qse_mbscpy (httpd->sname, QSE_MT("QSE-HTTPD " QSE_PACKAGE_VERSION));
@@ -86,6 +88,7 @@ int qse_httpd_init (qse_httpd_t* httpd, qse_mmgr_t* mmgr)
void qse_httpd_fini (qse_httpd_t* httpd)
{
free_server_list (httpd);
+ qse_tmr_close (httpd->tmr);
}
void qse_httpd_stop (qse_httpd_t* httpd)
@@ -379,11 +382,23 @@ static qse_htrd_recbs_t htrd_recbs =
};
/* ----------------------------------------------------------------------- */
+static void tmr_idle_updated (qse_tmr_t* tmr, qse_size_t old_index, qse_size_t new_index, void* ctx);
+static void check_if_client_is_idle (qse_tmr_t* tmr, const qse_ntime_t* now, void* ctx);
-static qse_httpd_client_t* new_client (
- qse_httpd_t* httpd, qse_httpd_client_t* tmpl)
+static void mark_bad_client (qse_httpd_client_t* client)
+{
+ if (!(client->status & CLIENT_BAD))
+ {
+ client->status |= CLIENT_BAD;
+ client->bad_next = client->server->httpd->client.bad;
+ client->server->httpd->client.bad = client;
+ }
+}
+
+static qse_httpd_client_t* new_client (qse_httpd_t* httpd, qse_httpd_client_t* tmpl)
{
qse_httpd_client_t* client;
+ qse_tmr_event_t idle_event;
htrd_xtn_t* xtn;
client = qse_httpd_allocmem (httpd, QSE_SIZEOF(*client));
@@ -400,6 +415,24 @@ static qse_httpd_client_t* new_client (
return QSE_NULL;
}
+ qse_gettime (&idle_event.when);
+ qse_addtime (&idle_event.when, &httpd->opt.idle_limit, &idle_event.when);
+ idle_event.ctx = client;
+ idle_event.handler = check_if_client_is_idle;
+ idle_event.updater = tmr_idle_updated;
+
+/*TODO: */ client->tmr_dns = QSE_TMR_INVALID;
+
+ client->tmr_idle = qse_tmr_insert (httpd->tmr, &idle_event);
+ if (client->tmr_idle == QSE_TMR_INVALID)
+ {
+ httpd->errnum = QSE_HTTPD_ENOMEM;
+ qse_htrd_close (client->htrd);
+ qse_httpd_freemem (httpd, client);
+ return QSE_NULL;
+ }
+printf ("tmr_insert %d\n", (int)client->tmr_idle);
+
qse_htrd_setoption (client->htrd, QSE_HTRD_REQUEST | QSE_HTRD_TRAILERS | QSE_HTRD_CANONQPATH);
/* copy the public fields,
@@ -449,6 +482,18 @@ qse_printf (QSE_T("Debug: CLOSING SOCKET %d\n"), client->handle.i);
httpd->opt.scb.client.close (httpd, client);
+ if (client->tmr_idle != QSE_TMR_INVALID)
+ {
+ qse_tmr_remove (httpd->tmr, client->tmr_idle);
+ client->tmr_idle = QSE_TMR_INVALID;
+ }
+
+ if (client->tmr_dns != QSE_TMR_INVALID)
+ {
+ qse_tmr_remove (httpd->tmr, client->tmr_dns);
+ client->tmr_dns = QSE_TMR_INVALID;
+ }
+
qse_httpd_freemem (httpd, client);
}
@@ -579,6 +624,51 @@ printf ("MUX ADDHND CLIENT READ %d\n", client->handle.i);
return 0;
}
+
+static void tmr_idle_updated (qse_tmr_t* tmr, qse_size_t old_index, qse_size_t new_index, void* ctx)
+{
+printf ("tmr_idle updated %d %d\n", (int)old_index, (int)new_index);
+ qse_httpd_client_t* client = (qse_httpd_client_t*)ctx;
+ QSE_ASSERT (client->tmr_idle == old_index);
+ client->tmr_idle = new_index;
+}
+
+static void check_if_client_is_idle (qse_tmr_t* tmr, const qse_ntime_t* now, void* ctx)
+{
+ qse_httpd_client_t* client = (qse_httpd_client_t*)ctx;
+
+printf ("check if client is idle...\n");
+ if (qse_cmptime(now, &client->last_active) >= 0)
+ {
+ qse_ntime_t diff;
+ qse_subtime (now, &client->last_active, &diff);
+ if (qse_cmptime(&diff, &client->server->httpd->opt.idle_limit) >= 0)
+ {
+ /* this client is idle */
+printf ("client is idle purging....\n");
+ purge_client (client->server->httpd, client);
+ }
+ else
+ {
+ qse_tmr_event_t idle_event;
+
+ /*qse_gettime (&idle_event.when);*/
+ idle_event.when = *now;
+ qse_addtime (&idle_event.when, &client->server->httpd->opt.idle_limit, &idle_event.when);
+ idle_event.ctx = client;
+ idle_event.handler = check_if_client_is_idle;
+ idle_event.updater = tmr_idle_updated;
+
+ /* the timer must have been deleted when this callback is called. */
+ QSE_ASSERT (client->tmr_idle == QSE_TMR_INVALID);
+ client->tmr_idle = qse_tmr_insert (tmr, &idle_event);
+ if (client->tmr_idle == QSE_TMR_INVALID) mark_bad_client (client);
+
+printf ("client is not idle rescheduling.....%d\n", (int)client->tmr_idle);
+ }
+ }
+}
+
/* ----------------------------------------------------------------------- */
static void deactivate_servers (qse_httpd_t* httpd)
@@ -603,6 +693,8 @@ static int activate_servers (qse_httpd_t* httpd)
for (server = httpd->server.list.head; server; server = server->next)
{
+ QSE_ASSERT (server->httpd == httpd);
+
if (httpd->opt.scb.server.open (httpd, server) <= -1)
{
qse_char_t buf[64];
@@ -672,6 +764,7 @@ qse_httpd_server_t* qse_httpd_attachserver (
/* and correct some fields in case the dope contains invalid stuffs */
server->dope.flags &= ~QSE_HTTPD_SERVER_ACTIVE;
+ server->httpd = httpd;
/* chain the server to the tail of the list */
server->prev = httpd->server.list.tail;
server->next = QSE_NULL;
@@ -1284,9 +1377,7 @@ static int perform_client_task (
oops:
/*purge_client (httpd, client);*/
- client->status |= CLIENT_BAD;
- client->bad_next = httpd->client.bad;
- httpd->client.bad = client;
+ mark_bad_client (client);
return -1;
}
@@ -1312,31 +1403,6 @@ static void purge_bad_clients (qse_httpd_t* httpd)
}
}
-static void purge_idle_clients (qse_httpd_t* httpd)
-{
- qse_httpd_client_t* client;
- qse_httpd_client_t* next_client;
- qse_ntime_t now;
-
- qse_gettime (&now);
-
- client = httpd->client.list.head;
- while (client)
- {
- next_client = client->next;
-
- /* TODO: check the nsec part */
- if (now.sec <= client->last_active.sec) break;
- if (now.sec - client->last_active.sec < httpd->opt.idle_limit.sec) break;
-
- purge_client (httpd, client);
- client = next_client;
- }
-
-/* TODO: */
-}
-
-
void* qse_httpd_gettaskxtn (qse_httpd_t* httpd, qse_httpd_task_t* task)
{
return (void*)((qse_httpd_real_task_t*)task + 1);
@@ -1355,7 +1421,7 @@ qse_httpd_task_t* qse_httpd_entask (
if (new_task == QSE_NULL)
{
/*purge_client (httpd, client);*/
- client->status |= CLIENT_BAD;
+ mark_bad_client (client);
}
else if (new_task->prev == QSE_NULL)
{
@@ -1364,7 +1430,7 @@ qse_httpd_task_t* qse_httpd_entask (
if (update_mux_for_next_task (httpd, client) <= -1)
{
/*purge_client (httpd, client);*/
- client->status |= CLIENT_BAD;
+ mark_bad_client (client);
/* call dequeue_task() to get new_task removed. this works
* because it is the only task. the task finalizer is also
@@ -1399,7 +1465,8 @@ static int dispatch_mux (
int qse_httpd_loop (qse_httpd_t* httpd)
{
- int xret;
+ int xret, count;
+ qse_ntime_t tmout;
QSE_ASSERTX (httpd->server.list.head != QSE_NULL,
"Add listeners before calling qse_httpd_loop()");
@@ -1458,9 +1525,8 @@ int qse_httpd_loop (qse_httpd_t* httpd)
while (!httpd->stopreq)
{
- int count;
-
- count = httpd->opt.scb.mux.poll (httpd, httpd->mux, &httpd->opt.tmout);
+ if (qse_tmr_gettmout (httpd->tmr, QSE_NULL, &tmout) <= -1) tmout = httpd->opt.tmout;
+ count = httpd->opt.scb.mux.poll (httpd, httpd->mux, &tmout);
if (count <= -1)
{
if (httpd->errnum != QSE_HTTPD_EINTR)
@@ -1470,10 +1536,8 @@ int qse_httpd_loop (qse_httpd_t* httpd)
}
}
-/* TODO: add timer and execute scheduled events here. */
-
+ qse_tmr_fire (httpd->tmr, QSE_NULL);
purge_bad_clients (httpd);
- purge_idle_clients (httpd);
if (httpd->impedereq)
{
@@ -1488,6 +1552,7 @@ int qse_httpd_loop (qse_httpd_t* httpd)
if (httpd->dnsactive) deactivate_dns (httpd);
httpd->opt.scb.mux.close (httpd, httpd->mux);
+ qse_tmr_clear (httpd->tmr);
return xret;
}
diff --git a/qse/lib/http/httpd.h b/qse/lib/http/httpd.h
index 73a3c833..b7861b83 100644
--- a/qse/lib/http/httpd.h
+++ b/qse/lib/http/httpd.h
@@ -33,6 +33,7 @@ struct qse_httpd_t
qse_mmgr_t* mmgr;
qse_httpd_errnum_t errnum;
qse_httpd_ecb_t* ecb; /* event callbacks */
+ qse_tmr_t* tmr;
struct
{