add user_data in esl_listen so the main function can pass a private user_data to callback
This commit is contained in:
parent
01e3eb458e
commit
11dee76381
|
@ -45,7 +45,7 @@ static void handle_SIGCHLD(int sig)
|
|||
return;
|
||||
}
|
||||
|
||||
static void my_forking_callback(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in *addr)
|
||||
static void my_forking_callback(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in *addr, void *user_data)
|
||||
{
|
||||
esl_handle_t handle = {{0}};
|
||||
char path_buffer[1024] = { 0 };
|
||||
|
@ -91,7 +91,7 @@ static void my_forking_callback(esl_socket_t server_sock, esl_socket_t client_so
|
|||
exit(0);
|
||||
}
|
||||
|
||||
static void mycallback(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in *addr)
|
||||
static void mycallback(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in *addr, void *user_data)
|
||||
{
|
||||
esl_handle_t handle = {{0}};
|
||||
const char *path;
|
||||
|
@ -162,11 +162,11 @@ int main(int argc, char *argv[])
|
|||
if (thread) {
|
||||
printf("Starting threaded listener.\n");
|
||||
fflush(stdout);
|
||||
esl_listen_threaded(ip, port, mycallback, 100000);
|
||||
esl_listen_threaded(ip, port, mycallback, NULL, 100000);
|
||||
} else {
|
||||
printf("Starting forking listener.\n");
|
||||
fflush(stdout);
|
||||
esl_listen(ip, port, my_forking_callback, NULL);
|
||||
esl_listen(ip, port, my_forking_callback, NULL, NULL);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -645,13 +645,14 @@ struct thread_handler {
|
|||
esl_socket_t server_sock;
|
||||
esl_socket_t client_sock;
|
||||
struct sockaddr_in addr;
|
||||
void *user_data;
|
||||
};
|
||||
|
||||
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);
|
||||
handler->callback(handler->server_sock, handler->client_sock, &handler->addr, handler->user_data);
|
||||
free(handler);
|
||||
|
||||
return NULL;
|
||||
|
@ -679,7 +680,7 @@ static int prepare_sock(esl_socket_t sock)
|
|||
}
|
||||
|
||||
|
||||
ESL_DECLARE(esl_status_t) esl_listen(const char *host, esl_port_t port, esl_listen_callback_t callback, esl_socket_t *server_sockP)
|
||||
ESL_DECLARE(esl_status_t) esl_listen(const char *host, esl_port_t port, esl_listen_callback_t callback, void *user_data, esl_socket_t *server_sockP)
|
||||
{
|
||||
esl_socket_t server_sock = ESL_SOCK_INVALID;
|
||||
struct sockaddr_in addr;
|
||||
|
@ -728,7 +729,7 @@ ESL_DECLARE(esl_status_t) esl_listen(const char *host, esl_port_t port, esl_list
|
|||
goto end;
|
||||
}
|
||||
prepare_sock(client_sock);
|
||||
callback(server_sock, client_sock, &echoClntAddr);
|
||||
callback(server_sock, client_sock, &echoClntAddr, user_data);
|
||||
}
|
||||
|
||||
end:
|
||||
|
@ -742,7 +743,7 @@ ESL_DECLARE(esl_status_t) esl_listen(const char *host, esl_port_t port, esl_list
|
|||
|
||||
}
|
||||
|
||||
ESL_DECLARE(esl_status_t) esl_listen_threaded(const char *host, esl_port_t port, esl_listen_callback_t callback, int max)
|
||||
ESL_DECLARE(esl_status_t) esl_listen_threaded(const char *host, esl_port_t port, esl_listen_callback_t callback, void *user_data, int max)
|
||||
{
|
||||
esl_socket_t server_sock = ESL_SOCK_INVALID;
|
||||
struct sockaddr_in addr;
|
||||
|
@ -796,6 +797,7 @@ ESL_DECLARE(esl_status_t) esl_listen_threaded(const char *host, esl_port_t port,
|
|||
handler->server_sock = server_sock;
|
||||
handler->client_sock = client_sock;
|
||||
handler->addr = echoClntAddr;
|
||||
handler->user_data = user_data;
|
||||
esl_thread_create_detached(client_thread, handler);
|
||||
}
|
||||
|
||||
|
|
|
@ -379,7 +379,7 @@ ESL_DECLARE(int) esl_tolower(int c);
|
|||
ESL_DECLARE(int) esl_snprintf(char *buffer, size_t count, const char *fmt, ...);
|
||||
|
||||
|
||||
typedef void (*esl_listen_callback_t)(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in *addr);
|
||||
typedef void (*esl_listen_callback_t)(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in *addr, void *user_data);
|
||||
/*!
|
||||
\brief Attach a handle to an established socket connection
|
||||
\param handle Handle to be attached
|
||||
|
@ -394,8 +394,8 @@ ESL_DECLARE(esl_status_t) esl_attach_handle(esl_handle_t *handle, esl_socket_t s
|
|||
\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_socket_t *server_sockP);
|
||||
ESL_DECLARE(esl_status_t) esl_listen_threaded(const char *host, esl_port_t port, esl_listen_callback_t callback, int max);
|
||||
ESL_DECLARE(esl_status_t) esl_listen(const char *host, esl_port_t port, esl_listen_callback_t callback, void *user_data, esl_socket_t *server_sockP);
|
||||
ESL_DECLARE(esl_status_t) esl_listen_threaded(const char *host, esl_port_t port, esl_listen_callback_t callback, void *user_data, int max);
|
||||
/*!
|
||||
\brief Executes application with sendmsg to a specific UUID. Used for outbound socket.
|
||||
\param handle Handle that the msg will be sent
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <esl.h>
|
||||
|
||||
static void mycallback(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in *addr)
|
||||
static void mycallback(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in *addr, void *user_data)
|
||||
{
|
||||
esl_handle_t handle = {{0}};
|
||||
int done = 0;
|
||||
|
@ -48,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_threaded("localhost", 8040, mycallback, 100000);
|
||||
esl_listen_threaded("localhost", 8040, mycallback, NULL, 100000);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <esl.h>
|
||||
|
||||
static void mycallback(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in *addr)
|
||||
static void mycallback(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in *addr, void *user_data)
|
||||
{
|
||||
esl_handle_t handle = {{0}};
|
||||
int done = 0;
|
||||
|
@ -66,7 +66,7 @@ int main(void)
|
|||
signal(SIGCHLD, SIG_IGN);
|
||||
|
||||
esl_global_set_default_logger(7);
|
||||
esl_listen("localhost", 8040, mycallback, &server_sock);
|
||||
esl_listen("localhost", 8040, mycallback, NULL, &server_sock);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue