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> |
11 | 11 |
12 #include "bin/builtin.h" | 12 #include "bin/builtin.h" |
| 13 #include "bin/dartutils.h" |
13 #include "bin/socket.h" | 14 #include "bin/socket.h" |
14 | 15 |
15 | 16 |
16 static const int kInfinityTimeout = -1; | 17 static const int kInfinityTimeout = -1; |
17 | 18 |
18 | 19 |
19 int64_t GetCurrentTimeMilliseconds() { | 20 int64_t GetCurrentTimeMilliseconds() { |
20 static const int64_t kTimeEpoc = 116444736000000000LL; | 21 static const int64_t kTimeEpoc = 116444736000000000LL; |
21 | 22 |
22 // Although win32 uses 64-bit integers for representing timestamps, | 23 // Although win32 uses 64-bit integers for representing timestamps, |
(...skipping 611 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
634 handle->EnsureInitialized(this); | 635 handle->EnsureInitialized(this); |
635 | 636 |
636 Handle::ScopedLock lock(handle); | 637 Handle::ScopedLock lock(handle); |
637 | 638 |
638 // If the data available callback has been requested and data are | 639 // If the data available callback has been requested and data are |
639 // available post it immediately. Otherwise make sure that a pending | 640 // available post it immediately. Otherwise make sure that a pending |
640 // read is issued unless the socket is already closed for read. | 641 // read is issued unless the socket is already closed for read. |
641 if ((msg->data & (1 << kInEvent)) != 0) { | 642 if ((msg->data & (1 << kInEvent)) != 0) { |
642 if (handle->Available() > 0) { | 643 if (handle->Available() > 0) { |
643 int event_mask = (1 << kInEvent); | 644 int event_mask = (1 << kInEvent); |
644 Dart_PostIntArray(handle->port(), 1, &event_mask); | 645 DartUtils::PostInteger(port, event_mask); |
645 } else if (!handle->HasPendingRead() && | 646 } else if (!handle->HasPendingRead() && |
646 !handle->IsClosedRead()) { | 647 !handle->IsClosedRead()) { |
647 handle->IssueRead(); | 648 handle->IssueRead(); |
648 } | 649 } |
649 } | 650 } |
650 | 651 |
651 // If can send callback had been requested and there is no pending | 652 // If can send callback had been requested and there is no pending |
652 // send post it immediately. | 653 // send post it immediately. |
653 if ((msg->data & (1 << kOutEvent)) != 0) { | 654 if ((msg->data & (1 << kOutEvent)) != 0) { |
654 if (!handle->HasPendingWrite()) { | 655 if (!handle->HasPendingWrite()) { |
655 int event_mask = (1 << kOutEvent); | 656 int event_mask = (1 << kOutEvent); |
656 Dart_PostIntArray(handle->port(), 1, &event_mask); | 657 DartUtils::PostInteger(port, event_mask); |
657 } | 658 } |
658 } | 659 } |
659 | 660 |
660 if (handle->is_client_socket()) { | 661 if (handle->is_client_socket()) { |
661 ClientSocket* client_socket = reinterpret_cast<ClientSocket*>(handle); | 662 ClientSocket* client_socket = reinterpret_cast<ClientSocket*>(handle); |
662 if ((msg->data & (1 << kShutdownReadCommand)) != 0) { | 663 if ((msg->data & (1 << kShutdownReadCommand)) != 0) { |
663 client_socket->Shutdown(SD_RECEIVE); | 664 client_socket->Shutdown(SD_RECEIVE); |
664 } | 665 } |
665 | 666 |
666 if ((msg->data & (1 << kShutdownWriteCommand)) != 0) { | 667 if ((msg->data & (1 << kShutdownWriteCommand)) != 0) { |
(...skipping 15 matching lines...) Expand all Loading... |
682 } | 683 } |
683 | 684 |
684 | 685 |
685 void EventHandlerImplementation::HandleAccept(ListenSocket* listen_socket, | 686 void EventHandlerImplementation::HandleAccept(ListenSocket* listen_socket, |
686 IOBuffer* buffer) { | 687 IOBuffer* buffer) { |
687 listen_socket->AcceptComplete(buffer, completion_port_); | 688 listen_socket->AcceptComplete(buffer, completion_port_); |
688 | 689 |
689 if (!listen_socket->IsClosing()) { | 690 if (!listen_socket->IsClosing()) { |
690 int event_mask = 1 << kInEvent; | 691 int event_mask = 1 << kInEvent; |
691 if ((listen_socket->mask() & event_mask) != 0) { | 692 if ((listen_socket->mask() & event_mask) != 0) { |
692 Dart_PostIntArray(listen_socket->port(), 1, &event_mask); | 693 DartUtils::PostInteger(port, event_mask); |
693 } | 694 } |
694 } | 695 } |
695 | 696 |
696 if (listen_socket->IsClosed()) { | 697 if (listen_socket->IsClosed()) { |
697 delete listen_socket; | 698 delete listen_socket; |
698 } | 699 } |
699 } | 700 } |
700 | 701 |
701 | 702 |
702 void EventHandlerImplementation::HandleClosed(Handle* handle) { | 703 void EventHandlerImplementation::HandleClosed(Handle* handle) { |
703 if (!handle->IsClosing()) { | 704 if (!handle->IsClosing()) { |
704 int event_mask = 1 << kCloseEvent; | 705 int event_mask = 1 << kCloseEvent; |
705 Dart_PostIntArray(handle->port(), 1, &event_mask); | 706 DartUtils::PostInteger(port, event_mask); |
706 } | 707 } |
707 } | 708 } |
708 | 709 |
709 | 710 |
710 void EventHandlerImplementation::HandleRead(Handle* handle, | 711 void EventHandlerImplementation::HandleRead(Handle* handle, |
711 int bytes, | 712 int bytes, |
712 IOBuffer* buffer) { | 713 IOBuffer* buffer) { |
713 buffer->set_data_length(bytes); | 714 buffer->set_data_length(bytes); |
714 handle->ReadComplete(buffer); | 715 handle->ReadComplete(buffer); |
715 if (bytes > 0) { | 716 if (bytes > 0) { |
716 if (!handle->IsClosing()) { | 717 if (!handle->IsClosing()) { |
717 int event_mask = 1 << kInEvent; | 718 int event_mask = 1 << kInEvent; |
718 if ((handle->mask() & event_mask) != 0) { | 719 if ((handle->mask() & event_mask) != 0) { |
719 Dart_PostIntArray(handle->port(), 1, &event_mask); | 720 DartUtils::PostInteger(port, event_mask); |
720 } | 721 } |
721 } | 722 } |
722 } else { | 723 } else { |
723 ASSERT(bytes == 0); | 724 ASSERT(bytes == 0); |
724 handle->MarkClosedRead(); | 725 handle->MarkClosedRead(); |
725 HandleClosed(handle); | 726 HandleClosed(handle); |
726 } | 727 } |
727 | 728 |
728 if (handle->IsClosed()) { | 729 if (handle->IsClosed()) { |
729 delete handle; | 730 delete handle; |
730 } | 731 } |
731 } | 732 } |
732 | 733 |
733 | 734 |
734 void EventHandlerImplementation::HandleWrite(Handle* handle, | 735 void EventHandlerImplementation::HandleWrite(Handle* handle, |
735 int bytes, | 736 int bytes, |
736 IOBuffer* buffer) { | 737 IOBuffer* buffer) { |
737 handle->WriteComplete(buffer); | 738 handle->WriteComplete(buffer); |
738 | 739 |
739 if (bytes > 0) { | 740 if (bytes > 0) { |
740 if (!handle->IsClosing()) { | 741 if (!handle->IsClosing()) { |
741 int event_mask = 1 << kOutEvent; | 742 int event_mask = 1 << kOutEvent; |
742 if ((handle->mask() & event_mask) != 0) { | 743 if ((handle->mask() & event_mask) != 0) { |
743 Dart_PostIntArray(handle->port(), 1, &event_mask); | 744 DartUtils::PostInteger(port, event_mask); |
744 } | 745 } |
745 } | 746 } |
746 } else { | 747 } else { |
747 ASSERT(bytes == 0); | 748 ASSERT(bytes == 0); |
748 HandleClosed(handle); | 749 HandleClosed(handle); |
749 } | 750 } |
750 | 751 |
751 if (handle->IsClosed()) { | 752 if (handle->IsClosed()) { |
752 delete handle; | 753 delete handle; |
753 } | 754 } |
754 } | 755 } |
755 | 756 |
756 | 757 |
757 void EventHandlerImplementation::HandleTimeout() { | 758 void EventHandlerImplementation::HandleTimeout() { |
758 // TODO(sgjesse) check if there actually is a timeout. | 759 // TODO(sgjesse) check if there actually is a timeout. |
759 Dart_PostIntArray(timeout_port_, 0, NULL); | 760 DartUtils::PostNull(timeout_port_); |
760 timeout_ = kInfinityTimeout; | 761 timeout_ = kInfinityTimeout; |
761 timeout_port_ = 0; | 762 timeout_port_ = 0; |
762 } | 763 } |
763 | 764 |
764 | 765 |
765 void EventHandlerImplementation::HandleIOCompletion(DWORD bytes, | 766 void EventHandlerImplementation::HandleIOCompletion(DWORD bytes, |
766 ULONG_PTR key, | 767 ULONG_PTR key, |
767 OVERLAPPED* overlapped) { | 768 OVERLAPPED* overlapped) { |
768 IOBuffer* buffer = IOBuffer::GetFromOverlapped(overlapped); | 769 IOBuffer* buffer = IOBuffer::GetFromOverlapped(overlapped); |
769 switch (buffer->operation()) { | 770 switch (buffer->operation()) { |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
883 reinterpret_cast<uword>(this)); | 884 reinterpret_cast<uword>(this)); |
884 if (result != 0) { | 885 if (result != 0) { |
885 FATAL1("Failed to start event handler thread %d", result); | 886 FATAL1("Failed to start event handler thread %d", result); |
886 } | 887 } |
887 | 888 |
888 // Initialize Winsock32 | 889 // Initialize Winsock32 |
889 if (!Socket::Initialize()) { | 890 if (!Socket::Initialize()) { |
890 FATAL("Failed to initialized Windows sockets"); | 891 FATAL("Failed to initialized Windows sockets"); |
891 } | 892 } |
892 } | 893 } |
OLD | NEW |