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

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: Part 3: Other platforms (preliminary) 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 : DescriptorInfo(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()) {
Søren Gjesse 2015/02/02 10:56:14 Indentation.
kustermann 2015/02/03 10:45:35 Done.
544 Dart_Port port = handle->port(); 521 while (handle->HasNextPort()) {
545 delete handle; 522 Dart_Port port = handle->NextPort();
546 if (port != ILLEGAL_PORT) { 523 DartUtils::PostInt32(port, 1 << kDestroyedEvent);
547 DartUtils::PostInt32(port, 1 << kDestroyedEvent); 524 handle->RemovePort(port);
548 } 525 }
526 delete handle;
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
1003 if (IS_COMMAND(msg->data, kReturnTokenCommand)) {
1004 int count = TOKEN_COUNT(msg->data);
1005 bool become_active = handle->ReturnTokens(msg->dart_port, count);
1006 // TODO(kustermann): on linux we do
Søren Gjesse 2015/02/02 10:56:14 On Windows we always have 5 outstanding accept req
1007 // if (become_active) {
1008 // ReturnToEpollSet();
1009 // }
1010 return;
1011 }
1012
1025 if (handle->is_listen_socket()) { 1013 if (handle->is_listen_socket()) {
1026 ListenSocket* listen_socket = 1014 ListenSocket* listen_socket =
1027 reinterpret_cast<ListenSocket*>(handle); 1015 reinterpret_cast<ListenSocket*>(handle);
1028 listen_socket->EnsureInitialized(this); 1016 listen_socket->EnsureInitialized(this);
1029 listen_socket->SetPortAndMask(msg->dart_port, msg->data); 1017
1018 listen_socket->SetMask(msg->data);
1019 listen_socket->AddPort(msg->dart_port);
1030 1020
1031 Handle::ScopedLock lock(listen_socket); 1021 Handle::ScopedLock lock(listen_socket);
1032 1022
1033 // If incoming connections are requested make sure to post already 1023 // If incoming connections are requested make sure to post already
1034 // accepted connections. 1024 // accepted connections.
1035 if ((msg->data & (1 << kInEvent)) != 0) { 1025 if ((msg->data & (1 << kInEvent)) != 0) {
1036 if (listen_socket->CanAccept()) { 1026 if (listen_socket->CanAccept()) {
1027 Dart_Port port = handle->NextPort();
1037 int event_mask = (1 << kInEvent); 1028 int event_mask = (1 << kInEvent);
1038 handle->set_mask(handle->mask() & ~event_mask); 1029 DartUtils::PostInt32(port, event_mask);
1039 DartUtils::PostInt32(handle->port(), event_mask); 1030 handle->SetMask(handle->Mask() & ~event_mask);
1031 if (listen_socket->TakeToken()) {
1032 // TODO(kustermann): On linux we remove listening sockeet from
1033 // epoll set. here
1034 // FIXME(kustermann): Should we do this only if there are no more
1035 // listeners?
1036 // handle->SetMask(handle->Mask() & ~event_mask);
1037 }
1040 } 1038 }
1041 } 1039 }
1042 } else { 1040 } else {
1043 handle->EnsureInitialized(this); 1041 handle->EnsureInitialized(this);
1044 1042
1045 Handle::ScopedLock lock(handle); 1043 Handle::ScopedLock lock(handle);
1046 1044
1047 // Only set mask if we turned on kInEvent or kOutEvent. 1045 // Only set mask if we turned on kInEvent or kOutEvent.
1048 if ((msg->data & ((1 << kInEvent) | (1 << kOutEvent))) != 0) { 1046 if ((msg->data & ((1 << kInEvent) | (1 << kOutEvent))) != 0) {
1049 handle->SetPortAndMask(msg->dart_port, msg->data); 1047 handle->SetMask(msg->data);
1048 handle->AddPort(msg->dart_port);
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 }
1090 }
1091 1089
1092 if ((msg->data & (1 << kCloseCommand)) != 0) { 1090 if ((msg->data & (1 << kCloseCommand)) != 0) {
1093 handle->SetPortAndMask(msg->dart_port, msg->data); 1091 handle->SetMask(msg->data);
1094 handle->Close(); 1092 handle->AddPort(msg->dart_port);
1093 handle->Close();
1094 }
1095 } 1095 }
1096 1096
1097 DeleteIfClosed(handle); 1097 DeleteIfClosed(handle);
1098 } 1098 }
1099 } 1099 }
1100 1100
1101 1101
1102 void EventHandlerImplementation::HandleAccept(ListenSocket* listen_socket, 1102 void EventHandlerImplementation::HandleAccept(ListenSocket* listen_socket,
1103 OverlappedBuffer* buffer) { 1103 OverlappedBuffer* buffer) {
1104 listen_socket->AcceptComplete(buffer, completion_port_); 1104 listen_socket->AcceptComplete(buffer, completion_port_);
1105 1105
1106 if (!listen_socket->IsClosing()) { 1106 if (!listen_socket->IsClosing()) {
1107 int event_mask = 1 << kInEvent; 1107 if ((listen_socket->Mask() & (1 << kInEvent)) != 0) {
1108 if ((listen_socket->mask() & event_mask) != 0) { 1108 DartUtils::PostInt32(listen_socket->NextPort(), 1 << kInEvent);
1109 DartUtils::PostInt32(listen_socket->port(), event_mask);
1110 } 1109 }
1111 } 1110 }
1112 1111
1113 DeleteIfClosed(listen_socket); 1112 DeleteIfClosed(listen_socket);
1114 } 1113 }
1115 1114
1116 1115
1117 void EventHandlerImplementation::HandleRead(Handle* handle, 1116 void EventHandlerImplementation::HandleRead(Handle* handle,
1118 int bytes, 1117 int bytes,
1119 OverlappedBuffer* buffer) { 1118 OverlappedBuffer* buffer) {
1120 buffer->set_data_length(bytes); 1119 buffer->set_data_length(bytes);
1121 handle->ReadComplete(buffer); 1120 handle->ReadComplete(buffer);
1122 if (bytes > 0) { 1121 if (bytes > 0) {
1123 if (!handle->IsClosing()) { 1122 if (!handle->IsClosing()) {
1124 int event_mask = 1 << kInEvent; 1123 int event_mask = 1 << kInEvent;
1125 if ((handle->mask() & event_mask) != 0) { 1124 if ((handle->Mask() & event_mask) != 0) {
1126 DartUtils::PostInt32(handle->port(), event_mask); 1125 DartUtils::PostInt32(handle->NextPort(), event_mask);
1127 } 1126 }
1128 } 1127 }
1129 } else { 1128 } else {
1130 handle->MarkClosedRead(); 1129 handle->MarkClosedRead();
1131 if (bytes == 0) { 1130 if (bytes == 0) {
1132 HandleClosed(handle); 1131 HandleClosed(handle);
1133 } else { 1132 } else {
1134 HandleError(handle); 1133 HandleError(handle);
1135 } 1134 }
1136 } 1135 }
1137 1136
1138 DeleteIfClosed(handle); 1137 DeleteIfClosed(handle);
1139 } 1138 }
1140 1139
1141 1140
1142 void EventHandlerImplementation::HandleRecvFrom(Handle* handle, 1141 void EventHandlerImplementation::HandleRecvFrom(Handle* handle,
1143 int bytes, 1142 int bytes,
1144 OverlappedBuffer* buffer) { 1143 OverlappedBuffer* buffer) {
1145 ASSERT(handle->is_datagram_socket()); 1144 ASSERT(handle->is_datagram_socket());
1146 buffer->set_data_length(bytes); 1145 buffer->set_data_length(bytes);
1147 handle->ReadComplete(buffer); 1146 handle->ReadComplete(buffer);
1148 if (!handle->IsClosing()) { 1147 if (!handle->IsClosing()) {
1149 int event_mask = 1 << kInEvent; 1148 int event_mask = 1 << kInEvent;
1150 if ((handle->mask() & event_mask) != 0) { 1149 if ((handle->Mask() & event_mask) != 0) {
1151 DartUtils::PostInt32(handle->port(), event_mask); 1150 DartUtils::PostInt32(handle->NextPort(), event_mask);
1152 } 1151 }
1153 } 1152 }
1154 1153
1155 DeleteIfClosed(handle); 1154 DeleteIfClosed(handle);
1156 } 1155 }
1157 1156
1158 1157
1159 void EventHandlerImplementation::HandleWrite(Handle* handle, 1158 void EventHandlerImplementation::HandleWrite(Handle* handle,
1160 int bytes, 1159 int bytes,
1161 OverlappedBuffer* buffer) { 1160 OverlappedBuffer* buffer) {
1162 handle->WriteComplete(buffer); 1161 handle->WriteComplete(buffer);
1163 1162
1164 if (bytes >= 0) { 1163 if (bytes >= 0) {
1165 if (!handle->IsError() && !handle->IsClosing()) { 1164 if (!handle->IsError() && !handle->IsClosing()) {
1166 int event_mask = 1 << kOutEvent; 1165 int event_mask = 1 << kOutEvent;
1167 ASSERT(!handle->is_client_socket() || 1166 ASSERT(!handle->is_client_socket() ||
1168 reinterpret_cast<ClientSocket*>(handle)->is_connected()); 1167 reinterpret_cast<ClientSocket*>(handle)->is_connected());
1169 if ((handle->mask() & event_mask) != 0) { 1168 if ((handle->Mask() & event_mask) != 0) {
1170 DartUtils::PostInt32(handle->port(), event_mask); 1169 DartUtils::PostInt32(handle->NextPort(), event_mask);
1171 } 1170 }
1172 } 1171 }
1173 } else { 1172 } else {
1174 HandleError(handle); 1173 HandleError(handle);
1175 } 1174 }
1176 1175
1177 DeleteIfClosed(handle); 1176 DeleteIfClosed(handle);
1178 } 1177 }
1179 1178
1180 1179
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
1343 } else { 1342 } else {
1344 handler_impl->HandleIOCompletion(bytes, key, overlapped); 1343 handler_impl->HandleIOCompletion(bytes, key, overlapped);
1345 } 1344 }
1346 } 1345 }
1347 delete handler; 1346 delete handler;
1348 } 1347 }
1349 1348
1350 1349
1351 void EventHandlerImplementation::Start(EventHandler* handler) { 1350 void EventHandlerImplementation::Start(EventHandler* handler) {
1352 int result = Thread::Start(EventHandlerEntry, 1351 int result = Thread::Start(EventHandlerEntry,
1353 reinterpret_cast<uword>(handler)); 1352 reinterpret_cast<uword>(handler));
1354 if (result != 0) { 1353 if (result != 0) {
1355 FATAL1("Failed to start event handler thread %d", result); 1354 FATAL1("Failed to start event handler thread %d", result);
1356 } 1355 }
1357 1356
1358 // Initialize Winsock32 1357 // Initialize Winsock32
1359 if (!Socket::Initialize()) { 1358 if (!Socket::Initialize()) {
1360 FATAL("Failed to initialized Windows sockets"); 1359 FATAL("Failed to initialized Windows sockets");
1361 } 1360 }
1362 } 1361 }
1363 1362
1364 1363
1365 void EventHandlerImplementation::Shutdown() { 1364 void EventHandlerImplementation::Shutdown() {
1366 SendData(kShutdownId, 0, 0); 1365 SendData(kShutdownId, 0, 0);
1367 } 1366 }
1368 1367
1369 } // namespace bin 1368 } // namespace bin
1370 } // namespace dart 1369 } // namespace dart
1371 1370
1372 #endif // defined(TARGET_OS_WINDOWS) 1371 #endif // defined(TARGET_OS_WINDOWS)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698