use threading in testserver and ivrd

This commit is contained in:
Anthony Minessale 2011-12-14 12:16:09 -06:00
parent e566057701
commit f7f9a43a40
4 changed files with 33 additions and 19 deletions

View File

@ -42,11 +42,6 @@ static void mycallback(esl_socket_t server_sock, esl_socket_t client_sock, struc
char path_buffer[1024] = { 0 };
const char *path;
if (fork()) {
close(client_sock);
return;
}
if (esl_attach_handle(&handle, client_sock, addr) != ESL_SUCCESS || !handle.info_event) {
esl_log(ESL_LOG_ERROR, "Socket Error\n");
exit(0);
@ -95,9 +90,7 @@ int main(int argc, char *argv[])
return -1;
}
signal(SIGCHLD, SIG_IGN);
esl_listen(ip, port, mycallback);
esl_listen(ip, port, mycallback, 100000);
return 0;
}

View File

@ -612,12 +612,31 @@ static int esl_socket_reuseaddr(esl_socket_t socket)
#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;
struct sockaddr_in addr;
esl_status_t status = ESL_SUCCESS;
struct thread_handler *handler = NULL;
if ((server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
return ESL_FAIL;
}
@ -634,7 +653,7 @@ ESL_DECLARE(esl_status_t) esl_listen(const char *host, esl_port_t port, esl_list
goto end;
}
if (listen(server_sock, 10000) < 0) {
if (listen(server_sock, max) < 0) {
status = ESL_FAIL;
goto end;
}
@ -655,7 +674,14 @@ ESL_DECLARE(esl_status_t) esl_listen(const char *host, esl_port_t port, esl_list
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:

View File

@ -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 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.
\param handle Handle that the msg will be sent

View File

@ -9,11 +9,6 @@ static void mycallback(esl_socket_t server_sock, esl_socket_t client_sock, struc
esl_status_t status;
time_t exp = 0;
if (fork()) {
close(client_sock);
return;
}
esl_attach_handle(&handle, client_sock, addr);
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
int main(void)
{
esl_global_set_default_logger(7);
esl_listen("localhost", 8084, mycallback);
esl_listen("localhost", 8084, mycallback, 100000);
return 0;
}