Commit 9338f06e authored by Cedric Roux's avatar Cedric Roux

socket_send does not crash anymore but returns an error

The application (enb.c basically) will now monitor the socket
and reconnect if it dies. See following commits.
parent 51292c24
......@@ -39,9 +39,10 @@ void is_on_changed(void *_d)
if (d->socket == -1) goto no_connection;
t = 1;
socket_send(d->socket, &t, 1);
socket_send(d->socket, &d->nevents, sizeof(int));
socket_send(d->socket, d->is_on, d->nevents * sizeof(int));
if (socket_send(d->socket, &t, 1) == -1 ||
socket_send(d->socket, &d->nevents, sizeof(int)) == -1 ||
socket_send(d->socket, d->is_on, d->nevents * sizeof(int)) == -1)
abort();
no_connection:
if (pthread_mutex_unlock(&d->lock)) abort();
......
......@@ -33,9 +33,10 @@ void is_on_changed(void *_d)
if (d->socket == -1) goto no_connection;
t = 1;
socket_send(d->socket, &t, 1);
socket_send(d->socket, &d->nevents, sizeof(int));
socket_send(d->socket, d->is_on, d->nevents * sizeof(int));
if (socket_send(d->socket, &t, 1) == -1 ||
socket_send(d->socket, &d->nevents, sizeof(int)) == -1 ||
socket_send(d->socket, d->is_on, d->nevents * sizeof(int)) == -1)
abort();
no_connection:
if (pthread_mutex_unlock(&d->lock)) abort();
......
......@@ -68,16 +68,17 @@ list *list_append(list *l, void *data)
/* socket */
/****************************************************************************/
void socket_send(int socket, void *buffer, int size)
int socket_send(int socket, void *buffer, int size)
{
char *x = buffer;
int ret;
while (size) {
ret = write(socket, x, size);
if (ret <= 0) abort();
if (ret <= 0) return -1;
size -= ret;
x += ret;
}
return 0;
}
int get_connection(char *addr, int port)
......
......@@ -20,7 +20,8 @@ list *list_append(list *l, void *data);
/* socket */
/****************************************************************************/
void socket_send(int socket, void *buffer, int size);
/* socket_send: return 0 if okay, -1 on error */
int socket_send(int socket, void *buffer, int size);
int get_connection(char *addr, int port);
int fullread(int fd, void *_buf, int count);
int connect_to(char *addr, int port);
......
......@@ -30,9 +30,10 @@ void is_on_changed(void *_d)
if (d->socket == -1) goto no_connection;
t = 1;
socket_send(d->socket, &t, 1);
socket_send(d->socket, &d->nevents, sizeof(int));
socket_send(d->socket, d->is_on, d->nevents * sizeof(int));
if (socket_send(d->socket, &t, 1) == -1 ||
socket_send(d->socket, &d->nevents, sizeof(int)) == -1 ||
socket_send(d->socket, d->is_on, d->nevents * sizeof(int)) == -1)
abort();
no_connection:
if (pthread_mutex_unlock(&d->lock)) abort();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment