Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: dart/runtime/bin/eventhandler_win.cc

Issue 879353003: Introduce optional 'bool shared' parameter to ServerSocket.bind() ... (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Part3 update: Mac/Linux working, still Windows issues Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 10
10 #include <winsock2.h> // NOLINT 11 #include <winsock2.h> // NOLINT
11 #include <ws2tcpip.h> // NOLINT 12 #include <ws2tcpip.h> // NOLINT
12 #include <mswsock.h> // NOLINT 13 #include <mswsock.h> // NOLINT
13 #include <io.h> // NOLINT 14 #include <io.h> // NOLINT
14 #include <fcntl.h> // NOLINT 15 #include <fcntl.h> // NOLINT
15 16
16 #include "bin/builtin.h" 17 #include "bin/builtin.h"
17 #include "bin/dartutils.h" 18 #include "bin/dartutils.h"
18 #include "bin/lockers.h" 19 #include "bin/lockers.h"
19 #include "bin/log.h" 20 #include "bin/log.h"
20 #include "bin/socket.h" 21 #include "bin/socket.h"
21 #include "bin/thread.h" 22 #include "bin/thread.h"
22 #include "bin/utils.h" 23 #include "bin/utils.h"
23 24
24 #include "platform/utils.h" 25 #include "platform/utils.h"
25 26
26 namespace dart { 27 namespace dart {
27 namespace bin { 28 namespace bin {
28 29
29 static const int kBufferSize = 64 * 1024; 30 static const int kBufferSize = 64 * 1024;
30 static const int kStdOverlappedBufferSize = 16 * 1024; 31 static const int kStdOverlappedBufferSize = 16 * 1024;
31 32
32 static const int kInfinityTimeout = -1;
33 static const int kTimeoutId = -1;
34 static const int kShutdownId = -2;
35
36 OverlappedBuffer* OverlappedBuffer::AllocateBuffer(int buffer_size, 33 OverlappedBuffer* OverlappedBuffer::AllocateBuffer(int buffer_size,
37 Operation operation) { 34 Operation operation) {
38 OverlappedBuffer* buffer = 35 OverlappedBuffer* buffer =
39 new(buffer_size) OverlappedBuffer(buffer_size, operation); 36 new(buffer_size) OverlappedBuffer(buffer_size, operation);
40 return buffer; 37 return buffer;
41 } 38 }
42 39
43 40
44 OverlappedBuffer* OverlappedBuffer::AllocateAcceptBuffer(int buffer_size) { 41 OverlappedBuffer* OverlappedBuffer::AllocateAcceptBuffer(int buffer_size) {
45 OverlappedBuffer* buffer = AllocateBuffer(buffer_size, kAccept); 42 OverlappedBuffer* buffer = AllocateBuffer(buffer_size, kAccept);
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 data_length_ = num_bytes; 105 data_length_ = num_bytes;
109 return num_bytes; 106 return num_bytes;
110 } 107 }
111 108
112 109
113 int OverlappedBuffer::GetRemainingLength() { 110 int OverlappedBuffer::GetRemainingLength() {
114 ASSERT(operation_ == kRead || operation_ == kRecvFrom); 111 ASSERT(operation_ == kRead || operation_ == kRecvFrom);
115 return data_length_ - index_; 112 return data_length_ - index_;
116 } 113 }
117 114
118 115 Handle::Handle(intptr_t handle)
119 Handle::Handle(HANDLE handle) 116 : DescriptorInfoBase(handle),
120 : handle_(reinterpret_cast<HANDLE>(handle)), 117 handle_(reinterpret_cast<HANDLE>(handle)),
121 port_(0),
122 mask_(0),
123 completion_port_(INVALID_HANDLE_VALUE), 118 completion_port_(INVALID_HANDLE_VALUE),
124 event_handler_(NULL), 119 event_handler_(NULL),
125 data_ready_(NULL), 120 data_ready_(NULL),
126 pending_read_(NULL),
127 pending_write_(NULL),
128 last_error_(NOERROR),
129 flags_(0) {
130 InitializeCriticalSection(&cs_);
131 }
132
133
134 Handle::Handle(HANDLE handle, Dart_Port port)
135 : handle_(reinterpret_cast<HANDLE>(handle)),
136 port_(port),
137 mask_(0),
138 completion_port_(INVALID_HANDLE_VALUE),
139 event_handler_(NULL),
140 data_ready_(NULL),
141 pending_read_(NULL), 121 pending_read_(NULL),
142 pending_write_(NULL), 122 pending_write_(NULL),
143 last_error_(NOERROR), 123 last_error_(NOERROR),
144 flags_(0) { 124 flags_(0) {
145 InitializeCriticalSection(&cs_); 125 InitializeCriticalSection(&cs_);
146 } 126 }
147 127
148 128
149 Handle::~Handle() { 129 Handle::~Handle() {
150 DeleteCriticalSection(&cs_); 130 DeleteCriticalSection(&cs_);
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 pending_read_ = buffer; 266 pending_read_ = buffer;
287 return true; 267 return true;
288 } 268 }
289 OverlappedBuffer::DisposeBuffer(buffer); 269 OverlappedBuffer::DisposeBuffer(buffer);
290 HandleIssueError(); 270 HandleIssueError();
291 return false; 271 return false;
292 } else { 272 } else {
293 // Completing asynchronously through thread. 273 // Completing asynchronously through thread.
294 pending_read_ = buffer; 274 pending_read_ = buffer;
295 int result = Thread::Start(ReadFileThread, 275 int result = Thread::Start(ReadFileThread,
296 reinterpret_cast<uword>(this)); 276 reinterpret_cast<uword>(this));
297 if (result != 0) { 277 if (result != 0) {
298 FATAL1("Failed to start read file thread %d", result); 278 FATAL1("Failed to start read file thread %d", result);
299 } 279 }
300 return true; 280 return true;
301 } 281 }
302 } 282 }
303 283
304 284
305 bool Handle::IssueRecvFrom() { 285 bool Handle::IssueRecvFrom() {
306 return false; 286 return false;
(...skipping 25 matching lines...) Expand all
332 312
333 313
334 bool Handle::IssueSendTo(struct sockaddr* sa, socklen_t sa_len) { 314 bool Handle::IssueSendTo(struct sockaddr* sa, socklen_t sa_len) {
335 return false; 315 return false;
336 } 316 }
337 317
338 318
339 static void HandleClosed(Handle* handle) { 319 static void HandleClosed(Handle* handle) {
340 if (!handle->IsClosing()) { 320 if (!handle->IsClosing()) {
341 int event_mask = 1 << kCloseEvent; 321 int event_mask = 1 << kCloseEvent;
342 DartUtils::PostInt32(handle->port(), event_mask); 322 DartUtils::PostInt32(handle->NextPort(), event_mask);
343 } 323 }
344 } 324 }
345 325
346 326
347 static void HandleError(Handle* handle) { 327 static void HandleError(Handle* handle) {
348 handle->set_last_error(WSAGetLastError()); 328 handle->set_last_error(WSAGetLastError());
349 handle->MarkError(); 329 handle->MarkError();
350 if (!handle->IsClosing()) { 330 if (!handle->IsClosing() && handle->HasNextPort()) {
351 Dart_Port port = handle->port(); 331 DartUtils::PostInt32(handle->NextPort(), 1 << kErrorEvent);
352 if (port != ILLEGAL_PORT) {
353 DartUtils::PostInt32(port, 1 << kErrorEvent);
354 }
355 } 332 }
356 } 333 }
357 334
358 335
359 void Handle::HandleIssueError() { 336 void Handle::HandleIssueError() {
360 DWORD error = GetLastError(); 337 DWORD error = GetLastError();
361 if (error == ERROR_BROKEN_PIPE) { 338 if (error == ERROR_BROKEN_PIPE) {
362 HandleClosed(this); 339 HandleClosed(this);
363 } else { 340 } else {
364 HandleError(this); 341 HandleError(this);
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 ScopedLock lock(this); 485 ScopedLock lock(this);
509 if (!IsClosing()) { 486 if (!IsClosing()) {
510 // Update the accepted socket to support the full range of API calls. 487 // Update the accepted socket to support the full range of API calls.
511 SOCKET s = socket(); 488 SOCKET s = socket();
512 int rc = setsockopt(buffer->client(), 489 int rc = setsockopt(buffer->client(),
513 SOL_SOCKET, 490 SOL_SOCKET,
514 SO_UPDATE_ACCEPT_CONTEXT, 491 SO_UPDATE_ACCEPT_CONTEXT,
515 reinterpret_cast<char*>(&s), sizeof(s)); 492 reinterpret_cast<char*>(&s), sizeof(s));
516 if (rc == NO_ERROR) { 493 if (rc == NO_ERROR) {
517 // Insert the accepted socket into the list. 494 // Insert the accepted socket into the list.
518 ClientSocket* client_socket = new ClientSocket(buffer->client(), 0); 495 ClientSocket* client_socket = new ClientSocket(buffer->client());
519 client_socket->mark_connected(); 496 client_socket->mark_connected();
520 client_socket->CreateCompletionPort(completion_port); 497 client_socket->CreateCompletionPort(completion_port);
521 if (accepted_head_ == NULL) { 498 if (accepted_head_ == NULL) {
522 accepted_head_ = client_socket; 499 accepted_head_ = client_socket;
523 accepted_tail_ = client_socket; 500 accepted_tail_ = client_socket;
524 } else { 501 } else {
525 ASSERT(accepted_tail_ != NULL); 502 ASSERT(accepted_tail_ != NULL);
526 accepted_tail_->set_next(client_socket); 503 accepted_tail_->set_next(client_socket);
527 accepted_tail_ = client_socket; 504 accepted_tail_ = client_socket;
528 } 505 }
529 } else { 506 } else {
530 closesocket(buffer->client()); 507 closesocket(buffer->client());
531 } 508 }
532 } else { 509 } else {
533 // Close the socket, as it's already accepted. 510 // Close the socket, as it's already accepted.
534 closesocket(buffer->client()); 511 closesocket(buffer->client());
535 } 512 }
536 513
537 pending_accept_count_--; 514 pending_accept_count_--;
538 OverlappedBuffer::DisposeBuffer(buffer); 515 OverlappedBuffer::DisposeBuffer(buffer);
539 } 516 }
540 517
541 518
542 static void DeleteIfClosed(Handle* handle) { 519 static void DeleteIfClosed(Handle* handle) {
543 if (handle->IsClosed()) { 520 if (handle->IsClosed()) {
544 Dart_Port port = handle->port(); 521 while (handle->HasNextPort()) {
522 Dart_Port port = handle->NextPort();
523 DartUtils::PostInt32(port, 1 << kDestroyedEvent);
524 handle->RemovePort(port);
525 }
545 delete handle; 526 delete handle;
546 if (port != ILLEGAL_PORT) {
547 DartUtils::PostInt32(port, 1 << kDestroyedEvent);
548 }
549 } 527 }
550 } 528 }
551 529
552 530
553 void ListenSocket::DoClose() { 531 void ListenSocket::DoClose() {
554 closesocket(socket()); 532 closesocket(socket());
555 handle_ = INVALID_HANDLE_VALUE; 533 handle_ = INVALID_HANDLE_VALUE;
556 while (CanAccept()) { 534 while (CanAccept()) {
557 // Get rid of connections already accepted. 535 // Get rid of connections already accepted.
558 ClientSocket *client = Accept(); 536 ClientSocket *client = Accept();
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after
873 851
874 void ClientSocket::IssueDisconnect() { 852 void ClientSocket::IssueDisconnect() {
875 OverlappedBuffer* buffer = OverlappedBuffer::AllocateDisconnectBuffer(); 853 OverlappedBuffer* buffer = OverlappedBuffer::AllocateDisconnectBuffer();
876 BOOL ok = DisconnectEx_( 854 BOOL ok = DisconnectEx_(
877 socket(), buffer->GetCleanOverlapped(), TF_REUSE_SOCKET, 0); 855 socket(), buffer->GetCleanOverlapped(), TF_REUSE_SOCKET, 0);
878 // DisconnectEx works like other OverlappedIO APIs, where we can get either an 856 // DisconnectEx works like other OverlappedIO APIs, where we can get either an
879 // immediate success or delayed operation by WSA_IO_PENDING being set. 857 // immediate success or delayed operation by WSA_IO_PENDING being set.
880 if (ok || WSAGetLastError() != WSA_IO_PENDING) { 858 if (ok || WSAGetLastError() != WSA_IO_PENDING) {
881 DisconnectComplete(buffer); 859 DisconnectComplete(buffer);
882 } 860 }
883 Dart_Port p = port(); 861 if (HasNextPort()) {
884 if (p != ILLEGAL_PORT) DartUtils::PostInt32(p, 1 << kDestroyedEvent); 862 Dart_Port p = NextPort();
885 port_ = ILLEGAL_PORT; 863 DartUtils::PostInt32(p, 1 << kDestroyedEvent);
864 RemovePort(p);
865 }
886 } 866 }
887 867
888 868
889 void ClientSocket::DisconnectComplete(OverlappedBuffer* buffer) { 869 void ClientSocket::DisconnectComplete(OverlappedBuffer* buffer) {
890 OverlappedBuffer::DisposeBuffer(buffer); 870 OverlappedBuffer::DisposeBuffer(buffer);
891 closesocket(socket()); 871 closesocket(socket());
892 if (data_ready_ != NULL) { 872 if (data_ready_ != NULL) {
893 OverlappedBuffer::DisposeBuffer(data_ready_); 873 OverlappedBuffer::DisposeBuffer(data_ready_);
894 } 874 }
895 closed_ = true; 875 closed_ = true;
896 } 876 }
897 877
898 878
899 void ClientSocket::ConnectComplete(OverlappedBuffer* buffer) { 879 void ClientSocket::ConnectComplete(OverlappedBuffer* buffer) {
900 OverlappedBuffer::DisposeBuffer(buffer); 880 OverlappedBuffer::DisposeBuffer(buffer);
901 // Update socket to support full socket API, after ConnectEx completed. 881 // Update socket to support full socket API, after ConnectEx completed.
902 setsockopt(socket(), SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, NULL, 0); 882 setsockopt(socket(), SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, NULL, 0);
903 Dart_Port p = port(); 883 if (HasNextPort()) {
904 if (p != ILLEGAL_PORT) {
905 // If the port is set, we already listen for this socket in Dart. 884 // If the port is set, we already listen for this socket in Dart.
906 // Handle the cases here. 885 // Handle the cases here.
907 if (!IsClosedRead()) { 886 if (!IsClosedRead()) {
908 IssueRead(); 887 IssueRead();
909 } 888 }
910 if (!IsClosedWrite()) { 889 if (!IsClosedWrite()) {
911 DartUtils::PostInt32(p, 1 << kOutEvent); 890 DartUtils::PostInt32(NextPort(), 1 << kOutEvent);
912 } 891 }
913 } 892 }
914 } 893 }
915 894
916 895
917 void ClientSocket::EnsureInitialized( 896 void ClientSocket::EnsureInitialized(
918 EventHandlerImplementation* event_handler) { 897 EventHandlerImplementation* event_handler) {
919 ScopedLock lock(this); 898 ScopedLock lock(this);
920 if (completion_port_ == INVALID_HANDLE_VALUE) { 899 if (completion_port_ == INVALID_HANDLE_VALUE) {
921 ASSERT(event_handler_ == NULL); 900 ASSERT(event_handler_ == NULL);
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
1004 // Just close the socket. This will cause any queued requests to be aborted. 983 // Just close the socket. This will cause any queued requests to be aborted.
1005 closesocket(socket()); 984 closesocket(socket());
1006 MarkClosedRead(); 985 MarkClosedRead();
1007 MarkClosedWrite(); 986 MarkClosedWrite();
1008 handle_ = INVALID_HANDLE_VALUE; 987 handle_ = INVALID_HANDLE_VALUE;
1009 } 988 }
1010 989
1011 990
1012 void EventHandlerImplementation::HandleInterrupt(InterruptMessage* msg) { 991 void EventHandlerImplementation::HandleInterrupt(InterruptMessage* msg) {
1013 ASSERT(this != NULL); 992 ASSERT(this != NULL);
1014 if (msg->id == kTimeoutId) { 993 if (msg->id == kTimerId) {
1015 // Change of timeout request. Just set the new timeout and port as the 994 // Change of timeout request. Just set the new timeout and port as the
1016 // completion thread will use the new timeout value for its next wait. 995 // completion thread will use the new timeout value for its next wait.
1017 timeout_queue_.UpdateTimeout(msg->dart_port, msg->data); 996 timeout_queue_.UpdateTimeout(msg->dart_port, msg->data);
1018 } else if (msg->id == kShutdownId) { 997 } else if (msg->id == kShutdownId) {
1019 shutdown_ = true; 998 shutdown_ = true;
1020 } else { 999 } else {
1021 // No tokens to return on Windows.
1022 if ((msg->data & (1 << kReturnTokenCommand)) != 0) return;
1023 Handle* handle = reinterpret_cast<Handle*>(msg->id); 1000 Handle* handle = reinterpret_cast<Handle*>(msg->id);
1024 ASSERT(handle != NULL); 1001 ASSERT(handle != NULL);
1002
1025 if (handle->is_listen_socket()) { 1003 if (handle->is_listen_socket()) {
1026 ListenSocket* listen_socket = 1004 ListenSocket* listen_socket =
1027 reinterpret_cast<ListenSocket*>(handle); 1005 reinterpret_cast<ListenSocket*>(handle);
1028 listen_socket->EnsureInitialized(this); 1006 listen_socket->EnsureInitialized(this);
1029 listen_socket->SetPortAndMask(msg->dart_port, msg->data);
1030 1007
1031 Handle::ScopedLock lock(listen_socket); 1008 Handle::ScopedLock lock(listen_socket);
1032 1009
1033 // If incoming connections are requested make sure to post already 1010 // If incoming connections are requested make sure to post already
1034 // accepted connections. 1011 // accepted connections.
1035 if ((msg->data & (1 << kInEvent)) != 0) { 1012 if ((msg->data & (1 << kInEvent)) != 0) {
1036 if (listen_socket->CanAccept()) { 1013 listen_socket->SetPortAndMask(msg->dart_port, msg->data & EVENT_MASK);
1037 int event_mask = (1 << kInEvent); 1014 TryDispatchingPendingAccepts(listen_socket);
1038 handle->set_mask(handle->mask() & ~event_mask); 1015 }
1039 DartUtils::PostInt32(handle->port(), event_mask); 1016
1017 if (IS_COMMAND(msg->data, kReturnTokenCommand)) {
1018 int count = TOKEN_COUNT(msg->data);
1019 listen_socket->ReturnTokens(msg->dart_port, count);
1020 TryDispatchingPendingAccepts(listen_socket);
1021 return;
1022 } else if (IS_COMMAND(msg->data, kCloseCommand)) {
1023 Dart_Port port = msg->dart_port;
1024 listen_socket->RemovePort(port);
1025
1026 MutexLocker locker(globalTcpListeningSocketRegistry.mutex());
1027 if (globalTcpListeningSocketRegistry.CloseSafe(
1028 reinterpret_cast<intptr_t>(listen_socket))) {
1029 handle->Close();
1040 } 1030 }
1031 DartUtils::PostInt32(port, 1 << kDestroyedEvent);
1041 } 1032 }
1042 } else { 1033 } else {
1043 handle->EnsureInitialized(this); 1034 handle->EnsureInitialized(this);
1044 1035
1045 Handle::ScopedLock lock(handle); 1036 Handle::ScopedLock lock(handle);
1046 1037
1038 if (IS_COMMAND(msg->data, kReturnTokenCommand)) {
1039 int count = TOKEN_COUNT(msg->data);
1040 handle->ReturnTokens(msg->dart_port, count);
1041 // TODO(kustermann): How can we continue with sending events
1042 // to dart from here?
1043 return;
1044 }
1045
1047 // Only set mask if we turned on kInEvent or kOutEvent. 1046 // Only set mask if we turned on kInEvent or kOutEvent.
1048 if ((msg->data & ((1 << kInEvent) | (1 << kOutEvent))) != 0) { 1047 if ((msg->data & ((1 << kInEvent) | (1 << kOutEvent))) != 0) {
1049 handle->SetPortAndMask(msg->dart_port, msg->data); 1048 handle->SetPortAndMask(msg->dart_port, msg->data & EVENT_MASK);
1050 } 1049 }
1051 1050
1052 // Issue a read. 1051 // Issue a read.
1053 if ((msg->data & (1 << kInEvent)) != 0) { 1052 if ((msg->data & (1 << kInEvent)) != 0) {
1054 if (handle->is_datagram_socket()) { 1053 if (handle->is_datagram_socket()) {
1055 handle->IssueRecvFrom(); 1054 handle->IssueRecvFrom();
1056 } else if (handle->is_client_socket()) { 1055 } else if (handle->is_client_socket()) {
1057 if (reinterpret_cast<ClientSocket*>(handle)->is_connected()) { 1056 if (reinterpret_cast<ClientSocket*>(handle)->is_connected()) {
1058 handle->IssueRead(); 1057 handle->IssueRead();
1059 } 1058 }
1060 } else { 1059 } else {
1061 handle->IssueRead(); 1060 handle->IssueRead();
1062 } 1061 }
1063 } 1062 }
1064 1063
1065 // If out events (can write events) have been requested, and there 1064 // If out events (can write events) have been requested, and there
1066 // are no pending writes, meaning any writes are already complete, 1065 // are no pending writes, meaning any writes are already complete,
1067 // post an out event immediately. 1066 // post an out event immediately.
1068 if ((msg->data & (1 << kOutEvent)) != 0) { 1067 if ((msg->data & (1 << kOutEvent)) != 0) {
1069 if (!handle->HasPendingWrite()) { 1068 if (!handle->HasPendingWrite()) {
1070 if (handle->is_client_socket()) { 1069 if (handle->is_client_socket()) {
1071 if (reinterpret_cast<ClientSocket*>(handle)->is_connected()) { 1070 if (reinterpret_cast<ClientSocket*>(handle)->is_connected()) {
1072 DartUtils::PostInt32(handle->port(), 1 << kOutEvent); 1071 DartUtils::PostInt32(handle->NextPort(), 1 << kOutEvent);
1073 } 1072 }
1074 } else { 1073 } else {
1075 DartUtils::PostInt32(handle->port(), 1 << kOutEvent); 1074 DartUtils::PostInt32(handle->NextPort(), 1 << kOutEvent);
1076 } 1075 }
1077 } 1076 }
1078 } 1077 }
1079 1078
1080 if (handle->is_client_socket()) { 1079 if (handle->is_client_socket()) {
1081 ClientSocket* client_socket = reinterpret_cast<ClientSocket*>(handle); 1080 ClientSocket* client_socket = reinterpret_cast<ClientSocket*>(handle);
1082 if ((msg->data & (1 << kShutdownReadCommand)) != 0) { 1081 if ((msg->data & (1 << kShutdownReadCommand)) != 0) {
1083 client_socket->Shutdown(SD_RECEIVE); 1082 client_socket->Shutdown(SD_RECEIVE);
1084 } 1083 }
1085 1084
1086 if ((msg->data & (1 << kShutdownWriteCommand)) != 0) { 1085 if ((msg->data & (1 << kShutdownWriteCommand)) != 0) {
1087 client_socket->Shutdown(SD_SEND); 1086 client_socket->Shutdown(SD_SEND);
1088 } 1087 }
1089 } 1088 }
1089
1090 if ((msg->data & (1 << kCloseCommand)) != 0) {
1091 handle->SetPortAndMask(msg->dart_port, msg->data & EVENT_MASK);
1092 handle->Close();
1093 }
1090 } 1094 }
1091
1092 if ((msg->data & (1 << kCloseCommand)) != 0) {
1093 handle->SetPortAndMask(msg->dart_port, msg->data);
1094 handle->Close();
1095 }
1096
1097 DeleteIfClosed(handle); 1095 DeleteIfClosed(handle);
1098 } 1096 }
1099 } 1097 }
1100 1098
1101 1099
1102 void EventHandlerImplementation::HandleAccept(ListenSocket* listen_socket, 1100 void EventHandlerImplementation::HandleAccept(ListenSocket* listen_socket,
1103 OverlappedBuffer* buffer) { 1101 OverlappedBuffer* buffer) {
1104 listen_socket->AcceptComplete(buffer, completion_port_); 1102 listen_socket->AcceptComplete(buffer, completion_port_);
1105 1103
1106 if (!listen_socket->IsClosing()) { 1104 TryDispatchingPendingAccepts(listen_socket);
1107 int event_mask = 1 << kInEvent; 1105
1108 if ((listen_socket->mask() & event_mask) != 0) { 1106 DeleteIfClosed(listen_socket);
1109 DartUtils::PostInt32(listen_socket->port(), event_mask); 1107 }
1108
1109
1110 void EventHandlerImplementation::TryDispatchingPendingAccepts(
1111 ListenSocket *listen_socket) {
1112 if (!listen_socket->IsClosing() && listen_socket->CanAccept()) {
1113 for (int i = 0; i < listen_socket->pending_accept_count(); i++) {
1114 if (listen_socket->HasNextPort()) {
1115 Dart_Port port = listen_socket->NextPort();
1116 DartUtils::PostInt32(port, 1 << kInEvent);
1117 if (listen_socket->TakeToken()) {
1118 break;
1119 }
1120 } else {
1121 break;
1122 }
1110 } 1123 }
1111 } 1124 }
1112
1113 DeleteIfClosed(listen_socket);
1114 } 1125 }
1115 1126
1116 1127
1117 void EventHandlerImplementation::HandleRead(Handle* handle, 1128 void EventHandlerImplementation::HandleRead(Handle* handle,
1118 int bytes, 1129 int bytes,
1119 OverlappedBuffer* buffer) { 1130 OverlappedBuffer* buffer) {
1120 buffer->set_data_length(bytes); 1131 buffer->set_data_length(bytes);
1121 handle->ReadComplete(buffer); 1132 handle->ReadComplete(buffer);
1122 if (bytes > 0) { 1133 if (bytes > 0) {
1123 if (!handle->IsClosing()) { 1134 if (!handle->IsClosing()) {
1124 int event_mask = 1 << kInEvent; 1135 int event_mask = 1 << kInEvent;
1125 if ((handle->mask() & event_mask) != 0) { 1136 if ((handle->Mask() & event_mask) != 0) {
1126 DartUtils::PostInt32(handle->port(), event_mask); 1137 DartUtils::PostInt32(handle->NextPort(), event_mask);
1127 } 1138 }
1128 } 1139 }
1129 } else { 1140 } else {
1130 handle->MarkClosedRead(); 1141 handle->MarkClosedRead();
1131 if (bytes == 0) { 1142 if (bytes == 0) {
1132 HandleClosed(handle); 1143 HandleClosed(handle);
1133 } else { 1144 } else {
1134 HandleError(handle); 1145 HandleError(handle);
1135 } 1146 }
1136 } 1147 }
1137 1148
1138 DeleteIfClosed(handle); 1149 DeleteIfClosed(handle);
1139 } 1150 }
1140 1151
1141 1152
1142 void EventHandlerImplementation::HandleRecvFrom(Handle* handle, 1153 void EventHandlerImplementation::HandleRecvFrom(Handle* handle,
1143 int bytes, 1154 int bytes,
1144 OverlappedBuffer* buffer) { 1155 OverlappedBuffer* buffer) {
1145 ASSERT(handle->is_datagram_socket()); 1156 ASSERT(handle->is_datagram_socket());
1146 buffer->set_data_length(bytes); 1157 buffer->set_data_length(bytes);
1147 handle->ReadComplete(buffer); 1158 handle->ReadComplete(buffer);
1148 if (!handle->IsClosing()) { 1159 if (!handle->IsClosing()) {
1149 int event_mask = 1 << kInEvent; 1160 int event_mask = 1 << kInEvent;
1150 if ((handle->mask() & event_mask) != 0) { 1161 if ((handle->Mask() & event_mask) != 0) {
1151 DartUtils::PostInt32(handle->port(), event_mask); 1162 DartUtils::PostInt32(handle->NextPort(), event_mask);
1152 } 1163 }
1153 } 1164 }
1154 1165
1155 DeleteIfClosed(handle); 1166 DeleteIfClosed(handle);
1156 } 1167 }
1157 1168
1158 1169
1159 void EventHandlerImplementation::HandleWrite(Handle* handle, 1170 void EventHandlerImplementation::HandleWrite(Handle* handle,
1160 int bytes, 1171 int bytes,
1161 OverlappedBuffer* buffer) { 1172 OverlappedBuffer* buffer) {
1162 handle->WriteComplete(buffer); 1173 handle->WriteComplete(buffer);
1163 1174
1164 if (bytes >= 0) { 1175 if (bytes >= 0) {
1165 if (!handle->IsError() && !handle->IsClosing()) { 1176 if (!handle->IsError() && !handle->IsClosing()) {
1166 int event_mask = 1 << kOutEvent; 1177 int event_mask = 1 << kOutEvent;
1167 ASSERT(!handle->is_client_socket() || 1178 ASSERT(!handle->is_client_socket() ||
1168 reinterpret_cast<ClientSocket*>(handle)->is_connected()); 1179 reinterpret_cast<ClientSocket*>(handle)->is_connected());
1169 if ((handle->mask() & event_mask) != 0) { 1180 if ((handle->Mask() & event_mask) != 0) {
1170 DartUtils::PostInt32(handle->port(), event_mask); 1181 DartUtils::PostInt32(handle->NextPort(), event_mask);
1171 } 1182 }
1172 } 1183 }
1173 } else { 1184 } else {
1174 HandleError(handle); 1185 HandleError(handle);
1175 } 1186 }
1176 1187
1177 DeleteIfClosed(handle); 1188 DeleteIfClosed(handle);
1178 } 1189 }
1179 1190
1180 1191
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
1343 } else { 1354 } else {
1344 handler_impl->HandleIOCompletion(bytes, key, overlapped); 1355 handler_impl->HandleIOCompletion(bytes, key, overlapped);
1345 } 1356 }
1346 } 1357 }
1347 delete handler; 1358 delete handler;
1348 } 1359 }
1349 1360
1350 1361
1351 void EventHandlerImplementation::Start(EventHandler* handler) { 1362 void EventHandlerImplementation::Start(EventHandler* handler) {
1352 int result = Thread::Start(EventHandlerEntry, 1363 int result = Thread::Start(EventHandlerEntry,
1353 reinterpret_cast<uword>(handler)); 1364 reinterpret_cast<uword>(handler));
1354 if (result != 0) { 1365 if (result != 0) {
1355 FATAL1("Failed to start event handler thread %d", result); 1366 FATAL1("Failed to start event handler thread %d", result);
1356 } 1367 }
1357 1368
1358 // Initialize Winsock32 1369 // Initialize Winsock32
1359 if (!Socket::Initialize()) { 1370 if (!Socket::Initialize()) {
1360 FATAL("Failed to initialized Windows sockets"); 1371 FATAL("Failed to initialized Windows sockets");
1361 } 1372 }
1362 } 1373 }
1363 1374
1364 1375
1365 void EventHandlerImplementation::Shutdown() { 1376 void EventHandlerImplementation::Shutdown() {
1366 SendData(kShutdownId, 0, 0); 1377 SendData(kShutdownId, 0, 0);
1367 } 1378 }
1368 1379
1369 } // namespace bin 1380 } // namespace bin
1370 } // namespace dart 1381 } // namespace dart
1371 1382
1372 #endif // defined(TARGET_OS_WINDOWS) 1383 #endif // defined(TARGET_OS_WINDOWS)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698