From 5fc741a911664c1b4e0c7044ee6607cdcfa7182b Mon Sep 17 00:00:00 2001 From: Martin Sustrik Date: Sun, 27 May 2012 07:44:10 +0200 Subject: Polling on SURVEYOR socket implemented Signed-off-by: Martin Sustrik --- src/surveyor.cpp | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) (limited to 'src/surveyor.cpp') diff --git a/src/surveyor.cpp b/src/surveyor.cpp index bd19b8a..6a789eb 100644 --- a/src/surveyor.cpp +++ b/src/surveyor.cpp @@ -31,13 +31,17 @@ xs::surveyor_t::surveyor_t (class ctx_t *parent_, uint32_t tid_, int sid_) : xsurveyor_t (parent_, tid_, sid_), receiving_responses (false), survey_id (generate_random ()), - timeout (0) + timeout (0), + has_prefetched (false) { options.type = XS_SURVEYOR; + + prefetched.init (); } xs::surveyor_t::~surveyor_t () { + prefetched.close (); } int xs::surveyor_t::xsend (msg_t *msg_, int flags_) @@ -90,6 +94,13 @@ int xs::surveyor_t::xrecv (msg_t *msg_, int flags_) return -1; } + // If we have a message prefetched we can return it straight away. + if (has_prefetched) { + msg_->move (prefetched); + has_prefetched = false; + return 0; + } + // Get the first part of the response -- the survey ID. rc = xsurveyor_t::xrecv (msg_, flags_); if (rc != 0) { @@ -128,9 +139,26 @@ int xs::surveyor_t::xrecv (msg_t *msg_, int flags_) bool xs::surveyor_t::xhas_in () { - // TODO: We need a prefetched_message here... - xs_assert (false); - return false; + // When no survey is underway, POLLIN is never signaled. + if (!receiving_responses) + return false; + + // If there's already a message prefetches signal POLLIN. + if (has_prefetched) + return true; + + // Try to prefetch a message. + int rc = xrecv (&prefetched, XS_DONTWAIT); + + // No message available. + if (rc != 0 && errno == EAGAIN) + return false; + + errno_assert (rc == 0); + + // We have a message prefetched. We can signal POLLIN now. + has_prefetched = true; + return true; } bool xs::surveyor_t::xhas_out () -- cgit v1.2.3