mirror of
https://github.com/lxsang/ant-http
synced 2025-02-24 00:52:48 +01:00
use async for server
This commit is contained in:
parent
60a2298e62
commit
3447b07fc6
@ -162,6 +162,11 @@ end:
|
|||||||
antd_close(client);
|
antd_close(client);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void* finish_request(void* data)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
int rule_check(const char*k, const char* v, const char* host, const char* _url, const char* _query, char* buf)
|
int rule_check(const char*k, const char* v, const char* host, const char* _url, const char* _query, char* buf)
|
||||||
{
|
{
|
||||||
// first perfom rule check on host, if not success, perform on url
|
// first perfom rule check on host, if not success, perform on url
|
||||||
|
@ -24,6 +24,7 @@
|
|||||||
extern config_t server_config;
|
extern config_t server_config;
|
||||||
|
|
||||||
void accept_request(void*);
|
void accept_request(void*);
|
||||||
|
void* finish_request(void*);
|
||||||
void cat(void*, FILE *);
|
void cat(void*, FILE *);
|
||||||
void cannot_execute(void*);
|
void cannot_execute(void*);
|
||||||
void error_die(const char *);
|
void error_die(const char *);
|
||||||
|
27
httpd.c
27
httpd.c
@ -2,6 +2,9 @@
|
|||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include "http_server.h"
|
#include "http_server.h"
|
||||||
#include "libs/ini.h"
|
#include "libs/ini.h"
|
||||||
|
#include "libs/scheduler.h"
|
||||||
|
#include <fcntl.h>
|
||||||
|
static antd_scheduler_t scheduler;
|
||||||
|
|
||||||
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
|
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
|
||||||
int server_sock = -1;
|
int server_sock = -1;
|
||||||
@ -175,10 +178,12 @@ void load_config(const char* file)
|
|||||||
init_file_system();
|
init_file_system();
|
||||||
}
|
}
|
||||||
void stop_serve(int dummy) {
|
void stop_serve(int dummy) {
|
||||||
|
UNUSED(dummy);
|
||||||
list_free(&(server_config.rules));
|
list_free(&(server_config.rules));
|
||||||
freedict(server_config.handlers);
|
freedict(server_config.handlers);
|
||||||
LOG("Unclosed connection: %d\n", server_config.connection);
|
LOG("Unclosed connection: %d\n", server_config.connection);
|
||||||
unload_all_plugin();
|
unload_all_plugin();
|
||||||
|
antd_scheduler_destroy(&scheduler);
|
||||||
#ifdef USE_OPENSSL
|
#ifdef USE_OPENSSL
|
||||||
SSL_CTX_free(ctx);
|
SSL_CTX_free(ctx);
|
||||||
#endif
|
#endif
|
||||||
@ -216,21 +221,19 @@ int main(int argc, char* argv[])
|
|||||||
|
|
||||||
server_sock = startup(&port);
|
server_sock = startup(&port);
|
||||||
LOG("httpd running on port %d\n", port);
|
LOG("httpd running on port %d\n", port);
|
||||||
|
// default to 4 workers
|
||||||
while (1)
|
antd_scheduler_init(&scheduler, 4);
|
||||||
|
fcntl(server_sock, F_SETFL, O_NONBLOCK);
|
||||||
|
while (scheduler.status)
|
||||||
{
|
{
|
||||||
if( server_config.connection >= server_config.maxcon )
|
antd_task_schedule(&scheduler);
|
||||||
{
|
|
||||||
LOG("Too many unclosed connection (%d). Wait for it\n", server_config.connection);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
antd_client_t* client = (antd_client_t*)malloc(sizeof(antd_client_t));
|
|
||||||
client_sock = accept(server_sock,(struct sockaddr *)&client_name,&client_name_len);
|
client_sock = accept(server_sock,(struct sockaddr *)&client_name,&client_name_len);
|
||||||
if (client_sock == -1)
|
if (client_sock == -1)
|
||||||
{
|
{
|
||||||
perror("Cannot accept client request\n");
|
//perror("Cannot accept client request\n");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
antd_client_t* client = (antd_client_t*)malloc(sizeof(antd_client_t));
|
||||||
/*
|
/*
|
||||||
get the remote IP
|
get the remote IP
|
||||||
*/
|
*/
|
||||||
@ -273,7 +276,9 @@ int main(int argc, char* argv[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
if (pthread_create(&newthread , NULL,(void *(*)(void *))accept_request, (void *)client) != 0)
|
// create callback for the server
|
||||||
|
antd_add_task(&scheduler, antd_create_task(accept_request,(void*)client, finish_request ));
|
||||||
|
/*if (pthread_create(&newthread , NULL,(void *(*)(void *))accept_request, (void *)client) != 0)
|
||||||
{
|
{
|
||||||
perror("pthread_create");
|
perror("pthread_create");
|
||||||
antd_close(client);
|
antd_close(client);
|
||||||
@ -282,7 +287,7 @@ int main(int argc, char* argv[])
|
|||||||
{
|
{
|
||||||
//reclaim the stack data when thread finish
|
//reclaim the stack data when thread finish
|
||||||
pthread_detach(newthread) ;
|
pthread_detach(newthread) ;
|
||||||
}
|
}*/
|
||||||
//accept_request(&client);
|
//accept_request(&client);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,6 +54,11 @@ typedef struct{
|
|||||||
char* ip;
|
char* ip;
|
||||||
} antd_client_t;
|
} antd_client_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
antd_client_t* client;
|
||||||
|
dictionary request;
|
||||||
|
} antd_request_t;
|
||||||
|
|
||||||
int response(void*, const char*);
|
int response(void*, const char*);
|
||||||
void ctype(void*,const char*);
|
void ctype(void*,const char*);
|
||||||
void redirect(void*,const char*);
|
void redirect(void*,const char*);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user