use safety macros to check mallocs, remove some dead code, optimize read loops by removing them, refactor a lot of code to not be indented halfway across the screen

SVN revision: 54133
This commit is contained in:
Mike Blumenkrantz 2010-11-03 19:08:31 +00:00
parent ddd151bf37
commit 3ffb7e4c16
1 changed files with 296 additions and 367 deletions

View File

@ -1177,8 +1177,8 @@ _ecore_con_client_free(Ecore_Con_Client *cl)
cl->dead = EINA_TRUE;
e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
if (e)
{
EINA_SAFETY_ON_NULL_RETURN(e);
cl->event_count++;
_ecore_con_cl_timer_update(cl);
e->client = cl;
@ -1186,7 +1186,6 @@ _ecore_con_client_free(Ecore_Con_Client *cl)
_ecore_con_event_client_del_free, NULL);
return;
}
}
ECORE_MAGIC_SET(cl, ECORE_MAGIC_NONE);
t_start = ecore_time_get();
@ -1230,14 +1229,13 @@ _ecore_con_server_kill(Ecore_Con_Server *svr)
Ecore_Con_Event_Server_Del *e;
e = calloc(1, sizeof(Ecore_Con_Event_Server_Del));
if (e)
{
EINA_SAFETY_ON_NULL_RETURN(e);
svr->event_count++;
e->server = svr;
ecore_event_add(ECORE_CON_EVENT_SERVER_DEL, e,
_ecore_con_event_server_del_free, NULL);
}
}
svr->dead = EINA_TRUE;
if (svr->fd_handler)
@ -1594,15 +1592,14 @@ svr_try_connect_plain(Ecore_Con_Server *svr)
svr->connecting = EINA_FALSE;
e = calloc(1, sizeof(Ecore_Con_Event_Server_Add));
if (e)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(e, ECORE_CON_CONNECTED);
svr->event_count++;
svr->start_time = ecore_time_get();
e->server = svr;
ecore_event_add(ECORE_CON_EVENT_SERVER_ADD, e,
_ecore_con_event_server_add_free, NULL);
}
}
if (svr->fd_handler && (!svr->write_buf))
ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
@ -1711,14 +1708,14 @@ _ecore_con_svr_tcp_handler(void *data,
Ecore_Con_Event_Client_Add *e;
e = calloc(1, sizeof(Ecore_Con_Event_Client_Add));
if (e)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(e, ECORE_CALLBACK_RENEW);
cl->event_count++;
_ecore_con_cl_timer_update(cl);
e->client = cl;
ecore_event_add(ECORE_CON_EVENT_CLIENT_ADD, e,
_ecore_con_event_client_add_free, NULL);
}
}
return ECORE_CALLBACK_RENEW;
@ -1733,20 +1730,14 @@ error:
static void
_ecore_con_cl_read(Ecore_Con_Server *svr)
{
unsigned char *inbuf = NULL;
int inbuf_num = 0;
int tries;
int num = 0;
Eina_Bool lost_server = EINA_TRUE;
unsigned char buf[READBUFSIZ];
/* only possible with non-ssl connections */
if (svr->connecting && (svr_try_connect_plain(svr) != ECORE_CON_CONNECTED))
return;
for (tries = 0; tries < 16; tries++)
{
int num = 0;
Eina_Bool lost_server = EINA_TRUE;
unsigned char buf[READBUFSIZ];
if (svr->handshaking)
{
DBG("Continuing ssl handshake");
@ -1756,51 +1747,34 @@ _ecore_con_cl_read(Ecore_Con_Server *svr)
if (!(svr->type & ECORE_CON_SSL))
{
num = read(svr->fd, buf, READBUFSIZ);
num = read(svr->fd, buf, sizeof(buf));
if ((num < 0) && (errno == EAGAIN))
lost_server = EINA_FALSE;
}
else if (!(num = ecore_con_ssl_server_read(svr, buf, READBUFSIZ)))
else if (!(num = ecore_con_ssl_server_read(svr, buf, sizeof(buf))))
lost_server = EINA_FALSE;
if (num < 1)
{
if (inbuf && (!svr->delete_me))
return;
if (!svr->delete_me)
{
Ecore_Con_Event_Server_Data *e;
e = malloc(sizeof(Ecore_Con_Event_Server_Data));
if (e)
{
EINA_SAFETY_ON_NULL_RETURN(e);
svr->event_count++;
e->server = svr;
e->data = inbuf;
e->size = inbuf_num;
e->data = malloc(num);
memcpy(e->data, buf, num);
e->size = num;
ecore_event_add(ECORE_CON_EVENT_SERVER_DATA, e,
_ecore_con_event_server_data_free, NULL);
}
}
if (lost_server)
_ecore_con_server_kill(svr);
break;
}
inbuf = realloc(inbuf, inbuf_num + num);
memcpy(inbuf + inbuf_num, buf, num);
inbuf_num += num;
}
/* #if USE_OPENSSL */
/* if (svr->fd_handler) */
/* { */
/* if (svr->ssl && ssl_err == SSL_ERROR_WANT_READ) */
/* ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ); */
/* else if (svr->ssl && ssl_err == SSL_ERROR_WANT_WRITE) */
/* ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_WRITE); */
/* } */
/* #endif */
}
static Eina_Bool
@ -1826,7 +1800,7 @@ _ecore_con_cl_handler(void *data,
#ifdef ISCOMFITOR
if (want_read)
{
char buf[32768];
char buf[READBUFSIZ];
ssize_t len;
len = recv(svr->fd, buf, sizeof(buf), MSG_DONTWAIT | MSG_PEEK);
DBG("%zu bytes in buffer", len);
@ -1839,14 +1813,13 @@ _ecore_con_cl_handler(void *data,
Ecore_Con_Event_Server_Del *e;
e = calloc(1, sizeof(Ecore_Con_Event_Server_Del));
if (e)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(e, ECORE_CALLBACK_RENEW);
svr->event_count++;
e->server = svr;
ecore_event_add(ECORE_CON_EVENT_SERVER_DEL, e,
_ecore_con_event_server_del_free, NULL);
}
}
else if (!svr->ssl_state)
{
/* we got our server! */
@ -1854,8 +1827,8 @@ _ecore_con_cl_handler(void *data,
svr->connecting = EINA_FALSE;
e = calloc(1, sizeof(Ecore_Con_Event_Server_Add));
if (e)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(e, ECORE_CALLBACK_RENEW);
svr->event_count++;
svr->start_time = ecore_time_get();
e->server = svr;
@ -1863,7 +1836,6 @@ _ecore_con_cl_handler(void *data,
_ecore_con_event_server_add_free, NULL);
}
}
}
else if (want_read)
_ecore_con_cl_read(svr);
else if (want_write) /* only possible with non-ssl connections */
@ -1881,56 +1853,53 @@ static Eina_Bool
_ecore_con_cl_udp_handler(void *data,
Ecore_Fd_Handler *fd_handler)
{
unsigned char buf[READBUFSIZ];
int num;
Ecore_Con_Server *svr;
Eina_Bool want_read, want_write;
want_read = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ);
want_write = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE);
svr = data;
if (svr->dead)
if (svr->dead || svr->delete_me || ((!want_read) && (!want_write)))
return ECORE_CALLBACK_RENEW;
if (svr->delete_me)
return ECORE_CALLBACK_RENEW;
if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
if (want_write)
{
unsigned char buf[65536];
int num;
_ecore_con_server_flush(svr);
return ECORE_CALLBACK_RENEW;
}
errno = 0;
num = read(svr->fd, buf, 65536);
if (num > 0)
{
if (!svr->delete_me)
{
num = read(svr->fd, buf, READBUFSIZ);
if ((errno == EIO) || (errno == EBADF) || (errno == EPIPE) || (errno == EINVAL) ||
(errno == ENOSPC) || (errno == ECONNREFUSED))
_ecore_con_server_kill(svr);
if ((num < 1) || (svr->delete_me))
return ECORE_CALLBACK_RENEW;
Ecore_Con_Event_Server_Data *e;
unsigned char *inbuf;
inbuf = malloc(num);
if(!inbuf)
return 1;
return ECORE_CALLBACK_RENEW;
memcpy(inbuf, buf, num);
e = calloc(1, sizeof(Ecore_Con_Event_Server_Data));
if (e)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(e, ECORE_CALLBACK_RENEW);
svr->event_count++;
e->server = svr;
e->data = inbuf;
e->size = num;
ecore_event_add(ECORE_CON_EVENT_SERVER_DATA, e,
_ecore_con_event_server_data_free,
NULL);
}
}
}
else if ((errno == EIO) || (errno == EBADF) ||
(errno == EPIPE) || (errno == EINVAL) ||
(errno == ENOSPC) || (errno == ECONNREFUSED))
_ecore_con_server_kill(svr);
}
else if (ecore_main_fd_handler_active_get(fd_handler,
ECORE_FD_WRITE))
_ecore_con_server_flush(svr);
_ecore_con_event_server_data_free, NULL);
return ECORE_CALLBACK_RENEW;
}
@ -1938,63 +1907,78 @@ _ecore_con_cl_udp_handler(void *data,
static Eina_Bool
_ecore_con_svr_udp_handler(void *data,
Ecore_Fd_Handler *fd_handler)
{
Ecore_Con_Server *svr;
Ecore_Con_Client *cl = NULL;
svr = data;
if (svr->dead)
return ECORE_CALLBACK_RENEW;
if (svr->delete_me)
return ECORE_CALLBACK_RENEW;
if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
{
unsigned char buf[READBUFSIZ];
unsigned char client_addr[256];
unsigned int client_addr_len = sizeof(client_addr);
int num;
Ecore_Con_Server *svr;
Ecore_Con_Client *cl = NULL;
svr = data;
if (svr->delete_me || svr->dead)
return ECORE_CALLBACK_RENEW;
if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
{
_ecore_con_client_flush(cl);
return ECORE_CALLBACK_RENEW;
}
if (!ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
return ECORE_CALLBACK_RENEW;
errno = 0;
#ifdef _WIN32
num = fcntl(svr->fd, F_SETFL, O_NONBLOCK);
if (num >= 0)
num =
recvfrom(svr->fd, buf, sizeof(buf), 0,
num = recvfrom(svr->fd, buf, sizeof(buf), 0,
(struct sockaddr *)&client_addr,
&client_addr_len);
#else
num =
recvfrom(svr->fd, buf, sizeof(buf), MSG_DONTWAIT,
num = recvfrom(svr->fd, buf, sizeof(buf), MSG_DONTWAIT,
(struct sockaddr *)&client_addr,
&client_addr_len);
#endif
if (num > 0)
if ((errno == EIO) || (errno == EBADF) || (errno == EPIPE) ||
(errno == EINVAL) || (errno == ENOSPC) || (errno == ECONNREFUSED))
{
if (!svr->delete_me)
{
Ecore_Con_Event_Client_Data *e;
unsigned char *inbuf;
/* we lost our client! */
Ecore_Con_Event_Client_Del *e;
e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
EINA_SAFETY_ON_NULL_RETURN_VAL(e, ECORE_CALLBACK_RENEW);
svr->event_count++;
/* be explicit here */
e->client = NULL;
ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
_ecore_con_event_client_del_free, NULL);
}
svr->dead = EINA_TRUE;
svr->fd_handler = NULL;
return ECORE_CALLBACK_CANCEL;
}
/* Create a new client for use in the client data event */
cl = calloc(1, sizeof(Ecore_Con_Client));
if(!cl)
return ECORE_CALLBACK_RENEW;
EINA_SAFETY_ON_NULL_RETURN_VAL(cl, ECORE_CALLBACK_RENEW);
cl->buf = NULL;
cl->fd = 0;
cl->fd_handler = NULL;
cl->host_server = svr;
cl->client_addr = calloc(1, client_addr_len);
cl->client_addr_len = client_addr_len;
if(!cl->client_addr)
{
free(cl);
return ECORE_CALLBACK_RENEW;
}
cl->client_addr_len = client_addr_len;
memcpy(cl->client_addr, &client_addr, client_addr_len);
ECORE_MAGIC_SET(cl, ECORE_MAGIC_CON_CLIENT);
@ -2004,96 +1988,57 @@ _ecore_con_svr_udp_handler(void *data,
cl->ip = _ecore_con_pretty_ip(cl->client_addr,
cl->client_addr_len);
inbuf = malloc(num);
if(!inbuf)
{
free(cl->client_addr);
free(cl);
return ECORE_CALLBACK_RENEW;
}
memcpy(inbuf, buf, num);
e = calloc(1, sizeof(Ecore_Con_Event_Client_Data));
if (e)
{
svr->event_count++;
_ecore_con_cl_timer_update(cl);
e->client = cl;
e->data = inbuf;
e->size = num;
ecore_event_add(ECORE_CON_EVENT_CLIENT_DATA, e,
_ecore_con_event_client_data_free,
NULL);
}
if (!cl->delete_me)
{
{ /* indent to keep it all nicely separated */
Ecore_Con_Event_Client_Add *add;
add = calloc(1, sizeof(Ecore_Con_Event_Client_Add));
if(add)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(add, ECORE_CALLBACK_RENEW);
/*cl->event_count++;*/
add->client = cl;
_ecore_con_cl_timer_update(cl);
ecore_event_add(ECORE_CON_EVENT_CLIENT_ADD, add,
_ecore_con_event_client_add_free, NULL);
}
}
}
}
else if ((errno == EIO) || (errno == EBADF) ||
(errno == EPIPE) || (errno == EINVAL) ||
(errno == ENOSPC) || (errno == ECONNREFUSED))
{
if (!svr->delete_me)
{
/* we lost our client! */
Ecore_Con_Event_Client_Del *e;
e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
if (e)
{
Ecore_Con_Event_Client_Data *e;
e = calloc(1, sizeof(Ecore_Con_Event_Client_Data));
EINA_SAFETY_ON_NULL_RETURN_VAL(e, ECORE_CALLBACK_RENEW);
svr->event_count++;
/* be explicit here */
e->client = NULL;
ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
_ecore_con_event_client_del_free, NULL);
}
_ecore_con_cl_timer_update(cl);
e->client = cl;
e->data = malloc(num);
if(!e->data)
{
free(cl->client_addr);
free(cl);
return ECORE_CALLBACK_RENEW;
}
svr->dead = EINA_TRUE;
if (svr->fd_handler)
ecore_main_fd_handler_del(svr->fd_handler);
svr->fd_handler = NULL;
memcpy(e->data, buf, num);
e->size = num;
ecore_event_add(ECORE_CON_EVENT_CLIENT_DATA, e,
_ecore_con_event_client_data_free, NULL);
}
}
else if (ecore_main_fd_handler_active_get(fd_handler,
ECORE_FD_WRITE))
_ecore_con_client_flush(cl);
return ECORE_CALLBACK_RENEW;
}
static void
_ecore_con_svr_cl_read(Ecore_Con_Client *cl)
{
unsigned char *inbuf = NULL;
int inbuf_num = 0;
int tries;
for (tries = 0; tries < 16; tries++)
{
int num = 0;
Eina_Bool lost_client = EINA_TRUE;
unsigned char buf[READBUFSIZ];
errno = 0;
if (cl->handshaking)
{
/* add an extra handshake attempt just before read, even though
* read also attempts to handshake, to try to finish sooner
*/
if (ecore_con_ssl_client_init(cl))
lost_client = EINA_FALSE;
@ -2102,45 +2047,36 @@ _ecore_con_svr_cl_read(Ecore_Con_Client *cl)
if (!(cl->host_server->type & ECORE_CON_SSL))
{
if ((num = read(cl->fd, buf, READBUFSIZ)) <= 0)
if ((num < 0) && (errno == EAGAIN))
errno = 0;
num = read(cl->fd, buf, sizeof(buf));
if (errno == EAGAIN)
lost_client = EINA_FALSE;
}
else if (!(num = ecore_con_ssl_client_read(cl, buf, READBUFSIZ)))
else if (!(num = ecore_con_ssl_client_read(cl, buf, sizeof(buf))))
lost_client = EINA_FALSE;
if (num > 0)
{
unsigned char *tmp;
if (!(tmp = realloc(inbuf, inbuf_num + num)))
{
/* FIXME: this should probably do something, but what? */
if (inbuf)
free(inbuf);
break;
}
inbuf = tmp;
memcpy(inbuf + inbuf_num, buf, num);
inbuf_num += num;
continue;
}
if (inbuf && (!cl->delete_me))
if (num && (!cl->delete_me))
{
Ecore_Con_Event_Client_Data *e;
e = malloc(sizeof(Ecore_Con_Event_Client_Data));
if (e)
{
EINA_SAFETY_ON_NULL_RETURN(e);
cl->event_count++;
_ecore_con_cl_timer_update(cl);
e->client = cl;
e->data = inbuf;
e->size = inbuf_num;
e->data = malloc(num);
if (!e->data)
{
free(e);
ERR("alloc!");
return;
}
memcpy(e->data, buf, num);
e->size = num;
ecore_event_add(ECORE_CON_EVENT_CLIENT_DATA, e,
_ecore_con_event_client_data_free, NULL);
}
}
if (lost_client && (!cl->delete_me))
{
@ -2148,15 +2084,14 @@ _ecore_con_svr_cl_read(Ecore_Con_Client *cl)
Ecore_Con_Event_Client_Del *e;
e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
if (e)
{
EINA_SAFETY_ON_NULL_RETURN(e);
cl->event_count++;
_ecore_con_cl_timer_update(cl);
e->client = cl;
ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
_ecore_con_event_client_del_free, NULL);
}
}
if (lost_client)
{
@ -2166,9 +2101,6 @@ _ecore_con_svr_cl_read(Ecore_Con_Client *cl)
cl->fd_handler = NULL;
}
break;
}
}
static Eina_Bool
@ -2195,29 +2127,27 @@ _ecore_con_svr_cl_handler(void *data,
cl->dead = EINA_TRUE;
e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
if (e)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(e, ECORE_CALLBACK_RENEW);
cl->event_count++;
_ecore_con_cl_timer_update(cl);
e->client = cl;
ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
_ecore_con_event_client_del_free, NULL);
}
}
else if (!cl->ssl_state)
{
Ecore_Con_Event_Client_Add *add;
Ecore_Con_Event_Client_Add *e;
add = calloc(1, sizeof(Ecore_Con_Event_Client_Add));
if(add)
{
add->client = cl;
e = calloc(1, sizeof(Ecore_Con_Event_Client_Add));
EINA_SAFETY_ON_NULL_RETURN_VAL(e, ECORE_CALLBACK_RENEW);
e->client = cl;
_ecore_con_cl_timer_update(cl);
ecore_event_add(ECORE_CON_EVENT_CLIENT_ADD, add,
ecore_event_add(ECORE_CON_EVENT_CLIENT_ADD, e,
_ecore_con_event_client_add_free, NULL);
}
}
}
else if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
_ecore_con_svr_cl_read(cl);
@ -2312,14 +2242,13 @@ _ecore_con_client_flush(Ecore_Con_Client *cl)
Ecore_Con_Event_Client_Del *e;
e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
if (e)
{
EINA_SAFETY_ON_NULL_RETURN(e);
cl->event_count++;
_ecore_con_cl_timer_update(cl);
e->client = cl;
ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
_ecore_con_event_client_del_free, NULL);
}
cl->dead = EINA_TRUE;
if (cl->fd_handler)