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