renamed existing qse_httpd_inserttimerevent() and qse_httpd_removetimerevent() to qse_httpd_insert_timer_event() and qse_httpd_remove_timer_event().

the renamed functions should be used internally only.
added new qse_httpd_inserttimerevent() and qse_httpd_removetimerevent() that can be used by external callers.
added qse_httpd_timer_event_t and other required definitions for the new functions
This commit is contained in:
hyung-hwan 2014-11-01 15:27:56 +00:00
parent baecb98181
commit 569b30039b
7 changed files with 180 additions and 45 deletions

View File

@ -30,14 +30,14 @@ typedef qse_size_t qse_tmr_index_t;
typedef void (*qse_tmr_handler_t) (
qse_tmr_t* tmr,
const qse_ntime_t* now,
void* ctx
qse_tmr_event_t* evt
);
typedef void (*qse_tmr_updater_t) (
qse_tmr_t* tmr,
qse_tmr_index_t old_index,
qse_tmr_index_t new_index,
void* ctx
qse_tmr_event_t* evt
);
struct qse_tmr_t
@ -50,8 +50,11 @@ struct qse_tmr_t
struct qse_tmr_event_t
{
void* ctx; /* primary context pointer */
void* ctx2; /* secondary context pointer */
void* ctx3; /* tertiary context pointer */
qse_ntime_t when;
void* ctx;
qse_tmr_handler_t handler;
qse_tmr_updater_t updater;
};
@ -130,6 +133,15 @@ QSE_EXPORT int qse_tmr_gettmout (
qse_ntime_t* tmout
);
/**
* The qse_tmr_getevent() function returns the
* pointer to the registered event at the given index.
*/
QSE_EXPORT qse_tmr_event_t* qse_tmr_getevent (
qse_tmr_t* tmr,
qse_tmr_index_t index
);
#ifdef __cplusplus
}
#endif

View File

@ -528,6 +528,33 @@ struct qse_httpd_rcb_t
qse_httpd_logact_t logact;
};
/* -------------------------------------------------------------------------- */
typedef qse_tmr_index_t qse_httpd_timer_index_t;
typedef void (*qse_httpd_timer_handler_t) (
qse_httpd_t* httpd,
const qse_ntime_t* now,
void* ctx
);
typedef void (*qse_httpd_timer_updater_t) (
qse_httpd_t* httpd,
qse_httpd_timer_index_t old_index,
qse_httpd_timer_index_t new_index,
void* ctx
);
typedef struct qse_httpd_timer_event_t qse_httpd_timer_event_t;
struct qse_httpd_timer_event_t
{
void* ctx; /* primary context pointer */
qse_ntime_t when;
qse_httpd_timer_handler_t handler;
qse_httpd_timer_updater_t updater;
};
/* -------------------------------------------------------------------------- */
typedef struct qse_httpd_task_t qse_httpd_task_t;
@ -1364,15 +1391,17 @@ QSE_EXPORT qse_httpd_mod_t* qse_httpd_findmod (
QSE_EXPORT int qse_httpd_inserttimerevent (
qse_httpd_t* httpd,
const qse_tmr_event_t* event,
qse_tmr_index_t* index
const qse_httpd_timer_event_t* event,
qse_httpd_timer_index_t* index
);
QSE_EXPORT void qse_httpd_removetimerevent (
qse_httpd_t* httpd,
qse_tmr_index_t index
qse_httpd_timer_index_t index
);
#ifdef __cplusplus
}
#endif

View File

