Skip to content
Snippets Groups Projects
Commit 9338f06e authored by Cédric Roux's avatar Cédric Roux
Browse files

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
No related branches found
No related tags found
No related merge requests found
...@@ -39,9 +39,10 @@ void is_on_changed(void *_d) ...@@ -39,9 +39,10 @@ void is_on_changed(void *_d)
if (d->socket == -1) goto no_connection; if (d->socket == -1) goto no_connection;
t = 1; t = 1;
socket_send(d->socket, &t, 1); if (socket_send(d->socket, &t, 1) == -1 ||
socket_send(d->socket, &d->nevents, sizeof(int)); socket_send(d->socket, &d->nevents, sizeof(int)) == -1 ||
socket_send(d->socket, d->is_on, d->nevents * sizeof(int)); socket_send(d->socket, d->is_on, d->nevents * sizeof(int)) == -1)
abort();
no_connection: no_connection:
if (pthread_mutex_unlock(&d->lock)) abort(); if (pthread_mutex_unlock(&d->lock)) abort();
......
...@@ -33,9 +33,10 @@ void is_on_changed(void *_d) ...@@ -33,9 +33,10 @@ void is_on_changed(void *_d)
if (d->socket == -1) goto no_connection; if (d->socket == -1) goto no_connection;
t = 1; t = 1;
socket_send(d->socket, &t, 1); if (socket_send(d->socket, &t, 1) == -1 ||
socket_send(d->socket, &d->nevents, sizeof(int)); socket_send(d->socket, &d->nevents, sizeof(int)) == -1 ||
socket_send(d->socket, d->is_on, d->nevents * sizeof(int)); socket_send(d->socket, d->is_on, d->nevents * sizeof(int)) == -1)
abort();
no_connection: no_connection:
if (pthread_mutex_unlock(&d->lock)) abort(); if (pthread_mutex_unlock(&d->lock)) abort();
......
...@@ -68,16 +68,17 @@ list *list_append(list *l, void *data) ...@@ -68,16 +68,17 @@ list *list_append(list *l, void *data)
/* socket */ /* socket */
/****************************************************************************/ /****************************************************************************/
void socket_send(int socket, void *buffer, int size) int socket_send(int socket, void *buffer, int size)
{ {
char *x = buffer; char *x = buffer;
int ret; int ret;
while (size) { while (size) {
ret = write(socket, x, size); ret = write(socket, x, size);
if (ret <= 0) abort(); if (ret <= 0) return -1;
size -= ret; size -= ret;
x += ret; x += ret;
} }
return 0;
} }
int get_connection(char *addr, int port) int get_connection(char *addr, int port)
......
...@@ -20,7 +20,8 @@ list *list_append(list *l, void *data); ...@@ -20,7 +20,8 @@ list *list_append(list *l, void *data);
/* socket */ /* 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 get_connection(char *addr, int port);
int fullread(int fd, void *_buf, int count); int fullread(int fd, void *_buf, int count);
int connect_to(char *addr, int port); int connect_to(char *addr, int port);
......
...@@ -30,9 +30,10 @@ void is_on_changed(void *_d) ...@@ -30,9 +30,10 @@ void is_on_changed(void *_d)
if (d->socket == -1) goto no_connection; if (d->socket == -1) goto no_connection;
t = 1; t = 1;
socket_send(d->socket, &t, 1); if (socket_send(d->socket, &t, 1) == -1 ||
socket_send(d->socket, &d->nevents, sizeof(int)); socket_send(d->socket, &d->nevents, sizeof(int)) == -1 ||
socket_send(d->socket, d->is_on, d->nevents * sizeof(int)); socket_send(d->socket, d->is_on, d->nevents * sizeof(int)) == -1)
abort();
no_connection: no_connection:
if (pthread_mutex_unlock(&d->lock)) abort(); if (pthread_mutex_unlock(&d->lock)) abort();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment