| 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; | |
| 678 Handle* handle = reinterpret_cast<Handle*>(msg->id); | 688 Handle* handle = reinterpret_cast<Handle*>(msg->id); |
| 679 ASSERT(handle != NULL); | 689 ASSERT(handle != NULL); |
| 680 if (handle->is_listen_socket()) { | 690 if (handle->is_listen_socket()) { |
| 681 ListenSocket* listen_socket = | 691 ListenSocket* listen_socket = |
| 682 reinterpret_cast<ListenSocket*>(handle); | 692 reinterpret_cast<ListenSocket*>(handle); |
| 683 listen_socket->EnsureInitialized(this); | 693 listen_socket->EnsureInitialized(this); |
| 684 listen_socket->SetPortAndMask(msg->dart_port, msg->data); | 694 listen_socket->SetPortAndMask(msg->dart_port, msg->data); |
| 685 | 695 |
| 686 Handle::ScopedLock lock(listen_socket); | 696 Handle::ScopedLock lock(listen_socket); |
| 687 | 697 |
| 688 // If incomming connections are requested make sure to post already | 698 // If incomming connections are requested make sure to post already |
| 689 // accepted connections. | 699 // accepted connections. |
| 690 if ((msg->data & (1 << kInEvent)) != 0) { | 700 if ((msg->data & (1 << kInEvent)) != 0) { |
| 691 if (listen_socket->CanAccept()) { | 701 if (listen_socket->CanAccept()) { |
| 692 int event_mask = (1 << kInEvent); | 702 int event_mask = (1 << kInEvent); |
| 693 handle->set_mask(handle->mask() & ~event_mask); | 703 handle->set_mask(handle->mask() & ~event_mask); |
| 694 DartUtils::PostInt32(handle->port(), event_mask); | 704 DartUtils::PostInt32(handle->port(), event_mask); |
| 695 } | 705 } |
| 696 // Always keep 5 outstanding accepts going, to enhance performance. | 706 // Always keep 5 outstanding accepts going, to enhance performance. |
| 697 while (listen_socket->pending_accept_count() < 5) { | 707 while (listen_socket->pending_accept_count() < 5) { |
| 698 listen_socket->IssueAccept(); | 708 listen_socket->IssueAccept(); |
| 699 } | 709 } |
| 700 } | 710 } |
| 701 | 711 |
| 702 if ((msg->data & (1 << kCloseCommand)) != 0) { | 712 if ((msg->data & (1 << kCloseCommand)) != 0) { |
| 703 listen_socket->Close(); | 713 listen_socket->Close(); |
| 704 if (listen_socket->IsClosed()) { | |
| 705 delete_handle = true; | |
| 706 } | |
| 707 } | 714 } |
| 708 } else { | 715 } else { |
| 709 handle->EnsureInitialized(this); | 716 handle->EnsureInitialized(this); |
| 710 | 717 |
| 711 Handle::ScopedLock lock(handle); | 718 Handle::ScopedLock lock(handle); |
| 712 | 719 |
| 713 if (!handle->IsError()) { | 720 if (!handle->IsError()) { |
| 714 if ((msg->data & ((1 << kInEvent) | (1 << kOutEvent))) != 0) { | 721 if ((msg->data & ((1 << kInEvent) | (1 << kOutEvent))) != 0) { |
| 715 // Only set mask if we turned on kInEvent or kOutEvent. | 722 // Only set mask if we turned on kInEvent or kOutEvent. |
| 716 handle->SetPortAndMask(msg->dart_port, msg->data); | 723 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); | 756 client_socket->Shutdown(SD_RECEIVE); |
| 750 } | 757 } |
| 751 | 758 |
| 752 if ((msg->data & (1 << kShutdownWriteCommand)) != 0) { | 759 if ((msg->data & (1 << kShutdownWriteCommand)) != 0) { |
| 753 client_socket->Shutdown(SD_SEND); | 760 client_socket->Shutdown(SD_SEND); |
| 754 } | 761 } |
| 755 } | 762 } |
| 756 } | 763 } |
| 757 | 764 |
| 758 if ((msg->data & (1 << kCloseCommand)) != 0) { | 765 if ((msg->data & (1 << kCloseCommand)) != 0) { |
| 766 handle->SetPortAndMask(msg->dart_port, msg->data); |
| 759 handle->Close(); | 767 handle->Close(); |
| 760 if (handle->IsClosed()) { | |
| 761 delete_handle = true; | |
| 762 } | |
| 763 } | 768 } |
| 764 } | 769 } |
| 765 if (delete_handle) { | 770 DeleteIfClosed(handle); |
| 766 delete handle; | |
| 767 } | |
| 768 } | 771 } |
| 769 } | 772 } |
| 770 | 773 |
| 771 | 774 |
| 772 void EventHandlerImplementation::HandleAccept(ListenSocket* listen_socket, | 775 void EventHandlerImplementation::HandleAccept(ListenSocket* listen_socket, |
| 773 IOBuffer* buffer) { | 776 IOBuffer* buffer) { |
| 774 listen_socket->AcceptComplete(buffer, completion_port_); | 777 listen_socket->AcceptComplete(buffer, completion_port_); |
| 775 | 778 |
| 776 if (!listen_socket->IsClosing()) { | 779 if (!listen_socket->IsClosing()) { |
| 777 int event_mask = 1 << kInEvent; | 780 int event_mask = 1 << kInEvent; |
| 778 if ((listen_socket->mask() & event_mask) != 0) { | 781 if ((listen_socket->mask() & event_mask) != 0) { |
| 779 DartUtils::PostInt32(listen_socket->port(), event_mask); | 782 DartUtils::PostInt32(listen_socket->port(), event_mask); |
| 780 } | 783 } |
| 781 } | 784 } |
| 782 | 785 |
| 783 if (listen_socket->IsClosed()) { | 786 DeleteIfClosed(listen_socket); |
| 784 delete listen_socket; | |
| 785 } | |
| 786 } | 787 } |
| 787 | 788 |
| 788 | 789 |
| 789 void EventHandlerImplementation::HandleClosed(Handle* handle) { | 790 void EventHandlerImplementation::HandleClosed(Handle* handle) { |
| 790 if (!handle->IsClosing()) { | 791 if (!handle->IsClosing()) { |
| 791 int event_mask = 1 << kCloseEvent; | 792 int event_mask = 1 << kCloseEvent; |
| 792 DartUtils::PostInt32(handle->port(), event_mask); | 793 DartUtils::PostInt32(handle->port(), event_mask); |
| 793 } | 794 } |
| 794 } | 795 } |
| 795 | 796 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 818 } | 819 } |
| 819 } else { | 820 } else { |
| 820 handle->MarkClosedRead(); | 821 handle->MarkClosedRead(); |
| 821 if (bytes == 0) { | 822 if (bytes == 0) { |
| 822 HandleClosed(handle); | 823 HandleClosed(handle); |
| 823 } else { | 824 } else { |
| 824 HandleError(handle); | 825 HandleError(handle); |
| 825 } | 826 } |
| 826 } | 827 } |
| 827 | 828 |
| 828 if (handle->IsClosed()) { | 829 DeleteIfClosed(handle); |
| 829 delete handle; | |
| 830 } | |
| 831 } | 830 } |
| 832 | 831 |
| 833 | 832 |
| 834 void EventHandlerImplementation::HandleWrite(Handle* handle, | 833 void EventHandlerImplementation::HandleWrite(Handle* handle, |
| 835 int bytes, | 834 int bytes, |
| 836 IOBuffer* buffer) { | 835 IOBuffer* buffer) { |
| 837 handle->WriteComplete(buffer); | 836 handle->WriteComplete(buffer); |
| 838 | 837 |
| 839 if (bytes > 0) { | 838 if (bytes > 0) { |
| 840 if (!handle->IsError() && !handle->IsClosing()) { | 839 if (!handle->IsError() && !handle->IsClosing()) { |
| 841 int event_mask = 1 << kOutEvent; | 840 int event_mask = 1 << kOutEvent; |
| 842 if ((handle->mask() & event_mask) != 0) { | 841 if ((handle->mask() & event_mask) != 0) { |
| 843 DartUtils::PostInt32(handle->port(), event_mask); | 842 DartUtils::PostInt32(handle->port(), event_mask); |
| 844 } | 843 } |
| 845 } | 844 } |
| 846 } else if (bytes == 0) { | 845 } else if (bytes == 0) { |
| 847 HandleClosed(handle); | 846 HandleClosed(handle); |
| 848 } else { | 847 } else { |
| 849 HandleError(handle); | 848 HandleError(handle); |
| 850 } | 849 } |
| 851 | 850 |
| 852 if (handle->IsClosed()) { | 851 DeleteIfClosed(handle); |
| 853 delete handle; | |
| 854 } | |
| 855 } | 852 } |
| 856 | 853 |
| 857 | 854 |
| 858 void EventHandlerImplementation::HandleDisconnect( | 855 void EventHandlerImplementation::HandleDisconnect( |
| 859 ClientSocket* client_socket, | 856 ClientSocket* client_socket, |
| 860 int bytes, | 857 int bytes, |
| 861 IOBuffer* buffer) { | 858 IOBuffer* buffer) { |
| 862 client_socket->DisconnectComplete(buffer); | 859 client_socket->DisconnectComplete(buffer); |
| 863 } | 860 } |
| 864 | 861 |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1012 | 1009 |
| 1013 | 1010 |
| 1014 void EventHandlerImplementation::Shutdown() { | 1011 void EventHandlerImplementation::Shutdown() { |
| 1015 SendData(kShutdownId, 0, 0); | 1012 SendData(kShutdownId, 0, 0); |
| 1016 } | 1013 } |
| 1017 | 1014 |
| 1018 } // namespace bin | 1015 } // namespace bin |
| 1019 } // namespace dart | 1016 } // namespace dart |
| 1020 | 1017 |
| 1021 #endif // defined(TARGET_OS_WINDOWS) | 1018 #endif // defined(TARGET_OS_WINDOWS) |
| OLD | NEW |