| Index: src/pkg/mdns/mdns_extension.cc
|
| diff --git a/src/pkg/mdns/mdns_extension.cc b/src/pkg/mdns/mdns_extension.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..72a70a33984551e55960321ec2791b90a81b7b2a
|
| --- /dev/null
|
| +++ b/src/pkg/mdns/mdns_extension.cc
|
| @@ -0,0 +1,106 @@
|
| +// Copyright (c) 2015, the Fletch project authors. Please see the AUTHORS file
|
| +// for details. All rights reserved. Use of this source code is governed by a
|
| +// BSD-style license that can be found in the LICENSE.md file.
|
| +
|
| +#include <assert.h>
|
| +#include <stdio.h>
|
| +#include <stdlib.h>
|
| +#include <string.h>
|
| +
|
| +#include "include/dart_api.h"
|
| +#include "include/dart_native_api.h"
|
| +
|
| +#include "mdns_extension.h"
|
| +
|
| +// Handle request send on the service port.
|
| +void HandleRequest(Dart_Port port_id, Dart_CObject* message) {
|
| + // Messages are expected to be three item lists:
|
| + // [0]: reply port
|
| + // [1]: request type
|
| + // [2]: request argument
|
| + if (message->type == Dart_CObject_kArray &&
|
| + message->value.as_array.length == 3) {
|
| + Dart_CObject* reply_port = message->value.as_array.values[0];
|
| + Dart_CObject* request_type = message->value.as_array.values[1];
|
| + Dart_CObject* argument = message->value.as_array.values[2];
|
| + if (reply_port->type == Dart_CObject_kSendPort) {
|
| + if (request_type->type == Dart_CObject_kInt32) {
|
| + switch (request_type->value.as_int32) {
|
| + case kEchoRequest:
|
| + HandleEcho(reply_port->value.as_send_port.id, argument);
|
| + return;
|
| +
|
| + case kLookupRequest:
|
| + HandleLookup(reply_port->value.as_send_port.id,
|
| + argument->value.as_string);
|
| + return;
|
| +
|
| + default:
|
| + break;
|
| + // Ignore invalid requests.
|
| + }
|
| + }
|
| + Dart_CObject result;
|
| + result.type = Dart_CObject_kNull;
|
| + Dart_PostCObject(reply_port->value.as_send_port.id, &result);
|
| + }
|
| + }
|
| +}
|
| +
|
| +// Handler for the echo request. Used for testing that the native extension
|
| +// can be loaded and called.
|
| +void HandleEcho(Dart_Port reply_port, Dart_CObject* argument) {
|
| + Dart_PostCObject(reply_port, argument);
|
| +}
|
| +
|
| +
|
| +Dart_Handle HandleError(Dart_Handle handle) {
|
| + if (Dart_IsError(handle)) Dart_PropagateError(handle);
|
| + return handle;
|
| +}
|
| +
|
| +
|
| +// Initialize a native port with a request handler.
|
| +void ServicePort(Dart_NativeArguments arguments) {
|
| + Dart_SetReturnValue(arguments, Dart_Null());
|
| + Dart_Port service_port =
|
| + Dart_NewNativePort("MDnsService", HandleRequest, true);
|
| + if (service_port != ILLEGAL_PORT) {
|
| + Dart_Handle send_port = Dart_NewSendPort(service_port);
|
| + Dart_SetReturnValue(arguments, send_port);
|
| + }
|
| +}
|
| +
|
| +
|
| +// Resolver for the extension library.
|
| +Dart_NativeFunction ResolveName(Dart_Handle name,
|
| + int argc,
|
| + bool* auto_setup_scope) {
|
| + const char* c_name;
|
| + Dart_Handle check_error;
|
| +
|
| + check_error = Dart_StringToCString(name, &c_name);
|
| + if (Dart_IsError(check_error)) {
|
| + Dart_PropagateError(check_error);
|
| + }
|
| + if ((strcmp("MDnsExtension_ServicePort", c_name) == 0) && (argc == 0)) {
|
| + return ServicePort;
|
| + }
|
| + return NULL;
|
| +}
|
| +
|
| +
|
| +// Entry point for the extension library.
|
| +DART_EXPORT Dart_Handle mdns_extension_lib_Init(Dart_Handle parent_library) {
|
| + Dart_Handle result_code;
|
| + if (Dart_IsError(parent_library)) {
|
| + return parent_library;
|
| + }
|
| +
|
| + result_code = Dart_SetNativeResolver(parent_library, ResolveName, NULL);
|
| + if (Dart_IsError(result_code)) {
|
| + return result_code;
|
| + }
|
| +
|
| + return parent_library;
|
| +}
|
|
|