OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "bin/eventhandler.h" | 5 #include "bin/eventhandler.h" |
6 | 6 |
7 #include <process.h> | 7 #include <process.h> |
8 #include <winsock2.h> | 8 #include <winsock2.h> |
9 #include <ws2tcpip.h> | 9 #include <ws2tcpip.h> |
10 #include <mswsock.h> | 10 #include <mswsock.h> |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 | 92 |
93 | 93 |
94 Handle::Handle(HANDLE handle) | 94 Handle::Handle(HANDLE handle) |
95 : handle_(reinterpret_cast<HANDLE>(handle)), | 95 : handle_(reinterpret_cast<HANDLE>(handle)), |
96 flags_(0), | 96 flags_(0), |
97 port_(0), | 97 port_(0), |
98 completion_port_(INVALID_HANDLE_VALUE), | 98 completion_port_(INVALID_HANDLE_VALUE), |
99 event_handler_(NULL), | 99 event_handler_(NULL), |
100 data_ready_(NULL), | 100 data_ready_(NULL), |
101 pending_read_(NULL), | 101 pending_read_(NULL), |
102 pending_write_(NULL) { | 102 pending_write_(NULL), |
| 103 last_error_(NOERROR) { |
103 InitializeCriticalSection(&cs_); | 104 InitializeCriticalSection(&cs_); |
104 } | 105 } |
105 | 106 |
106 | 107 |
107 Handle::Handle(HANDLE handle, Dart_Port port) | 108 Handle::Handle(HANDLE handle, Dart_Port port) |
108 : handle_(reinterpret_cast<HANDLE>(handle)), | 109 : handle_(reinterpret_cast<HANDLE>(handle)), |
109 flags_(0), | 110 flags_(0), |
110 port_(port), | 111 port_(port), |
111 completion_port_(INVALID_HANDLE_VALUE), | 112 completion_port_(INVALID_HANDLE_VALUE), |
112 event_handler_(NULL), | 113 event_handler_(NULL), |
113 data_ready_(NULL), | 114 data_ready_(NULL), |
114 pending_read_(NULL), | 115 pending_read_(NULL), |
115 pending_write_(NULL) { | 116 pending_write_(NULL), |
| 117 last_error_(NOERROR) { |
116 InitializeCriticalSection(&cs_); | 118 InitializeCriticalSection(&cs_); |
117 } | 119 } |
118 | 120 |
119 | 121 |
120 Handle::~Handle() { | 122 Handle::~Handle() { |
121 DeleteCriticalSection(&cs_); | 123 DeleteCriticalSection(&cs_); |
122 } | 124 } |
123 | 125 |
124 | 126 |
125 void Handle::Lock() { | 127 void Handle::Lock() { |
(...skipping 588 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
714 | 716 |
715 | 717 |
716 void EventHandlerImplementation::HandleClosed(Handle* handle) { | 718 void EventHandlerImplementation::HandleClosed(Handle* handle) { |
717 if (!handle->IsClosing()) { | 719 if (!handle->IsClosing()) { |
718 int event_mask = 1 << kCloseEvent; | 720 int event_mask = 1 << kCloseEvent; |
719 DartUtils::PostInt32(handle->port(), event_mask); | 721 DartUtils::PostInt32(handle->port(), event_mask); |
720 } | 722 } |
721 } | 723 } |
722 | 724 |
723 | 725 |
| 726 void EventHandlerImplementation::HandleError(Handle* handle) { |
| 727 handle->set_last_error(WSAGetLastError()); |
| 728 if (!handle->IsClosing()) { |
| 729 int event_mask = 1 << kErrorEvent; |
| 730 DartUtils::PostInt32(handle->port(), event_mask); |
| 731 } |
| 732 } |
| 733 |
| 734 |
724 void EventHandlerImplementation::HandleRead(Handle* handle, | 735 void EventHandlerImplementation::HandleRead(Handle* handle, |
725 int bytes, | 736 int bytes, |
726 IOBuffer* buffer) { | 737 IOBuffer* buffer) { |
727 buffer->set_data_length(bytes); | 738 buffer->set_data_length(bytes); |
728 handle->ReadComplete(buffer); | 739 handle->ReadComplete(buffer); |
729 if (bytes > 0) { | 740 if (bytes > 0) { |
730 if (!handle->IsClosing()) { | 741 if (!handle->IsClosing()) { |
731 int event_mask = 1 << kInEvent; | 742 int event_mask = 1 << kInEvent; |
732 if ((handle->mask() & event_mask) != 0) { | 743 if ((handle->mask() & event_mask) != 0) { |
733 DartUtils::PostInt32(handle->port(), event_mask); | 744 DartUtils::PostInt32(handle->port(), event_mask); |
734 } | 745 } |
735 } | 746 } |
736 } else { | 747 } else { |
737 ASSERT(bytes == 0); | |
738 handle->MarkClosedRead(); | 748 handle->MarkClosedRead(); |
739 HandleClosed(handle); | 749 if (bytes == 0) { |
| 750 HandleClosed(handle); |
| 751 } else { |
| 752 HandleError(handle); |
| 753 } |
740 } | 754 } |
741 | 755 |
742 if (handle->IsClosed()) { | 756 if (handle->IsClosed()) { |
743 delete handle; | 757 delete handle; |
744 } | 758 } |
745 } | 759 } |
746 | 760 |
747 | 761 |
748 void EventHandlerImplementation::HandleWrite(Handle* handle, | 762 void EventHandlerImplementation::HandleWrite(Handle* handle, |
749 int bytes, | 763 int bytes, |
750 IOBuffer* buffer) { | 764 IOBuffer* buffer) { |
751 handle->WriteComplete(buffer); | 765 handle->WriteComplete(buffer); |
752 | 766 |
753 if (bytes > 0) { | 767 if (bytes > 0) { |
754 if (!handle->IsClosing()) { | 768 if (!handle->IsClosing()) { |
755 int event_mask = 1 << kOutEvent; | 769 int event_mask = 1 << kOutEvent; |
756 if ((handle->mask() & event_mask) != 0) { | 770 if ((handle->mask() & event_mask) != 0) { |
757 DartUtils::PostInt32(handle->port(), event_mask); | 771 DartUtils::PostInt32(handle->port(), event_mask); |
758 } | 772 } |
759 } | 773 } |
| 774 } else if (bytes == 0) { |
| 775 HandleClosed(handle); |
760 } else { | 776 } else { |
761 ASSERT(bytes == 0); | 777 HandleError(handle); |
762 HandleClosed(handle); | |
763 } | 778 } |
764 | 779 |
765 if (handle->IsClosed()) { | 780 if (handle->IsClosed()) { |
766 delete handle; | 781 delete handle; |
767 } | 782 } |
768 } | 783 } |
769 | 784 |
770 | 785 |
771 void EventHandlerImplementation::HandleTimeout() { | 786 void EventHandlerImplementation::HandleTimeout() { |
772 // TODO(sgjesse) check if there actually is a timeout. | 787 // TODO(sgjesse) check if there actually is a timeout. |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
855 if (!ok && overlapped == NULL) { | 870 if (!ok && overlapped == NULL) { |
856 if (GetLastError() == ERROR_ABANDONED_WAIT_0) { | 871 if (GetLastError() == ERROR_ABANDONED_WAIT_0) { |
857 // The completion port should never be closed. | 872 // The completion port should never be closed. |
858 printf("Completion port closed\n"); | 873 printf("Completion port closed\n"); |
859 UNREACHABLE(); | 874 UNREACHABLE(); |
860 } else { | 875 } else { |
861 // Timeout is signalled by false result and NULL in overlapped. | 876 // Timeout is signalled by false result and NULL in overlapped. |
862 handler->HandleTimeout(); | 877 handler->HandleTimeout(); |
863 } | 878 } |
864 } else if (!ok) { | 879 } else if (!ok) { |
865 // If GetQueuedCompletionStatus return false and overlapped is | |
866 // not NULL then it did dequeue a request which failed. | |
867 | |
868 // Treat ERROR_CONNECTION_ABORTED as connection closed. | 880 // Treat ERROR_CONNECTION_ABORTED as connection closed. |
869 // The error ERROR_OPERATION_ABORTED is set for pending | 881 // The error ERROR_OPERATION_ABORTED is set for pending |
870 // accept requests for a listen socket which is closed. | 882 // accept requests for a listen socket which is closed. |
871 // ERROR_NETNAME_DELETED occurs when the client closes | 883 // ERROR_NETNAME_DELETED occurs when the client closes |
872 // the socket it is reading from. | 884 // the socket it is reading from. |
873 DWORD last_error = GetLastError(); | 885 DWORD last_error = GetLastError(); |
874 if (last_error == ERROR_CONNECTION_ABORTED || | 886 if (last_error == ERROR_CONNECTION_ABORTED || |
875 last_error == ERROR_OPERATION_ABORTED || | 887 last_error == ERROR_OPERATION_ABORTED || |
876 last_error == ERROR_NETNAME_DELETED || | 888 last_error == ERROR_NETNAME_DELETED || |
877 last_error == ERROR_BROKEN_PIPE) { | 889 last_error == ERROR_BROKEN_PIPE) { |
878 ASSERT(bytes == 0); | 890 ASSERT(bytes == 0); |
879 handler->HandleIOCompletion(bytes, key, overlapped); | 891 handler->HandleIOCompletion(bytes, key, overlapped); |
880 } else { | 892 } else { |
881 UNREACHABLE(); | 893 ASSERT(bytes == 0); |
882 } | 894 handler->HandleIOCompletion(-1, key, overlapped); |
| 895 } |
883 } else if (key == NULL) { | 896 } else if (key == NULL) { |
884 // A key of NULL signals an interrupt message. | 897 // A key of NULL signals an interrupt message. |
885 InterruptMessage* msg = reinterpret_cast<InterruptMessage*>(overlapped); | 898 InterruptMessage* msg = reinterpret_cast<InterruptMessage*>(overlapped); |
886 handler->HandleInterrupt(msg); | 899 handler->HandleInterrupt(msg); |
887 delete msg; | 900 delete msg; |
888 } else { | 901 } else { |
889 handler->HandleIOCompletion(bytes, key, overlapped); | 902 handler->HandleIOCompletion(bytes, key, overlapped); |
890 } | 903 } |
891 } | 904 } |
892 } | 905 } |
893 | 906 |
894 | 907 |
895 void EventHandlerImplementation::StartEventHandler() { | 908 void EventHandlerImplementation::StartEventHandler() { |
896 int result = dart::Thread::Start(EventHandlerThread, | 909 int result = dart::Thread::Start(EventHandlerThread, |
897 reinterpret_cast<uword>(this)); | 910 reinterpret_cast<uword>(this)); |
898 if (result != 0) { | 911 if (result != 0) { |
899 FATAL1("Failed to start event handler thread %d", result); | 912 FATAL1("Failed to start event handler thread %d", result); |
900 } | 913 } |
901 | 914 |
902 // Initialize Winsock32 | 915 // Initialize Winsock32 |
903 if (!Socket::Initialize()) { | 916 if (!Socket::Initialize()) { |
904 FATAL("Failed to initialized Windows sockets"); | 917 FATAL("Failed to initialized Windows sockets"); |
905 } | 918 } |
906 } | 919 } |
OLD | NEW |