| 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 #ifndef BIN_EVENTHANDLER_ANDROID_H_ | 5 #ifndef BIN_EVENTHANDLER_ANDROID_H_ |
| 6 #define BIN_EVENTHANDLER_ANDROID_H_ | 6 #define BIN_EVENTHANDLER_ANDROID_H_ |
| 7 | 7 |
| 8 #if !defined(BIN_EVENTHANDLER_H_) | 8 #if !defined(BIN_EVENTHANDLER_H_) |
| 9 #error Do not include eventhandler_android.h directly; | 9 #error Do not include eventhandler_android.h directly; |
| 10 #error use eventhandler.h instead. | 10 #error use eventhandler.h instead. |
| 11 #endif | 11 #endif |
| 12 | 12 |
| 13 #include <errno.h> | 13 #include <errno.h> |
| 14 #include <sys/epoll.h> | 14 #include <sys/epoll.h> |
| 15 #include <sys/socket.h> | 15 #include <sys/socket.h> |
| 16 #include <unistd.h> | 16 #include <unistd.h> |
| 17 | 17 |
| 18 #include "platform/hashmap.h" | 18 #include "platform/hashmap.h" |
| 19 #include "platform/signal_blocker.h" | 19 #include "platform/signal_blocker.h" |
| 20 | 20 |
| 21 | 21 |
| 22 namespace dart { | 22 namespace dart { |
| 23 namespace bin { | 23 namespace bin { |
| 24 | 24 |
| 25 class InterruptMessage { | 25 class DescriptorInfo : public DescriptorInfoBase { |
| 26 public: | 26 public: |
| 27 intptr_t id; | 27 explicit DescriptorInfo(intptr_t fd) : DescriptorInfoBase(fd) { } |
| 28 Dart_Port dart_port; | 28 |
| 29 int64_t data; | 29 virtual ~DescriptorInfo() { } |
| 30 |
| 31 intptr_t GetPollEvents(); |
| 32 |
| 33 virtual void Close() { |
| 34 VOID_TEMP_FAILURE_RETRY(close(fd_)); |
| 35 fd_ = -1; |
| 36 } |
| 30 }; | 37 }; |
| 31 | 38 |
| 32 | 39 |
| 33 class SocketData { | 40 class DescriptorInfoSingle |
| 41 : public DescriptorInfoSingleMixin<DescriptorInfo> { |
| 34 public: | 42 public: |
| 35 explicit SocketData(intptr_t fd) | 43 explicit DescriptorInfoSingle(intptr_t fd) |
| 36 : fd_(fd), port_(0), mask_(0), tokens_(16) { | 44 : DescriptorInfoSingleMixin(fd) {} |
| 37 ASSERT(fd_ != -1); | 45 virtual ~DescriptorInfoSingle() {} |
| 38 } | |
| 39 | |
| 40 intptr_t GetPollEvents(); | |
| 41 | |
| 42 void Close() { | |
| 43 port_ = 0; | |
| 44 mask_ = 0; | |
| 45 VOID_TEMP_FAILURE_RETRY(close(fd_)); | |
| 46 fd_ = -1; | |
| 47 } | |
| 48 | |
| 49 void SetPortAndMask(Dart_Port port, intptr_t mask) { | |
| 50 ASSERT(fd_ != -1); | |
| 51 port_ = port; | |
| 52 mask_ = mask; | |
| 53 } | |
| 54 | |
| 55 intptr_t fd() { return fd_; } | |
| 56 Dart_Port port() { return port_; } | |
| 57 | |
| 58 bool IsListeningSocket() { return (mask_ & (1 << kListeningSocket)) != 0; } | |
| 59 | |
| 60 // Returns true if the last token was taken. | |
| 61 bool TakeToken() { | |
| 62 ASSERT(tokens_ > 0); | |
| 63 tokens_--; | |
| 64 return tokens_ == 0; | |
| 65 } | |
| 66 | |
| 67 // Returns true if the tokens was 0 before adding. | |
| 68 bool ReturnToken() { | |
| 69 ASSERT(tokens_ >= 0); | |
| 70 tokens_++; | |
| 71 return tokens_ == 1; | |
| 72 } | |
| 73 | |
| 74 private: | |
| 75 intptr_t fd_; | |
| 76 Dart_Port port_; | |
| 77 intptr_t mask_; | |
| 78 int tokens_; | |
| 79 }; | 46 }; |
| 80 | 47 |
| 81 | 48 |
| 49 class DescriptorInfoMultiple |
| 50 : public DescriptorInfoMultipleMixin<DescriptorInfo> { |
| 51 public: |
| 52 explicit DescriptorInfoMultiple(intptr_t fd) |
| 53 : DescriptorInfoMultipleMixin(fd) {} |
| 54 virtual ~DescriptorInfoMultiple() {} |
| 55 }; |
| 56 |
| 57 |
| 82 class EventHandlerImplementation { | 58 class EventHandlerImplementation { |
| 83 public: | 59 public: |
| 84 EventHandlerImplementation(); | 60 EventHandlerImplementation(); |
| 85 ~EventHandlerImplementation(); | 61 ~EventHandlerImplementation(); |
| 86 | 62 |
| 87 // Gets the socket data structure for a given file | 63 // Gets the socket data structure for a given file |
| 88 // descriptor. Creates a new one if one is not found. | 64 // descriptor. Creates a new one if one is not found. |
| 89 SocketData* GetSocketData(intptr_t fd); | 65 DescriptorInfo* GetDescriptorInfo(intptr_t fd, bool is_listening); |
| 90 void SendData(intptr_t id, Dart_Port dart_port, intptr_t data); | 66 void SendData(intptr_t id, Dart_Port dart_port, int64_t data); |
| 91 void Start(EventHandler* handler); | 67 void Start(EventHandler* handler); |
| 92 void Shutdown(); | 68 void Shutdown(); |
| 93 | 69 |
| 94 private: | 70 private: |
| 95 int64_t GetTimeout(); | 71 int64_t GetTimeout(); |
| 96 void HandleEvents(struct epoll_event* events, int size); | 72 void HandleEvents(struct epoll_event* events, int size); |
| 97 void HandleTimeout(); | 73 void HandleTimeout(); |
| 98 static void Poll(uword args); | 74 static void Poll(uword args); |
| 99 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); | 75 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); |
| 100 void HandleInterruptFd(); | 76 void HandleInterruptFd(); |
| 101 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); | 77 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); |
| 102 intptr_t GetPollEvents(intptr_t events, SocketData* sd); | 78 intptr_t GetPollEvents(intptr_t events, DescriptorInfo* sd); |
| 103 static void* GetHashmapKeyFromFd(intptr_t fd); | 79 static void* GetHashmapKeyFromFd(intptr_t fd); |
| 104 static uint32_t GetHashmapHashFromFd(intptr_t fd); | 80 static uint32_t GetHashmapHashFromFd(intptr_t fd); |
| 105 | 81 |
| 106 HashMap socket_map_; | 82 HashMap socket_map_; |
| 107 TimeoutQueue timeout_queue_; | 83 TimeoutQueue timeout_queue_; |
| 108 bool shutdown_; | 84 bool shutdown_; |
| 109 int interrupt_fds_[2]; | 85 int interrupt_fds_[2]; |
| 110 int epoll_fd_; | 86 int epoll_fd_; |
| 111 }; | 87 }; |
| 112 | 88 |
| 113 } // namespace bin | 89 } // namespace bin |
| 114 } // namespace dart | 90 } // namespace dart |
| 115 | 91 |
| 116 #endif // BIN_EVENTHANDLER_ANDROID_H_ | 92 #endif // BIN_EVENTHANDLER_ANDROID_H_ |
| OLD | NEW |