OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 #if !defined(DART_IO_DISABLED) | 5 #if !defined(DART_IO_DISABLED) |
6 | 6 |
7 #include "bin/socket.h" | 7 #include "bin/socket.h" |
8 | 8 |
9 #include "bin/dartutils.h" | 9 #include "bin/dartutils.h" |
10 #include "bin/eventhandler.h" | 10 #include "bin/eventhandler.h" |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 // the file descriptor. | 141 // the file descriptor. |
142 os_socket->ref_count++; | 142 os_socket->ref_count++; |
143 | 143 |
144 // The same Socket is used by a second Dart _NativeSocket object. | 144 // The same Socket is used by a second Dart _NativeSocket object. |
145 // It Retains a reference. | 145 // It Retains a reference. |
146 os_socket->socketfd->Retain(); | 146 os_socket->socketfd->Retain(); |
147 // We set as a side-effect the file descriptor on the dart | 147 // We set as a side-effect the file descriptor on the dart |
148 // socket_object. | 148 // socket_object. |
149 Socket::ReuseSocketIdNativeField(socket_object, os_socket->socketfd, | 149 Socket::ReuseSocketIdNativeField(socket_object, os_socket->socketfd, |
150 Socket::kFinalizerListening); | 150 Socket::kFinalizerListening); |
151 | |
152 return Dart_True(); | 151 return Dart_True(); |
153 } | 152 } |
154 } | 153 } |
155 } | 154 } |
156 | 155 |
157 // There is no socket listening on that (address, port), so we create new one. | 156 // There is no socket listening on that (address, port), so we create new one. |
158 intptr_t fd = ServerSocket::CreateBindListen(addr, backlog, v6_only); | 157 intptr_t fd = ServerSocket::CreateBindListen(addr, backlog, v6_only); |
159 if (fd == -5) { | 158 if (fd == -5) { |
160 OSError os_error(-1, "Invalid host", OSError::kUnknown); | 159 OSError os_error(-1, "Invalid host", OSError::kUnknown); |
161 return DartUtils::NewDartOSError(&os_error); | 160 return DartUtils::NewDartOSError(&os_error); |
162 } | 161 } |
163 if (fd < 0) { | 162 if (fd < 0) { |
164 OSError error; | 163 OSError error; |
165 return DartUtils::NewDartOSError(&error); | 164 return DartUtils::NewDartOSError(&error); |
166 } | 165 } |
167 if (!ServerSocket::StartAccept(fd)) { | 166 if (!ServerSocket::StartAccept(fd)) { |
168 OSError os_error(-1, "Failed to start accept", OSError::kUnknown); | 167 OSError os_error(-1, "Failed to start accept", OSError::kUnknown); |
169 return DartUtils::NewDartOSError(&os_error); | 168 return DartUtils::NewDartOSError(&os_error); |
170 } | 169 } |
171 intptr_t allocated_port = Socket::GetPort(fd); | 170 intptr_t allocated_port = SocketBase::GetPort(fd); |
172 ASSERT(allocated_port > 0); | 171 ASSERT(allocated_port > 0); |
173 | 172 |
174 if (allocated_port != port) { | 173 if (allocated_port != port) { |
175 // There are two cases to consider: | 174 // There are two cases to consider: |
176 // | 175 // |
177 // a) The user requested (address, port) where port != 0 which means | 176 // a) The user requested (address, port) where port != 0 which means |
178 // we re-use an existing socket if available (and it is shared) or we | 177 // we re-use an existing socket if available (and it is shared) or we |
179 // create a new one. The new socket is guaranteed to have that | 178 // create a new one. The new socket is guaranteed to have that |
180 // selected port. | 179 // selected port. |
181 // | 180 // |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 return CloseOneSafe(os_socket, true); | 261 return CloseOneSafe(os_socket, true); |
263 } else { | 262 } else { |
264 // A finalizer may direct the event handler to close a listening socket | 263 // A finalizer may direct the event handler to close a listening socket |
265 // that it has never seen before. In this case, we return true to direct | 264 // that it has never seen before. In this case, we return true to direct |
266 // the eventhandler to clean up the socket. | 265 // the eventhandler to clean up the socket. |
267 return true; | 266 return true; |
268 } | 267 } |
269 } | 268 } |
270 | 269 |
271 | 270 |
272 void FUNCTION_NAME(InternetAddress_Parse)(Dart_NativeArguments args) { | |
273 const char* address = | |
274 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | |
275 ASSERT(address != NULL); | |
276 RawAddr raw; | |
277 memset(&raw, 0, sizeof(raw)); | |
278 int type = strchr(address, ':') == NULL ? SocketAddress::TYPE_IPV4 | |
279 : SocketAddress::TYPE_IPV6; | |
280 if (type == SocketAddress::TYPE_IPV4) { | |
281 raw.addr.sa_family = AF_INET; | |
282 } else { | |
283 raw.addr.sa_family = AF_INET6; | |
284 } | |
285 bool ok = Socket::ParseAddress(type, address, &raw); | |
286 if (!ok) { | |
287 Dart_SetReturnValue(args, Dart_Null()); | |
288 } else { | |
289 Dart_SetReturnValue(args, SocketAddress::ToTypedData(raw)); | |
290 } | |
291 } | |
292 | |
293 | |
294 void FUNCTION_NAME(NetworkInterface_ListSupported)(Dart_NativeArguments args) { | |
295 Dart_SetReturnValue(args, Dart_NewBoolean(Socket::ListInterfacesSupported())); | |
296 } | |
297 | |
298 | |
299 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { | 271 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { |
300 RawAddr addr; | 272 RawAddr addr; |
301 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); | 273 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); |
302 Dart_Handle port_arg = Dart_GetNativeArgument(args, 2); | 274 Dart_Handle port_arg = Dart_GetNativeArgument(args, 2); |
303 int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535); | 275 int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535); |
304 SocketAddress::SetAddrPort(&addr, static_cast<intptr_t>(port)); | 276 SocketAddress::SetAddrPort(&addr, static_cast<intptr_t>(port)); |
305 intptr_t socket = Socket::CreateConnect(addr); | 277 intptr_t socket = Socket::CreateConnect(addr); |
306 OSError error; | 278 OSError error; |
307 if (socket >= 0) { | 279 if (socket >= 0) { |
308 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket, | 280 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket, |
(...skipping 17 matching lines...) Expand all Loading... |
326 OSError error; | 298 OSError error; |
327 if (socket >= 0) { | 299 if (socket >= 0) { |
328 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket, | 300 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket, |
329 Socket::kFinalizerNormal); | 301 Socket::kFinalizerNormal); |
330 Dart_SetReturnValue(args, Dart_True()); | 302 Dart_SetReturnValue(args, Dart_True()); |
331 } else { | 303 } else { |
332 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error)); | 304 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error)); |
333 } | 305 } |
334 } | 306 } |
335 | 307 |
336 void FUNCTION_NAME(Socket_IsBindError)(Dart_NativeArguments args) { | |
337 intptr_t error_number = | |
338 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1)); | |
339 bool is_bind_error = Socket::IsBindError(error_number); | |
340 Dart_SetReturnValue(args, is_bind_error ? Dart_True() : Dart_False()); | |
341 } | |
342 | 308 |
343 void FUNCTION_NAME(Socket_CreateBindDatagram)(Dart_NativeArguments args) { | 309 void FUNCTION_NAME(Socket_CreateBindDatagram)(Dart_NativeArguments args) { |
344 RawAddr addr; | 310 RawAddr addr; |
345 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); | 311 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); |
346 Dart_Handle port_arg = Dart_GetNativeArgument(args, 2); | 312 Dart_Handle port_arg = Dart_GetNativeArgument(args, 2); |
347 int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535); | 313 int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535); |
348 SocketAddress::SetAddrPort(&addr, port); | 314 SocketAddress::SetAddrPort(&addr, port); |
349 bool reuse_addr = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3)); | 315 bool reuse_addr = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3)); |
350 intptr_t socket = Socket::CreateBindDatagram(addr, reuse_addr); | 316 intptr_t socket = Socket::CreateBindDatagram(addr, reuse_addr); |
351 if (socket >= 0) { | 317 if (socket >= 0) { |
352 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket, | 318 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket, |
353 Socket::kFinalizerNormal); | 319 Socket::kFinalizerNormal); |
354 Dart_SetReturnValue(args, Dart_True()); | 320 Dart_SetReturnValue(args, Dart_True()); |
355 } else { | 321 } else { |
356 OSError error; | 322 OSError error; |
357 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error)); | 323 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error)); |
358 } | 324 } |
359 } | 325 } |
360 | 326 |
361 | 327 |
362 void FUNCTION_NAME(Socket_Available)(Dart_NativeArguments args) { | 328 void FUNCTION_NAME(Socket_Available)(Dart_NativeArguments args) { |
363 Socket* socket = | 329 Socket* socket = |
364 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 330 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
365 intptr_t available = Socket::Available(socket->fd()); | 331 intptr_t available = SocketBase::Available(socket->fd()); |
366 if (available >= 0) { | 332 if (available >= 0) { |
367 Dart_SetReturnValue(args, Dart_NewInteger(available)); | 333 Dart_SetReturnValue(args, Dart_NewInteger(available)); |
368 } else { | 334 } else { |
369 // Available failed. Mark socket as having data, to trigger a future read | 335 // Available failed. Mark socket as having data, to trigger a future read |
370 // event where the actual error can be reported. | 336 // event where the actual error can be reported. |
371 Dart_SetReturnValue(args, Dart_NewInteger(1)); | 337 Dart_SetReturnValue(args, Dart_NewInteger(1)); |
372 } | 338 } |
373 } | 339 } |
374 | 340 |
375 | 341 |
376 void FUNCTION_NAME(Socket_Read)(Dart_NativeArguments args) { | 342 void FUNCTION_NAME(Socket_Read)(Dart_NativeArguments args) { |
377 Socket* socket = | 343 Socket* socket = |
378 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 344 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
379 int64_t length = 0; | 345 int64_t length = 0; |
380 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &length)) { | 346 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &length)) { |
381 if (short_socket_read) { | 347 if (short_socket_read) { |
382 length = (length + 1) / 2; | 348 length = (length + 1) / 2; |
383 } | 349 } |
384 uint8_t* buffer = NULL; | 350 uint8_t* buffer = NULL; |
385 Dart_Handle result = IOBuffer::Allocate(length, &buffer); | 351 Dart_Handle result = IOBuffer::Allocate(length, &buffer); |
386 if (Dart_IsError(result)) { | 352 if (Dart_IsError(result)) { |
387 Dart_PropagateError(result); | 353 Dart_PropagateError(result); |
388 } | 354 } |
389 ASSERT(buffer != NULL); | 355 ASSERT(buffer != NULL); |
390 intptr_t bytes_read = Socket::Read(socket->fd(), buffer, length); | 356 intptr_t bytes_read = SocketBase::Read(socket->fd(), buffer, length); |
391 if (bytes_read == length) { | 357 if (bytes_read == length) { |
392 Dart_SetReturnValue(args, result); | 358 Dart_SetReturnValue(args, result); |
393 } else if (bytes_read > 0) { | 359 } else if (bytes_read > 0) { |
394 uint8_t* new_buffer = NULL; | 360 uint8_t* new_buffer = NULL; |
395 Dart_Handle new_result = IOBuffer::Allocate(bytes_read, &new_buffer); | 361 Dart_Handle new_result = IOBuffer::Allocate(bytes_read, &new_buffer); |
396 if (Dart_IsError(new_result)) { | 362 if (Dart_IsError(new_result)) { |
397 Dart_PropagateError(new_result); | 363 Dart_PropagateError(new_result); |
398 } | 364 } |
399 ASSERT(new_buffer != NULL); | 365 ASSERT(new_buffer != NULL); |
400 memmove(new_buffer, buffer, bytes_read); | 366 memmove(new_buffer, buffer, bytes_read); |
(...skipping 19 matching lines...) Expand all Loading... |
420 | 386 |
421 // TODO(sgjesse): Use a MTU value here. Only the loopback adapter can | 387 // TODO(sgjesse): Use a MTU value here. Only the loopback adapter can |
422 // handle 64k datagrams. | 388 // handle 64k datagrams. |
423 IsolateData* isolate_data = | 389 IsolateData* isolate_data = |
424 reinterpret_cast<IsolateData*>(Dart_CurrentIsolateData()); | 390 reinterpret_cast<IsolateData*>(Dart_CurrentIsolateData()); |
425 if (isolate_data->udp_receive_buffer == NULL) { | 391 if (isolate_data->udp_receive_buffer == NULL) { |
426 isolate_data->udp_receive_buffer = | 392 isolate_data->udp_receive_buffer = |
427 reinterpret_cast<uint8_t*>(malloc(65536)); | 393 reinterpret_cast<uint8_t*>(malloc(65536)); |
428 } | 394 } |
429 RawAddr addr; | 395 RawAddr addr; |
430 intptr_t bytes_read = Socket::RecvFrom( | 396 intptr_t bytes_read = SocketBase::RecvFrom( |
431 socket->fd(), isolate_data->udp_receive_buffer, 65536, &addr); | 397 socket->fd(), isolate_data->udp_receive_buffer, 65536, &addr); |
432 if (bytes_read == 0) { | 398 if (bytes_read == 0) { |
433 Dart_SetReturnValue(args, Dart_Null()); | 399 Dart_SetReturnValue(args, Dart_Null()); |
434 return; | 400 return; |
435 } | 401 } |
436 if (bytes_read < 0) { | 402 if (bytes_read < 0) { |
437 ASSERT(bytes_read == -1); | 403 ASSERT(bytes_read == -1); |
438 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 404 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
439 return; | 405 return; |
440 } | 406 } |
(...skipping 10 matching lines...) Expand all Loading... |
451 // Get the port and clear it in the sockaddr structure. | 417 // Get the port and clear it in the sockaddr structure. |
452 int port = SocketAddress::GetAddrPort(addr); | 418 int port = SocketAddress::GetAddrPort(addr); |
453 if (addr.addr.sa_family == AF_INET) { | 419 if (addr.addr.sa_family == AF_INET) { |
454 addr.in.sin_port = 0; | 420 addr.in.sin_port = 0; |
455 } else { | 421 } else { |
456 ASSERT(addr.addr.sa_family == AF_INET6); | 422 ASSERT(addr.addr.sa_family == AF_INET6); |
457 addr.in6.sin6_port = 0; | 423 addr.in6.sin6_port = 0; |
458 } | 424 } |
459 // Format the address to a string using the numeric format. | 425 // Format the address to a string using the numeric format. |
460 char numeric_address[INET6_ADDRSTRLEN]; | 426 char numeric_address[INET6_ADDRSTRLEN]; |
461 Socket::FormatNumericAddress(addr, numeric_address, INET6_ADDRSTRLEN); | 427 SocketBase::FormatNumericAddress(addr, numeric_address, INET6_ADDRSTRLEN); |
462 | 428 |
463 // Create a Datagram object with the data and sender address and port. | 429 // Create a Datagram object with the data and sender address and port. |
464 const int kNumArgs = 4; | 430 const int kNumArgs = 4; |
465 Dart_Handle dart_args[kNumArgs]; | 431 Dart_Handle dart_args[kNumArgs]; |
466 dart_args[0] = data; | 432 dart_args[0] = data; |
467 dart_args[1] = Dart_NewStringFromCString(numeric_address); | 433 dart_args[1] = Dart_NewStringFromCString(numeric_address); |
468 if (Dart_IsError(dart_args[1])) { | 434 if (Dart_IsError(dart_args[1])) { |
469 Dart_PropagateError(dart_args[1]); | 435 Dart_PropagateError(dart_args[1]); |
470 } | 436 } |
471 dart_args[2] = SocketAddress::ToTypedData(addr); | 437 dart_args[2] = SocketAddress::ToTypedData(addr); |
(...skipping 29 matching lines...) Expand all Loading... |
501 Dart_TypedData_Type type; | 467 Dart_TypedData_Type type; |
502 uint8_t* buffer = NULL; | 468 uint8_t* buffer = NULL; |
503 intptr_t len; | 469 intptr_t len; |
504 Dart_Handle result = Dart_TypedDataAcquireData( | 470 Dart_Handle result = Dart_TypedDataAcquireData( |
505 buffer_obj, &type, reinterpret_cast<void**>(&buffer), &len); | 471 buffer_obj, &type, reinterpret_cast<void**>(&buffer), &len); |
506 if (Dart_IsError(result)) { | 472 if (Dart_IsError(result)) { |
507 Dart_PropagateError(result); | 473 Dart_PropagateError(result); |
508 } | 474 } |
509 ASSERT((offset + length) <= len); | 475 ASSERT((offset + length) <= len); |
510 buffer += offset; | 476 buffer += offset; |
511 intptr_t bytes_written = Socket::Write(socket->fd(), buffer, length); | 477 intptr_t bytes_written = SocketBase::Write(socket->fd(), buffer, length); |
512 if (bytes_written >= 0) { | 478 if (bytes_written >= 0) { |
513 Dart_TypedDataReleaseData(buffer_obj); | 479 Dart_TypedDataReleaseData(buffer_obj); |
514 if (short_write) { | 480 if (short_write) { |
515 // If the write was forced 'short', indicate by returning the negative | 481 // If the write was forced 'short', indicate by returning the negative |
516 // number of bytes. A forced short write may not trigger a write event. | 482 // number of bytes. A forced short write may not trigger a write event. |
517 Dart_SetReturnValue(args, Dart_NewInteger(-bytes_written)); | 483 Dart_SetReturnValue(args, Dart_NewInteger(-bytes_written)); |
518 } else { | 484 } else { |
519 Dart_SetReturnValue(args, Dart_NewInteger(bytes_written)); | 485 Dart_SetReturnValue(args, Dart_NewInteger(bytes_written)); |
520 } | 486 } |
521 } else { | 487 } else { |
(...skipping 21 matching lines...) Expand all Loading... |
543 Dart_TypedData_Type type; | 509 Dart_TypedData_Type type; |
544 uint8_t* buffer = NULL; | 510 uint8_t* buffer = NULL; |
545 intptr_t len; | 511 intptr_t len; |
546 Dart_Handle result = Dart_TypedDataAcquireData( | 512 Dart_Handle result = Dart_TypedDataAcquireData( |
547 buffer_obj, &type, reinterpret_cast<void**>(&buffer), &len); | 513 buffer_obj, &type, reinterpret_cast<void**>(&buffer), &len); |
548 if (Dart_IsError(result)) { | 514 if (Dart_IsError(result)) { |
549 Dart_PropagateError(result); | 515 Dart_PropagateError(result); |
550 } | 516 } |
551 ASSERT((offset + length) <= len); | 517 ASSERT((offset + length) <= len); |
552 buffer += offset; | 518 buffer += offset; |
553 intptr_t bytes_written = Socket::SendTo(socket->fd(), buffer, length, addr); | 519 intptr_t bytes_written = |
| 520 SocketBase::SendTo(socket->fd(), buffer, length, addr); |
554 if (bytes_written >= 0) { | 521 if (bytes_written >= 0) { |
555 Dart_TypedDataReleaseData(buffer_obj); | 522 Dart_TypedDataReleaseData(buffer_obj); |
556 Dart_SetReturnValue(args, Dart_NewInteger(bytes_written)); | 523 Dart_SetReturnValue(args, Dart_NewInteger(bytes_written)); |
557 } else { | 524 } else { |
558 // Extract OSError before we release data, as it may override the error. | 525 // Extract OSError before we release data, as it may override the error. |
559 OSError os_error; | 526 OSError os_error; |
560 Dart_TypedDataReleaseData(buffer_obj); | 527 Dart_TypedDataReleaseData(buffer_obj); |
561 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | 528 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
562 } | 529 } |
563 } | 530 } |
564 | 531 |
565 | 532 |
566 void FUNCTION_NAME(Socket_GetPort)(Dart_NativeArguments args) { | 533 void FUNCTION_NAME(Socket_GetPort)(Dart_NativeArguments args) { |
567 Socket* socket = | 534 Socket* socket = |
568 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 535 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
569 OSError os_error; | 536 OSError os_error; |
570 intptr_t port = Socket::GetPort(socket->fd()); | 537 intptr_t port = SocketBase::GetPort(socket->fd()); |
571 if (port > 0) { | 538 if (port > 0) { |
572 Dart_SetReturnValue(args, Dart_NewInteger(port)); | 539 Dart_SetReturnValue(args, Dart_NewInteger(port)); |
573 } else { | 540 } else { |
574 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 541 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
575 } | 542 } |
576 } | 543 } |
577 | 544 |
578 | 545 |
579 void FUNCTION_NAME(Socket_GetRemotePeer)(Dart_NativeArguments args) { | 546 void FUNCTION_NAME(Socket_GetRemotePeer)(Dart_NativeArguments args) { |
580 Socket* socket = | 547 Socket* socket = |
581 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 548 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
582 OSError os_error; | 549 OSError os_error; |
583 intptr_t port = 0; | 550 intptr_t port = 0; |
584 SocketAddress* addr = Socket::GetRemotePeer(socket->fd(), &port); | 551 SocketAddress* addr = SocketBase::GetRemotePeer(socket->fd(), &port); |
585 if (addr != NULL) { | 552 if (addr != NULL) { |
586 Dart_Handle list = Dart_NewList(2); | 553 Dart_Handle list = Dart_NewList(2); |
587 | 554 |
588 Dart_Handle entry = Dart_NewList(3); | 555 Dart_Handle entry = Dart_NewList(3); |
589 Dart_ListSetAt(entry, 0, Dart_NewInteger(addr->GetType())); | 556 Dart_ListSetAt(entry, 0, Dart_NewInteger(addr->GetType())); |
590 Dart_ListSetAt(entry, 1, Dart_NewStringFromCString(addr->as_string())); | 557 Dart_ListSetAt(entry, 1, Dart_NewStringFromCString(addr->as_string())); |
591 | 558 |
592 RawAddr raw = addr->addr(); | 559 RawAddr raw = addr->addr(); |
593 Dart_ListSetAt(entry, 2, SocketAddress::ToTypedData(raw)); | 560 Dart_ListSetAt(entry, 2, SocketAddress::ToTypedData(raw)); |
594 | 561 |
595 Dart_ListSetAt(list, 0, entry); | 562 Dart_ListSetAt(list, 0, entry); |
596 Dart_ListSetAt(list, 1, Dart_NewInteger(port)); | 563 Dart_ListSetAt(list, 1, Dart_NewInteger(port)); |
597 Dart_SetReturnValue(args, list); | 564 Dart_SetReturnValue(args, list); |
598 delete addr; | 565 delete addr; |
599 } else { | 566 } else { |
600 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 567 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
601 } | 568 } |
602 } | 569 } |
603 | 570 |
604 | 571 |
605 void FUNCTION_NAME(Socket_GetError)(Dart_NativeArguments args) { | 572 void FUNCTION_NAME(Socket_GetError)(Dart_NativeArguments args) { |
606 Socket* socket = | 573 Socket* socket = |
607 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 574 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
608 OSError os_error; | 575 OSError os_error; |
609 Socket::GetError(socket->fd(), &os_error); | 576 SocketBase::GetError(socket->fd(), &os_error); |
610 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | 577 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
611 } | 578 } |
612 | 579 |
613 | 580 |
614 void FUNCTION_NAME(Socket_GetType)(Dart_NativeArguments args) { | 581 void FUNCTION_NAME(Socket_GetType)(Dart_NativeArguments args) { |
615 Socket* socket = | 582 Socket* socket = |
616 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 583 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
617 OSError os_error; | 584 OSError os_error; |
618 intptr_t type = Socket::GetType(socket->fd()); | 585 intptr_t type = SocketBase::GetType(socket->fd()); |
619 if (type >= 0) { | 586 if (type >= 0) { |
620 Dart_SetReturnValue(args, Dart_NewInteger(type)); | 587 Dart_SetReturnValue(args, Dart_NewInteger(type)); |
621 } else { | 588 } else { |
622 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 589 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
623 } | 590 } |
624 } | 591 } |
625 | 592 |
626 | 593 |
627 void FUNCTION_NAME(Socket_GetStdioHandle)(Dart_NativeArguments args) { | 594 void FUNCTION_NAME(Socket_GetStdioHandle)(Dart_NativeArguments args) { |
628 int64_t num = | 595 int64_t num = |
629 DartUtils::GetInt64ValueCheckRange(Dart_GetNativeArgument(args, 1), 0, 2); | 596 DartUtils::GetInt64ValueCheckRange(Dart_GetNativeArgument(args, 1), 0, 2); |
630 intptr_t socket = Socket::GetStdioHandle(num); | 597 intptr_t socket = SocketBase::GetStdioHandle(num); |
631 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket, | 598 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket, |
632 Socket::kFinalizerStdio); | 599 Socket::kFinalizerStdio); |
633 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); | 600 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); |
634 } | 601 } |
635 | 602 |
636 | 603 |
637 void FUNCTION_NAME(Socket_GetSocketId)(Dart_NativeArguments args) { | 604 void FUNCTION_NAME(Socket_GetSocketId)(Dart_NativeArguments args) { |
638 Socket* socket = | 605 Socket* socket = |
639 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 606 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
640 intptr_t id = reinterpret_cast<intptr_t>(socket); | 607 intptr_t id = reinterpret_cast<intptr_t>(socket); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
684 | 651 |
685 | 652 |
686 CObject* Socket::LookupRequest(const CObjectArray& request) { | 653 CObject* Socket::LookupRequest(const CObjectArray& request) { |
687 if ((request.Length() == 2) && request[0]->IsString() && | 654 if ((request.Length() == 2) && request[0]->IsString() && |
688 request[1]->IsInt32()) { | 655 request[1]->IsInt32()) { |
689 CObjectString host(request[0]); | 656 CObjectString host(request[0]); |
690 CObjectInt32 type(request[1]); | 657 CObjectInt32 type(request[1]); |
691 CObject* result = NULL; | 658 CObject* result = NULL; |
692 OSError* os_error = NULL; | 659 OSError* os_error = NULL; |
693 AddressList<SocketAddress>* addresses = | 660 AddressList<SocketAddress>* addresses = |
694 Socket::LookupAddress(host.CString(), type.Value(), &os_error); | 661 SocketBase::LookupAddress(host.CString(), type.Value(), &os_error); |
695 if (addresses != NULL) { | 662 if (addresses != NULL) { |
696 CObjectArray* array = | 663 CObjectArray* array = |
697 new CObjectArray(CObject::NewArray(addresses->count() + 1)); | 664 new CObjectArray(CObject::NewArray(addresses->count() + 1)); |
698 array->SetAt(0, new CObjectInt32(CObject::NewInt32(0))); | 665 array->SetAt(0, new CObjectInt32(CObject::NewInt32(0))); |
699 for (intptr_t i = 0; i < addresses->count(); i++) { | 666 for (intptr_t i = 0; i < addresses->count(); i++) { |
700 SocketAddress* addr = addresses->GetAt(i); | 667 SocketAddress* addr = addresses->GetAt(i); |
701 CObjectArray* entry = new CObjectArray(CObject::NewArray(3)); | 668 CObjectArray* entry = new CObjectArray(CObject::NewArray(3)); |
702 | 669 |
703 CObjectInt32* type = | 670 CObjectInt32* type = |
704 new CObjectInt32(CObject::NewInt32(addr->GetType())); | 671 new CObjectInt32(CObject::NewInt32(addr->GetType())); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
739 } else { | 706 } else { |
740 ASSERT(len == sizeof(in6_addr)); | 707 ASSERT(len == sizeof(in6_addr)); |
741 addr.in6.sin6_family = AF_INET6; | 708 addr.in6.sin6_family = AF_INET6; |
742 memmove(reinterpret_cast<void*>(&addr.in6.sin6_addr), | 709 memmove(reinterpret_cast<void*>(&addr.in6.sin6_addr), |
743 addr_object.Buffer(), len); | 710 addr_object.Buffer(), len); |
744 } | 711 } |
745 | 712 |
746 OSError* os_error = NULL; | 713 OSError* os_error = NULL; |
747 const intptr_t kMaxHostLength = 1025; | 714 const intptr_t kMaxHostLength = 1025; |
748 char host[kMaxHostLength]; | 715 char host[kMaxHostLength]; |
749 if (Socket::ReverseLookup(addr, host, kMaxHostLength, &os_error)) { | 716 if (SocketBase::ReverseLookup(addr, host, kMaxHostLength, &os_error)) { |
750 return new CObjectString(CObject::NewString(host)); | 717 return new CObjectString(CObject::NewString(host)); |
751 } else { | 718 } else { |
752 CObject* result = CObject::NewOSError(os_error); | 719 CObject* result = CObject::NewOSError(os_error); |
753 delete os_error; | 720 delete os_error; |
754 return result; | 721 return result; |
755 } | 722 } |
756 } | 723 } |
757 return CObject::IllegalArgumentError(); | 724 return CObject::IllegalArgumentError(); |
758 } | 725 } |
759 | 726 |
760 | 727 |
761 CObject* Socket::ListInterfacesRequest(const CObjectArray& request) { | 728 CObject* Socket::ListInterfacesRequest(const CObjectArray& request) { |
762 if ((request.Length() == 1) && request[0]->IsInt32()) { | 729 if ((request.Length() == 1) && request[0]->IsInt32()) { |
763 CObjectInt32 type(request[0]); | 730 CObjectInt32 type(request[0]); |
764 CObject* result = NULL; | 731 CObject* result = NULL; |
765 OSError* os_error = NULL; | 732 OSError* os_error = NULL; |
766 AddressList<InterfaceSocketAddress>* addresses = | 733 AddressList<InterfaceSocketAddress>* addresses = |
767 Socket::ListInterfaces(type.Value(), &os_error); | 734 SocketBase::ListInterfaces(type.Value(), &os_error); |
768 if (addresses != NULL) { | 735 if (addresses != NULL) { |
769 CObjectArray* array = | 736 CObjectArray* array = |
770 new CObjectArray(CObject::NewArray(addresses->count() + 1)); | 737 new CObjectArray(CObject::NewArray(addresses->count() + 1)); |
771 array->SetAt(0, new CObjectInt32(CObject::NewInt32(0))); | 738 array->SetAt(0, new CObjectInt32(CObject::NewInt32(0))); |
772 for (intptr_t i = 0; i < addresses->count(); i++) { | 739 for (intptr_t i = 0; i < addresses->count(); i++) { |
773 InterfaceSocketAddress* interface = addresses->GetAt(i); | 740 InterfaceSocketAddress* interface = addresses->GetAt(i); |
774 SocketAddress* addr = interface->socket_address(); | 741 SocketAddress* addr = interface->socket_address(); |
775 CObjectArray* entry = new CObjectArray(CObject::NewArray(5)); | 742 CObjectArray* entry = new CObjectArray(CObject::NewArray(5)); |
776 | 743 |
777 CObjectInt32* type = | 744 CObjectInt32* type = |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
811 void FUNCTION_NAME(Socket_GetOption)(Dart_NativeArguments args) { | 778 void FUNCTION_NAME(Socket_GetOption)(Dart_NativeArguments args) { |
812 Socket* socket = | 779 Socket* socket = |
813 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 780 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
814 int64_t option = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); | 781 int64_t option = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); |
815 intptr_t protocol = static_cast<intptr_t>( | 782 intptr_t protocol = static_cast<intptr_t>( |
816 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2))); | 783 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2))); |
817 bool ok = false; | 784 bool ok = false; |
818 switch (option) { | 785 switch (option) { |
819 case 0: { // TCP_NODELAY. | 786 case 0: { // TCP_NODELAY. |
820 bool enabled; | 787 bool enabled; |
821 ok = Socket::GetNoDelay(socket->fd(), &enabled); | 788 ok = SocketBase::GetNoDelay(socket->fd(), &enabled); |
822 if (ok) { | 789 if (ok) { |
823 Dart_SetReturnValue(args, enabled ? Dart_True() : Dart_False()); | 790 Dart_SetReturnValue(args, enabled ? Dart_True() : Dart_False()); |
824 } | 791 } |
825 break; | 792 break; |
826 } | 793 } |
827 case 1: { // IP_MULTICAST_LOOP. | 794 case 1: { // IP_MULTICAST_LOOP. |
828 bool enabled; | 795 bool enabled; |
829 ok = Socket::GetMulticastLoop(socket->fd(), protocol, &enabled); | 796 ok = SocketBase::GetMulticastLoop(socket->fd(), protocol, &enabled); |
830 if (ok) { | 797 if (ok) { |
831 Dart_SetReturnValue(args, enabled ? Dart_True() : Dart_False()); | 798 Dart_SetReturnValue(args, enabled ? Dart_True() : Dart_False()); |
832 } | 799 } |
833 break; | 800 break; |
834 } | 801 } |
835 case 2: { // IP_MULTICAST_TTL. | 802 case 2: { // IP_MULTICAST_TTL. |
836 int value; | 803 int value; |
837 ok = Socket::GetMulticastHops(socket->fd(), protocol, &value); | 804 ok = SocketBase::GetMulticastHops(socket->fd(), protocol, &value); |
838 if (ok) { | 805 if (ok) { |
839 Dart_SetReturnValue(args, Dart_NewInteger(value)); | 806 Dart_SetReturnValue(args, Dart_NewInteger(value)); |
840 } | 807 } |
841 break; | 808 break; |
842 } | 809 } |
843 case 3: { // IP_MULTICAST_IF. | 810 case 3: { // IP_MULTICAST_IF. |
844 UNIMPLEMENTED(); | 811 UNIMPLEMENTED(); |
845 break; | 812 break; |
846 } | 813 } |
847 case 4: { // IP_BROADCAST. | 814 case 4: { // IP_BROADCAST. |
848 bool enabled; | 815 bool enabled; |
849 ok = Socket::GetBroadcast(socket->fd(), &enabled); | 816 ok = SocketBase::GetBroadcast(socket->fd(), &enabled); |
850 if (ok) { | 817 if (ok) { |
851 Dart_SetReturnValue(args, enabled ? Dart_True() : Dart_False()); | 818 Dart_SetReturnValue(args, enabled ? Dart_True() : Dart_False()); |
852 } | 819 } |
853 break; | 820 break; |
854 } | 821 } |
855 default: | 822 default: |
856 UNREACHABLE(); | 823 UNREACHABLE(); |
857 break; | 824 break; |
858 } | 825 } |
859 // In case of failure the return value is not set above. | 826 // In case of failure the return value is not set above. |
860 if (!ok) { | 827 if (!ok) { |
861 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 828 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
862 } | 829 } |
863 } | 830 } |
864 | 831 |
865 | 832 |
866 void FUNCTION_NAME(Socket_SetOption)(Dart_NativeArguments args) { | 833 void FUNCTION_NAME(Socket_SetOption)(Dart_NativeArguments args) { |
867 bool result = false; | 834 bool result = false; |
868 Socket* socket = | 835 Socket* socket = |
869 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 836 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
870 int64_t option = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); | 837 int64_t option = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); |
871 int64_t protocol = DartUtils::GetInt64ValueCheckRange( | 838 int64_t protocol = DartUtils::GetInt64ValueCheckRange( |
872 Dart_GetNativeArgument(args, 2), SocketAddress::TYPE_IPV4, | 839 Dart_GetNativeArgument(args, 2), SocketAddress::TYPE_IPV4, |
873 SocketAddress::TYPE_IPV6); | 840 SocketAddress::TYPE_IPV6); |
874 switch (option) { | 841 switch (option) { |
875 case 0: // TCP_NODELAY. | 842 case 0: // TCP_NODELAY. |
876 result = Socket::SetNoDelay( | 843 result = SocketBase::SetNoDelay( |
877 socket->fd(), | 844 socket->fd(), |
878 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); | 845 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); |
879 break; | 846 break; |
880 case 1: // IP_MULTICAST_LOOP. | 847 case 1: // IP_MULTICAST_LOOP. |
881 result = Socket::SetMulticastLoop( | 848 result = SocketBase::SetMulticastLoop( |
882 socket->fd(), protocol, | 849 socket->fd(), protocol, |
883 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); | 850 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); |
884 break; | 851 break; |
885 case 2: // IP_MULTICAST_TTL. | 852 case 2: // IP_MULTICAST_TTL. |
886 result = Socket::SetMulticastHops( | 853 result = SocketBase::SetMulticastHops( |
887 socket->fd(), protocol, | 854 socket->fd(), protocol, |
888 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3))); | 855 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3))); |
889 break; | 856 break; |
890 case 3: { // IP_MULTICAST_IF. | 857 case 3: { // IP_MULTICAST_IF. |
891 UNIMPLEMENTED(); | 858 UNIMPLEMENTED(); |
892 break; | 859 break; |
893 } | 860 } |
894 case 4: // IP_BROADCAST. | 861 case 4: // IP_BROADCAST. |
895 result = Socket::SetBroadcast( | 862 result = SocketBase::SetBroadcast( |
896 socket->fd(), | 863 socket->fd(), |
897 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); | 864 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); |
898 break; | 865 break; |
899 default: | 866 default: |
900 Dart_PropagateError(Dart_NewApiError("Value outside expected range")); | 867 Dart_PropagateError(Dart_NewApiError("Value outside expected range")); |
901 break; | 868 break; |
902 } | 869 } |
903 if (result) { | 870 if (result) { |
904 Dart_SetReturnValue(args, Dart_Null()); | 871 Dart_SetReturnValue(args, Dart_Null()); |
905 } else { | 872 } else { |
906 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 873 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
907 } | 874 } |
908 } | 875 } |
909 | 876 |
910 | 877 |
911 void FUNCTION_NAME(Socket_JoinMulticast)(Dart_NativeArguments args) { | 878 void FUNCTION_NAME(Socket_JoinMulticast)(Dart_NativeArguments args) { |
912 Socket* socket = | 879 Socket* socket = |
913 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 880 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
914 RawAddr addr; | 881 RawAddr addr; |
915 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); | 882 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); |
916 RawAddr interface; | 883 RawAddr interface; |
917 if (Dart_GetNativeArgument(args, 2) != Dart_Null()) { | 884 if (Dart_GetNativeArgument(args, 2) != Dart_Null()) { |
918 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 2), &interface); | 885 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 2), &interface); |
919 } | 886 } |
920 int interfaceIndex = | 887 int interfaceIndex = |
921 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); | 888 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); |
922 if (Socket::JoinMulticast(socket->fd(), addr, interface, interfaceIndex)) { | 889 if (SocketBase::JoinMulticast(socket->fd(), addr, interface, |
| 890 interfaceIndex)) { |
923 Dart_SetReturnValue(args, Dart_Null()); | 891 Dart_SetReturnValue(args, Dart_Null()); |
924 } else { | 892 } else { |
925 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 893 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
926 } | 894 } |
927 } | 895 } |
928 | 896 |
929 | 897 |
930 void FUNCTION_NAME(Socket_LeaveMulticast)(Dart_NativeArguments args) { | 898 void FUNCTION_NAME(Socket_LeaveMulticast)(Dart_NativeArguments args) { |
931 Socket* socket = | 899 Socket* socket = |
932 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 900 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
933 RawAddr addr; | 901 RawAddr addr; |
934 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); | 902 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); |
935 RawAddr interface; | 903 RawAddr interface; |
936 if (Dart_GetNativeArgument(args, 2) != Dart_Null()) { | 904 if (Dart_GetNativeArgument(args, 2) != Dart_Null()) { |
937 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 2), &interface); | 905 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 2), &interface); |
938 } | 906 } |
939 int interfaceIndex = | 907 int interfaceIndex = |
940 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); | 908 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); |
941 if (Socket::LeaveMulticast(socket->fd(), addr, interface, interfaceIndex)) { | 909 if (SocketBase::LeaveMulticast(socket->fd(), addr, interface, |
| 910 interfaceIndex)) { |
942 Dart_SetReturnValue(args, Dart_Null()); | 911 Dart_SetReturnValue(args, Dart_Null()); |
943 } else { | 912 } else { |
944 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 913 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
945 } | 914 } |
946 } | 915 } |
947 | 916 |
948 | 917 |
949 static void NormalSocketFinalizer(void* isolate_data, | 918 static void NormalSocketFinalizer(void* isolate_data, |
950 Dart_WeakPersistentHandle handle, | 919 Dart_WeakPersistentHandle handle, |
951 void* data) { | 920 void* data) { |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1032 Dart_PropagateError(err); | 1001 Dart_PropagateError(err); |
1033 } | 1002 } |
1034 Socket* socket = reinterpret_cast<Socket*>(id); | 1003 Socket* socket = reinterpret_cast<Socket*>(id); |
1035 return socket; | 1004 return socket; |
1036 } | 1005 } |
1037 | 1006 |
1038 } // namespace bin | 1007 } // namespace bin |
1039 } // namespace dart | 1008 } // namespace dart |
1040 | 1009 |
1041 #endif // !defined(DART_IO_DISABLED) | 1010 #endif // !defined(DART_IO_DISABLED) |
OLD | NEW |