| 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 DescriptorInfo : public DescriptorInfoBase { | 25 class InterruptMessage { |
| 26 public: | 26 public: |
| 27 explicit DescriptorInfo(intptr_t fd) : DescriptorInfoBase(fd) { } | 27 intptr_t id; |
| 28 Dart_Port dart_port; |
| 29 int64_t data; |
| 30 }; |
| 28 | 31 |
| 29 virtual ~DescriptorInfo() { } | 32 |
| 33 class SocketData { |
| 34 public: |
| 35 explicit SocketData(intptr_t fd) |
| 36 : fd_(fd), port_(0), mask_(0), tokens_(16) { |
| 37 ASSERT(fd_ != -1); |
| 38 } |
| 30 | 39 |
| 31 intptr_t GetPollEvents(); | 40 intptr_t GetPollEvents(); |
| 32 | 41 |
| 33 virtual void Close() { | 42 void Close() { |
| 43 port_ = 0; |
| 44 mask_ = 0; |
| 34 VOID_TEMP_FAILURE_RETRY(close(fd_)); | 45 VOID_TEMP_FAILURE_RETRY(close(fd_)); |
| 35 fd_ = -1; | 46 fd_ = -1; |
| 36 } | 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_; |
| 37 }; | 79 }; |
| 38 | 80 |
| 39 | 81 |
| 40 class DescriptorInfoSingle | |
| 41 : public DescriptorInfoSingleMixin<DescriptorInfo> { | |
| 42 public: | |
| 43 explicit DescriptorInfoSingle(intptr_t fd) | |
| 44 : DescriptorInfoSingleMixin(fd) {} | |
| 45 virtual ~DescriptorInfoSingle() {} | |
| 46 }; | |
| 47 | |
| 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 | |
| 58 class EventHandlerImplementation { | 82 class EventHandlerImplementation { |
| 59 public: | 83 public: |
| 60 EventHandlerImplementation(); | 84 EventHandlerImplementation(); |
| 61 ~EventHandlerImplementation(); | 85 ~EventHandlerImplementation(); |
| 62 | 86 |
| 63 // Gets the socket data structure for a given file | 87 // Gets the socket data structure for a given file |
| 64 // descriptor. Creates a new one if one is not found. | 88 // descriptor. Creates a new one if one is not found. |
| 65 DescriptorInfo* GetDescriptorInfo(intptr_t fd, bool is_listening); | 89 SocketData* GetSocketData(intptr_t fd); |
| 66 void SendData(intptr_t id, Dart_Port dart_port, int64_t data); | 90 void SendData(intptr_t id, Dart_Port dart_port, intptr_t data); |
| 67 void Start(EventHandler* handler); | 91 void Start(EventHandler* handler); |
| 68 void Shutdown(); | 92 void Shutdown(); |
| 69 | 93 |
| 70 private: | 94 private: |
| 71 int64_t GetTimeout(); | 95 int64_t GetTimeout(); |
| 72 void HandleEvents(struct epoll_event* events, int size); | 96 void HandleEvents(struct epoll_event* events, int size); |
| 73 void HandleTimeout(); | 97 void HandleTimeout(); |
| 74 static void Poll(uword args); | 98 static void Poll(uword args); |
| 75 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); | 99 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); |
| 76 void HandleInterruptFd(); | 100 void HandleInterruptFd(); |
| 77 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); | 101 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); |
| 78 intptr_t GetPollEvents(intptr_t events, DescriptorInfo* sd); | 102 intptr_t GetPollEvents(intptr_t events, SocketData* sd); |
| 79 static void* GetHashmapKeyFromFd(intptr_t fd); | 103 static void* GetHashmapKeyFromFd(intptr_t fd); |
| 80 static uint32_t GetHashmapHashFromFd(intptr_t fd); | 104 static uint32_t GetHashmapHashFromFd(intptr_t fd); |
| 81 | 105 |
| 82 HashMap socket_map_; | 106 HashMap socket_map_; |
| 83 TimeoutQueue timeout_queue_; | 107 TimeoutQueue timeout_queue_; |
| 84 bool shutdown_; | 108 bool shutdown_; |
| 85 int interrupt_fds_[2]; | 109 int interrupt_fds_[2]; |
| 86 int epoll_fd_; | 110 int epoll_fd_; |
| 87 }; | 111 }; |
| 88 | 112 |
| 89 } // namespace bin | 113 } // namespace bin |
| 90 } // namespace dart | 114 } // namespace dart |
| 91 | 115 |
| 92 #endif // BIN_EVENTHANDLER_ANDROID_H_ | 116 #endif // BIN_EVENTHANDLER_ANDROID_H_ |
| OLD | NEW |