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

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

Issue 11312242: Revised CL for customisable logging (replacing printfs). (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 1 month 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) 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>
11 11
12 #include "bin/builtin.h" 12 #include "bin/builtin.h"
13 #include "bin/dartutils.h" 13 #include "bin/dartutils.h"
14 #include "bin/log.h"
14 #include "bin/socket.h" 15 #include "bin/socket.h"
15 #include "platform/thread.h" 16 #include "platform/thread.h"
16 17
17 18
18 static const int kInfinityTimeout = -1; 19 static const int kInfinityTimeout = -1;
19 static const int kTimeoutId = -1; 20 static const int kTimeoutId = -1;
20 static const int kShutdownId = -2; 21 static const int kShutdownId = -2;
21 22
22 23
23 int64_t GetCurrentTimeMilliseconds() { 24 int64_t GetCurrentTimeMilliseconds() {
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 LeaveCriticalSection(&cs_); 137 LeaveCriticalSection(&cs_);
137 } 138 }
138 139
139 140
140 bool Handle::CreateCompletionPort(HANDLE completion_port) { 141 bool Handle::CreateCompletionPort(HANDLE completion_port) {
141 completion_port_ = CreateIoCompletionPort(handle_, 142 completion_port_ = CreateIoCompletionPort(handle_,
142 completion_port, 143 completion_port,
143 reinterpret_cast<ULONG_PTR>(this), 144 reinterpret_cast<ULONG_PTR>(this),
144 0); 145 0);
145 if (completion_port_ == NULL) { 146 if (completion_port_ == NULL) {
146 fprintf(stderr, "Error CreateIoCompletionPort: %d\n", GetLastError()); 147 Log::PrintErr("Error CreateIoCompletionPort: %d\n", GetLastError());
147 return false; 148 return false;
148 } 149 }
149 return true; 150 return true;
150 } 151 }
151 152
152 153
153 void Handle::close() { 154 void Handle::close() {
154 ScopedLock lock(this); 155 ScopedLock lock(this);
155 if (!IsClosing()) { 156 if (!IsClosing()) {
156 // Close the socket and set the closing state. This close method can be 157 // Close the socket and set the closing state. This close method can be
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 void Handle::ReadSyncCompleteAsync() { 218 void Handle::ReadSyncCompleteAsync() {
218 ASSERT(pending_read_ != NULL); 219 ASSERT(pending_read_ != NULL);
219 DWORD bytes_read = 0; 220 DWORD bytes_read = 0;
220 BOOL ok = ReadFile(handle_, 221 BOOL ok = ReadFile(handle_,
221 pending_read_->GetBufferStart(), 222 pending_read_->GetBufferStart(),
222 pending_read_->GetBufferSize(), 223 pending_read_->GetBufferSize(),
223 &bytes_read, 224 &bytes_read,
224 NULL); 225 NULL);
225 if (!ok) { 226 if (!ok) {
226 if (GetLastError() != ERROR_BROKEN_PIPE) { 227 if (GetLastError() != ERROR_BROKEN_PIPE) {
227 fprintf(stderr, "ReadFile failed %d\n", GetLastError()); 228 Log::PrintErr("ReadFile failed %d\n", GetLastError());
228 } 229 }
229 bytes_read = 0; 230 bytes_read = 0;
230 } 231 }
231 OVERLAPPED* overlapped = pending_read_->GetCleanOverlapped(); 232 OVERLAPPED* overlapped = pending_read_->GetCleanOverlapped();
232 ok = PostQueuedCompletionStatus(event_handler_->completion_port(), 233 ok = PostQueuedCompletionStatus(event_handler_->completion_port(),
233 bytes_read, 234 bytes_read,
234 reinterpret_cast<ULONG_PTR>(this), 235 reinterpret_cast<ULONG_PTR>(this),
235 overlapped); 236 overlapped);
236 if (!ok) { 237 if (!ok) {
237 FATAL("PostQueuedCompletionStatus failed"); 238 FATAL("PostQueuedCompletionStatus failed");
(...skipping 14 matching lines...) Expand all
252 buffer->GetBufferSize(), 253 buffer->GetBufferSize(),
253 NULL, 254 NULL,
254 buffer->GetCleanOverlapped()); 255 buffer->GetCleanOverlapped());
255 if (ok || GetLastError() == ERROR_IO_PENDING) { 256 if (ok || GetLastError() == ERROR_IO_PENDING) {
256 // Completing asynchronously. 257 // Completing asynchronously.
257 pending_read_ = buffer; 258 pending_read_ = buffer;
258 return true; 259 return true;
259 } 260 }
260 261
261 if (GetLastError() != ERROR_BROKEN_PIPE) { 262 if (GetLastError() != ERROR_BROKEN_PIPE) {
262 fprintf(stderr, "ReadFile failed: %d\n", GetLastError()); 263 Log::PrintErr("ReadFile failed: %d\n", GetLastError());
263 } 264 }
264 event_handler_->HandleClosed(this); 265 event_handler_->HandleClosed(this);
265 IOBuffer::DisposeBuffer(buffer); 266 IOBuffer::DisposeBuffer(buffer);
266 return false; 267 return false;
267 } else { 268 } else {
268 // Completing asynchronously through thread. 269 // Completing asynchronously through thread.
269 pending_read_ = buffer; 270 pending_read_ = buffer;
270 uint32_t tid; 271 uint32_t tid;
271 uintptr_t thread_handle = 272 uintptr_t thread_handle =
272 _beginthreadex(NULL, 32 * 1024, ReadFileThread, this, 0, &tid); 273 _beginthreadex(NULL, 32 * 1024, ReadFileThread, this, 0, &tid);
(...skipping 18 matching lines...) Expand all
291 buffer->GetBufferSize(), 292 buffer->GetBufferSize(),
292 NULL, 293 NULL,
293 buffer->GetCleanOverlapped()); 294 buffer->GetCleanOverlapped());
294 if (ok || GetLastError() == ERROR_IO_PENDING) { 295 if (ok || GetLastError() == ERROR_IO_PENDING) {
295 // Completing asynchronously. 296 // Completing asynchronously.
296 pending_write_ = buffer; 297 pending_write_ = buffer;
297 return true; 298 return true;
298 } 299 }
299 300
300 if (GetLastError() != ERROR_BROKEN_PIPE) { 301 if (GetLastError() != ERROR_BROKEN_PIPE) {
301 fprintf(stderr, "WriteFile failed: %d\n", GetLastError()); 302 Log::PrintErr("WriteFile failed: %d\n", GetLastError());
302 } 303 }
303 event_handler_->HandleClosed(this); 304 event_handler_->HandleClosed(this);
304 IOBuffer::DisposeBuffer(buffer); 305 IOBuffer::DisposeBuffer(buffer);
305 return false; 306 return false;
306 } 307 }
307 308
308 309
309 void FileHandle::EnsureInitialized(EventHandlerImplementation* event_handler) { 310 void FileHandle::EnsureInitialized(EventHandlerImplementation* event_handler) {
310 ScopedLock lock(this); 311 ScopedLock lock(this);
311 event_handler_ = event_handler; 312 event_handler_ = event_handler;
(...skipping 25 matching lines...) Expand all
337 int status = WSAIoctl(socket(), 338 int status = WSAIoctl(socket(),
338 SIO_GET_EXTENSION_FUNCTION_POINTER, 339 SIO_GET_EXTENSION_FUNCTION_POINTER,
339 &guid_accept_ex, 340 &guid_accept_ex,
340 sizeof(guid_accept_ex), 341 sizeof(guid_accept_ex),
341 &AcceptEx_, 342 &AcceptEx_,
342 sizeof(AcceptEx_), 343 sizeof(AcceptEx_),
343 &bytes, 344 &bytes,
344 NULL, 345 NULL,
345 NULL); 346 NULL);
346 if (status == SOCKET_ERROR) { 347 if (status == SOCKET_ERROR) {
347 fprintf(stderr, "Error WSAIoctl failed: %d\n", WSAGetLastError()); 348 Log::PrintErr("Error WSAIoctl failed: %d\n", WSAGetLastError());
348 return false; 349 return false;
349 } 350 }
350 return true; 351 return true;
351 } 352 }
352 353
353 354
354 bool ListenSocket::IssueAccept() { 355 bool ListenSocket::IssueAccept() {
355 ScopedLock lock(this); 356 ScopedLock lock(this);
356 357
357 // For AcceptEx there needs to be buffer storage for address 358 // For AcceptEx there needs to be buffer storage for address
(...skipping 11 matching lines...) Expand all
369 ok = AcceptEx_(socket(), 370 ok = AcceptEx_(socket(),
370 buffer->client(), 371 buffer->client(),
371 buffer->GetBufferStart(), 372 buffer->GetBufferStart(),
372 0, // For now don't receive data with accept. 373 0, // For now don't receive data with accept.
373 kAcceptExAddressStorageSize, 374 kAcceptExAddressStorageSize,
374 kAcceptExAddressStorageSize, 375 kAcceptExAddressStorageSize,
375 &received, 376 &received,
376 buffer->GetCleanOverlapped()); 377 buffer->GetCleanOverlapped());
377 if (!ok) { 378 if (!ok) {
378 if (WSAGetLastError() != WSA_IO_PENDING) { 379 if (WSAGetLastError() != WSA_IO_PENDING) {
379 fprintf(stderr, "AcceptEx failed: %d\n", WSAGetLastError()); 380 Log::PrintErr("AcceptEx failed: %d\n", WSAGetLastError());
380 closesocket(buffer->client()); 381 closesocket(buffer->client());
381 IOBuffer::DisposeBuffer(buffer); 382 IOBuffer::DisposeBuffer(buffer);
382 return false; 383 return false;
383 } 384 }
384 } 385 }
385 386
386 pending_accept_count_++; 387 pending_accept_count_++;
387 388
388 return true; 389 return true;
389 } 390 }
(...skipping 25 matching lines...) Expand all
415 client_socket->CreateCompletionPort(completion_port); 416 client_socket->CreateCompletionPort(completion_port);
416 if (accepted_head_ == NULL) { 417 if (accepted_head_ == NULL) {
417 accepted_head_ = client_socket; 418 accepted_head_ = client_socket;
418 accepted_tail_ = client_socket; 419 accepted_tail_ = client_socket;
419 } else { 420 } else {
420 ASSERT(accepted_tail_ != NULL); 421 ASSERT(accepted_tail_ != NULL);
421 accepted_tail_->set_next(client_socket); 422 accepted_tail_->set_next(client_socket);
422 accepted_tail_ = client_socket; 423 accepted_tail_ = client_socket;
423 } 424 }
424 } else { 425 } else {
425 fprintf(stderr, "setsockopt failed: %d\n", WSAGetLastError()); 426 Log::PrintErr("setsockopt failed: %d\n", WSAGetLastError());
426 closesocket(buffer->client()); 427 closesocket(buffer->client());
427 } 428 }
428 } 429 }
429 430
430 pending_accept_count_--; 431 pending_accept_count_--;
431 IOBuffer::DisposeBuffer(buffer); 432 IOBuffer::DisposeBuffer(buffer);
432 } 433 }
433 434
434 435
435 ClientSocket* ListenSocket::Accept() { 436 ClientSocket* ListenSocket::Accept() {
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
507 return num_bytes; 508 return num_bytes;
508 } else { 509 } else {
509 DWORD bytes_written = -1; 510 DWORD bytes_written = -1;
510 BOOL ok = WriteFile(handle_, 511 BOOL ok = WriteFile(handle_,
511 buffer, 512 buffer,
512 num_bytes, 513 num_bytes,
513 &bytes_written, 514 &bytes_written,
514 NULL); 515 NULL);
515 if (!ok) { 516 if (!ok) {
516 if (GetLastError() != ERROR_BROKEN_PIPE) { 517 if (GetLastError() != ERROR_BROKEN_PIPE) {
517 fprintf(stderr, "WriteFile failed: %d\n", GetLastError()); 518 Log::PrintErr("WriteFile failed: %d\n", GetLastError());
518 } 519 }
519 event_handler_->HandleClosed(this); 520 event_handler_->HandleClosed(this);
520 } 521 }
521 return bytes_written; 522 return bytes_written;
522 } 523 }
523 } 524 }
524 525
525 526
526 void ClientSocket::Shutdown(int how) { 527 void ClientSocket::Shutdown(int how) {
527 int rc = shutdown(socket(), how); 528 int rc = shutdown(socket(), how);
528 if (rc == SOCKET_ERROR) { 529 if (rc == SOCKET_ERROR) {
529 fprintf(stderr, "shutdown failed: %d %d\n", socket(), WSAGetLastError()); 530 Log::PrintErr("shutdown failed: %d %d\n", socket(), WSAGetLastError());
530 } 531 }
531 if (how == SD_RECEIVE) MarkClosedRead(); 532 if (how == SD_RECEIVE) MarkClosedRead();
532 if (how == SD_SEND) MarkClosedWrite(); 533 if (how == SD_SEND) MarkClosedWrite();
533 if (how == SD_BOTH) { 534 if (how == SD_BOTH) {
534 MarkClosedRead(); 535 MarkClosedRead();
535 MarkClosedWrite(); 536 MarkClosedWrite();
536 } 537 }
537 } 538 }
538 539
539 540
(...skipping 12 matching lines...) Expand all
552 NULL, 553 NULL,
553 &flags, 554 &flags,
554 buffer->GetCleanOverlapped(), 555 buffer->GetCleanOverlapped(),
555 NULL); 556 NULL);
556 if (rc == NO_ERROR || WSAGetLastError() == WSA_IO_PENDING) { 557 if (rc == NO_ERROR || WSAGetLastError() == WSA_IO_PENDING) {
557 pending_read_ = buffer; 558 pending_read_ = buffer;
558 return true; 559 return true;
559 } 560 }
560 561
561 if (WSAGetLastError() != WSAECONNRESET) { 562 if (WSAGetLastError() != WSAECONNRESET) {
562 fprintf(stderr, "WSARecv failed: %d\n", WSAGetLastError()); 563 Log::PrintErr("WSARecv failed: %d\n", WSAGetLastError());
563 } 564 }
564 event_handler_->HandleClosed(this); 565 event_handler_->HandleClosed(this);
565 IOBuffer::DisposeBuffer(buffer); 566 IOBuffer::DisposeBuffer(buffer);
566 return false; 567 return false;
567 } 568 }
568 569
569 570
570 bool ClientSocket::IssueWrite() { 571 bool ClientSocket::IssueWrite() {
571 ScopedLock lock(this); 572 ScopedLock lock(this);
572 ASSERT(completion_port_ != INVALID_HANDLE_VALUE); 573 ASSERT(completion_port_ != INVALID_HANDLE_VALUE);
573 ASSERT(pending_write_ != NULL); 574 ASSERT(pending_write_ != NULL);
574 ASSERT(pending_write_->operation() == IOBuffer::kWrite); 575 ASSERT(pending_write_->operation() == IOBuffer::kWrite);
575 576
576 int rc = WSASend(socket(), 577 int rc = WSASend(socket(),
577 pending_write_->GetWASBUF(), 578 pending_write_->GetWASBUF(),
578 1, 579 1,
579 NULL, 580 NULL,
580 0, 581 0,
581 pending_write_->GetCleanOverlapped(), 582 pending_write_->GetCleanOverlapped(),
582 NULL); 583 NULL);
583 if (rc == NO_ERROR || WSAGetLastError() == WSA_IO_PENDING) { 584 if (rc == NO_ERROR || WSAGetLastError() == WSA_IO_PENDING) {
584 return true; 585 return true;
585 } 586 }
586 587
587 fprintf(stderr, "WSASend failed: %d\n", WSAGetLastError()); 588 Log::PrintErr("WSASend failed: %d\n", WSAGetLastError());
588 IOBuffer::DisposeBuffer(pending_write_); 589 IOBuffer::DisposeBuffer(pending_write_);
589 pending_write_ = NULL; 590 pending_write_ = NULL;
590 return false; 591 return false;
591 } 592 }
592 593
593 594
594 void ClientSocket::EnsureInitialized( 595 void ClientSocket::EnsureInitialized(
595 EventHandlerImplementation* event_handler) { 596 EventHandlerImplementation* event_handler) {
596 ScopedLock lock(this); 597 ScopedLock lock(this);
597 if (completion_port_ == INVALID_HANDLE_VALUE) { 598 if (completion_port_ == INVALID_HANDLE_VALUE) {
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after
869 OVERLAPPED* overlapped; 870 OVERLAPPED* overlapped;
870 intptr_t millis = handler->GetTimeout(); 871 intptr_t millis = handler->GetTimeout();
871 BOOL ok = GetQueuedCompletionStatus(handler->completion_port(), 872 BOOL ok = GetQueuedCompletionStatus(handler->completion_port(),
872 &bytes, 873 &bytes,
873 &key, 874 &key,
874 &overlapped, 875 &overlapped,
875 millis); 876 millis);
876 if (!ok && overlapped == NULL) { 877 if (!ok && overlapped == NULL) {
877 if (GetLastError() == ERROR_ABANDONED_WAIT_0) { 878 if (GetLastError() == ERROR_ABANDONED_WAIT_0) {
878 // The completion port should never be closed. 879 // The completion port should never be closed.
879 printf("Completion port closed\n"); 880 Log::Print("Completion port closed\n");
880 UNREACHABLE(); 881 UNREACHABLE();
881 } else { 882 } else {
882 // Timeout is signalled by false result and NULL in overlapped. 883 // Timeout is signalled by false result and NULL in overlapped.
883 handler->HandleTimeout(); 884 handler->HandleTimeout();
884 } 885 }
885 } else if (!ok) { 886 } else if (!ok) {
886 // Treat ERROR_CONNECTION_ABORTED as connection closed. 887 // Treat ERROR_CONNECTION_ABORTED as connection closed.
887 // The error ERROR_OPERATION_ABORTED is set for pending 888 // The error ERROR_OPERATION_ABORTED is set for pending
888 // accept requests for a listen socket which is closed. 889 // accept requests for a listen socket which is closed.
889 // ERROR_NETNAME_DELETED occurs when the client closes 890 // ERROR_NETNAME_DELETED occurs when the client closes
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
921 // Initialize Winsock32 922 // Initialize Winsock32
922 if (!Socket::Initialize()) { 923 if (!Socket::Initialize()) {
923 FATAL("Failed to initialized Windows sockets"); 924 FATAL("Failed to initialized Windows sockets");
924 } 925 }
925 } 926 }
926 927
927 928
928 void EventHandlerImplementation::Shutdown() { 929 void EventHandlerImplementation::Shutdown() {
929 SendData(kShutdownId, 0, 0); 930 SendData(kShutdownId, 0, 0);
930 } 931 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698