OLD | NEW |
1 // Copyright (c) 2015, the Fletch project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE.md file. |
4 | 4 |
5 #include <assert.h> | 5 #include <assert.h> |
6 #include <stdio.h> | 6 #include <stdio.h> |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include "include/dart_api.h" | 10 #include "include/dart_api.h" |
11 #include "include/dart_native_api.h" | 11 #include "include/dart_native_api.h" |
12 | 12 |
13 #include "mdns_extension.h" | 13 #include "mdns_extension.h" |
14 | 14 |
15 // Handle request send on the service port. | 15 // Handle request send on the service port. |
16 void HandleRequest(Dart_Port port_id, Dart_CObject* message) { | 16 void HandleRequest(Dart_Port port_id, Dart_CObject* message) { |
17 // Messages are expected to be three item lists: | 17 // Messages are expected to be three item lists: |
18 // [0]: reply port | 18 // [0]: reply port |
19 // [1]: request type | 19 // [1]: request type |
20 // [2]: request argument | 20 // [2]: request argument |
21 if (message->type == Dart_CObject_kArray && | 21 if (message->type == Dart_CObject_kArray && |
22 message->value.as_array.length == 3) { | 22 message->value.as_array.length > 2) { |
23 Dart_CObject* reply_port = message->value.as_array.values[0]; | 23 Dart_CObject* reply_port = message->value.as_array.values[0]; |
24 Dart_CObject* request_type = message->value.as_array.values[1]; | 24 Dart_CObject* request_type = message->value.as_array.values[1]; |
25 Dart_CObject* argument = message->value.as_array.values[2]; | 25 if (reply_port->type == Dart_CObject_kSendPort && |
26 if (reply_port->type == Dart_CObject_kSendPort) { | 26 request_type->type == Dart_CObject_kInt32) { |
27 if (request_type->type == Dart_CObject_kInt32) { | 27 switch (request_type->value.as_int32) { |
28 switch (request_type->value.as_int32) { | 28 case kEchoRequest: |
29 case kEchoRequest: | 29 if (message->value.as_array.length == 3) { |
| 30 Dart_CObject* argument = message->value.as_array.values[2]; |
30 HandleEcho(reply_port->value.as_send_port.id, argument); | 31 HandleEcho(reply_port->value.as_send_port.id, argument); |
31 return; | 32 return; |
| 33 } |
32 | 34 |
33 case kLookupRequest: | 35 case kLookupRequest: |
34 HandleLookup(reply_port->value.as_send_port.id, | 36 if (message->value.as_array.length == 4) { |
35 argument->value.as_string); | 37 Dart_CObject* type = message->value.as_array.values[2]; |
| 38 Dart_CObject* name = message->value.as_array.values[3]; |
| 39 if (type->type == Dart_CObject_kInt32 && |
| 40 name->type == Dart_CObject_kString) { |
| 41 HandleLookup(reply_port->value.as_send_port.id, |
| 42 type->value.as_int32, |
| 43 name->value.as_string); |
| 44 } |
36 return; | 45 return; |
| 46 } |
37 | 47 |
38 default: | 48 default: |
39 break; | 49 break; |
40 // Ignore invalid requests. | 50 // Ignore invalid requests. |
41 } | |
42 } | 51 } |
43 Dart_CObject result; | |
44 result.type = Dart_CObject_kNull; | |
45 Dart_PostCObject(reply_port->value.as_send_port.id, &result); | |
46 } | 52 } |
| 53 Dart_CObject result; |
| 54 result.type = Dart_CObject_kNull; |
| 55 Dart_PostCObject(reply_port->value.as_send_port.id, &result); |
47 } | 56 } |
48 } | 57 } |
49 | 58 |
50 // Handler for the echo request. Used for testing that the native extension | 59 // Handler for the echo request. Used for testing that the native extension |
51 // can be loaded and called. | 60 // can be loaded and called. |
52 void HandleEcho(Dart_Port reply_port, Dart_CObject* argument) { | 61 void HandleEcho(Dart_Port reply_port, Dart_CObject* argument) { |
53 Dart_PostCObject(reply_port, argument); | 62 Dart_PostCObject(reply_port, argument); |
54 } | 63 } |
55 | 64 |
56 | 65 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 return parent_library; | 106 return parent_library; |
98 } | 107 } |
99 | 108 |
100 result_code = Dart_SetNativeResolver(parent_library, ResolveName, NULL); | 109 result_code = Dart_SetNativeResolver(parent_library, ResolveName, NULL); |
101 if (Dart_IsError(result_code)) { | 110 if (Dart_IsError(result_code)) { |
102 return result_code; | 111 return result_code; |
103 } | 112 } |
104 | 113 |
105 return parent_library; | 114 return parent_library; |
106 } | 115 } |
OLD | NEW |