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 #include "platform/globals.h" | 5 #include "platform/globals.h" |
6 #if defined(TARGET_OS_WINDOWS) | 6 #if defined(TARGET_OS_WINDOWS) |
7 | 7 |
8 #include "bin/eventhandler.h" | 8 #include "bin/eventhandler.h" |
9 #include "bin/eventhandler_win.h" | 9 #include "bin/eventhandler_win.h" |
10 | 10 |
| 11 #include <fcntl.h> // NOLINT |
| 12 #include <io.h> // NOLINT |
| 13 #include <mswsock.h> // NOLINT |
11 #include <winsock2.h> // NOLINT | 14 #include <winsock2.h> // NOLINT |
12 #include <ws2tcpip.h> // NOLINT | 15 #include <ws2tcpip.h> // NOLINT |
13 #include <mswsock.h> // NOLINT | |
14 #include <io.h> // NOLINT | |
15 #include <fcntl.h> // NOLINT | |
16 | 16 |
17 #include "bin/builtin.h" | 17 #include "bin/builtin.h" |
18 #include "bin/dartutils.h" | 18 #include "bin/dartutils.h" |
19 #include "bin/lockers.h" | 19 #include "bin/lockers.h" |
20 #include "bin/log.h" | 20 #include "bin/log.h" |
21 #include "bin/socket.h" | 21 #include "bin/socket.h" |
22 #include "bin/thread.h" | 22 #include "bin/thread.h" |
23 #include "bin/utils.h" | 23 #include "bin/utils.h" |
24 | 24 |
25 #include "platform/utils.h" | 25 #include "platform/utils.h" |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 Handle::~Handle() { | 133 Handle::~Handle() { |
134 delete monitor_; | 134 delete monitor_; |
135 } | 135 } |
136 | 136 |
137 | 137 |
138 bool Handle::CreateCompletionPort(HANDLE completion_port) { | 138 bool Handle::CreateCompletionPort(HANDLE completion_port) { |
139 completion_port_ = CreateIoCompletionPort(handle(), | 139 completion_port_ = CreateIoCompletionPort(handle(), |
140 completion_port, | 140 completion_port, |
141 reinterpret_cast<ULONG_PTR>(this), | 141 reinterpret_cast<ULONG_PTR>(this), |
142 0); | 142 0); |
143 if (completion_port_ == NULL) { | 143 return (completion_port_ != NULL); |
144 return false; | |
145 } | |
146 return true; | |
147 } | 144 } |
148 | 145 |
149 | 146 |
150 void Handle::Close() { | 147 void Handle::Close() { |
151 MonitorLocker ml(monitor_); | 148 MonitorLocker ml(monitor_); |
152 if (!IsClosing()) { | 149 if (!IsClosing()) { |
153 // Close the socket and set the closing state. This close method can be | 150 // Close the socket and set the closing state. This close method can be |
154 // called again if this socket has pending IO operations in flight. | 151 // called again if this socket has pending IO operations in flight. |
155 MarkClosing(); | 152 MarkClosing(); |
156 // Perform handle type specific closing. | 153 // Perform handle type specific closing. |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 ASSERT(pending_read_ == NULL); | 297 ASSERT(pending_read_ == NULL); |
301 OverlappedBuffer* buffer = OverlappedBuffer::AllocateReadBuffer(kBufferSize); | 298 OverlappedBuffer* buffer = OverlappedBuffer::AllocateReadBuffer(kBufferSize); |
302 if (SupportsOverlappedIO()) { | 299 if (SupportsOverlappedIO()) { |
303 ASSERT(completion_port_ != INVALID_HANDLE_VALUE); | 300 ASSERT(completion_port_ != INVALID_HANDLE_VALUE); |
304 | 301 |
305 BOOL ok = ReadFile(handle_, | 302 BOOL ok = ReadFile(handle_, |
306 buffer->GetBufferStart(), | 303 buffer->GetBufferStart(), |
307 buffer->GetBufferSize(), | 304 buffer->GetBufferSize(), |
308 NULL, | 305 NULL, |
309 buffer->GetCleanOverlapped()); | 306 buffer->GetCleanOverlapped()); |
310 if (ok || GetLastError() == ERROR_IO_PENDING) { | 307 if (ok || (GetLastError() == ERROR_IO_PENDING)) { |
311 // Completing asynchronously. | 308 // Completing asynchronously. |
312 pending_read_ = buffer; | 309 pending_read_ = buffer; |
313 return true; | 310 return true; |
314 } | 311 } |
315 OverlappedBuffer::DisposeBuffer(buffer); | 312 OverlappedBuffer::DisposeBuffer(buffer); |
316 HandleIssueError(); | 313 HandleIssueError(); |
317 return false; | 314 return false; |
318 } else { | 315 } else { |
319 // Completing asynchronously through thread. | 316 // Completing asynchronously through thread. |
320 pending_read_ = buffer; | 317 pending_read_ = buffer; |
(...skipping 19 matching lines...) Expand all Loading... |
340 ASSERT(completion_port_ != INVALID_HANDLE_VALUE); | 337 ASSERT(completion_port_ != INVALID_HANDLE_VALUE); |
341 ASSERT(pending_write_ != NULL); | 338 ASSERT(pending_write_ != NULL); |
342 ASSERT(pending_write_->operation() == OverlappedBuffer::kWrite); | 339 ASSERT(pending_write_->operation() == OverlappedBuffer::kWrite); |
343 | 340 |
344 OverlappedBuffer* buffer = pending_write_; | 341 OverlappedBuffer* buffer = pending_write_; |
345 BOOL ok = WriteFile(handle_, | 342 BOOL ok = WriteFile(handle_, |
346 buffer->GetBufferStart(), | 343 buffer->GetBufferStart(), |
347 buffer->GetBufferSize(), | 344 buffer->GetBufferSize(), |
348 NULL, | 345 NULL, |
349 buffer->GetCleanOverlapped()); | 346 buffer->GetCleanOverlapped()); |
350 if (ok || GetLastError() == ERROR_IO_PENDING) { | 347 if (ok || (GetLastError() == ERROR_IO_PENDING)) { |
351 // Completing asynchronously. | 348 // Completing asynchronously. |
352 pending_write_ = buffer; | 349 pending_write_ = buffer; |
353 return true; | 350 return true; |
354 } | 351 } |
355 OverlappedBuffer::DisposeBuffer(buffer); | 352 OverlappedBuffer::DisposeBuffer(buffer); |
356 HandleIssueError(); | 353 HandleIssueError(); |
357 return false; | 354 return false; |
358 } | 355 } |
359 | 356 |
360 | 357 |
(...skipping 26 matching lines...) Expand all Loading... |
387 } else { | 384 } else { |
388 HandleError(this); | 385 HandleError(this); |
389 } | 386 } |
390 SetLastError(error); | 387 SetLastError(error); |
391 } | 388 } |
392 | 389 |
393 | 390 |
394 void FileHandle::EnsureInitialized(EventHandlerImplementation* event_handler) { | 391 void FileHandle::EnsureInitialized(EventHandlerImplementation* event_handler) { |
395 MonitorLocker ml(monitor_); | 392 MonitorLocker ml(monitor_); |
396 event_handler_ = event_handler; | 393 event_handler_ = event_handler; |
397 if (SupportsOverlappedIO() && completion_port_ == INVALID_HANDLE_VALUE) { | 394 if (SupportsOverlappedIO() && (completion_port_ == INVALID_HANDLE_VALUE)) { |
398 CreateCompletionPort(event_handler_->completion_port()); | 395 CreateCompletionPort(event_handler_->completion_port()); |
399 } | 396 } |
400 } | 397 } |
401 | 398 |
402 | 399 |
403 bool FileHandle::IsClosed() { | 400 bool FileHandle::IsClosed() { |
404 return IsClosing() && !HasPendingRead() && !HasPendingWrite(); | 401 return IsClosing() && !HasPendingRead() && !HasPendingWrite(); |
405 } | 402 } |
406 | 403 |
407 | 404 |
408 void DirectoryWatchHandle::EnsureInitialized( | 405 void DirectoryWatchHandle::EnsureInitialized( |
409 EventHandlerImplementation* event_handler) { | 406 EventHandlerImplementation* event_handler) { |
410 MonitorLocker ml(monitor_); | 407 MonitorLocker ml(monitor_); |
411 event_handler_ = event_handler; | 408 event_handler_ = event_handler; |
412 if (completion_port_ == INVALID_HANDLE_VALUE) { | 409 if (completion_port_ == INVALID_HANDLE_VALUE) { |
413 CreateCompletionPort(event_handler_->completion_port()); | 410 CreateCompletionPort(event_handler_->completion_port()); |
414 } | 411 } |
415 } | 412 } |
416 | 413 |
417 | 414 |
418 bool DirectoryWatchHandle::IsClosed() { | 415 bool DirectoryWatchHandle::IsClosed() { |
419 return IsClosing() && pending_read_ == NULL; | 416 return IsClosing() && (pending_read_ == NULL); |
420 } | 417 } |
421 | 418 |
422 | 419 |
423 bool DirectoryWatchHandle::IssueRead() { | 420 bool DirectoryWatchHandle::IssueRead() { |
424 // It may have been started before, as we start the directory-handler when | 421 // It may have been started before, as we start the directory-handler when |
425 // we create it. | 422 // we create it. |
426 if (pending_read_ != NULL || data_ready_ != NULL) return true; | 423 if ((pending_read_ != NULL) || (data_ready_ != NULL)) { |
| 424 return true; |
| 425 } |
427 OverlappedBuffer* buffer = OverlappedBuffer::AllocateReadBuffer(kBufferSize); | 426 OverlappedBuffer* buffer = OverlappedBuffer::AllocateReadBuffer(kBufferSize); |
428 ASSERT(completion_port_ != INVALID_HANDLE_VALUE); | 427 ASSERT(completion_port_ != INVALID_HANDLE_VALUE); |
429 BOOL ok = ReadDirectoryChangesW(handle_, | 428 BOOL ok = ReadDirectoryChangesW(handle_, |
430 buffer->GetBufferStart(), | 429 buffer->GetBufferStart(), |
431 buffer->GetBufferSize(), | 430 buffer->GetBufferSize(), |
432 recursive_, | 431 recursive_, |
433 events_, | 432 events_, |
434 NULL, | 433 NULL, |
435 buffer->GetCleanOverlapped(), | 434 buffer->GetCleanOverlapped(), |
436 NULL); | 435 NULL); |
437 if (ok || GetLastError() == ERROR_IO_PENDING) { | 436 if (ok || (GetLastError() == ERROR_IO_PENDING)) { |
438 // Completing asynchronously. | 437 // Completing asynchronously. |
439 pending_read_ = buffer; | 438 pending_read_ = buffer; |
440 return true; | 439 return true; |
441 } | 440 } |
442 OverlappedBuffer::DisposeBuffer(buffer); | 441 OverlappedBuffer::DisposeBuffer(buffer); |
443 return false; | 442 return false; |
444 } | 443 } |
445 | 444 |
446 | 445 |
447 void DirectoryWatchHandle::Stop() { | 446 void DirectoryWatchHandle::Stop() { |
(...skipping 26 matching lines...) Expand all Loading... |
474 DWORD bytes; | 473 DWORD bytes; |
475 int status = WSAIoctl(socket(), | 474 int status = WSAIoctl(socket(), |
476 SIO_GET_EXTENSION_FUNCTION_POINTER, | 475 SIO_GET_EXTENSION_FUNCTION_POINTER, |
477 &guid_accept_ex, | 476 &guid_accept_ex, |
478 sizeof(guid_accept_ex), | 477 sizeof(guid_accept_ex), |
479 &AcceptEx_, | 478 &AcceptEx_, |
480 sizeof(AcceptEx_), | 479 sizeof(AcceptEx_), |
481 &bytes, | 480 &bytes, |
482 NULL, | 481 NULL, |
483 NULL); | 482 NULL); |
484 if (status == SOCKET_ERROR) { | 483 return (status != SOCKET_ERROR); |
485 return false; | |
486 } | |
487 return true; | |
488 } | 484 } |
489 | 485 |
490 | 486 |
491 bool ListenSocket::IssueAccept() { | 487 bool ListenSocket::IssueAccept() { |
492 MonitorLocker ml(monitor_); | 488 MonitorLocker ml(monitor_); |
493 | 489 |
494 // For AcceptEx there needs to be buffer storage for address | 490 // For AcceptEx there needs to be buffer storage for address |
495 // information for two addresses (local and remote address). The | 491 // information for two addresses (local and remote address). The |
496 // AcceptEx documentation says: "This value must be at least 16 | 492 // AcceptEx documentation says: "This value must be at least 16 |
497 // bytes more than the maximum address length for the transport | 493 // bytes more than the maximum address length for the transport |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
596 | 592 |
597 | 593 |
598 ClientSocket* ListenSocket::Accept() { | 594 ClientSocket* ListenSocket::Accept() { |
599 MonitorLocker ml(monitor_); | 595 MonitorLocker ml(monitor_); |
600 | 596 |
601 ClientSocket *result = NULL; | 597 ClientSocket *result = NULL; |
602 | 598 |
603 if (accepted_head_ != NULL) { | 599 if (accepted_head_ != NULL) { |
604 result = accepted_head_; | 600 result = accepted_head_; |
605 accepted_head_ = accepted_head_->next(); | 601 accepted_head_ = accepted_head_->next(); |
606 if (accepted_head_ == NULL) accepted_tail_ = NULL; | 602 if (accepted_head_ == NULL) { |
| 603 accepted_tail_ = NULL; |
| 604 } |
607 result->set_next(NULL); | 605 result->set_next(NULL); |
608 accepted_count_--; | 606 accepted_count_--; |
609 } | 607 } |
610 | 608 |
611 if (pending_accept_count_ < 5) { | 609 if (pending_accept_count_ < 5) { |
612 // We have less than 5 pending accepts, queue another. | 610 // We have less than 5 pending accepts, queue another. |
613 if (!IsClosing()) { | 611 if (!IsClosing()) { |
614 if (!IssueAccept()) { | 612 if (!IssueAccept()) { |
615 HandleError(this); | 613 HandleError(this); |
616 } | 614 } |
(...skipping 17 matching lines...) Expand all Loading... |
634 } | 632 } |
635 | 633 |
636 | 634 |
637 bool ListenSocket::IsClosed() { | 635 bool ListenSocket::IsClosed() { |
638 return IsClosing() && !HasPendingAccept(); | 636 return IsClosing() && !HasPendingAccept(); |
639 } | 637 } |
640 | 638 |
641 | 639 |
642 intptr_t Handle::Available() { | 640 intptr_t Handle::Available() { |
643 MonitorLocker ml(monitor_); | 641 MonitorLocker ml(monitor_); |
644 if (data_ready_ == NULL) return 0; | 642 if (data_ready_ == NULL) { |
| 643 return 0; |
| 644 } |
645 ASSERT(!data_ready_->IsEmpty()); | 645 ASSERT(!data_ready_->IsEmpty()); |
646 return data_ready_->GetRemainingLength(); | 646 return data_ready_->GetRemainingLength(); |
647 } | 647 } |
648 | 648 |
649 | 649 |
650 intptr_t Handle::Read(void* buffer, intptr_t num_bytes) { | 650 intptr_t Handle::Read(void* buffer, intptr_t num_bytes) { |
651 MonitorLocker ml(monitor_); | 651 MonitorLocker ml(monitor_); |
652 if (data_ready_ == NULL) return 0; | 652 if (data_ready_ == NULL) { |
| 653 return 0; |
| 654 } |
653 num_bytes = data_ready_->Read( | 655 num_bytes = data_ready_->Read( |
654 buffer, Utils::Minimum<intptr_t>(num_bytes, INT_MAX)); | 656 buffer, Utils::Minimum<intptr_t>(num_bytes, INT_MAX)); |
655 if (data_ready_->IsEmpty()) { | 657 if (data_ready_->IsEmpty()) { |
656 OverlappedBuffer::DisposeBuffer(data_ready_); | 658 OverlappedBuffer::DisposeBuffer(data_ready_); |
657 data_ready_ = NULL; | 659 data_ready_ = NULL; |
658 if (!IsClosing() && !IsClosedRead()) IssueRead(); | 660 if (!IsClosing() && !IsClosedRead()) { |
| 661 IssueRead(); |
| 662 } |
659 } | 663 } |
660 return num_bytes; | 664 return num_bytes; |
661 } | 665 } |
662 | 666 |
663 | 667 |
664 intptr_t Handle::RecvFrom( | 668 intptr_t Handle::RecvFrom( |
665 void* buffer, intptr_t num_bytes, struct sockaddr* sa, socklen_t sa_len) { | 669 void* buffer, intptr_t num_bytes, struct sockaddr* sa, socklen_t sa_len) { |
666 MonitorLocker ml(monitor_); | 670 MonitorLocker ml(monitor_); |
667 if (data_ready_ == NULL) return 0; | 671 if (data_ready_ == NULL) { |
| 672 return 0; |
| 673 } |
668 num_bytes = data_ready_->Read( | 674 num_bytes = data_ready_->Read( |
669 buffer, Utils::Minimum<intptr_t>(num_bytes, INT_MAX)); | 675 buffer, Utils::Minimum<intptr_t>(num_bytes, INT_MAX)); |
670 if (data_ready_->from()->sa_family == AF_INET) { | 676 if (data_ready_->from()->sa_family == AF_INET) { |
671 ASSERT(sa_len >= sizeof(struct sockaddr_in)); | 677 ASSERT(sa_len >= sizeof(struct sockaddr_in)); |
672 memmove(sa, data_ready_->from(), sizeof(struct sockaddr_in)); | 678 memmove(sa, data_ready_->from(), sizeof(struct sockaddr_in)); |
673 } else { | 679 } else { |
674 ASSERT(data_ready_->from()->sa_family == AF_INET6); | 680 ASSERT(data_ready_->from()->sa_family == AF_INET6); |
675 ASSERT(sa_len >= sizeof(struct sockaddr_in6)); | 681 ASSERT(sa_len >= sizeof(struct sockaddr_in6)); |
676 memmove(sa, data_ready_->from(), sizeof(struct sockaddr_in6)); | 682 memmove(sa, data_ready_->from(), sizeof(struct sockaddr_in6)); |
677 } | 683 } |
678 // Always dispose of the buffer, as UDP messages must be read in their | 684 // Always dispose of the buffer, as UDP messages must be read in their |
679 // entirety to match how recvfrom works in a socket. | 685 // entirety to match how recvfrom works in a socket. |
680 OverlappedBuffer::DisposeBuffer(data_ready_); | 686 OverlappedBuffer::DisposeBuffer(data_ready_); |
681 data_ready_ = NULL; | 687 data_ready_ = NULL; |
682 if (!IsClosing() && !IsClosedRead()) IssueRecvFrom(); | 688 if (!IsClosing() && !IsClosedRead()) { |
| 689 IssueRecvFrom(); |
| 690 } |
683 return num_bytes; | 691 return num_bytes; |
684 } | 692 } |
685 | 693 |
686 | 694 |
687 intptr_t Handle::Write(const void* buffer, intptr_t num_bytes) { | 695 intptr_t Handle::Write(const void* buffer, intptr_t num_bytes) { |
688 MonitorLocker ml(monitor_); | 696 MonitorLocker ml(monitor_); |
689 if (pending_write_ != NULL) return 0; | 697 if (pending_write_ != NULL) { |
690 if (num_bytes > kBufferSize) num_bytes = kBufferSize; | 698 return 0; |
| 699 } |
| 700 if (num_bytes > kBufferSize) { |
| 701 num_bytes = kBufferSize; |
| 702 } |
691 ASSERT(SupportsOverlappedIO()); | 703 ASSERT(SupportsOverlappedIO()); |
692 if (completion_port_ == INVALID_HANDLE_VALUE) return 0; | 704 if (completion_port_ == INVALID_HANDLE_VALUE) { |
| 705 return 0; |
| 706 } |
693 int truncated_bytes = Utils::Minimum<intptr_t>(num_bytes, INT_MAX); | 707 int truncated_bytes = Utils::Minimum<intptr_t>(num_bytes, INT_MAX); |
694 pending_write_ = OverlappedBuffer::AllocateWriteBuffer(truncated_bytes); | 708 pending_write_ = OverlappedBuffer::AllocateWriteBuffer(truncated_bytes); |
695 pending_write_->Write(buffer, truncated_bytes); | 709 pending_write_->Write(buffer, truncated_bytes); |
696 if (!IssueWrite()) return -1; | 710 if (!IssueWrite()) { |
| 711 return -1; |
| 712 } |
697 return truncated_bytes; | 713 return truncated_bytes; |
698 } | 714 } |
699 | 715 |
700 | 716 |
701 intptr_t Handle::SendTo(const void* buffer, | 717 intptr_t Handle::SendTo(const void* buffer, |
702 intptr_t num_bytes, | 718 intptr_t num_bytes, |
703 struct sockaddr* sa, | 719 struct sockaddr* sa, |
704 socklen_t sa_len) { | 720 socklen_t sa_len) { |
705 MonitorLocker ml(monitor_); | 721 MonitorLocker ml(monitor_); |
706 if (pending_write_ != NULL) return 0; | 722 if (pending_write_ != NULL) { |
707 if (num_bytes > kBufferSize) num_bytes = kBufferSize; | 723 return 0; |
| 724 } |
| 725 if (num_bytes > kBufferSize) { |
| 726 num_bytes = kBufferSize; |
| 727 } |
708 ASSERT(SupportsOverlappedIO()); | 728 ASSERT(SupportsOverlappedIO()); |
709 if (completion_port_ == INVALID_HANDLE_VALUE) return 0; | 729 if (completion_port_ == INVALID_HANDLE_VALUE) { |
| 730 return 0; |
| 731 } |
710 pending_write_ = OverlappedBuffer::AllocateSendToBuffer(num_bytes); | 732 pending_write_ = OverlappedBuffer::AllocateSendToBuffer(num_bytes); |
711 pending_write_->Write(buffer, num_bytes); | 733 pending_write_->Write(buffer, num_bytes); |
712 if (!IssueSendTo(sa, sa_len)) return -1; | 734 if (!IssueSendTo(sa, sa_len)) { |
| 735 return -1; |
| 736 } |
713 return num_bytes; | 737 return num_bytes; |
714 } | 738 } |
715 | 739 |
716 | 740 |
717 static void WriteFileThread(uword args) { | 741 static void WriteFileThread(uword args) { |
718 StdHandle* handle = reinterpret_cast<StdHandle*>(args); | 742 StdHandle* handle = reinterpret_cast<StdHandle*>(args); |
719 handle->RunWriteLoop(); | 743 handle->RunWriteLoop(); |
720 } | 744 } |
721 | 745 |
722 | 746 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
758 bytes_written, | 782 bytes_written, |
759 reinterpret_cast<ULONG_PTR>(this), | 783 reinterpret_cast<ULONG_PTR>(this), |
760 overlapped); | 784 overlapped); |
761 if (!ok) { | 785 if (!ok) { |
762 FATAL("PostQueuedCompletionStatus failed"); | 786 FATAL("PostQueuedCompletionStatus failed"); |
763 } | 787 } |
764 } | 788 } |
765 | 789 |
766 intptr_t StdHandle::Write(const void* buffer, intptr_t num_bytes) { | 790 intptr_t StdHandle::Write(const void* buffer, intptr_t num_bytes) { |
767 MonitorLocker ml(monitor_); | 791 MonitorLocker ml(monitor_); |
768 if (pending_write_ != NULL) return 0; | 792 if (pending_write_ != NULL) { |
769 if (num_bytes > kBufferSize) num_bytes = kBufferSize; | 793 return 0; |
| 794 } |
| 795 if (num_bytes > kBufferSize) { |
| 796 num_bytes = kBufferSize; |
| 797 } |
770 // In the case of stdout and stderr, OverlappedIO is not supported. | 798 // In the case of stdout and stderr, OverlappedIO is not supported. |
771 // Here we'll instead use a thread, to make it async. | 799 // Here we'll instead use a thread, to make it async. |
772 // This code is actually never exposed to the user, as stdout and stderr is | 800 // This code is actually never exposed to the user, as stdout and stderr is |
773 // not available as a RawSocket, but only wrapped in a Socket. | 801 // not available as a RawSocket, but only wrapped in a Socket. |
774 // Note that we return '0', unless a thread have already completed a write. | 802 // Note that we return '0', unless a thread have already completed a write. |
775 if (thread_wrote_ > 0) { | 803 if (thread_wrote_ > 0) { |
776 if (num_bytes > thread_wrote_) num_bytes = thread_wrote_; | 804 if (num_bytes > thread_wrote_) { |
| 805 num_bytes = thread_wrote_; |
| 806 } |
777 thread_wrote_ -= num_bytes; | 807 thread_wrote_ -= num_bytes; |
778 return num_bytes; | 808 return num_bytes; |
779 } | 809 } |
780 if (!write_thread_exists_) { | 810 if (!write_thread_exists_) { |
781 write_thread_exists_ = true; | 811 write_thread_exists_ = true; |
782 int result = Thread::Start( | 812 int result = Thread::Start( |
783 WriteFileThread, reinterpret_cast<uword>(this)); | 813 WriteFileThread, reinterpret_cast<uword>(this)); |
784 if (result != 0) { | 814 if (result != 0) { |
785 FATAL1("Failed to start write file thread %d", result); | 815 FATAL1("Failed to start write file thread %d", result); |
786 } | 816 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
819 DWORD bytes; | 849 DWORD bytes; |
820 int status = WSAIoctl(socket(), | 850 int status = WSAIoctl(socket(), |
821 SIO_GET_EXTENSION_FUNCTION_POINTER, | 851 SIO_GET_EXTENSION_FUNCTION_POINTER, |
822 &guid_disconnect_ex, | 852 &guid_disconnect_ex, |
823 sizeof(guid_disconnect_ex), | 853 sizeof(guid_disconnect_ex), |
824 &DisconnectEx_, | 854 &DisconnectEx_, |
825 sizeof(DisconnectEx_), | 855 sizeof(DisconnectEx_), |
826 &bytes, | 856 &bytes, |
827 NULL, | 857 NULL, |
828 NULL); | 858 NULL); |
829 if (status == SOCKET_ERROR) { | 859 return (status != SOCKET_ERROR); |
830 return false; | |
831 } | |
832 return true; | |
833 } | 860 } |
834 | 861 |
835 | 862 |
836 void ClientSocket::Shutdown(int how) { | 863 void ClientSocket::Shutdown(int how) { |
837 int rc = shutdown(socket(), how); | 864 int rc = shutdown(socket(), how); |
838 if (how == SD_RECEIVE) MarkClosedRead(); | 865 if (how == SD_RECEIVE) { |
839 if (how == SD_SEND) MarkClosedWrite(); | 866 MarkClosedRead(); |
| 867 } |
| 868 if (how == SD_SEND) { |
| 869 MarkClosedWrite(); |
| 870 } |
840 if (how == SD_BOTH) { | 871 if (how == SD_BOTH) { |
841 MarkClosedRead(); | 872 MarkClosedRead(); |
842 MarkClosedWrite(); | 873 MarkClosedWrite(); |
843 } | 874 } |
844 } | 875 } |
845 | 876 |
846 | 877 |
847 void ClientSocket::DoClose() { | 878 void ClientSocket::DoClose() { |
848 // Always do a shutdown before initiating a disconnect. | 879 // Always do a shutdown before initiating a disconnect. |
849 shutdown(socket(), SD_BOTH); | 880 shutdown(socket(), SD_BOTH); |
(...skipping 13 matching lines...) Expand all Loading... |
863 | 894 |
864 DWORD flags; | 895 DWORD flags; |
865 flags = 0; | 896 flags = 0; |
866 int rc = WSARecv(socket(), | 897 int rc = WSARecv(socket(), |
867 buffer->GetWASBUF(), | 898 buffer->GetWASBUF(), |
868 1, | 899 1, |
869 NULL, | 900 NULL, |
870 &flags, | 901 &flags, |
871 buffer->GetCleanOverlapped(), | 902 buffer->GetCleanOverlapped(), |
872 NULL); | 903 NULL); |
873 if (rc == NO_ERROR || WSAGetLastError() == WSA_IO_PENDING) { | 904 if ((rc == NO_ERROR) || (WSAGetLastError() == WSA_IO_PENDING)) { |
874 pending_read_ = buffer; | 905 pending_read_ = buffer; |
875 return true; | 906 return true; |
876 } | 907 } |
877 OverlappedBuffer::DisposeBuffer(buffer); | 908 OverlappedBuffer::DisposeBuffer(buffer); |
878 pending_read_ = NULL; | 909 pending_read_ = NULL; |
879 HandleIssueError(); | 910 HandleIssueError(); |
880 return false; | 911 return false; |
881 } | 912 } |
882 | 913 |
883 | 914 |
884 bool ClientSocket::IssueWrite() { | 915 bool ClientSocket::IssueWrite() { |
885 MonitorLocker ml(monitor_); | 916 MonitorLocker ml(monitor_); |
886 ASSERT(completion_port_ != INVALID_HANDLE_VALUE); | 917 ASSERT(completion_port_ != INVALID_HANDLE_VALUE); |
887 ASSERT(pending_write_ != NULL); | 918 ASSERT(pending_write_ != NULL); |
888 ASSERT(pending_write_->operation() == OverlappedBuffer::kWrite); | 919 ASSERT(pending_write_->operation() == OverlappedBuffer::kWrite); |
889 | 920 |
890 int rc = WSASend(socket(), | 921 int rc = WSASend(socket(), |
891 pending_write_->GetWASBUF(), | 922 pending_write_->GetWASBUF(), |
892 1, | 923 1, |
893 NULL, | 924 NULL, |
894 0, | 925 0, |
895 pending_write_->GetCleanOverlapped(), | 926 pending_write_->GetCleanOverlapped(), |
896 NULL); | 927 NULL); |
897 if (rc == NO_ERROR || WSAGetLastError() == WSA_IO_PENDING) { | 928 if ((rc == NO_ERROR) || (WSAGetLastError() == WSA_IO_PENDING)) { |
898 return true; | 929 return true; |
899 } | 930 } |
900 OverlappedBuffer::DisposeBuffer(pending_write_); | 931 OverlappedBuffer::DisposeBuffer(pending_write_); |
901 pending_write_ = NULL; | 932 pending_write_ = NULL; |
902 HandleIssueError(); | 933 HandleIssueError(); |
903 return false; | 934 return false; |
904 } | 935 } |
905 | 936 |
906 | 937 |
907 void ClientSocket::IssueDisconnect() { | 938 void ClientSocket::IssueDisconnect() { |
908 OverlappedBuffer* buffer = OverlappedBuffer::AllocateDisconnectBuffer(); | 939 OverlappedBuffer* buffer = OverlappedBuffer::AllocateDisconnectBuffer(); |
909 BOOL ok = DisconnectEx_( | 940 BOOL ok = DisconnectEx_( |
910 socket(), buffer->GetCleanOverlapped(), TF_REUSE_SOCKET, 0); | 941 socket(), buffer->GetCleanOverlapped(), TF_REUSE_SOCKET, 0); |
911 // DisconnectEx works like other OverlappedIO APIs, where we can get either an | 942 // DisconnectEx works like other OverlappedIO APIs, where we can get either an |
912 // immediate success or delayed operation by WSA_IO_PENDING being set. | 943 // immediate success or delayed operation by WSA_IO_PENDING being set. |
913 if (ok || WSAGetLastError() != WSA_IO_PENDING) { | 944 if (ok || (WSAGetLastError() != WSA_IO_PENDING)) { |
914 DisconnectComplete(buffer); | 945 DisconnectComplete(buffer); |
915 } | 946 } |
916 NotifyAllDartPorts(1 << kDestroyedEvent); | 947 NotifyAllDartPorts(1 << kDestroyedEvent); |
917 RemoveAllPorts(); | 948 RemoveAllPorts(); |
918 } | 949 } |
919 | 950 |
920 | 951 |
921 void ClientSocket::DisconnectComplete(OverlappedBuffer* buffer) { | 952 void ClientSocket::DisconnectComplete(OverlappedBuffer* buffer) { |
922 OverlappedBuffer::DisposeBuffer(buffer); | 953 OverlappedBuffer::DisposeBuffer(buffer); |
923 closesocket(socket()); | 954 closesocket(socket()); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
968 | 999 |
969 int rc = WSASendTo(socket(), | 1000 int rc = WSASendTo(socket(), |
970 pending_write_->GetWASBUF(), | 1001 pending_write_->GetWASBUF(), |
971 1, | 1002 1, |
972 NULL, | 1003 NULL, |
973 0, | 1004 0, |
974 sa, | 1005 sa, |
975 sa_len, | 1006 sa_len, |
976 pending_write_->GetCleanOverlapped(), | 1007 pending_write_->GetCleanOverlapped(), |
977 NULL); | 1008 NULL); |
978 if (rc == NO_ERROR || WSAGetLastError() == WSA_IO_PENDING) { | 1009 if ((rc == NO_ERROR) || (WSAGetLastError() == WSA_IO_PENDING)) { |
979 return true; | 1010 return true; |
980 } | 1011 } |
981 OverlappedBuffer::DisposeBuffer(pending_write_); | 1012 OverlappedBuffer::DisposeBuffer(pending_write_); |
982 pending_write_ = NULL; | 1013 pending_write_ = NULL; |
983 HandleIssueError(); | 1014 HandleIssueError(); |
984 return false; | 1015 return false; |
985 } | 1016 } |
986 | 1017 |
987 | 1018 |
988 bool DatagramSocket::IssueRecvFrom() { | 1019 bool DatagramSocket::IssueRecvFrom() { |
989 MonitorLocker ml(monitor_); | 1020 MonitorLocker ml(monitor_); |
990 ASSERT(completion_port_ != INVALID_HANDLE_VALUE); | 1021 ASSERT(completion_port_ != INVALID_HANDLE_VALUE); |
991 ASSERT(pending_read_ == NULL); | 1022 ASSERT(pending_read_ == NULL); |
992 | 1023 |
993 OverlappedBuffer* buffer = | 1024 OverlappedBuffer* buffer = |
994 OverlappedBuffer::AllocateRecvFromBuffer(kMaxUDPPackageLength); | 1025 OverlappedBuffer::AllocateRecvFromBuffer(kMaxUDPPackageLength); |
995 | 1026 |
996 DWORD flags; | 1027 DWORD flags; |
997 flags = 0; | 1028 flags = 0; |
998 int rc = WSARecvFrom(socket(), | 1029 int rc = WSARecvFrom(socket(), |
999 buffer->GetWASBUF(), | 1030 buffer->GetWASBUF(), |
1000 1, | 1031 1, |
1001 NULL, | 1032 NULL, |
1002 &flags, | 1033 &flags, |
1003 buffer->from(), | 1034 buffer->from(), |
1004 buffer->from_len_addr(), | 1035 buffer->from_len_addr(), |
1005 buffer->GetCleanOverlapped(), | 1036 buffer->GetCleanOverlapped(), |
1006 NULL); | 1037 NULL); |
1007 if (rc == NO_ERROR || WSAGetLastError() == WSA_IO_PENDING) { | 1038 if ((rc == NO_ERROR) || (WSAGetLastError() == WSA_IO_PENDING)) { |
1008 pending_read_ = buffer; | 1039 pending_read_ = buffer; |
1009 return true; | 1040 return true; |
1010 } | 1041 } |
1011 OverlappedBuffer::DisposeBuffer(buffer); | 1042 OverlappedBuffer::DisposeBuffer(buffer); |
1012 pending_read_ = NULL; | 1043 pending_read_ = NULL; |
1013 HandleIssueError(); | 1044 HandleIssueError(); |
1014 return false; | 1045 return false; |
1015 } | 1046 } |
1016 | 1047 |
1017 | 1048 |
1018 void DatagramSocket::EnsureInitialized( | 1049 void DatagramSocket::EnsureInitialized( |
1019 EventHandlerImplementation* event_handler) { | 1050 EventHandlerImplementation* event_handler) { |
1020 MonitorLocker ml(monitor_); | 1051 MonitorLocker ml(monitor_); |
1021 if (completion_port_ == INVALID_HANDLE_VALUE) { | 1052 if ((completion_port_ == INVALID_HANDLE_VALUE)) { |
1022 ASSERT(event_handler_ == NULL); | 1053 ASSERT(event_handler_ == NULL); |
1023 event_handler_ = event_handler; | 1054 event_handler_ = event_handler; |
1024 CreateCompletionPort(event_handler_->completion_port()); | 1055 CreateCompletionPort(event_handler_->completion_port()); |
1025 } | 1056 } |
1026 } | 1057 } |
1027 | 1058 |
1028 | 1059 |
1029 bool DatagramSocket::IsClosed() { | 1060 bool DatagramSocket::IsClosed() { |
1030 return IsClosing() && !HasPendingRead() && !HasPendingWrite(); | 1061 return IsClosing() && !HasPendingRead() && !HasPendingWrite(); |
1031 } | 1062 } |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1167 | 1198 |
1168 DeleteIfClosed(listen_socket); | 1199 DeleteIfClosed(listen_socket); |
1169 } | 1200 } |
1170 | 1201 |
1171 | 1202 |
1172 void EventHandlerImplementation::TryDispatchingPendingAccepts( | 1203 void EventHandlerImplementation::TryDispatchingPendingAccepts( |
1173 ListenSocket *listen_socket) { | 1204 ListenSocket *listen_socket) { |
1174 if (!listen_socket->IsClosing() && listen_socket->CanAccept()) { | 1205 if (!listen_socket->IsClosing() && listen_socket->CanAccept()) { |
1175 intptr_t event_mask = 1 << kInEvent; | 1206 intptr_t event_mask = 1 << kInEvent; |
1176 for (int i = 0; | 1207 for (int i = 0; |
1177 i < listen_socket->accepted_count() && | 1208 (i < listen_socket->accepted_count()) && |
1178 listen_socket->Mask() == event_mask; | 1209 (listen_socket->Mask() == event_mask); |
1179 i++) { | 1210 i++) { |
1180 Dart_Port port = listen_socket->NextNotifyDartPort(event_mask); | 1211 Dart_Port port = listen_socket->NextNotifyDartPort(event_mask); |
1181 DartUtils::PostInt32(port, event_mask); | 1212 DartUtils::PostInt32(port, event_mask); |
1182 } | 1213 } |
1183 } | 1214 } |
1184 } | 1215 } |
1185 | 1216 |
1186 | 1217 |
1187 void EventHandlerImplementation::HandleRead(Handle* handle, | 1218 void EventHandlerImplementation::HandleRead(Handle* handle, |
1188 int bytes, | 1219 int bytes, |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1273 OverlappedBuffer::DisposeBuffer(buffer); | 1304 OverlappedBuffer::DisposeBuffer(buffer); |
1274 } else { | 1305 } else { |
1275 client_socket->ConnectComplete(buffer); | 1306 client_socket->ConnectComplete(buffer); |
1276 } | 1307 } |
1277 client_socket->mark_connected(); | 1308 client_socket->mark_connected(); |
1278 DeleteIfClosed(client_socket); | 1309 DeleteIfClosed(client_socket); |
1279 } | 1310 } |
1280 | 1311 |
1281 | 1312 |
1282 void EventHandlerImplementation::HandleTimeout() { | 1313 void EventHandlerImplementation::HandleTimeout() { |
1283 if (!timeout_queue_.HasTimeout()) return; | 1314 if (!timeout_queue_.HasTimeout()) { |
| 1315 return; |
| 1316 } |
1284 DartUtils::PostNull(timeout_queue_.CurrentPort()); | 1317 DartUtils::PostNull(timeout_queue_.CurrentPort()); |
1285 timeout_queue_.RemoveCurrent(); | 1318 timeout_queue_.RemoveCurrent(); |
1286 } | 1319 } |
1287 | 1320 |
1288 | 1321 |
1289 void EventHandlerImplementation::HandleIOCompletion(DWORD bytes, | 1322 void EventHandlerImplementation::HandleIOCompletion(DWORD bytes, |
1290 ULONG_PTR key, | 1323 ULONG_PTR key, |
1291 OVERLAPPED* overlapped) { | 1324 OVERLAPPED* overlapped) { |
1292 OverlappedBuffer* buffer = OverlappedBuffer::GetFromOverlapped(overlapped); | 1325 OverlappedBuffer* buffer = OverlappedBuffer::GetFromOverlapped(overlapped); |
1293 switch (buffer->operation()) { | 1326 switch (buffer->operation()) { |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1382 handler_impl->handler_thread_id_ = Thread::GetCurrentThreadId(); | 1415 handler_impl->handler_thread_id_ = Thread::GetCurrentThreadId(); |
1383 ml.Notify(); | 1416 ml.Notify(); |
1384 } | 1417 } |
1385 | 1418 |
1386 while (!handler_impl->shutdown_) { | 1419 while (!handler_impl->shutdown_) { |
1387 DWORD bytes; | 1420 DWORD bytes; |
1388 ULONG_PTR key; | 1421 ULONG_PTR key; |
1389 OVERLAPPED* overlapped; | 1422 OVERLAPPED* overlapped; |
1390 int64_t millis = handler_impl->GetTimeout(); | 1423 int64_t millis = handler_impl->GetTimeout(); |
1391 ASSERT(millis == kInfinityTimeout || millis >= 0); | 1424 ASSERT(millis == kInfinityTimeout || millis >= 0); |
1392 if (millis > kMaxInt32) millis = kMaxInt32; | 1425 if (millis > kMaxInt32) { |
| 1426 millis = kMaxInt32; |
| 1427 } |
1393 ASSERT(sizeof(int32_t) == sizeof(DWORD)); | 1428 ASSERT(sizeof(int32_t) == sizeof(DWORD)); |
1394 BOOL ok = GetQueuedCompletionStatus(handler_impl->completion_port(), | 1429 BOOL ok = GetQueuedCompletionStatus(handler_impl->completion_port(), |
1395 &bytes, | 1430 &bytes, |
1396 &key, | 1431 &key, |
1397 &overlapped, | 1432 &overlapped, |
1398 static_cast<DWORD>(millis)); | 1433 static_cast<DWORD>(millis)); |
1399 | 1434 |
1400 if (!ok && overlapped == NULL) { | 1435 if (!ok && (overlapped == NULL)) { |
1401 if (GetLastError() == ERROR_ABANDONED_WAIT_0) { | 1436 if (GetLastError() == ERROR_ABANDONED_WAIT_0) { |
1402 // The completion port should never be closed. | 1437 // The completion port should never be closed. |
1403 Log::Print("Completion port closed\n"); | 1438 Log::Print("Completion port closed\n"); |
1404 UNREACHABLE(); | 1439 UNREACHABLE(); |
1405 } else { | 1440 } else { |
1406 // Timeout is signalled by false result and NULL in overlapped. | 1441 // Timeout is signalled by false result and NULL in overlapped. |
1407 handler_impl->HandleTimeout(); | 1442 handler_impl->HandleTimeout(); |
1408 } | 1443 } |
1409 } else if (!ok) { | 1444 } else if (!ok) { |
1410 // Treat ERROR_CONNECTION_ABORTED as connection closed. | 1445 // Treat ERROR_CONNECTION_ABORTED as connection closed. |
1411 // The error ERROR_OPERATION_ABORTED is set for pending | 1446 // The error ERROR_OPERATION_ABORTED is set for pending |
1412 // accept requests for a listen socket which is closed. | 1447 // accept requests for a listen socket which is closed. |
1413 // ERROR_NETNAME_DELETED occurs when the client closes | 1448 // ERROR_NETNAME_DELETED occurs when the client closes |
1414 // the socket it is reading from. | 1449 // the socket it is reading from. |
1415 DWORD last_error = GetLastError(); | 1450 DWORD last_error = GetLastError(); |
1416 if (last_error == ERROR_CONNECTION_ABORTED || | 1451 if ((last_error == ERROR_CONNECTION_ABORTED) || |
1417 last_error == ERROR_OPERATION_ABORTED || | 1452 (last_error == ERROR_OPERATION_ABORTED) || |
1418 last_error == ERROR_NETNAME_DELETED || | 1453 (last_error == ERROR_NETNAME_DELETED) || |
1419 last_error == ERROR_BROKEN_PIPE) { | 1454 (last_error == ERROR_BROKEN_PIPE)) { |
1420 ASSERT(bytes == 0); | 1455 ASSERT(bytes == 0); |
1421 handler_impl->HandleIOCompletion(bytes, key, overlapped); | 1456 handler_impl->HandleIOCompletion(bytes, key, overlapped); |
1422 } else if (last_error == ERROR_MORE_DATA) { | 1457 } else if (last_error == ERROR_MORE_DATA) { |
1423 // Don't ASSERT no bytes in this case. This can happen if the receive | 1458 // Don't ASSERT no bytes in this case. This can happen if the receive |
1424 // buffer for datagram sockets is to small to contain a full datagram, | 1459 // buffer for datagram sockets is to small to contain a full datagram, |
1425 // and in this case bytes hold the bytes that was read. | 1460 // and in this case bytes hold the bytes that was read. |
1426 handler_impl->HandleIOCompletion(-1, key, overlapped); | 1461 handler_impl->HandleIOCompletion(-1, key, overlapped); |
1427 } else { | 1462 } else { |
1428 ASSERT(bytes == 0); | 1463 ASSERT(bytes == 0); |
1429 handler_impl->HandleIOCompletion(-1, key, overlapped); | 1464 handler_impl->HandleIOCompletion(-1, key, overlapped); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1463 | 1498 |
1464 | 1499 |
1465 void EventHandlerImplementation::Shutdown() { | 1500 void EventHandlerImplementation::Shutdown() { |
1466 SendData(kShutdownId, 0, 0); | 1501 SendData(kShutdownId, 0, 0); |
1467 } | 1502 } |
1468 | 1503 |
1469 } // namespace bin | 1504 } // namespace bin |
1470 } // namespace dart | 1505 } // namespace dart |
1471 | 1506 |
1472 #endif // defined(TARGET_OS_WINDOWS) | 1507 #endif // defined(TARGET_OS_WINDOWS) |
OLD | NEW |