Chromium Code Reviews| 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 | 9 |
| 10 #include "platform/globals.h" | 10 #include "platform/globals.h" |
| 11 #include "platform/thread.h" | 11 #include "platform/thread.h" |
| 12 #include "platform/utils.h" | 12 #include "platform/utils.h" |
| 13 | 13 |
| 14 #include "include/dart_api.h" | 14 #include "include/dart_api.h" |
| 15 | 15 |
| 16 dart::Mutex Socket::mutex_; | 16 dart::Mutex Socket::mutex_; |
| 17 int Socket::service_ports_size_ = 0; | 17 int Socket::service_ports_size_ = 0; |
| 18 Dart_Port* Socket::service_ports_ = NULL; | 18 Dart_Port* Socket::service_ports_ = NULL; |
| 19 int Socket::service_ports_index_ = 0; | 19 int Socket::service_ports_index_ = 0; |
| 20 | 20 |
| 21 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { | 21 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { |
| 22 Dart_EnterScope(); | 22 Dart_EnterScope(); |
| 23 Dart_Handle socketobj = Dart_GetNativeArgument(args, 0); | 23 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); |
| 24 const char* host = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); | 24 const char* host = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); |
| 25 int64_t port = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); | 25 int64_t port = 0; |
| 26 intptr_t socket = Socket::CreateConnect(host, port); | 26 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 2), &port)) { |
| 27 if (socket >= 0) { | 27 intptr_t socket = Socket::CreateConnect(host, port); |
| 28 DartUtils::SetIntegerField(socketobj, DartUtils::kIdFieldName, socket); | 28 if (socket >= 0) { |
| 29 Dart_SetReturnValue(args, Dart_True()); | 29 DartUtils::SetIntegerField(socket_obj, DartUtils::kIdFieldName, socket); |
| 30 Dart_SetReturnValue(args, Dart_True()); | |
| 31 } else { | |
| 32 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | |
| 33 } | |
| 30 } else { | 34 } else { |
| 31 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 35 OSError os_error(-1, "Invalid argument", OSError::kUnknown); |
| 36 Dart_Handle err = DartUtils::NewDartOSError(&os_error); | |
| 37 if (Dart_IsError(err)) Dart_PropagateError(err); | |
| 38 Dart_SetReturnValue(args, err); | |
| 32 } | 39 } |
| 33 Dart_ExitScope(); | 40 Dart_ExitScope(); |
| 34 } | 41 } |
| 35 | 42 |
| 36 | 43 |
| 37 void FUNCTION_NAME(Socket_Available)(Dart_NativeArguments args) { | 44 void FUNCTION_NAME(Socket_Available)(Dart_NativeArguments args) { |
| 38 Dart_EnterScope(); | 45 Dart_EnterScope(); |
| 39 int64_t socket = DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), | 46 int64_t socket = DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), |
| 40 DartUtils::kIdFieldName); | 47 DartUtils::kIdFieldName); |
| 41 intptr_t available = Socket::Available(socket); | 48 intptr_t available = Socket::Available(socket); |
| 42 if (available >= 0) { | 49 if (available >= 0) { |
| 43 Dart_SetReturnValue(args, Dart_NewInteger(available)); | 50 Dart_SetReturnValue(args, Dart_NewInteger(available)); |
| 44 } else { | 51 } else { |
| 45 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 52 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 46 } | 53 } |
| 47 Dart_ExitScope(); | 54 Dart_ExitScope(); |
| 48 } | 55 } |
| 49 | 56 |
| 50 | 57 |
| 51 void FUNCTION_NAME(Socket_ReadList)(Dart_NativeArguments args) { | 58 void FUNCTION_NAME(Socket_ReadList)(Dart_NativeArguments args) { |
| 52 Dart_EnterScope(); | 59 Dart_EnterScope(); |
| 53 intptr_t socket = | 60 intptr_t socket = |
| 54 DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), | 61 DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), |
| 55 DartUtils::kIdFieldName); | 62 DartUtils::kIdFieldName); |
| 56 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | 63 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); |
| 57 ASSERT(Dart_IsList(buffer_obj)); | 64 int64_t offset = 0; |
| 58 intptr_t offset = | 65 int64_t length = 0; |
| 59 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); | 66 Dart_Handle offset_obj = Dart_GetNativeArgument(args, 2); |
| 60 intptr_t length = | 67 Dart_Handle length_obj = Dart_GetNativeArgument(args, 3); |
| 61 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); | 68 if (Dart_IsList(buffer_obj) && |
| 62 intptr_t buffer_len = 0; | 69 DartUtils::GetInt64Value(offset_obj, &offset) && |
| 63 Dart_Handle result = Dart_ListLength(buffer_obj, &buffer_len); | 70 DartUtils::GetInt64Value(length_obj, &length)) { |
| 64 if (Dart_IsError(result)) { | 71 intptr_t buffer_len = 0; |
| 65 Dart_PropagateError(result); | 72 Dart_Handle result = Dart_ListLength(buffer_obj, &buffer_len); |
| 66 } | 73 if (Dart_IsError(result)) { |
| 67 ASSERT((offset + length) <= buffer_len); | 74 Dart_PropagateError(result); |
| 68 | 75 } |
| 69 if (Dart_IsVMFlagSet("short_socket_read")) { | 76 ASSERT((offset + length) <= buffer_len); |
| 70 length = (length + 1) / 2; | 77 if (Dart_IsVMFlagSet("short_socket_read")) { |
| 78 length = (length + 1) / 2; | |
| 79 } | |
| 80 uint8_t* buffer = new uint8_t[length]; | |
| 81 intptr_t bytes_read = Socket::Read(socket, buffer, length); | |
| 82 if (bytes_read > 0) { | |
| 83 Dart_Handle result = | |
| 84 Dart_ListSetAsBytes(buffer_obj, offset, buffer, bytes_read); | |
| 85 if (Dart_IsError(result)) { | |
| 86 delete[] buffer; | |
| 87 Dart_PropagateError(result); | |
| 88 } | |
| 89 } | |
| 90 delete[] buffer; | |
| 91 if (bytes_read >= 0) { | |
| 92 Dart_SetReturnValue(args, Dart_NewInteger(bytes_read)); | |
| 93 } else { | |
| 94 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | |
| 95 } | |
| 96 } else { | |
| 97 OSError os_error(-1, "Invalid argument", OSError::kUnknown); | |
| 98 Dart_Handle err = DartUtils::NewDartOSError(&os_error); | |
| 99 if (Dart_IsError(err)) Dart_PropagateError(err); | |
| 100 Dart_SetReturnValue(args, err); | |
| 71 } | 101 } |
| 72 | 102 |
| 73 uint8_t* buffer = new uint8_t[length]; | |
| 74 intptr_t bytes_read = Socket::Read(socket, buffer, length); | |
| 75 if (bytes_read > 0) { | |
| 76 Dart_Handle result = | |
| 77 Dart_ListSetAsBytes(buffer_obj, offset, buffer, bytes_read); | |
| 78 if (Dart_IsError(result)) { | |
| 79 delete[] buffer; | |
| 80 Dart_PropagateError(result); | |
| 81 } | |
| 82 } | |
| 83 delete[] buffer; | |
| 84 if (bytes_read >= 0) { | |
| 85 Dart_SetReturnValue(args, Dart_NewInteger(bytes_read)); | |
| 86 } else { | |
| 87 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | |
| 88 } | |
| 89 Dart_ExitScope(); | 103 Dart_ExitScope(); |
| 90 } | 104 } |
| 91 | 105 |
| 92 | 106 |
| 93 void FUNCTION_NAME(Socket_WriteList)(Dart_NativeArguments args) { | 107 void FUNCTION_NAME(Socket_WriteList)(Dart_NativeArguments args) { |
| 94 Dart_EnterScope(); | 108 Dart_EnterScope(); |
| 95 intptr_t socket = | 109 intptr_t socket = |
| 96 DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), | 110 DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), |
| 97 DartUtils::kIdFieldName); | 111 DartUtils::kIdFieldName); |
| 98 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | 112 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 186 DartUtils::kIdFieldName); | 200 DartUtils::kIdFieldName); |
| 187 OSError os_error; | 201 OSError os_error; |
| 188 Socket::GetError(socket, &os_error); | 202 Socket::GetError(socket, &os_error); |
| 189 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | 203 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
| 190 Dart_ExitScope(); | 204 Dart_ExitScope(); |
| 191 } | 205 } |
| 192 | 206 |
| 193 | 207 |
| 194 void FUNCTION_NAME(Socket_GetStdioHandle)(Dart_NativeArguments args) { | 208 void FUNCTION_NAME(Socket_GetStdioHandle)(Dart_NativeArguments args) { |
| 195 Dart_EnterScope(); | 209 Dart_EnterScope(); |
| 196 Dart_Handle socketobj = Dart_GetNativeArgument(args, 0); | 210 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); |
| 197 intptr_t num = | 211 intptr_t num = |
| 198 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); | 212 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); |
| 199 ASSERT(num == 0 || num == 1 || num == 2); | 213 ASSERT(num == 0 || num == 1 || num == 2); |
| 200 intptr_t socket = Socket::GetStdioHandle(num); | 214 intptr_t socket = Socket::GetStdioHandle(num); |
| 201 DartUtils::SetIntegerField( | 215 DartUtils::SetIntegerField( |
| 202 socketobj, DartUtils::kIdFieldName, socket); | 216 socket_obj, DartUtils::kIdFieldName, socket); |
| 203 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); | 217 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); |
| 204 Dart_ExitScope(); | 218 Dart_ExitScope(); |
| 205 } | 219 } |
| 206 | 220 |
| 207 | 221 |
| 208 void FUNCTION_NAME(ServerSocket_CreateBindListen)(Dart_NativeArguments args) { | 222 void FUNCTION_NAME(ServerSocket_CreateBindListen)(Dart_NativeArguments args) { |
| 209 Dart_EnterScope(); | 223 Dart_EnterScope(); |
| 210 Dart_Handle socketobj = Dart_GetNativeArgument(args, 0); | 224 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); |
| 211 const char* bindAddress = | 225 Dart_Handle bind_address_obj = Dart_GetNativeArgument(args, 1); |
| 212 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); | 226 Dart_Handle port_obj = Dart_GetNativeArgument(args, 2); |
| 213 intptr_t port = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); | 227 Dart_Handle backlog_obj = Dart_GetNativeArgument(args, 3); |
| 214 intptr_t backlog = | 228 int64_t port = 0; |
| 215 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); | 229 int64_t backlog = 0; |
| 216 intptr_t socket = | 230 if (Dart_IsString(bind_address_obj) && |
| 217 ServerSocket::CreateBindListen(bindAddress, port, backlog); | 231 DartUtils::GetInt64Value(port_obj, &port) && |
| 218 if (socket >= 0) { | 232 DartUtils::GetInt64Value(backlog_obj, &backlog)) { |
| 219 DartUtils::SetIntegerField( | 233 const char* bind_address = DartUtils::GetStringValue(bind_address_obj); |
|
Søren Gjesse
2012/05/30 07:07:15
We could consider having GetStringValue work like
Mads Ager (google)
2012/05/30 07:35:12
Yes, that could be done. It makes sense to have tw
| |
| 220 socketobj, DartUtils::kIdFieldName, socket); | 234 intptr_t socket = |
| 221 Dart_SetReturnValue(args, Dart_True()); | 235 ServerSocket::CreateBindListen(bind_address, port, backlog); |
| 236 if (socket >= 0) { | |
| 237 DartUtils::SetIntegerField( | |
| 238 socket_obj, DartUtils::kIdFieldName, socket); | |
| 239 Dart_SetReturnValue(args, Dart_True()); | |
| 240 } else { | |
| 241 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | |
| 242 } | |
| 222 } else { | 243 } else { |
| 223 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 244 OSError os_error(-1, "Invalid argument", OSError::kUnknown); |
| 245 Dart_Handle err = DartUtils::NewDartOSError(&os_error); | |
| 246 if (Dart_IsError(err)) Dart_PropagateError(err); | |
| 247 Dart_SetReturnValue(args, err); | |
| 224 } | 248 } |
| 225 Dart_ExitScope(); | 249 Dart_ExitScope(); |
| 226 } | 250 } |
| 227 | 251 |
| 228 | 252 |
| 229 void FUNCTION_NAME(ServerSocket_Accept)(Dart_NativeArguments args) { | 253 void FUNCTION_NAME(ServerSocket_Accept)(Dart_NativeArguments args) { |
| 230 Dart_EnterScope(); | 254 Dart_EnterScope(); |
| 231 intptr_t socket = | 255 intptr_t socket = |
| 232 DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), | 256 DartUtils::GetIntegerField(Dart_GetNativeArgument(args, 0), |
| 233 DartUtils::kIdFieldName); | 257 DartUtils::kIdFieldName); |
| 234 Dart_Handle socketobj = Dart_GetNativeArgument(args, 1); | 258 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 1); |
| 235 intptr_t newSocket = ServerSocket::Accept(socket); | 259 intptr_t new_socket = ServerSocket::Accept(socket); |
| 236 if (newSocket >= 0) { | 260 if (new_socket >= 0) { |
| 237 DartUtils::SetIntegerField( | 261 DartUtils::SetIntegerField( |
| 238 socketobj, DartUtils::kIdFieldName, newSocket); | 262 socket_obj, DartUtils::kIdFieldName, new_socket); |
| 239 Dart_SetReturnValue(args, Dart_True()); | 263 Dart_SetReturnValue(args, Dart_True()); |
| 240 } else if (newSocket == ServerSocket::kTemporaryFailure) { | 264 } else if (new_socket == ServerSocket::kTemporaryFailure) { |
| 241 Dart_SetReturnValue(args, Dart_False()); | 265 Dart_SetReturnValue(args, Dart_False()); |
| 242 } else { | 266 } else { |
| 243 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 267 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 244 } | 268 } |
| 245 Dart_ExitScope(); | 269 Dart_ExitScope(); |
| 246 } | 270 } |
| 247 | 271 |
| 248 | 272 |
| 249 static CObject* LookupRequest(const CObjectArray& request) { | 273 static CObject* LookupRequest(const CObjectArray& request) { |
| 250 if (request.Length() == 2 && request[1]->IsString()) { | 274 if (request.Length() == 2 && request[1]->IsString()) { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 266 } | 290 } |
| 267 | 291 |
| 268 | 292 |
| 269 void SocketService(Dart_Port dest_port_id, | 293 void SocketService(Dart_Port dest_port_id, |
| 270 Dart_Port reply_port_id, | 294 Dart_Port reply_port_id, |
| 271 Dart_CObject* message) { | 295 Dart_CObject* message) { |
| 272 CObject* response = CObject::False(); | 296 CObject* response = CObject::False(); |
| 273 CObjectArray request(message); | 297 CObjectArray request(message); |
| 274 if (message->type == Dart_CObject::kArray) { | 298 if (message->type == Dart_CObject::kArray) { |
| 275 if (request.Length() > 1 && request[0]->IsInt32()) { | 299 if (request.Length() > 1 && request[0]->IsInt32()) { |
| 276 CObjectInt32 requestType(request[0]); | 300 CObjectInt32 request_type(request[0]); |
| 277 switch (requestType.Value()) { | 301 switch (request_type.Value()) { |
| 278 case Socket::kLookupRequest: | 302 case Socket::kLookupRequest: |
| 279 response = LookupRequest(request); | 303 response = LookupRequest(request); |
| 280 break; | 304 break; |
| 281 default: | 305 default: |
| 282 UNREACHABLE(); | 306 UNREACHABLE(); |
| 283 } | 307 } |
| 284 } | 308 } |
| 285 } | 309 } |
| 286 | 310 |
| 287 Dart_PostCObject(reply_port_id, response->AsApiCObject()); | 311 Dart_PostCObject(reply_port_id, response->AsApiCObject()); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 317 Dart_EnterScope(); | 341 Dart_EnterScope(); |
| 318 Dart_SetReturnValue(args, Dart_Null()); | 342 Dart_SetReturnValue(args, Dart_Null()); |
| 319 Dart_Port service_port = Socket::GetServicePort(); | 343 Dart_Port service_port = Socket::GetServicePort(); |
| 320 if (service_port != kIllegalPort) { | 344 if (service_port != kIllegalPort) { |
| 321 // Return a send port for the service port. | 345 // Return a send port for the service port. |
| 322 Dart_Handle send_port = Dart_NewSendPort(service_port); | 346 Dart_Handle send_port = Dart_NewSendPort(service_port); |
| 323 Dart_SetReturnValue(args, send_port); | 347 Dart_SetReturnValue(args, send_port); |
| 324 } | 348 } |
| 325 Dart_ExitScope(); | 349 Dart_ExitScope(); |
| 326 } | 350 } |
| OLD | NEW |