Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, 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 | 9 |
| 10 #include <process.h> // NOLINT | 10 #include <process.h> // NOLINT |
| 11 #include <winsock2.h> // NOLINT | 11 #include <winsock2.h> // NOLINT |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 317 void FileHandle::EnsureInitialized(EventHandlerImplementation* event_handler) { | 317 void FileHandle::EnsureInitialized(EventHandlerImplementation* event_handler) { |
| 318 ScopedLock lock(this); | 318 ScopedLock lock(this); |
| 319 event_handler_ = event_handler; | 319 event_handler_ = event_handler; |
| 320 if (SupportsOverlappedIO() && completion_port_ == INVALID_HANDLE_VALUE) { | 320 if (SupportsOverlappedIO() && completion_port_ == INVALID_HANDLE_VALUE) { |
| 321 CreateCompletionPort(event_handler_->completion_port()); | 321 CreateCompletionPort(event_handler_->completion_port()); |
| 322 } | 322 } |
| 323 } | 323 } |
| 324 | 324 |
| 325 | 325 |
| 326 bool FileHandle::IsClosed() { | 326 bool FileHandle::IsClosed() { |
| 327 return false; | 327 return IsClosing(); |
| 328 } | 328 } |
| 329 | 329 |
| 330 | 330 |
| 331 void FileHandle::DoClose() { | 331 void FileHandle::DoClose() { |
| 332 if (GetStdHandle(STD_OUTPUT_HANDLE) == handle_) { | 332 if (GetStdHandle(STD_OUTPUT_HANDLE) == handle_) { |
| 333 int fd = _open("NUL", _O_WRONLY); | 333 int fd = _open("NUL", _O_WRONLY); |
| 334 ASSERT(fd >= 0); | 334 ASSERT(fd >= 0); |
| 335 _dup2(fd, _fileno(stdout)); | 335 _dup2(fd, _fileno(stdout)); |
| 336 close(fd); | 336 close(fd); |
| 337 } else { | 337 } else { |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 566 if (how == SD_RECEIVE) MarkClosedRead(); | 566 if (how == SD_RECEIVE) MarkClosedRead(); |
| 567 if (how == SD_SEND) MarkClosedWrite(); | 567 if (how == SD_SEND) MarkClosedWrite(); |
| 568 if (how == SD_BOTH) { | 568 if (how == SD_BOTH) { |
| 569 MarkClosedRead(); | 569 MarkClosedRead(); |
| 570 MarkClosedWrite(); | 570 MarkClosedWrite(); |
| 571 } | 571 } |
| 572 } | 572 } |
| 573 | 573 |
| 574 | 574 |
| 575 void ClientSocket::DoClose() { | 575 void ClientSocket::DoClose() { |
| 576 // Always do a suhtdown before initiating a disconnect. | 576 // Always do a shutdown before initiating a disconnect. |
| 577 shutdown(socket(), SD_BOTH); | 577 shutdown(socket(), SD_BOTH); |
| 578 IssueDisconnect(); | 578 IssueDisconnect(); |
| 579 } | 579 } |
| 580 | 580 |
| 581 | 581 |
| 582 bool ClientSocket::IssueRead() { | 582 bool ClientSocket::IssueRead() { |
| 583 ScopedLock lock(this); | 583 ScopedLock lock(this); |
| 584 ASSERT(completion_port_ != INVALID_HANDLE_VALUE); | 584 ASSERT(completion_port_ != INVALID_HANDLE_VALUE); |
| 585 ASSERT(pending_read_ == NULL); | 585 ASSERT(pending_read_ == NULL); |
| 586 | 586 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 623 return true; | 623 return true; |
| 624 } | 624 } |
| 625 IOBuffer::DisposeBuffer(pending_write_); | 625 IOBuffer::DisposeBuffer(pending_write_); |
| 626 pending_write_ = NULL; | 626 pending_write_ = NULL; |
| 627 HandleIssueError(); | 627 HandleIssueError(); |
| 628 return false; | 628 return false; |
| 629 } | 629 } |
| 630 | 630 |
| 631 | 631 |
| 632 void ClientSocket::IssueDisconnect() { | 632 void ClientSocket::IssueDisconnect() { |
| 633 Dart_Port p = port(); | |
| 633 IOBuffer* buffer = IOBuffer::AllocateDisconnectBuffer(); | 634 IOBuffer* buffer = IOBuffer::AllocateDisconnectBuffer(); |
| 634 BOOL ok = DisconnectEx_( | 635 BOOL ok = DisconnectEx_( |
| 635 socket(), buffer->GetCleanOverlapped(), TF_REUSE_SOCKET, 0); | 636 socket(), buffer->GetCleanOverlapped(), TF_REUSE_SOCKET, 0); |
| 636 if (!ok && WSAGetLastError() != WSA_IO_PENDING) { | 637 if (!ok && WSAGetLastError() != WSA_IO_PENDING) { |
| 637 DisconnectComplete(buffer); | 638 DisconnectComplete(buffer); |
| 638 } | 639 } |
| 640 if (p != ILLEGAL_PORT) DartUtils::PostInt32(p, 1 << kDestroyedEvent); | |
| 639 } | 641 } |
| 640 | 642 |
| 641 | 643 |
| 642 void ClientSocket::DisconnectComplete(IOBuffer* buffer) { | 644 void ClientSocket::DisconnectComplete(IOBuffer* buffer) { |
| 643 IOBuffer::DisposeBuffer(buffer); | 645 IOBuffer::DisposeBuffer(buffer); |
| 644 closesocket(socket()); | 646 closesocket(socket()); |
| 645 if (data_ready_ != NULL) { | 647 if (data_ready_ != NULL) { |
| 646 IOBuffer::DisposeBuffer(data_ready_); | 648 IOBuffer::DisposeBuffer(data_ready_); |
| 647 } | 649 } |
| 648 // When disconnect is complete get rid of the object. | 650 // When disconnect is complete get rid of the object. |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 659 CreateCompletionPort(event_handler_->completion_port()); | 661 CreateCompletionPort(event_handler_->completion_port()); |
| 660 } | 662 } |
| 661 } | 663 } |
| 662 | 664 |
| 663 | 665 |
| 664 bool ClientSocket::IsClosed() { | 666 bool ClientSocket::IsClosed() { |
| 665 return false; | 667 return false; |
| 666 } | 668 } |
| 667 | 669 |
| 668 | 670 |
| 671 static void DeleteIfClosed(Handle* handle) { | |
| 672 if (handle->IsClosed()) { | |
| 673 Dart_Port port = handle->port(); | |
| 674 delete handle; | |
| 675 DartUtils::PostInt32(port, 1 << kDestroyedEvent); | |
| 676 } | |
| 677 } | |
| 678 | |
| 679 | |
| 669 void EventHandlerImplementation::HandleInterrupt(InterruptMessage* msg) { | 680 void EventHandlerImplementation::HandleInterrupt(InterruptMessage* msg) { |
| 670 if (msg->id == kTimeoutId) { | 681 if (msg->id == kTimeoutId) { |
| 671 // Change of timeout request. Just set the new timeout and port as the | 682 // Change of timeout request. Just set the new timeout and port as the |
| 672 // completion thread will use the new timeout value for its next wait. | 683 // completion thread will use the new timeout value for its next wait. |
| 673 timeout_queue_.UpdateTimeout(msg->dart_port, msg->data); | 684 timeout_queue_.UpdateTimeout(msg->dart_port, msg->data); |
| 674 } else if (msg->id == kShutdownId) { | 685 } else if (msg->id == kShutdownId) { |
| 675 shutdown_ = true; | 686 shutdown_ = true; |
| 676 } else { | 687 } else { |
| 677 bool delete_handle = false; | 688 bool delete_handle = false; |
|
kustermann
2013/07/19 11:59:18
Remove that boolean.
Anders Johnsen
2013/07/19 13:25:58
Done.
| |
| 678 Handle* handle = reinterpret_cast<Handle*>(msg->id); | 689 Handle* handle = reinterpret_cast<Handle*>(msg->id); |
| 679 ASSERT(handle != NULL); | 690 ASSERT(handle != NULL); |
| 680 if (handle->is_listen_socket()) { | 691 if (handle->is_listen_socket()) { |
| 681 ListenSocket* listen_socket = | 692 ListenSocket* listen_socket = |
| 682 reinterpret_cast<ListenSocket*>(handle); | 693 reinterpret_cast<ListenSocket*>(handle); |
| 683 listen_socket->EnsureInitialized(this); | 694 listen_socket->EnsureInitialized(this); |
| 684 listen_socket->SetPortAndMask(msg->dart_port, msg->data); | 695 listen_socket->SetPortAndMask(msg->dart_port, msg->data); |
| 685 | 696 |
| 686 Handle::ScopedLock lock(listen_socket); | 697 Handle::ScopedLock lock(listen_socket); |
| 687 | 698 |
| 688 // If incomming connections are requested make sure to post already | 699 // If incomming connections are requested make sure to post already |
| 689 // accepted connections. | 700 // accepted connections. |
| 690 if ((msg->data & (1 << kInEvent)) != 0) { | 701 if ((msg->data & (1 << kInEvent)) != 0) { |
| 691 if (listen_socket->CanAccept()) { | 702 if (listen_socket->CanAccept()) { |
| 692 int event_mask = (1 << kInEvent); | 703 int event_mask = (1 << kInEvent); |
| 693 handle->set_mask(handle->mask() & ~event_mask); | 704 handle->set_mask(handle->mask() & ~event_mask); |
| 694 DartUtils::PostInt32(handle->port(), event_mask); | 705 DartUtils::PostInt32(handle->port(), event_mask); |
| 695 } | 706 } |
| 696 // Always keep 5 outstanding accepts going, to enhance performance. | 707 // Always keep 5 outstanding accepts going, to enhance performance. |
| 697 while (listen_socket->pending_accept_count() < 5) { | 708 while (listen_socket->pending_accept_count() < 5) { |
| 698 listen_socket->IssueAccept(); | 709 listen_socket->IssueAccept(); |
| 699 } | 710 } |
| 700 } | 711 } |
| 701 | 712 |
| 702 if ((msg->data & (1 << kCloseCommand)) != 0) { | 713 if ((msg->data & (1 << kCloseCommand)) != 0) { |
| 703 listen_socket->Close(); | 714 listen_socket->Close(); |
| 704 if (listen_socket->IsClosed()) { | 715 if (listen_socket->IsClosed()) { |
| 705 delete_handle = true; | 716 delete_handle = true; |
| 706 } | 717 } |
|
kustermann
2013/07/19 11:59:18
remove this.
Anders Johnsen
2013/07/19 13:25:58
Done.
| |
| 707 } | 718 } |
| 708 } else { | 719 } else { |
| 709 handle->EnsureInitialized(this); | 720 handle->EnsureInitialized(this); |
| 710 | 721 |
| 711 Handle::ScopedLock lock(handle); | 722 Handle::ScopedLock lock(handle); |
| 712 | 723 |
| 713 if (!handle->IsError()) { | 724 if (!handle->IsError()) { |
| 714 if ((msg->data & ((1 << kInEvent) | (1 << kOutEvent))) != 0) { | 725 if ((msg->data & ((1 << kInEvent) | (1 << kOutEvent))) != 0) { |
| 715 // Only set mask if we turned on kInEvent or kOutEvent. | 726 // Only set mask if we turned on kInEvent or kOutEvent. |
| 716 handle->SetPortAndMask(msg->dart_port, msg->data); | 727 handle->SetPortAndMask(msg->dart_port, msg->data); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 749 client_socket->Shutdown(SD_RECEIVE); | 760 client_socket->Shutdown(SD_RECEIVE); |
| 750 } | 761 } |
| 751 | 762 |
| 752 if ((msg->data & (1 << kShutdownWriteCommand)) != 0) { | 763 if ((msg->data & (1 << kShutdownWriteCommand)) != 0) { |
| 753 client_socket->Shutdown(SD_SEND); | 764 client_socket->Shutdown(SD_SEND); |
| 754 } | 765 } |
| 755 } | 766 } |
| 756 } | 767 } |
| 757 | 768 |
| 758 if ((msg->data & (1 << kCloseCommand)) != 0) { | 769 if ((msg->data & (1 << kCloseCommand)) != 0) { |
| 770 handle->SetPortAndMask(msg->dart_port, msg->data); | |
| 759 handle->Close(); | 771 handle->Close(); |
| 760 if (handle->IsClosed()) { | 772 if (handle->IsClosed()) { |
| 761 delete_handle = true; | 773 delete_handle = true; |
|
kustermann
2013/07/19 11:59:18
remove this.
Anders Johnsen
2013/07/19 13:25:58
Done.
| |
| 762 } | 774 } |
| 763 } | 775 } |
| 764 } | 776 } |
| 765 if (delete_handle) { | 777 if (delete_handle) { |
| 766 delete handle; | 778 DeleteIfClosed(handle); |
|
kustermann
2013/07/19 11:59:18
Remove the condition.
Anders Johnsen
2013/07/19 13:25:58
Done.
| |
| 767 } | 779 } |
| 768 } | 780 } |
| 769 } | 781 } |
| 770 | 782 |
| 771 | 783 |
| 772 void EventHandlerImplementation::HandleAccept(ListenSocket* listen_socket, | 784 void EventHandlerImplementation::HandleAccept(ListenSocket* listen_socket, |
| 773 IOBuffer* buffer) { | 785 IOBuffer* buffer) { |
| 774 listen_socket->AcceptComplete(buffer, completion_port_); | 786 listen_socket->AcceptComplete(buffer, completion_port_); |
| 775 | 787 |
| 776 if (!listen_socket->IsClosing()) { | 788 if (!listen_socket->IsClosing()) { |
| 777 int event_mask = 1 << kInEvent; | 789 int event_mask = 1 << kInEvent; |
| 778 if ((listen_socket->mask() & event_mask) != 0) { | 790 if ((listen_socket->mask() & event_mask) != 0) { |
| 779 DartUtils::PostInt32(listen_socket->port(), event_mask); | 791 DartUtils::PostInt32(listen_socket->port(), event_mask); |
| 780 } | 792 } |
| 781 } | 793 } |
| 782 | 794 |
| 783 if (listen_socket->IsClosed()) { | 795 DeleteIfClosed(listen_socket); |
| 784 delete listen_socket; | |
| 785 } | |
| 786 } | 796 } |
| 787 | 797 |
| 788 | 798 |
| 789 void EventHandlerImplementation::HandleClosed(Handle* handle) { | 799 void EventHandlerImplementation::HandleClosed(Handle* handle) { |
| 790 if (!handle->IsClosing()) { | 800 if (!handle->IsClosing()) { |
| 791 int event_mask = 1 << kCloseEvent; | 801 int event_mask = 1 << kCloseEvent; |
| 792 DartUtils::PostInt32(handle->port(), event_mask); | 802 DartUtils::PostInt32(handle->port(), event_mask); |
| 793 } | 803 } |
| 794 } | 804 } |
| 795 | 805 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 818 } | 828 } |
| 819 } else { | 829 } else { |
| 820 handle->MarkClosedRead(); | 830 handle->MarkClosedRead(); |
| 821 if (bytes == 0) { | 831 if (bytes == 0) { |
| 822 HandleClosed(handle); | 832 HandleClosed(handle); |
| 823 } else { | 833 } else { |
| 824 HandleError(handle); | 834 HandleError(handle); |
| 825 } | 835 } |
| 826 } | 836 } |
| 827 | 837 |
| 828 if (handle->IsClosed()) { | 838 DeleteIfClosed(handle); |
| 829 delete handle; | |
| 830 } | |
| 831 } | 839 } |
| 832 | 840 |
| 833 | 841 |
| 834 void EventHandlerImplementation::HandleWrite(Handle* handle, | 842 void EventHandlerImplementation::HandleWrite(Handle* handle, |
| 835 int bytes, | 843 int bytes, |
| 836 IOBuffer* buffer) { | 844 IOBuffer* buffer) { |
| 837 handle->WriteComplete(buffer); | 845 handle->WriteComplete(buffer); |
| 838 | 846 |
| 839 if (bytes > 0) { | 847 if (bytes > 0) { |
| 840 if (!handle->IsError() && !handle->IsClosing()) { | 848 if (!handle->IsError() && !handle->IsClosing()) { |
| 841 int event_mask = 1 << kOutEvent; | 849 int event_mask = 1 << kOutEvent; |
| 842 if ((handle->mask() & event_mask) != 0) { | 850 if ((handle->mask() & event_mask) != 0) { |
| 843 DartUtils::PostInt32(handle->port(), event_mask); | 851 DartUtils::PostInt32(handle->port(), event_mask); |
| 844 } | 852 } |
| 845 } | 853 } |
| 846 } else if (bytes == 0) { | 854 } else if (bytes == 0) { |
| 847 HandleClosed(handle); | 855 HandleClosed(handle); |
| 848 } else { | 856 } else { |
| 849 HandleError(handle); | 857 HandleError(handle); |
| 850 } | 858 } |
| 851 | 859 |
| 852 if (handle->IsClosed()) { | 860 DeleteIfClosed(handle); |
| 853 delete handle; | |
| 854 } | |
| 855 } | 861 } |
| 856 | 862 |
| 857 | 863 |
| 858 void EventHandlerImplementation::HandleDisconnect( | 864 void EventHandlerImplementation::HandleDisconnect( |
| 859 ClientSocket* client_socket, | 865 ClientSocket* client_socket, |
| 860 int bytes, | 866 int bytes, |
| 861 IOBuffer* buffer) { | 867 IOBuffer* buffer) { |
| 862 client_socket->DisconnectComplete(buffer); | 868 client_socket->DisconnectComplete(buffer); |
| 863 } | 869 } |
| 864 | 870 |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1012 | 1018 |
| 1013 | 1019 |
| 1014 void EventHandlerImplementation::Shutdown() { | 1020 void EventHandlerImplementation::Shutdown() { |
| 1015 SendData(kShutdownId, 0, 0); | 1021 SendData(kShutdownId, 0, 0); |
| 1016 } | 1022 } |
| 1017 | 1023 |
| 1018 } // namespace bin | 1024 } // namespace bin |
| 1019 } // namespace dart | 1025 } // namespace dart |
| 1020 | 1026 |
| 1021 #endif // defined(TARGET_OS_WINDOWS) | 1027 #endif // defined(TARGET_OS_WINDOWS) |
| OLD | NEW |