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.
This commit is contained in:
Cedric Roux
2016-06-05 13:10:52 +02:00
parent 51292c2472
commit 9338f06e90
5 changed files with 17 additions and 12 deletions
+4 -3
View File
@@ -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();
+4 -3
View File
@@ -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();
+3 -2
View File
@@ -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)
+2 -1
View File
@@ -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);
+4 -3
View File
@@ -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();