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 == 5) { |
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 Dart_CObject* timeout = message->value.as_array.values[4]; |
| 40 if (type->type == Dart_CObject_kInt32 && |
| 41 name->type == Dart_CObject_kString && |
| 42 timeout->type == Dart_CObject_kInt32) { |
| 43 HandleLookup(reply_port->value.as_send_port.id, |
| 44 type->value.as_int32, |
| 45 name->value.as_string, |
| 46 timeout->value.as_int32); |
| 47 } |
36 return; | 48 return; |
| 49 } |
37 | 50 |
38 default: | 51 default: |
39 break; | 52 break; |
40 // Ignore invalid requests. | 53 // Ignore invalid requests. |
41 } | |
42 } | 54 } |
43 Dart_CObject result; | |
44 result.type = Dart_CObject_kNull; | |
45 Dart_PostCObject(reply_port->value.as_send_port.id, &result); | |
46 } | 55 } |
| 56 Dart_CObject result; |
| 57 result.type = Dart_CObject_kNull; |
| 58 Dart_PostCObject(reply_port->value.as_send_port.id, &result); |
47 } | 59 } |
48 } | 60 } |
49 | 61 |
50 // Handler for the echo request. Used for testing that the native extension | 62 // Handler for the echo request. Used for testing that the native extension |
51 // can be loaded and called. | 63 // can be loaded and called. |
52 void HandleEcho(Dart_Port reply_port, Dart_CObject* argument) { | 64 void HandleEcho(Dart_Port reply_port, Dart_CObject* argument) { |
53 Dart_PostCObject(reply_port, argument); | 65 Dart_PostCObject(reply_port, argument); |
54 } | 66 } |
55 | 67 |
56 | 68 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 return parent_library; | 109 return parent_library; |
98 } | 110 } |
99 | 111 |
100 result_code = Dart_SetNativeResolver(parent_library, ResolveName, NULL); | 112 result_code = Dart_SetNativeResolver(parent_library, ResolveName, NULL); |
101 if (Dart_IsError(result_code)) { | 113 if (Dart_IsError(result_code)) { |
102 return result_code; | 114 return result_code; |
103 } | 115 } |
104 | 116 |
105 return parent_library; | 117 return parent_library; |
106 } | 118 } |
OLD | NEW |