| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2015, the Fletch project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE.md file. | |
| 4 | |
| 5 #include <assert.h> | |
| 6 #include <stdio.h> | |
| 7 #include <stdlib.h> | |
| 8 #include <string.h> | |
| 9 | |
| 10 #include "include/dart_api.h" | |
| 11 #include "include/dart_native_api.h" | |
| 12 | |
| 13 #include "mdns_extension.h" | |
| 14 | |
| 15 // Handle request send on the service port. | |
| 16 void HandleRequest(Dart_Port port_id, Dart_CObject* message) { | |
| 17 // Messages are expected to be three item lists: | |
| 18 // [0]: reply port | |
| 19 // [1]: request type | |
| 20 // [2]: request argument | |
| 21 if (message->type == Dart_CObject_kArray && | |
| 22 message->value.as_array.length == 3) { | |
| 23 Dart_CObject* reply_port = message->value.as_array.values[0]; | |
| 24 Dart_CObject* request_type = message->value.as_array.values[1]; | |
| 25 Dart_CObject* argument = message->value.as_array.values[2]; | |
| 26 if (reply_port->type == Dart_CObject_kSendPort) { | |
| 27 if (request_type->type == Dart_CObject_kInt32) { | |
| 28 switch (request_type->value.as_int32) { | |
| 29 case kEchoRequest: | |
| 30 HandleEcho(reply_port->value.as_send_port.id, argument); | |
| 31 return; | |
| 32 | |
| 33 case kLookupRequest: | |
| 34 HandleLookup(reply_port->value.as_send_port.id, | |
| 35 argument->value.as_string); | |
| 36 return; | |
| 37 | |
| 38 default: | |
| 39 break; | |
| 40 // Ignore invalid requests. | |
| 41 } | |
| 42 } | |
| 43 Dart_CObject result; | |
| 44 result.type = Dart_CObject_kNull; | |
| 45 Dart_PostCObject(reply_port->value.as_send_port.id, &result); | |
| 46 } | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 // Handler for the echo request. Used for testing that the native extension | |
| 51 // can be loaded and called. | |
| 52 void HandleEcho(Dart_Port reply_port, Dart_CObject* argument) { | |
| 53 Dart_PostCObject(reply_port, argument); | |
| 54 } | |
| 55 | |
| 56 | |
| 57 Dart_Handle HandleError(Dart_Handle handle) { | |
| 58 if (Dart_IsError(handle)) Dart_PropagateError(handle); | |
| 59 return handle; | |
| 60 } | |
| 61 | |
| 62 | |
| 63 // Initialize a native port with a request handler. | |
| 64 void ServicePort(Dart_NativeArguments arguments) { | |
| 65 Dart_SetReturnValue(arguments, Dart_Null()); | |
| 66 Dart_Port service_port = | |
| 67 Dart_NewNativePort("MDnsService", HandleRequest, true); | |
| 68 if (service_port != ILLEGAL_PORT) { | |
| 69 Dart_Handle send_port = Dart_NewSendPort(service_port); | |
| 70 Dart_SetReturnValue(arguments, send_port); | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 | |
| 75 // Resolver for the extension library. | |
| 76 Dart_NativeFunction ResolveName(Dart_Handle name, | |
| 77 int argc, | |
| 78 bool* auto_setup_scope) { | |
| 79 const char* c_name; | |
| 80 Dart_Handle check_error; | |
| 81 | |
| 82 check_error = Dart_StringToCString(name, &c_name); | |
| 83 if (Dart_IsError(check_error)) { | |
| 84 Dart_PropagateError(check_error); | |
| 85 } | |
| 86 if ((strcmp("MDnsExtension_ServicePort", c_name) == 0) && (argc == 0)) { | |
| 87 return ServicePort; | |
| 88 } | |
| 89 return NULL; | |
| 90 } | |
| 91 | |
| 92 | |
| 93 // Entry point for the extension library. | |
| 94 DART_EXPORT Dart_Handle mdns_extension_lib_Init(Dart_Handle parent_library) { | |
| 95 Dart_Handle result_code; | |
| 96 if (Dart_IsError(parent_library)) { | |
| 97 return parent_library; | |
| 98 } | |
| 99 | |
| 100 result_code = Dart_SetNativeResolver(parent_library, ResolveName, NULL); | |
| 101 if (Dart_IsError(result_code)) { | |
| 102 return result_code; | |
| 103 } | |
| 104 | |
| 105 return parent_library; | |
| 106 } | |
| OLD | NEW |