cleanup ESL a little

This commit is contained in:
Anthony Minessale 2013-11-14 00:43:15 +05:00
parent f9a19ee57f
commit c469e7ada2
6 changed files with 81 additions and 45 deletions

View File

@ -17,7 +17,7 @@ SOLINK=-shared -Xlinker -x
# comment the next line to disable c++ (no swig mods for you then)
OBJS += src/esl_oop.o
all: $(MYLIB) fs_cli testclient testserver ivrd
all: $(MYLIB) fs_cli testclient testserver testserver_fork ivrd
$(MYLIB): $(OBJS) $(HEADERS) $(SRC)
ar rcs $(MYLIB) $(OBJS)
@ -26,6 +26,9 @@ $(MYLIB): $(OBJS) $(HEADERS) $(SRC)
testserver: $(MYLIB) testserver.c
$(CC) $(CC_CFLAGS) $(CFLAGS) testserver.c -o testserver $(LDFLAGS) $(LIBS)
testserver_fork: $(MYLIB) testserver_fork.c
$(CC) $(CC_CFLAGS) $(CFLAGS) testserver_fork.c -o testserver_fork $(LDFLAGS) $(LIBS)
ivrd: $(MYLIB) ivrd.c
$(CC) $(CC_CFLAGS) $(CFLAGS) ivrd.c -o ivrd $(LDFLAGS) $(LIBS)
@ -42,7 +45,7 @@ fs_cli: $(MYLIB) fs_cli.c
$(CXX) $(CXX_CFLAGS) $(CXXFLAGS) -c $< -o $@
clean:
rm -f *.o src/*.o testclient testserver ivrd fs_cli libesl.a *~ src/*~ src/include/*~
rm -f *.o src/*.o testclient testserver testserver_fork ivrd fs_cli libesl.a *~ src/*~ src/include/*~
$(MAKE) -C perl clean
$(MAKE) -C php clean
$(MAKE) -C lua clean

View File

@ -166,7 +166,7 @@ int main(int argc, char *argv[])
} else {
printf("Starting forking listener.\n");
fflush(stdout);
esl_listen(ip, port, my_forking_callback);
esl_listen(ip, port, my_forking_callback, NULL);
}
return 0;

View File

@ -476,15 +476,14 @@ ESL_DECLARE(esl_status_t) esl_attach_handle(esl_handle_t *handle, esl_socket_t s
esl_send_recv(handle, "connect\n\n");
if (handle->last_sr_event) {
handle->info_event = handle->last_sr_event;
handle->last_sr_event = NULL;
return ESL_SUCCESS;
}
handle->connected = 0;
esl_disconnect(handle);
return ESL_FAIL;
}
@ -658,16 +657,41 @@ static void *client_thread(esl_thread_t *me, void *obj)
}
ESL_DECLARE(esl_status_t) esl_listen(const char *host, esl_port_t port, esl_listen_callback_t callback)
static int prepare_sock(esl_socket_t sock)
{
int r = 0;
#ifdef WIN32
u_long arg = 1;
if (ioctlsocket(sock, FIONBIO, &arg) == SOCKET_ERROR) {
r = -1;
}
#else
int fd_flags = fcntl(sock, F_GETFL, 0);
if (fcntl(sock, F_SETFL, fd_flags | O_NONBLOCK)) {
r = -1;
}
#endif
}
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_socket_t server_sock = ESL_SOCK_INVALID;
struct sockaddr_in addr;
esl_status_t status = ESL_SUCCESS;
if ((server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
return ESL_FAIL;
}
if (server_sockP) {
*server_sockP = server_sock;
}
esl_socket_reuseaddr(server_sock);
memset(&addr, 0, sizeof(addr));
@ -700,7 +724,7 @@ ESL_DECLARE(esl_status_t) esl_listen(const char *host, esl_port_t port, esl_list
status = ESL_FAIL;
goto end;
}
prepare_sock(client_sock);
callback(server_sock, client_sock, &echoClntAddr);
}
@ -759,6 +783,8 @@ ESL_DECLARE(esl_status_t) esl_listen_threaded(const char *host, esl_port_t port,
goto end;
}
prepare_sock(client_sock);
handler = malloc(sizeof(*handler));
esl_assert(handler);
@ -767,7 +793,6 @@ 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;
esl_thread_create_detached(client_thread, handler);
}
@ -1078,7 +1103,7 @@ ESL_DECLARE(esl_status_t) esl_connect_timeout(esl_handle_t *handle, const char *
return ESL_SUCCESS;
fail:
handle->connected = 0;
esl_disconnect(handle);
@ -1089,7 +1114,8 @@ ESL_DECLARE(esl_status_t) esl_disconnect(esl_handle_t *handle)
{
esl_mutex_t *mutex = handle->mutex;
esl_status_t status = ESL_FAIL;
esl_event_t *ep;
if (handle->destroyed) {
return ESL_FAIL;
}
@ -1098,10 +1124,17 @@ ESL_DECLARE(esl_status_t) esl_disconnect(esl_handle_t *handle)
esl_mutex_lock(mutex);
}
handle->destroyed = 1;
handle->connected = 0;
esl_event_safe_destroy(&handle->race_event);
ep = handle->race_event;
while(ep) {
esl_event_t *e = ep;
ep = ep->next;
esl_event_safe_destroy(&e);
}
esl_event_safe_destroy(&handle->last_event);
esl_event_safe_destroy(&handle->last_sr_event);
esl_event_safe_destroy(&handle->last_ievent);
@ -1119,11 +1152,13 @@ ESL_DECLARE(esl_status_t) esl_disconnect(esl_handle_t *handle)
esl_mutex_unlock(mutex);
esl_mutex_destroy(&mutex);
}
if (handle->packet_buf) {
esl_buffer_destroy(&handle->packet_buf);
}
memset(handle, 0, sizeof(*handle));
handle->destroyed = 1;
return status;
}
@ -1150,7 +1185,11 @@ ESL_DECLARE(esl_status_t) esl_recv_event_timed(esl_handle_t *handle, uint32_t ms
esl_mutex_unlock(handle->mutex);
}
activity = esl_wait_sock(handle->sock, ms, ESL_POLL_READ|ESL_POLL_ERROR);
if (handle->packet_buf && esl_buffer_inuse(handle->packet_buf)) {
activity = ESL_POLL_READ;
} else {
activity = esl_wait_sock(handle->sock, ms, ESL_POLL_READ|ESL_POLL_ERROR);
}
if (activity < 0) {
handle->connected = 0;
@ -1161,9 +1200,6 @@ ESL_DECLARE(esl_status_t) esl_recv_event_timed(esl_handle_t *handle, uint32_t ms
return ESL_BREAK;
}
activity = esl_wait_sock(handle->sock, ms, ESL_POLL_READ|ESL_POLL_ERROR);
if (activity < 0) {
handle->connected = 0;
status = ESL_FAIL;
@ -1183,21 +1219,23 @@ ESL_DECLARE(esl_status_t) esl_recv_event_timed(esl_handle_t *handle, uint32_t ms
static esl_ssize_t handle_recv(esl_handle_t *handle, void *data, esl_size_t datalen)
{
int activity;
esl_ssize_t activity = -1;
while (handle->connected) {
activity = esl_wait_sock(handle->sock, 1000, ESL_POLL_READ|ESL_POLL_ERROR);
if (activity > 0 && (activity & ESL_POLL_READ)) {
return recv(handle->sock, data, datalen, 0);
if (handle->connected) {
if ((activity = esl_wait_sock(handle->sock, -1, ESL_POLL_READ|ESL_POLL_ERROR)) > 0) {
if ((activity & ESL_POLL_ERROR)) {
activity = -1;
} else if ((activity & ESL_POLL_READ)) {
activity = recv(handle->sock, data, datalen, 0);
if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
activity = 0;
}
}
}
if (activity < 0) {
return errno == EINTR ? 0 : -1;
}
}
return -1;
return activity;
}
ESL_DECLARE(esl_status_t) esl_recv_event(esl_handle_t *handle, int check_q, esl_event_t **save_event)
@ -1232,7 +1270,6 @@ ESL_DECLARE(esl_status_t) esl_recv_event(esl_handle_t *handle, int check_q, esl_
goto parse_event;
}
while(!revent && handle->connected) {
esl_size_t len1;
@ -1277,14 +1314,10 @@ ESL_DECLARE(esl_status_t) esl_recv_event(esl_handle_t *handle, int check_q, esl_
break;
}
rrval = handle_recv(handle, handle->socket_buf, sizeof(handle->socket_buf) - 1);
*((char *)handle->socket_buf + ESL_CLAMP(0, sizeof(handle->socket_buf) - 1, rrval)) = '\0';
rrval = handle_recv(handle, handle->socket_buf, sizeof(handle->socket_buf) - 1);
if (rrval == 0) {
if (++zc >= 100) {
goto fail;
}
continue;
} else if (rrval < 0) {
if (!(strerror_r(handle->errnum, handle->err, sizeof(handle->err))))
@ -1292,6 +1325,7 @@ ESL_DECLARE(esl_status_t) esl_recv_event(esl_handle_t *handle, int check_q, esl_
goto fail;
}
*((char *)handle->socket_buf + ESL_CLAMP(0, sizeof(handle->socket_buf) - 1, rrval)) = '\0';
zc = 0;
esl_buffer_write(handle->packet_buf, handle->socket_buf, rrval);
@ -1317,19 +1351,16 @@ ESL_DECLARE(esl_status_t) esl_recv_event(esl_handle_t *handle, int check_q, esl_
sofar = esl_buffer_read(handle->packet_buf, body, len);
} else {
r = handle_recv(handle, handle->socket_buf, sizeof(handle->socket_buf) - 1);
*((char *)handle->socket_buf + ESL_CLAMP(0, sizeof(handle->socket_buf) - 1, r)) = '\0';
if (r < 0) {
if (!(strerror_r(handle->errnum, handle->err, sizeof(handle->err))))
*(handle->err)=0;
goto fail;
} else if (r == 0) {
if (++zc >= 100) {
goto fail;
}
continue;
}
*((char *)handle->socket_buf + ESL_CLAMP(0, sizeof(handle->socket_buf) - 1, r)) = '\0';
zc = 0;
esl_buffer_write(handle->packet_buf, handle->socket_buf, r);
@ -1527,7 +1558,7 @@ ESL_DECLARE(esl_status_t) esl_send_recv_timed(esl_handle_t *handle, const char *
esl_event_t *ep;
for(ep = handle->race_event; ep && ep->next; ep = ep->next);
if (ep) {
ep->next = handle->last_sr_event;
} else {

View File

@ -54,6 +54,7 @@ ESL_DECLARE(esl_status_t) esl_buffer_create(esl_buffer_t **buffer, esl_size_t bl
esl_buffer_t *new_buffer;
new_buffer = malloc(sizeof(*new_buffer));
if (new_buffer) {
memset(new_buffer, 0, sizeof(*new_buffer));
@ -335,9 +336,10 @@ ESL_DECLARE(void) esl_buffer_destroy(esl_buffer_t **buffer)
{
if (*buffer) {
free((*buffer)->data);
(*buffer)->data = NULL;
free(*buffer);
}
*buffer = NULL;
}

View File

@ -394,7 +394,7 @@ 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_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);
/*!
\brief Executes application with sendmsg to a specific UUID. Used for outbound socket.

View File

@ -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", 8084, mycallback, 100000);
esl_listen_threaded("localhost", 8040, mycallback, 100000);
return 0;
}