Chromium Code Reviews| Index: src/pkg/mdns/mdns_extension_macos.cc |
| diff --git a/src/pkg/mdns/mdns_extension_macos.cc b/src/pkg/mdns/mdns_extension_macos.cc |
| index 329596d99b6d251b606a906cf7648033cf6fe8c6..90751c9797114e3470dcc5c87f135f7094c97526 100644 |
| --- a/src/pkg/mdns/mdns_extension_macos.cc |
| +++ b/src/pkg/mdns/mdns_extension_macos.cc |
| @@ -10,6 +10,7 @@ |
| #include <string.h> |
| #include <arpa/inet.h> |
| #include <pthread.h> |
| +#include <sys/time.h> |
| #include <dns_sd.h> |
| @@ -18,8 +19,15 @@ |
| #include "mdns_extension.h" |
| +// Constants. |
| +static uint32_t kUsecsInSecs = 1000000; |
| +static uint32_t kMillisecsInUsecs = 1000; |
| + |
| // Context passed to the callback receiving the mDNS information. |
| struct Context { |
| + DNSServiceRef ref; |
| + // The timeout waiting for results. |
| + int timeout; |
| // The Dart port where the result should be delivered. |
| Dart_Port port; |
| }; |
| @@ -30,25 +38,66 @@ static void Fatal(const char *message) { |
| exit(-1); |
| } |
| +int64_t timeval_to_usec(const struct timeval* tv) { |
| + return( (int64_t)tv->tv_sec * kUsecsInSecs + tv->tv_usec ) ; |
| +} |
| + |
| +struct timeval* usec_to_timeval(int64_t usec, struct timeval* tv) { |
| + tv->tv_sec = usec / kUsecsInSecs; |
| + tv->tv_usec = usec % kUsecsInSecs; |
| + return tv; |
| +} |
| + |
| +// Free allocated resources associated with a context. |
| +static void FreeContext(Context* ctx) { |
| + DNSServiceRefDeallocate(ctx->ref); |
| + free(ctx); |
| +} |
| + |
| // Code running in the threads started for pumping the results. |
| static void* ThreadFunction(void* data) { |
| - DNSServiceRef ref = reinterpret_cast<DNSServiceRef>(data); |
| - |
| - // Preocess results until an error occurs. |
| - DNSServiceErrorType result = kDNSServiceErr_NoError; |
| - while (result == kDNSServiceErr_NoError) { |
| - result = DNSServiceProcessResult(ref); |
| + Context* ctx = reinterpret_cast<Context*>(data); |
| + |
| + // Determint the end-time for responses to this request. Timeout from Dart |
| + // is in milliseconds. |
| + struct timeval time; |
| + if (gettimeofday(&time, NULL) == -1) { |
| + FreeContext(ctx); |
| + return NULL; |
| } |
| - // The expected error from deallocation the ref. |
| - if (result != kDNSServiceErr_BadReference) { |
| - fprintf(stderr, "Error from DNSServiceProcessResult: %d\n", result); |
| + int64_t end_time = timeval_to_usec(&time) + ctx->timeout * kMillisecsInUsecs; |
| + |
| + // Setup single file descriptor for select. |
| + int fd = DNSServiceRefSockFD(ctx->ref); |
| + fd_set readfds; |
| + FD_ZERO(&readfds); |
| + FD_SET(fd, &readfds); |
| + while (true) { |
| + struct timeval timeout; |
| + int64_t timeout_usec = end_time - timeval_to_usec(&time); |
| + if (timeout_usec <= 0) break; |
| + usec_to_timeval(timeout_usec, &timeout); |
| + int rc = select(fd + 1, &readfds, NULL, NULL, &timeout); |
| + if (rc == -1 && errno == EINTR) continue; |
| + |
| + // Terminate the loop if timeout or error. |
| + if (rc <= 0) break; |
| + |
| + // Process the result which is ready. |
| + DNSServiceErrorType result = kDNSServiceErr_NoError; |
| + result = DNSServiceProcessResult(ctx->ref); |
|
wibling
2015/11/10 14:44:56
NIT: Merge the two above lines into one.
Søren Gjesse
2015/11/10 15:30:05
Done.
|
| + if (result != kDNSServiceErr_NoError) break; |
| + |
| + // Prepare new timeout. |
| + if (gettimeofday(&time, NULL) == -1) break; |
| } |
| + FreeContext(ctx); |
| return NULL; |
| } |
| // Start a thread for receiving results from a specific DNSServiceRef. |
| -static int StartThread(DNSServiceRef ref) { |
| +static int StartThread(Context* ctx) { |
| pthread_attr_t attr; |
| int result = pthread_attr_init(&attr); |
| if (result != 0) Fatal("Error from pthread_attr_init"); |
| @@ -57,7 +106,7 @@ static int StartThread(DNSServiceRef ref) { |
| if (result != 0) Fatal("Error from pthread_attr_setdetachstate"); |
| pthread_t tid; |
| - result = pthread_create(&tid, &attr, ThreadFunction, ref); |
| + result = pthread_create(&tid, &attr, ThreadFunction, ctx); |
| if (result != 0) Fatal("Error from pthread_create"); |
| result = pthread_attr_destroy(&attr); |
| @@ -113,19 +162,14 @@ static void QueryRecordCallback(DNSServiceRef ref, |
| cobject_result.value.as_array.length = 4; |
| cobject_result.value.as_array.values = result_array; |
| Dart_PostCObject(ctx->port, &cobject_result); |
| - |
| - // Result received, free allocated data and stop lookup. |
| - free(ctx); |
| - DNSServiceRefDeallocate(ref); |
| } |
| // Lookup request from Dart. |
| -void HandleLookup(Dart_Port port_id, int type, char* fullname) { |
| +void HandleLookup(Dart_Port port_id, int type, char* fullname, int timeout) { |
| DNSServiceRef ref; |
| DNSServiceErrorType result; |
| struct Context* context = |
| reinterpret_cast<struct Context*>(malloc(sizeof(struct Context))); |
| - context->port = port_id; |
| result = DNSServiceQueryRecord(&ref, |
| 0, |
| 0, |
| @@ -137,10 +181,11 @@ void HandleLookup(Dart_Port port_id, int type, char* fullname) { |
| if (result != kDNSServiceErr_NoError) { |
| fprintf(stderr, "Error from DNSServiceQueryRecord: %d\n", result); |
| } else { |
| - // Start a thread for retreiving the results. |
| - // TODO(sgjesse): Add a timeout for killing the thread if there |
| - // are no responses. |
| - StartThread(ref); |
| + // Start a thread for pumping the results. |
| + context->ref = ref; |
| + context->timeout = timeout; |
| + context->port = port_id; |
| + StartThread(context); |
| } |
| } |