| 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 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 BOOL ok = ReadFile(handle_, | 251 BOOL ok = ReadFile(handle_, |
| 252 buffer->GetBufferStart(), | 252 buffer->GetBufferStart(), |
| 253 buffer->GetBufferSize(), | 253 buffer->GetBufferSize(), |
| 254 NULL, | 254 NULL, |
| 255 buffer->GetCleanOverlapped()); | 255 buffer->GetCleanOverlapped()); |
| 256 if (ok || GetLastError() == ERROR_IO_PENDING) { | 256 if (ok || GetLastError() == ERROR_IO_PENDING) { |
| 257 // Completing asynchronously. | 257 // Completing asynchronously. |
| 258 pending_read_ = buffer; | 258 pending_read_ = buffer; |
| 259 return true; | 259 return true; |
| 260 } | 260 } |
| 261 |
| 262 if (GetLastError() != ERROR_BROKEN_PIPE) { |
| 263 Log::PrintErr("ReadFile failed: %d\n", GetLastError()); |
| 264 } |
| 265 event_handler_->HandleClosed(this); |
| 261 IOBuffer::DisposeBuffer(buffer); | 266 IOBuffer::DisposeBuffer(buffer); |
| 262 | |
| 263 if (GetLastError() == ERROR_BROKEN_PIPE) { | |
| 264 event_handler_->HandleClosed(this); | |
| 265 } else { | |
| 266 event_handler_->HandleError(this); | |
| 267 } | |
| 268 return false; | 267 return false; |
| 269 } else { | 268 } else { |
| 270 // Completing asynchronously through thread. | 269 // Completing asynchronously through thread. |
| 271 pending_read_ = buffer; | 270 pending_read_ = buffer; |
| 272 uint32_t tid; | 271 uint32_t tid; |
| 273 uintptr_t thread_handle = | 272 uintptr_t thread_handle = |
| 274 _beginthreadex(NULL, 32 * 1024, ReadFileThread, this, 0, &tid); | 273 _beginthreadex(NULL, 32 * 1024, ReadFileThread, this, 0, &tid); |
| 275 if (thread_handle == -1) { | 274 if (thread_handle == -1) { |
| 276 FATAL("Failed to start read file thread"); | 275 FATAL("Failed to start read file thread"); |
| 277 } | 276 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 291 BOOL ok = WriteFile(handle_, | 290 BOOL ok = WriteFile(handle_, |
| 292 buffer->GetBufferStart(), | 291 buffer->GetBufferStart(), |
| 293 buffer->GetBufferSize(), | 292 buffer->GetBufferSize(), |
| 294 NULL, | 293 NULL, |
| 295 buffer->GetCleanOverlapped()); | 294 buffer->GetCleanOverlapped()); |
| 296 if (ok || GetLastError() == ERROR_IO_PENDING) { | 295 if (ok || GetLastError() == ERROR_IO_PENDING) { |
| 297 // Completing asynchronously. | 296 // Completing asynchronously. |
| 298 pending_write_ = buffer; | 297 pending_write_ = buffer; |
| 299 return true; | 298 return true; |
| 300 } | 299 } |
| 300 |
| 301 if (GetLastError() != ERROR_BROKEN_PIPE) { |
| 302 Log::PrintErr("WriteFile failed: %d\n", GetLastError()); |
| 303 } |
| 304 event_handler_->HandleClosed(this); |
| 301 IOBuffer::DisposeBuffer(buffer); | 305 IOBuffer::DisposeBuffer(buffer); |
| 302 | |
| 303 if (GetLastError() == ERROR_BROKEN_PIPE) { | |
| 304 event_handler_->HandleClosed(this); | |
| 305 } else { | |
| 306 event_handler_->HandleError(this); | |
| 307 } | |
| 308 return false; | 306 return false; |
| 309 } | 307 } |
| 310 | 308 |
| 311 | 309 |
| 312 void FileHandle::EnsureInitialized(EventHandlerImplementation* event_handler) { | 310 void FileHandle::EnsureInitialized(EventHandlerImplementation* event_handler) { |
| 313 ScopedLock lock(this); | 311 ScopedLock lock(this); |
| 314 event_handler_ = event_handler; | 312 event_handler_ = event_handler; |
| 315 if (SupportsOverlappedIO() && completion_port_ == INVALID_HANDLE_VALUE) { | 313 if (SupportsOverlappedIO() && completion_port_ == INVALID_HANDLE_VALUE) { |
| 316 CreateCompletionPort(event_handler_->completion_port()); | 314 CreateCompletionPort(event_handler_->completion_port()); |
| 317 } | 315 } |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 553 buffer->GetWASBUF(), | 551 buffer->GetWASBUF(), |
| 554 1, | 552 1, |
| 555 NULL, | 553 NULL, |
| 556 &flags, | 554 &flags, |
| 557 buffer->GetCleanOverlapped(), | 555 buffer->GetCleanOverlapped(), |
| 558 NULL); | 556 NULL); |
| 559 if (rc == NO_ERROR || WSAGetLastError() == WSA_IO_PENDING) { | 557 if (rc == NO_ERROR || WSAGetLastError() == WSA_IO_PENDING) { |
| 560 pending_read_ = buffer; | 558 pending_read_ = buffer; |
| 561 return true; | 559 return true; |
| 562 } | 560 } |
| 561 |
| 562 if (WSAGetLastError() != WSAECONNRESET) { |
| 563 Log::PrintErr("WSARecv failed: %d\n", WSAGetLastError()); |
| 564 } |
| 565 event_handler_->HandleClosed(this); |
| 563 IOBuffer::DisposeBuffer(buffer); | 566 IOBuffer::DisposeBuffer(buffer); |
| 564 pending_read_ = NULL; | |
| 565 | |
| 566 if (WSAGetLastError() == WSAECONNRESET) { | |
| 567 event_handler_->HandleClosed(this); | |
| 568 } else { | |
| 569 event_handler_->HandleError(this); | |
| 570 } | |
| 571 return false; | 567 return false; |
| 572 } | 568 } |
| 573 | 569 |
| 574 | 570 |
| 575 bool ClientSocket::IssueWrite() { | 571 bool ClientSocket::IssueWrite() { |
| 576 ScopedLock lock(this); | 572 ScopedLock lock(this); |
| 577 ASSERT(completion_port_ != INVALID_HANDLE_VALUE); | 573 ASSERT(completion_port_ != INVALID_HANDLE_VALUE); |
| 578 ASSERT(pending_write_ != NULL); | 574 ASSERT(pending_write_ != NULL); |
| 579 ASSERT(pending_write_->operation() == IOBuffer::kWrite); | 575 ASSERT(pending_write_->operation() == IOBuffer::kWrite); |
| 580 | 576 |
| 581 int rc = WSASend(socket(), | 577 int rc = WSASend(socket(), |
| 582 pending_write_->GetWASBUF(), | 578 pending_write_->GetWASBUF(), |
| 583 1, | 579 1, |
| 584 NULL, | 580 NULL, |
| 585 0, | 581 0, |
| 586 pending_write_->GetCleanOverlapped(), | 582 pending_write_->GetCleanOverlapped(), |
| 587 NULL); | 583 NULL); |
| 588 if (rc == NO_ERROR || WSAGetLastError() == WSA_IO_PENDING) { | 584 if (rc == NO_ERROR || WSAGetLastError() == WSA_IO_PENDING) { |
| 589 return true; | 585 return true; |
| 590 } | 586 } |
| 587 |
| 588 Log::PrintErr("WSASend failed: %d\n", WSAGetLastError()); |
| 591 IOBuffer::DisposeBuffer(pending_write_); | 589 IOBuffer::DisposeBuffer(pending_write_); |
| 592 pending_write_ = NULL; | 590 pending_write_ = NULL; |
| 593 | |
| 594 if (WSAGetLastError() == WSAECONNRESET) { | |
| 595 event_handler_->HandleClosed(this); | |
| 596 } else { | |
| 597 event_handler_->HandleError(this); | |
| 598 } | |
| 599 return false; | 591 return false; |
| 600 } | 592 } |
| 601 | 593 |
| 602 | 594 |
| 603 void ClientSocket::EnsureInitialized( | 595 void ClientSocket::EnsureInitialized( |
| 604 EventHandlerImplementation* event_handler) { | 596 EventHandlerImplementation* event_handler) { |
| 605 ScopedLock lock(this); | 597 ScopedLock lock(this); |
| 606 if (completion_port_ == INVALID_HANDLE_VALUE) { | 598 if (completion_port_ == INVALID_HANDLE_VALUE) { |
| 607 ASSERT(event_handler_ == NULL); | 599 ASSERT(event_handler_ == NULL); |
| 608 event_handler_ = event_handler; | 600 event_handler_ = event_handler; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 658 if (listen_socket->IsClosed()) { | 650 if (listen_socket->IsClosed()) { |
| 659 delete_handle = true; | 651 delete_handle = true; |
| 660 } | 652 } |
| 661 } | 653 } |
| 662 } else { | 654 } else { |
| 663 handle->SetPortAndMask(msg->dart_port, msg->data); | 655 handle->SetPortAndMask(msg->dart_port, msg->data); |
| 664 handle->EnsureInitialized(this); | 656 handle->EnsureInitialized(this); |
| 665 | 657 |
| 666 Handle::ScopedLock lock(handle); | 658 Handle::ScopedLock lock(handle); |
| 667 | 659 |
| 668 if (!handle->IsError()) { | 660 // If the data available callback has been requested and data are |
| 669 // If in events (data available events) have been requested, and data | 661 // available post it immediately. Otherwise make sure that a pending |
| 670 // is available, post an in event immediately. Otherwise make sure | 662 // read is issued unless the socket is already closed for read. |
| 671 // that a pending read is issued, unless the socket is already closed | 663 if ((msg->data & (1 << kInEvent)) != 0) { |
| 672 // for read. | 664 if (handle->Available() > 0) { |
| 673 if ((msg->data & (1 << kInEvent)) != 0) { | 665 int event_mask = (1 << kInEvent); |
| 674 if (handle->Available() > 0) { | 666 DartUtils::PostInt32(handle->port(), event_mask); |
| 675 int event_mask = (1 << kInEvent); | 667 } else if (!handle->HasPendingRead() && |
| 676 handle->set_mask(handle->mask() & ~event_mask); | 668 !handle->IsClosedRead()) { |
| 677 DartUtils::PostInt32(handle->port(), event_mask); | 669 handle->IssueRead(); |
| 678 } else if (!handle->HasPendingRead() && | |
| 679 !handle->IsClosedRead()) { | |
| 680 handle->IssueRead(); | |
| 681 } | |
| 682 } | |
| 683 | |
| 684 // If out events (can write events) have been requested, and there | |
| 685 // are no pending writes, post an out event immediately. | |
| 686 if ((msg->data & (1 << kOutEvent)) != 0) { | |
| 687 if (!handle->HasPendingWrite()) { | |
| 688 int event_mask = (1 << kOutEvent); | |
| 689 handle->set_mask(handle->mask() & ~event_mask); | |
| 690 DartUtils::PostInt32(handle->port(), event_mask); | |
| 691 } | |
| 692 } | |
| 693 | |
| 694 if (handle->is_client_socket()) { | |
| 695 ClientSocket* client_socket = reinterpret_cast<ClientSocket*>(handle); | |
| 696 if ((msg->data & (1 << kShutdownReadCommand)) != 0) { | |
| 697 client_socket->Shutdown(SD_RECEIVE); | |
| 698 } | |
| 699 | |
| 700 if ((msg->data & (1 << kShutdownWriteCommand)) != 0) { | |
| 701 client_socket->Shutdown(SD_SEND); | |
| 702 } | |
| 703 } | 670 } |
| 704 } | 671 } |
| 705 | 672 |
| 673 // If can send callback had been requested and there is no pending |
| 674 // send post it immediately. |
| 675 if ((msg->data & (1 << kOutEvent)) != 0) { |
| 676 if (!handle->HasPendingWrite()) { |
| 677 int event_mask = (1 << kOutEvent); |
| 678 DartUtils::PostInt32(handle->port(), event_mask); |
| 679 } |
| 680 } |
| 681 |
| 682 if (handle->is_client_socket()) { |
| 683 ClientSocket* client_socket = reinterpret_cast<ClientSocket*>(handle); |
| 684 if ((msg->data & (1 << kShutdownReadCommand)) != 0) { |
| 685 client_socket->Shutdown(SD_RECEIVE); |
| 686 } |
| 687 |
| 688 if ((msg->data & (1 << kShutdownWriteCommand)) != 0) { |
| 689 client_socket->Shutdown(SD_SEND); |
| 690 } |
| 691 } |
| 692 |
| 706 if ((msg->data & (1 << kCloseCommand)) != 0) { | 693 if ((msg->data & (1 << kCloseCommand)) != 0) { |
| 707 handle->close(); | 694 handle->close(); |
| 708 if (handle->IsClosed()) { | 695 if (handle->IsClosed()) { |
| 709 delete_handle = true; | 696 delete_handle = true; |
| 710 } | 697 } |
| 711 } | 698 } |
| 712 } | 699 } |
| 713 if (delete_handle) { | 700 if (delete_handle) { |
| 714 delete handle; | 701 delete handle; |
| 715 } | 702 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 737 void EventHandlerImplementation::HandleClosed(Handle* handle) { | 724 void EventHandlerImplementation::HandleClosed(Handle* handle) { |
| 738 if (!handle->IsClosing()) { | 725 if (!handle->IsClosing()) { |
| 739 int event_mask = 1 << kCloseEvent; | 726 int event_mask = 1 << kCloseEvent; |
| 740 DartUtils::PostInt32(handle->port(), event_mask); | 727 DartUtils::PostInt32(handle->port(), event_mask); |
| 741 } | 728 } |
| 742 } | 729 } |
| 743 | 730 |
| 744 | 731 |
| 745 void EventHandlerImplementation::HandleError(Handle* handle) { | 732 void EventHandlerImplementation::HandleError(Handle* handle) { |
| 746 handle->set_last_error(WSAGetLastError()); | 733 handle->set_last_error(WSAGetLastError()); |
| 747 handle->MarkError(); | |
| 748 if (!handle->IsClosing()) { | 734 if (!handle->IsClosing()) { |
| 749 int event_mask = 1 << kErrorEvent; | 735 int event_mask = 1 << kErrorEvent; |
| 750 DartUtils::PostInt32(handle->port(), event_mask); | 736 DartUtils::PostInt32(handle->port(), event_mask); |
| 751 } | 737 } |
| 752 } | 738 } |
| 753 | 739 |
| 754 | 740 |
| 755 void EventHandlerImplementation::HandleRead(Handle* handle, | 741 void EventHandlerImplementation::HandleRead(Handle* handle, |
| 756 int bytes, | 742 int bytes, |
| 757 IOBuffer* buffer) { | 743 IOBuffer* buffer) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 778 } | 764 } |
| 779 } | 765 } |
| 780 | 766 |
| 781 | 767 |
| 782 void EventHandlerImplementation::HandleWrite(Handle* handle, | 768 void EventHandlerImplementation::HandleWrite(Handle* handle, |
| 783 int bytes, | 769 int bytes, |
| 784 IOBuffer* buffer) { | 770 IOBuffer* buffer) { |
| 785 handle->WriteComplete(buffer); | 771 handle->WriteComplete(buffer); |
| 786 | 772 |
| 787 if (bytes > 0) { | 773 if (bytes > 0) { |
| 788 if (!handle->IsError() && !handle->IsClosing()) { | 774 if (!handle->IsClosing()) { |
| 789 int event_mask = 1 << kOutEvent; | 775 int event_mask = 1 << kOutEvent; |
| 790 if ((handle->mask() & event_mask) != 0) { | 776 if ((handle->mask() & event_mask) != 0) { |
| 791 DartUtils::PostInt32(handle->port(), event_mask); | 777 DartUtils::PostInt32(handle->port(), event_mask); |
| 792 } | 778 } |
| 793 } | 779 } |
| 794 } else if (bytes == 0) { | 780 } else if (bytes == 0) { |
| 795 HandleClosed(handle); | 781 HandleClosed(handle); |
| 796 } else { | 782 } else { |
| 797 HandleError(handle); | 783 HandleError(handle); |
| 798 } | 784 } |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 936 // Initialize Winsock32 | 922 // Initialize Winsock32 |
| 937 if (!Socket::Initialize()) { | 923 if (!Socket::Initialize()) { |
| 938 FATAL("Failed to initialized Windows sockets"); | 924 FATAL("Failed to initialized Windows sockets"); |
| 939 } | 925 } |
| 940 } | 926 } |
| 941 | 927 |
| 942 | 928 |
| 943 void EventHandlerImplementation::Shutdown() { | 929 void EventHandlerImplementation::Shutdown() { |
| 944 SendData(kShutdownId, 0, 0); | 930 SendData(kShutdownId, 0, 0); |
| 945 } | 931 } |
| OLD | NEW |