| 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_LINUX_H_ | 5 #ifndef BIN_EVENTHANDLER_LINUX_H_ |
| 6 #define BIN_EVENTHANDLER_LINUX_H_ | 6 #define BIN_EVENTHANDLER_LINUX_H_ |
| 7 | 7 |
| 8 #if !defined(BIN_EVENTHANDLER_H_) | 8 #if !defined(BIN_EVENTHANDLER_H_) |
| 9 #error Do not include eventhandler_linux.h directly; use eventhandler.h instead. | 9 #error Do not include eventhandler_linux.h directly; use eventhandler.h instead. |
| 10 #endif | 10 #endif |
| 11 | 11 |
| 12 #include <unistd.h> | 12 #include <unistd.h> |
| 13 #include <sys/socket.h> | 13 #include <sys/socket.h> |
| 14 | 14 |
| 15 #include "bin/thread_pool.h" | 15 #include "bin/hashmap.h" |
| 16 | 16 |
| 17 class InterruptMessage { | 17 class InterruptMessage { |
| 18 public: | 18 public: |
| 19 intptr_t id; | 19 intptr_t id; |
| 20 Dart_Port dart_port; | 20 Dart_Port dart_port; |
| 21 int64_t data; | 21 int64_t data; |
| 22 }; | 22 }; |
| 23 | 23 |
| 24 | 24 |
| 25 enum PortDataFlags { | 25 enum PortDataFlags { |
| 26 kClosedRead = 0, | 26 kClosedRead = 0, |
| 27 kClosedWrite = 1, | 27 kClosedWrite = 1, |
| 28 }; | 28 }; |
| 29 | 29 |
| 30 | 30 |
| 31 class SocketData { | 31 class SocketData { |
| 32 public: | 32 public: |
| 33 explicit SocketData(intptr_t fd) : fd_(fd), port_(0), mask_(0), flags_(0) { |
| 34 ASSERT(fd_ != -1); |
| 35 } |
| 36 |
| 33 intptr_t GetPollEvents(); | 37 intptr_t GetPollEvents(); |
| 34 | 38 |
| 35 void Unregister() { | 39 void Unregister() { |
| 36 port_ = 0; | 40 port_ = 0; |
| 37 mask_ = 0; | 41 mask_ = 0; |
| 38 } | 42 } |
| 39 | 43 |
| 40 void ShutdownRead() { | 44 void ShutdownRead() { |
| 41 shutdown(fd_, SHUT_RD); | 45 shutdown(fd_, SHUT_RD); |
| 42 MarkClosedRead(); | 46 MarkClosedRead(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 64 | 68 |
| 65 bool HasPollEvents() { return mask_ != 0; } | 69 bool HasPollEvents() { return mask_ != 0; } |
| 66 | 70 |
| 67 void SetPortAndMask(Dart_Port port, intptr_t mask) { | 71 void SetPortAndMask(Dart_Port port, intptr_t mask) { |
| 68 ASSERT(fd_ != -1); | 72 ASSERT(fd_ != -1); |
| 69 port_ = port; | 73 port_ = port; |
| 70 mask_ = mask; | 74 mask_ = mask; |
| 71 } | 75 } |
| 72 | 76 |
| 73 intptr_t fd() { return fd_; } | 77 intptr_t fd() { return fd_; } |
| 74 void set_fd(intptr_t fd) { fd_ = fd; } | |
| 75 Dart_Port port() { return port_; } | 78 Dart_Port port() { return port_; } |
| 76 intptr_t mask() { return mask_; } | 79 intptr_t mask() { return mask_; } |
| 77 | 80 |
| 78 private: | 81 private: |
| 79 intptr_t fd_; | 82 intptr_t fd_; |
| 80 Dart_Port port_; | 83 Dart_Port port_; |
| 81 intptr_t mask_; | 84 intptr_t mask_; |
| 82 intptr_t flags_; | 85 intptr_t flags_; |
| 83 }; | 86 }; |
| 84 | 87 |
| 85 | 88 |
| 86 class EventHandlerImplementation { | 89 class EventHandlerImplementation { |
| 87 public: | 90 public: |
| 88 EventHandlerImplementation(); | 91 EventHandlerImplementation(); |
| 89 ~EventHandlerImplementation(); | 92 ~EventHandlerImplementation(); |
| 90 | 93 |
| 94 // Gets the socket data structure for a given file |
| 95 // descriptor. Creates a new one of one is not found. |
| 91 SocketData* GetSocketData(intptr_t fd); | 96 SocketData* GetSocketData(intptr_t fd); |
| 92 void SendData(intptr_t id, Dart_Port dart_port, intptr_t data); | 97 void SendData(intptr_t id, Dart_Port dart_port, intptr_t data); |
| 93 void StartEventHandler(); | 98 void StartEventHandler(); |
| 94 | 99 |
| 95 private: | 100 private: |
| 96 intptr_t GetTimeout(); | 101 intptr_t GetTimeout(); |
| 97 bool GetInterruptMessage(InterruptMessage* msg); | 102 bool GetInterruptMessage(InterruptMessage* msg); |
| 98 struct pollfd* GetPollFds(intptr_t* size); | 103 struct pollfd* GetPollFds(intptr_t* size); |
| 99 void HandleEvents(struct pollfd* pollfds, int pollfds_size, int result_size); | 104 void HandleEvents(struct pollfd* pollfds, int pollfds_size, int result_size); |
| 100 void HandleTimeout(); | 105 void HandleTimeout(); |
| 101 static void* Poll(void* args); | 106 static void* Poll(void* args); |
| 102 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); | 107 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); |
| 103 void HandleInterruptFd(); | 108 void HandleInterruptFd(); |
| 104 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); | 109 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); |
| 105 intptr_t GetPollEvents(struct pollfd* pollfd); | 110 intptr_t GetPollEvents(struct pollfd* pollfd); |
| 111 static void* GetHashmapKeyFromFd(intptr_t fd); |
| 112 static uint32_t GetHashmapHashFromFd(intptr_t fd); |
| 106 | 113 |
| 107 SocketData* socket_map_; | 114 HashMap socket_map_; |
| 108 intptr_t socket_map_size_; | |
| 109 int64_t timeout_; // Time for next timeout. | 115 int64_t timeout_; // Time for next timeout. |
| 110 Dart_Port timeout_port_; | 116 Dart_Port timeout_port_; |
| 111 int interrupt_fds_[2]; | 117 int interrupt_fds_[2]; |
| 112 }; | 118 }; |
| 113 | 119 |
| 114 | 120 |
| 115 #endif // BIN_EVENTHANDLER_LINUX_H_ | 121 #endif // BIN_EVENTHANDLER_LINUX_H_ |
| OLD | NEW |