OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart 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 file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "bin/socket.h" | 5 #include "bin/socket.h" |
6 #include "bin/dartutils.h" | 6 #include "bin/dartutils.h" |
7 #include "bin/thread.h" | 7 #include "bin/thread.h" |
8 #include "bin/utils.h" | 8 #include "bin/utils.h" |
9 #include "platform/thread.h" | 9 #include "platform/thread.h" |
10 #include "platform/utils.h" | 10 #include "platform/utils.h" |
11 | 11 |
12 #include "include/dart_api.h" | 12 #include "include/dart_api.h" |
13 | 13 |
14 dart::Mutex Socket::mutex_; | 14 dart::Mutex Socket::mutex_; |
15 int Socket::service_ports_size_ = 0; | 15 int Socket::service_ports_size_ = 0; |
16 Dart_Port* Socket::service_ports_ = NULL; | 16 Dart_Port* Socket::service_ports_ = NULL; |
17 int Socket::service_ports_index_ = 0; | 17 int Socket::service_ports_index_ = 0; |
18 | 18 |
19 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { | 19 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { |
20 Dart_EnterScope(); | 20 Dart_EnterScope(); |
21 Dart_Handle socketobj = Dart_GetNativeArgument(args, 0); | 21 Dart_Handle socketobj = Dart_GetNativeArgument(args, 0); |
22 const char* host = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); | 22 const char* host = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); |
23 int64_t port = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); | 23 int64_t port = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); |
24 intptr_t socket = Socket::CreateConnect(host, port); | 24 intptr_t socket = Socket::CreateConnect(host, port); |
25 DartUtils::SetIntegerField(socketobj, DartUtils::kIdFieldName, socket); | 25 if (socket >= 0) { |
26 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); | 26 DartUtils::SetIntegerField(socketobj, DartUtils::kIdFieldName, socket); |
| 27 Dart_SetReturnValue(args, Dart_True()); |
| 28 } else { |
| 29 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 30 } |
27 Dart_ExitScope(); | 31 Dart_ExitScope(); |
28 } | 32 } |
29 | 33 |
30 | 34 |
31 void FUNCTION_NAME(Socket_Available)(Dart_NativeArguments args) { | 35 void FUNCTION_NAME(Socket_Available)(Dart_NativeArguments args) { |
32 Dart_EnterScope(); | 36 Dart_EnterScope(); |
33 int64_t socket = DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), | 37 int64_t socket = DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), |
34 DartUtils::kIdFieldName); | 38 DartUtils::kIdFieldName); |
35 intptr_t available = Socket::Available(socket); | 39 intptr_t available = Socket::Available(socket); |
36 Dart_SetReturnValue(args, Dart_NewInteger(available)); | 40 Dart_SetReturnValue(args, Dart_NewInteger(available)); |
(...skipping 27 matching lines...) Expand all Loading... |
64 intptr_t bytes_read = Socket::Read(socket, buffer, length); | 68 intptr_t bytes_read = Socket::Read(socket, buffer, length); |
65 if (bytes_read > 0) { | 69 if (bytes_read > 0) { |
66 Dart_Handle result = | 70 Dart_Handle result = |
67 Dart_ListSetAsBytes(buffer_obj, offset, buffer, bytes_read); | 71 Dart_ListSetAsBytes(buffer_obj, offset, buffer, bytes_read); |
68 if (Dart_IsError(result)) { | 72 if (Dart_IsError(result)) { |
69 delete[] buffer; | 73 delete[] buffer; |
70 Dart_PropagateError(result); | 74 Dart_PropagateError(result); |
71 } | 75 } |
72 } | 76 } |
73 delete[] buffer; | 77 delete[] buffer; |
74 Dart_SetReturnValue(args, Dart_NewInteger(bytes_read)); | 78 if (bytes_read >= 0) { |
| 79 Dart_SetReturnValue(args, Dart_NewInteger(bytes_read)); |
| 80 } else { |
| 81 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 82 } |
75 Dart_ExitScope(); | 83 Dart_ExitScope(); |
76 } | 84 } |
77 | 85 |
78 | 86 |
79 void FUNCTION_NAME(Socket_WriteList)(Dart_NativeArguments args) { | 87 void FUNCTION_NAME(Socket_WriteList)(Dart_NativeArguments args) { |
80 Dart_EnterScope(); | 88 Dart_EnterScope(); |
81 intptr_t socket = | 89 intptr_t socket = |
82 DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), | 90 DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), |
83 DartUtils::kIdFieldName); | 91 DartUtils::kIdFieldName); |
84 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | 92 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); |
(...skipping 28 matching lines...) Expand all Loading... |
113 chunk_length); | 121 chunk_length); |
114 if (Dart_IsError(result)) { | 122 if (Dart_IsError(result)) { |
115 delete[] buffer; | 123 delete[] buffer; |
116 Dart_PropagateError(result); | 124 Dart_PropagateError(result); |
117 } | 125 } |
118 bytes_written = | 126 bytes_written = |
119 Socket::Write(socket, reinterpret_cast<void*>(buffer), chunk_length); | 127 Socket::Write(socket, reinterpret_cast<void*>(buffer), chunk_length); |
120 total_bytes_written += bytes_written; | 128 total_bytes_written += bytes_written; |
121 } while (bytes_written > 0 && total_bytes_written < length); | 129 } while (bytes_written > 0 && total_bytes_written < length); |
122 delete[] buffer; | 130 delete[] buffer; |
123 Dart_SetReturnValue(args, Dart_NewInteger(total_bytes_written)); | 131 if (bytes_written >= 0) { |
| 132 Dart_SetReturnValue(args, Dart_NewInteger(total_bytes_written)); |
| 133 } else { |
| 134 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 135 } |
124 Dart_ExitScope(); | 136 Dart_ExitScope(); |
125 } | 137 } |
126 | 138 |
127 | 139 |
128 void FUNCTION_NAME(Socket_GetPort)(Dart_NativeArguments args) { | 140 void FUNCTION_NAME(Socket_GetPort)(Dart_NativeArguments args) { |
129 Dart_EnterScope(); | 141 Dart_EnterScope(); |
130 intptr_t socket = | 142 intptr_t socket = |
131 DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), | 143 DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), |
132 DartUtils::kIdFieldName); | 144 DartUtils::kIdFieldName); |
| 145 OSError os_error; |
133 intptr_t port = Socket::GetPort(socket); | 146 intptr_t port = Socket::GetPort(socket); |
134 Dart_SetReturnValue(args, Dart_NewInteger(port)); | 147 if (port > 0) { |
| 148 Dart_SetReturnValue(args, Dart_NewInteger(port)); |
| 149 } else { |
| 150 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 151 } |
135 Dart_ExitScope(); | 152 Dart_ExitScope(); |
136 } | 153 } |
137 | 154 |
| 155 |
| 156 void FUNCTION_NAME(Socket_GetError)(Dart_NativeArguments args) { |
| 157 Dart_EnterScope(); |
| 158 intptr_t socket = |
| 159 DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), |
| 160 DartUtils::kIdFieldName); |
| 161 OSError os_error; |
| 162 Socket::GetError(socket, &os_error); |
| 163 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
| 164 Dart_ExitScope(); |
| 165 } |
| 166 |
138 | 167 |
139 void FUNCTION_NAME(Socket_GetStdioHandle)(Dart_NativeArguments args) { | 168 void FUNCTION_NAME(Socket_GetStdioHandle)(Dart_NativeArguments args) { |
140 Dart_EnterScope(); | 169 Dart_EnterScope(); |
141 Dart_Handle socketobj = Dart_GetNativeArgument(args, 0); | 170 Dart_Handle socketobj = Dart_GetNativeArgument(args, 0); |
142 intptr_t num = | 171 intptr_t num = |
143 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); | 172 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); |
144 ASSERT(num == 0 || num == 1 || num == 2); | 173 ASSERT(num == 0 || num == 1 || num == 2); |
145 intptr_t socket = Socket::GetStdioHandle(num); | 174 intptr_t socket = Socket::GetStdioHandle(num); |
146 DartUtils::SetIntegerField( | 175 DartUtils::SetIntegerField( |
147 socketobj, DartUtils::kIdFieldName, socket); | 176 socketobj, DartUtils::kIdFieldName, socket); |
148 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); | 177 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); |
149 Dart_ExitScope(); | 178 Dart_ExitScope(); |
150 } | 179 } |
151 | 180 |
152 | 181 |
153 void FUNCTION_NAME(ServerSocket_CreateBindListen)(Dart_NativeArguments args) { | 182 void FUNCTION_NAME(ServerSocket_CreateBindListen)(Dart_NativeArguments args) { |
154 Dart_EnterScope(); | 183 Dart_EnterScope(); |
155 Dart_Handle socketobj = Dart_GetNativeArgument(args, 0); | 184 Dart_Handle socketobj = Dart_GetNativeArgument(args, 0); |
156 const char* bindAddress = | 185 const char* bindAddress = |
157 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); | 186 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); |
158 intptr_t port = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); | 187 intptr_t port = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); |
159 intptr_t backlog = | 188 intptr_t backlog = |
160 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); | 189 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); |
161 intptr_t socket = | 190 intptr_t socket = |
162 ServerSocket::CreateBindListen(bindAddress, port, backlog); | 191 ServerSocket::CreateBindListen(bindAddress, port, backlog); |
163 DartUtils::SetIntegerField( | 192 if (socket >= 0) { |
164 socketobj, DartUtils::kIdFieldName, socket); | 193 DartUtils::SetIntegerField( |
165 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); | 194 socketobj, DartUtils::kIdFieldName, socket); |
| 195 Dart_SetReturnValue(args, Dart_True()); |
| 196 } else { |
| 197 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 198 } |
166 Dart_ExitScope(); | 199 Dart_ExitScope(); |
167 } | 200 } |
168 | 201 |
169 | 202 |
170 void FUNCTION_NAME(ServerSocket_Accept)(Dart_NativeArguments args) { | 203 void FUNCTION_NAME(ServerSocket_Accept)(Dart_NativeArguments args) { |
171 Dart_EnterScope(); | 204 Dart_EnterScope(); |
172 intptr_t socket = | 205 intptr_t socket = |
173 DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), | 206 DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), |
174 DartUtils::kIdFieldName); | 207 DartUtils::kIdFieldName); |
175 Dart_Handle socketobj = Dart_GetNativeArgument(args, 1); | 208 Dart_Handle socketobj = Dart_GetNativeArgument(args, 1); |
176 intptr_t newSocket = ServerSocket::Accept(socket); | 209 intptr_t newSocket = ServerSocket::Accept(socket); |
177 if (newSocket >= 0) { | 210 if (newSocket >= 0) { |
178 DartUtils::SetIntegerField( | 211 DartUtils::SetIntegerField( |
179 socketobj, DartUtils::kIdFieldName, newSocket); | 212 socketobj, DartUtils::kIdFieldName, newSocket); |
| 213 Dart_SetReturnValue(args, Dart_True()); |
| 214 } else { |
| 215 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
180 } | 216 } |
181 Dart_SetReturnValue(args, Dart_NewBoolean(newSocket >= 0)); | |
182 Dart_ExitScope(); | 217 Dart_ExitScope(); |
183 } | 218 } |
184 | 219 |
185 | 220 |
186 static CObject* LookupRequest(const CObjectArray& request) { | 221 static CObject* LookupRequest(const CObjectArray& request) { |
187 if (request.Length() == 2 && request[1]->IsString()) { | 222 if (request.Length() == 2 && request[1]->IsString()) { |
188 CObjectString host(request[1]); | 223 CObjectString host(request[1]); |
189 CObject* result = NULL; | 224 CObject* result = NULL; |
190 OSError* os_error = NULL; | 225 OSError* os_error = NULL; |
191 const char* ip_address = | 226 const char* ip_address = |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 Dart_EnterScope(); | 289 Dart_EnterScope(); |
255 Dart_SetReturnValue(args, Dart_Null()); | 290 Dart_SetReturnValue(args, Dart_Null()); |
256 Dart_Port service_port = Socket::GetServicePort(); | 291 Dart_Port service_port = Socket::GetServicePort(); |
257 if (service_port != kIllegalPort) { | 292 if (service_port != kIllegalPort) { |
258 // Return a send port for the service port. | 293 // Return a send port for the service port. |
259 Dart_Handle send_port = Dart_NewSendPort(service_port); | 294 Dart_Handle send_port = Dart_NewSendPort(service_port); |
260 Dart_SetReturnValue(args, send_port); | 295 Dart_SetReturnValue(args, send_port); |
261 } | 296 } |
262 Dart_ExitScope(); | 297 Dart_ExitScope(); |
263 } | 298 } |
OLD | NEW |