@ -106,7 +106,7 @@ static qse_tmr_index_t sift_up (qse_tmr_t* tmr, qse_tmr_index_t index, int notif
{
/* 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);
tmr->event[index].updater (tmr, parent, index, &tmr->event[index]);
/* traverse up */
index = parent;
@ -119,7 +119,7 @@ static qse_tmr_index_t sift_up (qse_tmr_t* tmr, qse_tmr_index_t index, int notif
* reply on the return value. */
tmr->event[index] = item;
if (notify && index != old_index)
tmr->event[index].updater (tmr, old_index, index, tmr->event[index].ctx);
tmr->event[index].updater (tmr, old_index, index, &tmr->event[index]);
}
return index;
@ -153,7 +153,7 @@ static qse_tmr_index_t sift_down (qse_tmr_t* tmr, qse_tmr_index_t index, int not
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);
tmr->event[index].updater (tmr, younger, index, &tmr->event[index]);
index = younger;
}
@ -161,7 +161,7 @@ static qse_tmr_index_t sift_down (qse_tmr_t* tmr, qse_tmr_index_t index, int not
tmr->event[index] = item;
if (notify && index != old_index)
tmr->event[index].updater (tmr, old_index, index, tmr->event[index].ctx);
tmr->event[index].updater (tmr, old_index, index, &tmr->event[index]);
}
return index;
@ -174,13 +174,13 @@ void qse_tmr_remove (qse_tmr_t* tmr, qse_tmr_index_t index)
QSE_ASSERT (index < tmr->size);
item = tmr->event[index];
tmr->event[index].updater (tmr, index, QSE_TMR_INVALID_INDEX, tmr->event[index].ctx);
tmr->event[index].updater (tmr, index, QSE_TMR_INVALID_INDEX, &tmr->event[index]);
tmr->size = tmr->size - 1;
if (tmr->size > 0 && index != tmr->size)
{
tmr->event[index] = tmr->event[tmr->size];
tmr->event[index].updater (tmr, tmr->size, index, tmr->event[index].ctx);
tmr->event[index].updater (tmr, tmr->size, index, &tmr->event[index]);
YOUNGER_THAN(&tmr->event[index], &item)? sift_up(tmr, index, 1): sift_down(tmr, index, 1);
}
}
@ -231,10 +231,10 @@ qse_size_t qse_tmr_fire (qse_tmr_t* tmr, const qse_ntime_t* tm)
if (qse_cmptime(&tmr->event[0].when, &now) > 0) break;
event = tmr->event[0];
qse_tmr_remove (tmr, 0);
qse_tmr_remove (tmr, 0); /* remove the registered event structure */
fire_count++;
event.handler (tmr, &now, event.ctx);
event.handler (tmr, &now, &event); /* then fire the event */
}
return fire_count;
@ -257,3 +257,7 @@ int qse_tmr_gettmout (qse_tmr_t* tmr, const qse_ntime_t* tm, qse_ntime_t* tmout)
return 0;
}
qse_tmr_event_t* qse_tmr_getevent (qse_tmr_t* tmr, qse_tmr_index_t index)
{
return (index < 0 || index >= tmr->size)? QSE_NULL: &tmr->event[index];
}

View File

