OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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_MACOS_H_ | 5 #ifndef BIN_EVENTHANDLER_MACOS_H_ |
6 #define BIN_EVENTHANDLER_MACOS_H_ | 6 #define BIN_EVENTHANDLER_MACOS_H_ |
7 | 7 |
| 8 #include <unistd.h> |
| 9 #include <sys/socket.h> |
| 10 |
8 | 11 |
9 class InterruptMessage { | 12 class InterruptMessage { |
10 public: | 13 public: |
11 intptr_t id; | 14 intptr_t id; |
12 Dart_Port dart_port; | 15 Dart_Port dart_port; |
13 int64_t data; | 16 int64_t data; |
14 }; | 17 }; |
15 | 18 |
16 | 19 |
| 20 enum PortDataFlags { |
| 21 kClosedRead = 0, |
| 22 kClosedWrite = 1, |
| 23 }; |
| 24 |
| 25 |
17 class SocketData { | 26 class SocketData { |
18 public: | 27 public: |
19 void FillPollEvents(struct pollfd* pollfds); | 28 intptr_t GetPollEvents(); |
20 bool IsListeningSocket() { return (_mask & (1 << kListeningSocket)) != 0; } | |
21 | 29 |
22 Dart_Port port() { return _port; } | 30 void Unregister() { |
23 void set_port(Dart_Port port) { _port = port; } | 31 port_ = 0; |
24 intptr_t mask() { return _mask; } | 32 mask_ = 0; |
25 void set_mask(intptr_t mask) { _mask = mask; } | 33 } |
| 34 |
| 35 void ShutdownRead() { |
| 36 shutdown(fd_, SHUT_RD); |
| 37 MarkClosedRead(); |
| 38 } |
| 39 |
| 40 void ShutdownWrite() { |
| 41 shutdown(fd_, SHUT_WR); |
| 42 MarkClosedWrite(); |
| 43 } |
| 44 |
| 45 void Close() { |
| 46 Unregister(); |
| 47 flags_ = 0; |
| 48 close(fd_); |
| 49 fd_ = 0; |
| 50 } |
| 51 |
| 52 bool IsListeningSocket() { return (mask_ & (1 << kListeningSocket)) != 0; } |
| 53 bool IsClosedRead() { return (flags_ & (1 << kClosedRead)) != 0; } |
| 54 bool IsClosedWrite() { return (flags_ & (1 << kClosedWrite)) != 0; } |
| 55 |
| 56 void MarkClosedRead() { flags_ |= (1 << kClosedRead); } |
| 57 void MarkClosedWrite() { flags_ |= (1 << kClosedWrite); } |
| 58 |
| 59 bool HasPollEvents() { return mask_ != 0; } |
| 60 |
| 61 void SetPortAndMask(Dart_Port port, intptr_t mask) { |
| 62 port_ = port; |
| 63 mask_ = mask; |
| 64 } |
| 65 |
| 66 intptr_t fd() { return fd_; } |
| 67 void set_fd(intptr_t fd) { fd_ = fd; } |
| 68 Dart_Port port() { return port_; } |
| 69 intptr_t mask() { return mask_; } |
26 | 70 |
27 private: | 71 private: |
28 Dart_Port _port; | 72 intptr_t fd_; |
29 intptr_t _mask; | 73 Dart_Port port_; |
| 74 intptr_t mask_; |
| 75 intptr_t flags_; |
30 }; | 76 }; |
31 | 77 |
32 | 78 |
33 class EventHandlerImplementation { | 79 class EventHandlerImplementation { |
34 public: | 80 public: |
35 EventHandlerImplementation(); | 81 EventHandlerImplementation(); |
36 ~EventHandlerImplementation(); | 82 ~EventHandlerImplementation(); |
37 | 83 |
38 SocketData* GetSocketData(intptr_t fd); | 84 SocketData* GetSocketData(intptr_t fd); |
39 void SendData(intptr_t id, Dart_Port dart_port, intptr_t data); | 85 void SendData(intptr_t id, Dart_Port dart_port, intptr_t data); |
40 void StartEventHandler(); | 86 void StartEventHandler(); |
41 | 87 |
42 private: | 88 private: |
43 intptr_t GetTimeout(); | 89 intptr_t GetTimeout(); |
44 bool GetInterruptMessage(InterruptMessage* msg); | 90 bool GetInterruptMessage(InterruptMessage* msg); |
45 struct pollfd* GetPollFds(intptr_t* size); | 91 struct pollfd* GetPollFds(intptr_t* size); |
46 void RegisterFdWakeup(intptr_t id, Dart_Port dart_port, intptr_t data); | |
47 void UnregisterFdWakeup(intptr_t id); | |
48 void CloseFd(intptr_t id); | |
49 void UnregisterFd(intptr_t id); | |
50 void HandleEvents(struct pollfd* pollfds, int pollfds_size, int result_size); | 92 void HandleEvents(struct pollfd* pollfds, int pollfds_size, int result_size); |
51 void HandleTimeout(); | 93 void HandleTimeout(); |
52 static void* Poll(void* args); | 94 static void* Poll(void* args); |
53 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); | 95 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); |
54 void HandleInterruptFd(); | 96 void HandleInterruptFd(); |
55 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); | 97 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); |
56 intptr_t GetPollEvents(struct pollfd* pollfd); | 98 intptr_t GetPollEvents(struct pollfd* pollfd); |
57 | 99 |
58 SocketData* socket_map_; | 100 SocketData* socket_map_; |
59 intptr_t socket_map_entries_; | |
60 intptr_t socket_map_size_; | 101 intptr_t socket_map_size_; |
61 int64_t timeout_; // Time for next timeout. | 102 int64_t timeout_; // Time for next timeout. |
62 Dart_Port timeout_port_; | 103 Dart_Port timeout_port_; |
63 int interrupt_fds_[2]; | 104 int interrupt_fds_[2]; |
64 }; | 105 }; |
65 | 106 |
66 | 107 |
67 #endif // BIN_EVENTHANDLER_MACOS_H_ | 108 #endif // BIN_EVENTHANDLER_MACOS_H_ |
OLD | NEW |