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 DescriptorInfoAndroid : public DescriptorInfo { |
Søren Gjesse
2015/02/02 10:56:14
How about just calling the class DescriptorInfo he
kustermann
2015/02/03 10:45:35
Done. Also renamed the other two to Mixins.
| |
26 public: | 26 public: |
27 intptr_t id; | 27 explicit DescriptorInfoAndroid(intptr_t fd) : DescriptorInfo(fd) { } |
28 Dart_Port dart_port; | 28 |
29 int64_t data; | 29 virtual ~DescriptorInfoAndroid() { } |
30 | |
31 | |
32 intptr_t GetPollEvents(); | |
33 | |
34 virtual void Close() { | |
35 VOID_TEMP_FAILURE_RETRY(close(fd_)); | |
36 fd_ = -1; | |
37 } | |
38 }; | |
39 | |
40 class DescriptorInfoSingleLinux | |
Søren Gjesse
2015/02/02 10:56:14
DescriptorInfoSingleLinux -> DescriptorInfoSingle
kustermann
2015/02/03 10:45:35
Done.
| |
41 : public DescriptorInfoSingle<DescriptorInfoLinux> { | |
42 public: | |
43 explicit DescriptorInfoSingleLinux(intptr_t fd) : DescriptorInfoSingle(fd) { } | |
44 virtual ~DescriptorInfoSingleLinux() { } | |
30 }; | 45 }; |
31 | 46 |
32 | 47 |
33 class SocketData { | 48 class DescriptorInfoMultipleLinux |
Søren Gjesse
2015/02/02 10:56:14
Ditto.
kustermann
2015/02/03 10:45:35
Done.
| |
49 : public DescriptorInfoMultiple<DescriptorInfoLinux> { | |
34 public: | 50 public: |
35 explicit SocketData(intptr_t fd) | 51 explicit DescriptorInfoMultipleLinux(intptr_t fd) |
36 : fd_(fd), port_(0), mask_(0), tokens_(16) { | 52 : DescriptorInfoMultiple(fd) {} |
37 ASSERT(fd_ != -1); | 53 virtual ~DescriptorInfoMultipleLinux() {} |
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 }; | 54 }; |
80 | 55 |
81 | 56 |
82 class EventHandlerImplementation { | 57 class EventHandlerImplementation { |
83 public: | 58 public: |
84 EventHandlerImplementation(); | 59 EventHandlerImplementation(); |
85 ~EventHandlerImplementation(); | 60 ~EventHandlerImplementation(); |
86 | 61 |
87 // Gets the socket data structure for a given file | 62 // Gets the socket data structure for a given file |
88 // descriptor. Creates a new one if one is not found. | 63 // descriptor. Creates a new one if one is not found. |
89 SocketData* GetSocketData(intptr_t fd); | 64 DescriptorInfoAndroid* GetDescriptorInfoAndroid( |
90 void SendData(intptr_t id, Dart_Port dart_port, intptr_t data); | 65 intptr_t fd, bool is_listening); |
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, DescriptorInfoAndroid* 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 |