mirror of
https://github.com/lxsang/ant-http
synced 2024-12-27 09:18:22 +01:00
Use blocking select to monitor ports, no need to use a thread for each port
This commit is contained in:
parent
9da33124bc
commit
708e492c49
@ -1,5 +1,5 @@
|
|||||||
# initialise autoconf and set up some basic information about the program we’re packaging
|
# initialise autoconf and set up some basic information about the program we’re packaging
|
||||||
AC_INIT([antd], [1.0.5b], [xsang.le@gmail.com])
|
AC_INIT([antd], [1.0.6b], [xsang.le@gmail.com])
|
||||||
|
|
||||||
# We’re going to use automake for this project
|
# We’re going to use automake for this project
|
||||||
AM_INIT_AUTOMAKE([subdir-objects])
|
AM_INIT_AUTOMAKE([subdir-objects])
|
||||||
|
BIN
dist/antd-1.0.6b.tar.gz
vendored
Normal file
BIN
dist/antd-1.0.6b.tar.gz
vendored
Normal file
Binary file not shown.
@ -104,6 +104,8 @@ static int config_handler(void *conf, const char *section, const char *name,
|
|||||||
//char * ppath = NULL;
|
//char * ppath = NULL;
|
||||||
if (MATCH("SERVER", "plugins"))
|
if (MATCH("SERVER", "plugins"))
|
||||||
{
|
{
|
||||||
|
if (pconfig->plugins_dir)
|
||||||
|
free(pconfig->plugins_dir);
|
||||||
pconfig->plugins_dir = strdup(value);
|
pconfig->plugins_dir = strdup(value);
|
||||||
if (stat(pconfig->plugins_dir, &st) == -1)
|
if (stat(pconfig->plugins_dir, &st) == -1)
|
||||||
mkdirp(pconfig->plugins_dir, 0755);
|
mkdirp(pconfig->plugins_dir, 0755);
|
||||||
|
89
httpd.c
89
httpd.c
@ -8,6 +8,7 @@
|
|||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
#include <errno.h>
|
||||||
#include "http_server.h"
|
#include "http_server.h"
|
||||||
#include "lib/ini.h"
|
#include "lib/ini.h"
|
||||||
#include "lib/scheduler.h"
|
#include "lib/scheduler.h"
|
||||||
@ -163,26 +164,14 @@ static void stop_serve(int dummy)
|
|||||||
sigprocmask(SIG_UNBLOCK, &mask, NULL);
|
sigprocmask(SIG_UNBLOCK, &mask, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void *antd_monitor(port_config_t *pcnf)
|
static void antd_monitor(port_config_t *pcnf)
|
||||||
{
|
{
|
||||||
antd_task_t *task = NULL;
|
antd_task_t *task = NULL;
|
||||||
struct timeval timeout;
|
|
||||||
int client_sock = -1;
|
int client_sock = -1;
|
||||||
struct sockaddr_in client_name;
|
struct sockaddr_in client_name;
|
||||||
socklen_t client_name_len = sizeof(client_name);
|
socklen_t client_name_len = sizeof(client_name);
|
||||||
char *client_ip = NULL;
|
char *client_ip = NULL;
|
||||||
config_t *conf = config();
|
config_t *conf = config();
|
||||||
LOG("Listening on port %d", pcnf->port);
|
|
||||||
while (scheduler.status)
|
|
||||||
{
|
|
||||||
if (conf->connection > conf->maxcon)
|
|
||||||
{
|
|
||||||
//ERROR("Reach max connection %d", conf->connection);
|
|
||||||
timeout.tv_sec = 0;
|
|
||||||
timeout.tv_usec = 10000; // 5 ms
|
|
||||||
select(0, NULL, NULL, NULL, &timeout);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (pcnf->sock > 0)
|
if (pcnf->sock > 0)
|
||||||
{
|
{
|
||||||
client_sock = accept(pcnf->sock, (struct sockaddr *)&client_name, &client_name_len);
|
client_sock = accept(pcnf->sock, (struct sockaddr *)&client_name, &client_name_len);
|
||||||
@ -226,7 +215,10 @@ static void *antd_monitor(port_config_t *pcnf)
|
|||||||
{
|
{
|
||||||
client->ssl = (void *)SSL_new(ctx);
|
client->ssl = (void *)SSL_new(ctx);
|
||||||
if (!client->ssl)
|
if (!client->ssl)
|
||||||
continue;
|
{
|
||||||
|
finish_request(request);
|
||||||
|
return;
|
||||||
|
}
|
||||||
SSL_set_fd((SSL *)client->ssl, client->sock);
|
SSL_set_fd((SSL *)client->ssl, client->sock);
|
||||||
// this can be used in the protocol select callback to
|
// this can be used in the protocol select callback to
|
||||||
// set the protocol selected by the server
|
// set the protocol selected by the server
|
||||||
@ -251,8 +243,6 @@ static void *antd_monitor(port_config_t *pcnf)
|
|||||||
antd_add_task(&scheduler, task);
|
antd_add_task(&scheduler, task);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void client_statistic(int fd, void *user_data)
|
static void client_statistic(int fd, void *user_data)
|
||||||
@ -305,10 +295,13 @@ static void client_statistic(int fd, void *user_data)
|
|||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
pthread_t monitor_th;
|
pthread_t sched_th;
|
||||||
// startup port
|
// startup port
|
||||||
chain_t it;
|
chain_t it;
|
||||||
|
struct timeval timeout;
|
||||||
port_config_t *pcnf;
|
port_config_t *pcnf;
|
||||||
|
fd_set master_set, working_set;
|
||||||
|
int status, maxfd = 0;
|
||||||
int nlisten = 0;
|
int nlisten = 0;
|
||||||
// load the config first
|
// load the config first
|
||||||
if (argc == 1)
|
if (argc == 1)
|
||||||
@ -348,6 +341,7 @@ int main(int argc, char *argv[])
|
|||||||
stop_serve(0);
|
stop_serve(0);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
FD_ZERO(&master_set);
|
||||||
for_each_assoc(it, conf->ports)
|
for_each_assoc(it, conf->ports)
|
||||||
{
|
{
|
||||||
pcnf = (port_config_t *)it->value;
|
pcnf = (port_config_t *)it->value;
|
||||||
@ -356,17 +350,10 @@ int main(int argc, char *argv[])
|
|||||||
pcnf->sock = startup(&pcnf->port);
|
pcnf->sock = startup(&pcnf->port);
|
||||||
if (pcnf->sock > 0)
|
if (pcnf->sock > 0)
|
||||||
{
|
{
|
||||||
if (pthread_create(&monitor_th, NULL, (void *(*)(void *))antd_monitor, (void *)pcnf) != 0)
|
set_nonblock(pcnf->sock);
|
||||||
{
|
FD_SET(pcnf->sock, &master_set);
|
||||||
ERROR("pthread_create: cannot create worker");
|
LOG("Listening on port %d", pcnf->port);
|
||||||
stop_serve(0);
|
maxfd = pcnf->sock > maxfd ? pcnf->sock : maxfd;
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// reclaim data when exit
|
|
||||||
pthread_detach(monitor_th);
|
|
||||||
}
|
|
||||||
nlisten++;
|
nlisten++;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -381,7 +368,51 @@ int main(int argc, char *argv[])
|
|||||||
stop_serve(0);
|
stop_serve(0);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
antd_wait(&scheduler);
|
// Start scheduler
|
||||||
|
if (pthread_create(&sched_th, NULL, (void *(*)(void *))antd_wait, (void *)&scheduler) != 0)
|
||||||
|
{
|
||||||
|
ERROR("pthread_create: cannot start scheduler thread");
|
||||||
|
stop_serve(0);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// reclaim data when exit
|
||||||
|
pthread_detach(sched_th);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (scheduler.status)
|
||||||
|
{
|
||||||
|
if (conf->connection > conf->maxcon)
|
||||||
|
{
|
||||||
|
//ERROR("Reach max connection %d", conf->connection);
|
||||||
|
timeout.tv_sec = 0;
|
||||||
|
timeout.tv_usec = 10000; // 5 ms
|
||||||
|
select(0, NULL, NULL, NULL, &timeout);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
FD_ZERO(&working_set);
|
||||||
|
memcpy(&working_set, &master_set, sizeof(master_set));
|
||||||
|
// blocking select
|
||||||
|
status = select(maxfd + 1, &working_set, NULL, NULL, NULL);
|
||||||
|
if (status < 0)
|
||||||
|
{
|
||||||
|
ERROR("select() error: %s", strerror(errno));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (status == 0)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for_each_assoc(it, conf->ports)
|
||||||
|
{
|
||||||
|
pcnf = (port_config_t *)it->value;
|
||||||
|
if (pcnf && pcnf->sock > 0 && FD_ISSET(pcnf->sock, &working_set))
|
||||||
|
{
|
||||||
|
antd_monitor(pcnf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
stop_serve(0);
|
stop_serve(0);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user