@ -462,7 +462,7 @@ static void dns_remove_tmr_tmout (qse_httpd_t* httpd, dns_req_t* req)
{
if (req->tmr_tmout != QSE_TMR_INVALID_INDEX)
{
qse_httpd_removetimerevent (httpd, req->tmr_tmout);
qse_httpd_remove_timer_event (httpd, req->tmr_tmout);
req->tmr_tmout = QSE_TMR_INVALID_INDEX;
}
}
@ -789,22 +789,23 @@ resolved:
return 0;
}
static void tmr_dns_tmout_update (qse_tmr_t* tmr, qse_tmr_index_t old_index, qse_tmr_index_t new_index, void* ctx)
static void tmr_dns_tmout_update (qse_tmr_t* tmr, qse_tmr_index_t old_index, qse_tmr_index_t new_index, qse_tmr_event_t* evt)
{
dns_req_t* req = (dns_req_t*)ctx;
dns_req_t* req = (dns_req_t*)evt->ctx;
QSE_ASSERT (req->tmr_tmout == old_index);
req->tmr_tmout = new_index;
}
static void tmr_dns_tmout_handle (qse_tmr_t* tmr, const qse_ntime_t* now, void* ctx)
static void tmr_dns_tmout_handle (qse_tmr_t* tmr, const qse_ntime_t* now, qse_tmr_event_t* evt)
{
/* destory the unanswered request if timed out */
dns_req_t* req = (dns_req_t*)ctx;
dns_req_t* req = (dns_req_t*)evt->ctx;
dns_ctx_t* dc = req->dc;
qse_uint16_t xid;
printf (">>tmr_dns_tmout_handle req->>%p\n", req);
HTTPD_DBGOUT1 ("DNS timed out [%s]\n", req->name);
/* when this handler is called, the event must be removed from the timer */
QSE_ASSERT (req->tmr_tmout == QSE_TMR_INVALID_INDEX);
@ -818,6 +819,7 @@ printf (">>tmr_dns_tmout_handle req->>%p\n", req);
httpd_xtn = qse_httpd_getxtn (dc->httpd);
QSE_MEMSET (&tmout_event, 0, QSE_SIZEOF(tmout_event));
qse_gettime (&tmout_event.when);
qse_addtime (&tmout_event.when, &req->dns_tmout, &tmout_event.when);
tmout_event.ctx = req;
@ -841,7 +843,7 @@ printf (">>tmr_dns_tmout_handle req->>%p\n", req);
{
send_ok:
QSE_ASSERT (tmr == dc->httpd->tmr);
if (qse_httpd_inserttimerevent (dc->httpd, &tmout_event, &req->tmr_tmout) >= 0)
if (qse_httpd_insert_timer_event (dc->httpd, &tmout_event, &req->tmr_tmout) >= 0)
{
req->dns_retries--;
return; /* resend ok */
@ -987,12 +989,13 @@ static int dns_send (qse_httpd_t* httpd, qse_httpd_dns_t* dns, const qse_mchar_t
goto oops;
}
QSE_MEMSET (&tmout_event, 0, QSE_SIZEOF(tmout_event));
qse_gettime (&tmout_event.when);
qse_addtime (&tmout_event.when, &req->dns_tmout, &tmout_event.when);
tmout_event.ctx = req;
tmout_event.handler = tmr_dns_tmout_handle;
tmout_event.updater = tmr_dns_tmout_update;
if (qse_httpd_inserttimerevent (httpd, &tmout_event, &req->tmr_tmout) <= -1) goto oops;
if (qse_httpd_insert_timer_event (httpd, &tmout_event, &req->tmr_tmout) <= -1) goto oops;
if ((req->qalen > 0 && sendto (req->dns_socket, req->qa, req->qalen, 0, (struct sockaddr*)&req->dns_skad, req->dns_skadlen) != req->qalen) ||
(req->qaaaalen > 0 && sendto (req->dns_socket, req->qaaaa, req->qaaaalen, 0, (struct sockaddr*)&req->dns_skad, req->dns_skadlen) != req->qaaaalen))

View File

@ -233,7 +233,7 @@ static void urs_remove_tmr_tmout (qse_httpd_t* httpd, urs_req_t* req)
{
if (req->tmr_tmout != QSE_TMR_INVALID_INDEX)
{
qse_httpd_removetimerevent (httpd, req->tmr_tmout);
qse_httpd_remove_timer_event (httpd, req->tmr_tmout);
req->tmr_tmout = QSE_TMR_INVALID_INDEX;
}
}
@ -335,19 +335,19 @@ printf ("URS_RECV............................................\n");
return 0;
}
static void tmr_urs_tmout_update (qse_tmr_t* tmr, qse_tmr_index_t old_index, qse_tmr_index_t new_index, void* ctx)
static void tmr_urs_tmout_update (qse_tmr_t* tmr, qse_tmr_index_t old_index, qse_tmr_index_t new_index, qse_tmr_event_t* evt)
{
urs_req_t* req = (urs_req_t*)ctx;
urs_req_t* req = (urs_req_t*)evt->ctx;
QSE_ASSERT (req->tmr_tmout == old_index);
req->tmr_tmout = new_index;
}
static void tmr_urs_tmout_handle (qse_tmr_t* tmr, const qse_ntime_t* now, void* ctx)
static void tmr_urs_tmout_handle (qse_tmr_t* tmr, const qse_ntime_t* now, qse_tmr_event_t* evt)
{
/* destory the unanswered request if timed out */
urs_req_t* req = (urs_req_t*)ctx;
urs_req_t* req = (urs_req_t*)evt->ctx;
urs_ctx_t* dc = req->dc;
qse_uint16_t xid;
@ -364,6 +364,7 @@ static void tmr_urs_tmout_handle (qse_tmr_t* tmr, const qse_ntime_t* now, void*
httpd_xtn = qse_httpd_getxtn (dc->httpd);
QSE_MEMSET (&tmout_event, 0, QSE_SIZEOF(tmout_event));
qse_gettime (&tmout_event.when);
qse_addtime (&tmout_event.when, &req->urs_tmout, &tmout_event.when);
tmout_event.ctx = req;
@ -387,7 +388,7 @@ static void tmr_urs_tmout_handle (qse_tmr_t* tmr, const qse_ntime_t* now, void*
{
send_ok:
QSE_ASSERT (tmr == dc->httpd->tmr);
if (qse_httpd_inserttimerevent (dc->httpd, &tmout_event, &req->tmr_tmout) >= 0)
if (qse_httpd_insert_timer_event (dc->httpd, &tmout_event, &req->tmr_tmout) >= 0)
{
req->urs_retries--;
return; /* resend ok */
@ -507,12 +508,13 @@ static int urs_send (qse_httpd_t* httpd, qse_httpd_urs_t* urs, const qse_mchar_t
}
}
QSE_MEMSET (&tmout_event, 0, QSE_SIZEOF(tmout_event));
qse_gettime (&tmout_event.when);
qse_addtime (&tmout_event.when, &req->urs_tmout, &tmout_event.when);
tmout_event.ctx = req;
tmout_event.handler = tmr_urs_tmout_handle;
tmout_event.updater = tmr_urs_tmout_update;
if (qse_httpd_inserttimerevent (httpd, &tmout_event, &req->tmr_tmout) <= -1) goto oops;
if (qse_httpd_insert_timer_event (httpd, &tmout_event, &req->tmr_tmout) <= -1) goto oops;
/*
{

View File

@ -51,6 +51,11 @@ struct htrd_xtn_t
qse_httpd_client_t* client;
};
struct tmr_xtn_t
{
qse_httpd_t* httpd;
};
static void free_server_list (qse_httpd_t* httpd);
static int perform_client_task (
qse_httpd_t* httpd, void* mux, qse_httpd_hnd_t handle, int mask, void* cbarg);
@ -84,12 +89,18 @@ void qse_httpd_close (qse_httpd_t* httpd)
int qse_httpd_init (qse_httpd_t* httpd, qse_mmgr_t* mmgr)
{
tmr_xtn_t* tmr_xtn;
QSE_MEMSET (httpd, 0, QSE_SIZEOF(*httpd));
httpd->mmgr = mmgr;
httpd->tmr = qse_tmr_open (mmgr, 0, 2048);
httpd->tmr = qse_tmr_open (mmgr, QSE_SIZEOF(tmr_xtn_t), 2048);
if (httpd->tmr == QSE_NULL) return -1;
tmr_xtn = qse_tmr_getxtn (httpd->tmr);
QSE_MEMSET (tmr_xtn, 0, QSE_SIZEOF(*tmr_xtn));
tmr_xtn->httpd = httpd;
qse_mbscpy (httpd->sname, QSE_MT("QSE-HTTPD " QSE_PACKAGE_VERSION));
httpd->opt.tmout.sec = 3;
@ -444,8 +455,8 @@ static qse_htrd_recbs_t htrd_recbs =
};
/* ----------------------------------------------------------------------- */
static void tmr_idle_update (qse_tmr_t* tmr, qse_tmr_index_t old_index, qse_tmr_index_t new_index, void* ctx);
static void tmr_idle_handle (qse_tmr_t* tmr, const qse_ntime_t* now, void* ctx);
static void tmr_idle_update (qse_tmr_t* tmr, qse_tmr_index_t old_index, qse_tmr_index_t new_index, qse_tmr_event_t* evt);
static void tmr_idle_handle (qse_tmr_t* tmr, const qse_ntime_t* now, qse_tmr_event_t* evt);
static void mark_bad_client (qse_httpd_client_t* client)
{
@ -482,13 +493,14 @@ static qse_httpd_client_t* new_client (qse_httpd_t* httpd, qse_httpd_client_t* t
(httpd->opt.idle_limit.sec == 0 && httpd->opt.idle_limit.nsec > 0))
{
/* idle limit is enabled when the limit is greater than 0.0 */
QSE_MEMSET (&idle_event, 0, QSE_SIZEOF(idle_event));
qse_gettime (&idle_event.when);
qse_addtime (&idle_event.when, &httpd->opt.idle_limit, &idle_event.when);
idle_event.ctx = client;
idle_event.handler = tmr_idle_handle;
idle_event.updater = tmr_idle_update;
if (qse_httpd_inserttimerevent (httpd, &idle_event, &client->tmr_idle) <= -1) goto oops;
if (qse_httpd_insert_timer_event (httpd, &idle_event, &client->tmr_idle) <= -1) goto oops;
}
qse_htrd_setoption (client->htrd, QSE_HTRD_REQUEST | QSE_HTRD_TRAILERS | QSE_HTRD_CANONQPATH);
@ -519,7 +531,7 @@ oops:
{
if (client->tmr_idle != QSE_TMR_INVALID_INDEX)
{
qse_httpd_removetimerevent (httpd, client->tmr_idle);
qse_httpd_remove_timer_event (httpd, client->tmr_idle);
client->tmr_idle = QSE_TMR_INVALID_INDEX;
}
if (client->htrd) qse_htrd_close (client->htrd);
@ -554,7 +566,7 @@ static void free_client (
if (client->tmr_idle != QSE_TMR_INVALID_INDEX)
{
qse_httpd_removetimerevent (httpd, client->tmr_idle);
qse_httpd_remove_timer_event (httpd, client->tmr_idle);
client->tmr_idle = QSE_TMR_INVALID_INDEX;
}
@ -719,16 +731,16 @@ static int accept_client (
return 0;
}
static void tmr_idle_update (qse_tmr_t* tmr, qse_tmr_index_t old_index, qse_tmr_index_t new_index, void* ctx)
static void tmr_idle_update (qse_tmr_t* tmr, qse_tmr_index_t old_index, qse_tmr_index_t new_index, qse_tmr_event_t* evt)
{
qse_httpd_client_t* client = (qse_httpd_client_t*)ctx;
qse_httpd_client_t* client = (qse_httpd_client_t*)evt->ctx;
QSE_ASSERT (client->tmr_idle == old_index);
client->tmr_idle = new_index;
}
static void tmr_idle_handle (qse_tmr_t* tmr, const qse_ntime_t* now, void* ctx)
static void tmr_idle_handle (qse_tmr_t* tmr, const qse_ntime_t* now, qse_tmr_event_t* evt)
{
qse_httpd_client_t* client = (qse_httpd_client_t*)ctx;
qse_httpd_client_t* client = (qse_httpd_client_t*)evt->ctx;
if (qse_cmptime(now, &client->last_active) >= 0)
{
@ -747,6 +759,7 @@ static void tmr_idle_handle (qse_tmr_t* tmr, const qse_ntime_t* now, void* ctx)
QSE_ASSERT (client->server->httpd->tmr == tmr);
/*qse_gettime (&idle_event.when);*/
QSE_MEMSET (&idle_event, 0, QSE_SIZEOF(idle_event));
idle_event.when = *now;
qse_addtime (&idle_event.when, &client->server->httpd->opt.idle_limit, &idle_event.when);
idle_event.ctx = client;
@ -755,7 +768,7 @@ static void tmr_idle_handle (qse_tmr_t* tmr, const qse_ntime_t* now, void* ctx)
/* the timer must have been deleted when this callback is called. */
QSE_ASSERT (client->tmr_idle == QSE_TMR_INVALID_INDEX);
if (qse_httpd_inserttimerevent (client->server->httpd, &idle_event, &client->tmr_idle) <= -1)
if (qse_httpd_insert_timer_event (client->server->httpd, &idle_event, &client->tmr_idle) <= -1)
{
HTTPD_DBGOUT1 ("Cannot update idle timer for client %d. Marking it bad", (int)client->handle);
mark_bad_client (client);
@ -1980,7 +1993,68 @@ int qse_httpd_inactivatetasktrigger (qse_httpd_t* httpd, qse_httpd_client_t* cli
return update_mux_for_current_task (httpd, client, task);
}
int qse_httpd_inserttimerevent (qse_httpd_t* httpd, const qse_tmr_event_t* event, qse_tmr_index_t* index)
/* ------------------------------------------------------------------- */
static void update_timer_event (qse_tmr_t* tmr, qse_tmr_index_t old_index, qse_tmr_index_t new_index, qse_tmr_event_t* evt)
{
tmr_xtn_t* tmr_xtn;
qse_httpd_t* httpd;
qse_httpd_timer_updater_t updater;
tmr_xtn = qse_tmr_getxtn (tmr);
httpd = tmr_xtn->httpd;
updater = evt->ctx2;
updater (httpd, old_index, new_index, evt->ctx);
}
static void handle_timer_event (qse_tmr_t* tmr, const qse_ntime_t* now, qse_tmr_event_t* evt)
{
tmr_xtn_t* tmr_xtn;
qse_httpd_t* httpd;
qse_httpd_timer_handler_t handler;
tmr_xtn = qse_tmr_getxtn (tmr);
httpd = tmr_xtn->httpd;
handler = evt->ctx3;
handler (httpd, now, evt->ctx);
}
int qse_httpd_inserttimerevent (qse_httpd_t* httpd, const qse_httpd_timer_event_t* event, qse_httpd_timer_index_t* index)
{
qse_tmr_event_t timer_event;
qse_tmr_index_t timer_index;
QSE_MEMSET (&timer_event, 0, QSE_SIZEOF(timer_event));
timer_event.ctx = event->ctx;
timer_event.ctx2 = event->updater;
timer_event.ctx3 = event->handler;
timer_event.when = event->when;
timer_event.updater = update_timer_event;
timer_event.handler = handle_timer_event;
timer_index = qse_tmr_insert (httpd->tmr, &timer_event);
if (timer_index == QSE_TMR_INVALID_INDEX)
{
qse_httpd_seterrnum (httpd, QSE_HTTPD_ENOMEM);
return -1;
}
*index = timer_index;
return 0;
}
void qse_httpd_removetimerevent (qse_httpd_t* httpd, qse_httpd_timer_index_t index)
{
qse_tmr_remove (httpd->tmr, index);
}
/* qse_httpd_insert_timer_event() is a lighter-weight version of
* qse_httpd_inserttimerevent() and intended for internal use only */
int qse_httpd_insert_timer_event (qse_httpd_t* httpd, const qse_tmr_event_t* event, qse_tmr_index_t* index)
{
qse_tmr_index_t tmp = qse_tmr_insert (httpd->tmr, event);
if (tmp == QSE_TMR_INVALID_INDEX)
@ -1992,12 +2066,12 @@ int qse_httpd_inserttimerevent (qse_httpd_t* httpd, const qse_tmr_event_t* event
*index = tmp;
return 0;
}
void qse_httpd_removetimerevent (qse_httpd_t* httpd, qse_tmr_index_t index)
void qse_httpd_remove_timer_event (qse_httpd_t* httpd, qse_tmr_index_t index)
{
qse_tmr_remove (httpd->tmr, index);
}
/* ----------------------------------------------------------------------- */
static void unload_all_modules (qse_httpd_t* httpd)
{

View File

@ -177,6 +177,17 @@ int qse_httpd_inactivatetasktrigger (
qse_httpd_task_t* task
);
int qse_httpd_insert_timer_event (
qse_httpd_t* httpd,
const qse_tmr_event_t* event,
qse_tmr_index_t* index
);
void qse_httpd_remove_timer_event (
qse_httpd_t* httpd,
qse_tmr_index_t index
);
#ifdef __cplusplus
}
#endif