提交 f7f9a43a authored 作者: Anthony Minessale's avatar Anthony Minessale

use threading in testserver and ivrd

上级 e5660577
...@@ -42,11 +42,6 @@ static void mycallback(esl_socket_t server_sock, esl_socket_t client_sock, struc ...@@ -42,11 +42,6 @@ static void mycallback(esl_socket_t server_sock, esl_socket_t client_sock, struc
char path_buffer[1024] = { 0 }; char path_buffer[1024] = { 0 };
const char *path; const char *path;
if (fork()) {
close(client_sock);
return;
}
if (esl_attach_handle(&handle, client_sock, addr) != ESL_SUCCESS || !handle.info_event) { if (esl_attach_handle(&handle, client_sock, addr) != ESL_SUCCESS || !handle.info_event) {
esl_log(ESL_LOG_ERROR, "Socket Error\n"); esl_log(ESL_LOG_ERROR, "Socket Error\n");
exit(0); exit(0);
...@@ -95,9 +90,7 @@ int main(int argc, char *argv[]) ...@@ -95,9 +90,7 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
signal(SIGCHLD, SIG_IGN); esl_listen(ip, port, mycallback, 100000);
esl_listen(ip, port, mycallback);
return 0; return 0;
} }
...@@ -612,12 +612,31 @@ static int esl_socket_reuseaddr(esl_socket_t socket) ...@@ -612,12 +612,31 @@ static int esl_socket_reuseaddr(esl_socket_t socket)
#endif #endif
} }
ESL_DECLARE(esl_status_t) esl_listen(const char *host, esl_port_t port, esl_listen_callback_t callback) struct thread_handler {
esl_listen_callback_t callback;
int server_sock;
int client_sock;
struct sockaddr_in addr;
};
static void *client_thread(esl_thread_t *me, void *obj)
{
struct thread_handler *handler = (struct thread_handler *) obj;
handler->callback(handler->server_sock, handler->client_sock, &handler->addr);
free(handler);
return NULL;
}
ESL_DECLARE(esl_status_t) esl_listen(const char *host, esl_port_t port, esl_listen_callback_t callback, int max)
{ {
esl_socket_t server_sock = ESL_SOCK_INVALID; esl_socket_t server_sock = ESL_SOCK_INVALID;
struct sockaddr_in addr; struct sockaddr_in addr;
esl_status_t status = ESL_SUCCESS; esl_status_t status = ESL_SUCCESS;
struct thread_handler *handler = NULL;
if ((server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { if ((server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
return ESL_FAIL; return ESL_FAIL;
} }
...@@ -634,7 +653,7 @@ ESL_DECLARE(esl_status_t) esl_listen(const char *host, esl_port_t port, esl_list ...@@ -634,7 +653,7 @@ ESL_DECLARE(esl_status_t) esl_listen(const char *host, esl_port_t port, esl_list
goto end; goto end;
} }
if (listen(server_sock, 10000) < 0) { if (listen(server_sock, max) < 0) {
status = ESL_FAIL; status = ESL_FAIL;
goto end; goto end;
} }
...@@ -655,7 +674,14 @@ ESL_DECLARE(esl_status_t) esl_listen(const char *host, esl_port_t port, esl_list ...@@ -655,7 +674,14 @@ ESL_DECLARE(esl_status_t) esl_listen(const char *host, esl_port_t port, esl_list
goto end; goto end;
} }
callback(server_sock, client_sock, &echoClntAddr); handler = malloc(sizeof(*handler));
memset(handler, 0, sizeof(*handler));
handler->callback = callback;
handler->server_sock = server_sock;
handler->client_sock = client_sock;
handler->addr = echoClntAddr;
esl_thread_create_detached(client_thread, handler);
} }
end: end:
......
...@@ -391,7 +391,7 @@ ESL_DECLARE(esl_status_t) esl_attach_handle(esl_handle_t *handle, esl_socket_t s ...@@ -391,7 +391,7 @@ ESL_DECLARE(esl_status_t) esl_attach_handle(esl_handle_t *handle, esl_socket_t s
\param port Port to bind to \param port Port to bind to
\param callback Callback that will be called upon data received \param callback Callback that will be called upon data received
*/ */
ESL_DECLARE(esl_status_t) esl_listen(const char *host, esl_port_t port, esl_listen_callback_t callback); ESL_DECLARE(esl_status_t) esl_listen(const char *host, esl_port_t port, esl_listen_callback_t callback, int max);
/*! /*!
\brief Executes application with sendmsg to a specific UUID. Used for outbound socket. \brief Executes application with sendmsg to a specific UUID. Used for outbound socket.
\param handle Handle that the msg will be sent \param handle Handle that the msg will be sent
......
...@@ -9,11 +9,6 @@ static void mycallback(esl_socket_t server_sock, esl_socket_t client_sock, struc ...@@ -9,11 +9,6 @@ static void mycallback(esl_socket_t server_sock, esl_socket_t client_sock, struc
esl_status_t status; esl_status_t status;
time_t exp = 0; time_t exp = 0;
if (fork()) {
close(client_sock);
return;
}
esl_attach_handle(&handle, client_sock, addr); esl_attach_handle(&handle, client_sock, addr);
esl_log(ESL_LOG_INFO, "Connected! %d\n", handle.sock); esl_log(ESL_LOG_INFO, "Connected! %d\n", handle.sock);
...@@ -53,7 +48,7 @@ static void mycallback(esl_socket_t server_sock, esl_socket_t client_sock, struc ...@@ -53,7 +48,7 @@ static void mycallback(esl_socket_t server_sock, esl_socket_t client_sock, struc
int main(void) int main(void)
{ {
esl_global_set_default_logger(7); esl_global_set_default_logger(7);
esl_listen("localhost", 8084, mycallback); esl_listen("localhost", 8084, mycallback, 100000);
return 0; return 0;
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论