aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2016-08-06 04:41:19 +0200
committerMattias Andrée <maandree@kth.se>2016-08-06 04:41:19 +0200
commitb865212e6141ae8b538d347ae384bba0dcdad7e8 (patch)
tree0ee8ce890372c30260647d4a33602d67645a17d9
parentwhitespace (diff)
downloadlibcoopgamma-b865212e6141ae8b538d347ae384bba0dcdad7e8.tar.gz
libcoopgamma-b865212e6141ae8b538d347ae384bba0dcdad7e8.tar.bz2
libcoopgamma-b865212e6141ae8b538d347ae384bba0dcdad7e8.tar.xz
Appearently it is not easy to interrupt recv(3)
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r--src/libcoopgamma.c21
-rw-r--r--src/libcoopgamma.h7
2 files changed, 24 insertions, 4 deletions
diff --git a/src/libcoopgamma.c b/src/libcoopgamma.c
index 7bfd36a..2d1ca37 100644
--- a/src/libcoopgamma.c
+++ b/src/libcoopgamma.c
@@ -26,6 +26,7 @@
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
+#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -826,6 +827,7 @@ int libcoopgamma_context_initialise(libcoopgamma_context_t* restrict this)
{
memset(this, 0, sizeof(*this));
this->fd = -1;
+ this->blocking = 1;
return 0;
}
@@ -879,6 +881,7 @@ size_t libcoopgamma_context_marshal(const libcoopgamma_context_t* restrict this,
marshal_prim(this->in_response_to, uint32_t);
marshal_prim(this->have_all_headers, int);
marshal_prim(this->bad_message, int);
+ marshal_prim(this->blocking, int);
MARSHAL_EPILOGUE;
}
@@ -916,6 +919,7 @@ int libcoopgamma_context_unmarshal(libcoopgamma_context_t* restrict this,
unmarshal_prim(this->in_response_to, uint32_t);
unmarshal_prim(this->have_all_headers, int);
unmarshal_prim(this->bad_message, int);
+ unmarshal_prim(this->blocking, int);
UNMARSHAL_EPILOGUE;
}
@@ -1358,6 +1362,8 @@ int libcoopgamma_connect(const char* restrict method, const char* restrict site,
pid_t pid;
size_t i = 1;
+ ctx->blocking = 1;
+
if (method != NULL) args[i++] = "-m", args[i++] = method;
if (site != NULL) args[i++] = "-s", args[i++] = site;
args[i] = NULL;
@@ -1449,7 +1455,10 @@ int libcoopgamma_set_nonblocking(libcoopgamma_context_t* restrict ctx, int nonbl
flags |= O_NONBLOCK;
else
flags &= ~O_NONBLOCK;
- return -(fcntl(ctx->fd, F_SETFL, flags) == -1);
+ if (fcntl(ctx->fd, F_SETFL, flags) == -1)
+ return -1;
+ ctx->blocking = !nonblocking;
+ return 0;
}
@@ -1530,6 +1539,7 @@ int libcoopgamma_synchronise(libcoopgamma_context_t* restrict ctx,
char* p;
char* line;
char* value;
+ struct pollfd pollfd;
if (ctx->inbound_head == ctx->inbound_tail)
ctx->inbound_head = ctx->inbound_tail = ctx->curline = 0;
@@ -1540,6 +1550,9 @@ int libcoopgamma_synchronise(libcoopgamma_context_t* restrict ctx,
ctx->inbound_tail = 0;
}
+ pollfd.fd = ctx->fd;
+ pollfd.events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI;
+
if (ctx->inbound_head)
goto skip_recv;
for (;;)
@@ -1554,6 +1567,12 @@ int libcoopgamma_synchronise(libcoopgamma_context_t* restrict ctx,
ctx->inbound_size = new_size;
}
+ if (ctx->blocking)
+ {
+ pollfd.revents = 0;
+ if (poll(&pollfd, (nfds_t)1, -1) < 0)
+ return -1;
+ }
got = recv(ctx->fd, ctx->inbound + ctx->inbound_head, ctx->inbound_size - ctx->inbound_head, 0);
if (got <= 0)
{
diff --git a/src/libcoopgamma.h b/src/libcoopgamma.h
index 1022f1e..1737e7f 100644
--- a/src/libcoopgamma.h
+++ b/src/libcoopgamma.h
@@ -763,9 +763,10 @@ typedef struct libcoopgamma_context
*/
int bad_message;
-#if INT_MAX != LONG_MAX
- int padding__;
-#endif
+ /**
+ * Is communication blocking?
+ */
+ int blocking;
/**
* Message ID of the next message