| 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_MACOS_H_ | 5 #ifndef BIN_EVENTHANDLER_MACOS_H_ |
| 6 #define BIN_EVENTHANDLER_MACOS_H_ | 6 #define BIN_EVENTHANDLER_MACOS_H_ |
| 7 | 7 |
| 8 #if !defined(BIN_EVENTHANDLER_H_) | 8 #if !defined(BIN_EVENTHANDLER_H_) |
| 9 #error Do not include eventhandler_macos.h directly; use eventhandler.h instead. | 9 #error Do not include eventhandler_macos.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/hashmap.h" |
| 15 | 16 |
| 16 class InterruptMessage { | 17 class InterruptMessage { |
| 17 public: | 18 public: |
| 18 intptr_t id; | 19 intptr_t id; |
| 19 Dart_Port dart_port; | 20 Dart_Port dart_port; |
| 20 int64_t data; | 21 int64_t data; |
| 21 }; | 22 }; |
| 22 | 23 |
| 23 | 24 |
| 24 enum PortDataFlags { | 25 enum PortDataFlags { |
| 25 kClosedRead = 0, | 26 kClosedRead = 0, |
| 26 kClosedWrite = 1, | 27 kClosedWrite = 1, |
| 27 }; | 28 }; |
| 28 | 29 |
| 29 | 30 |
| 30 class SocketData { | 31 class SocketData { |
| 31 public: | 32 public: |
| 33 explicit SocketData(intptr_t fd) : fd_(fd), port_(0), mask_(0), flags_(0) { |
| 34 ASSERT(fd_ != -1); |
| 35 } |
| 36 |
| 32 intptr_t GetPollEvents(); | 37 intptr_t GetPollEvents(); |
| 33 | 38 |
| 34 void Unregister() { | 39 void Unregister() { |
| 35 port_ = 0; | 40 port_ = 0; |
| 36 mask_ = 0; | 41 mask_ = 0; |
| 37 } | 42 } |
| 38 | 43 |
| 39 void ShutdownRead() { | 44 void ShutdownRead() { |
| 40 shutdown(fd_, SHUT_RD); | 45 shutdown(fd_, SHUT_RD); |
| 41 MarkClosedRead(); | 46 MarkClosedRead(); |
| 42 } | 47 } |
| 43 | 48 |
| 44 void ShutdownWrite() { | 49 void ShutdownWrite() { |
| 45 shutdown(fd_, SHUT_WR); | 50 shutdown(fd_, SHUT_WR); |
| 46 MarkClosedWrite(); | 51 MarkClosedWrite(); |
| 47 } | 52 } |
| 48 | 53 |
| 49 void Close() { | 54 void Close() { |
| 50 Unregister(); | 55 Unregister(); |
| 51 flags_ = 0; | 56 flags_ = 0; |
| 52 close(fd_); | 57 close(fd_); |
| 53 fd_ = 0; | 58 fd_ = -1; |
| 54 } | 59 } |
| 55 | 60 |
| 56 bool IsListeningSocket() { return (mask_ & (1 << kListeningSocket)) != 0; } | 61 bool IsListeningSocket() { return (mask_ & (1 << kListeningSocket)) != 0; } |
| 57 bool IsPipe() { return (mask_ & (1 << kPipe)) != 0; } | 62 bool IsPipe() { return (mask_ & (1 << kPipe)) != 0; } |
| 58 bool IsClosedRead() { return (flags_ & (1 << kClosedRead)) != 0; } | 63 bool IsClosedRead() { return (flags_ & (1 << kClosedRead)) != 0; } |
| 59 bool IsClosedWrite() { return (flags_ & (1 << kClosedWrite)) != 0; } | 64 bool IsClosedWrite() { return (flags_ & (1 << kClosedWrite)) != 0; } |
| 60 | 65 |
| 61 void MarkClosedRead() { flags_ |= (1 << kClosedRead); } | 66 void MarkClosedRead() { flags_ |= (1 << kClosedRead); } |
| 62 void MarkClosedWrite() { flags_ |= (1 << kClosedWrite); } | 67 void MarkClosedWrite() { flags_ |= (1 << kClosedWrite); } |
| 63 | 68 |
| 64 bool HasPollEvents() { return mask_ != 0; } | 69 bool HasPollEvents() { return mask_ != 0; } |
| 65 | 70 |
| 66 void SetPortAndMask(Dart_Port port, intptr_t mask) { | 71 void SetPortAndMask(Dart_Port port, intptr_t mask) { |
| 72 ASSERT(fd_ != -1); |
| 67 port_ = port; | 73 port_ = port; |
| 68 mask_ = mask; | 74 mask_ = mask; |
| 69 } | 75 } |
| 70 | 76 |
| 71 intptr_t fd() { return fd_; } | 77 intptr_t fd() { return fd_; } |
| 72 void set_fd(intptr_t fd) { fd_ = fd; } | |
| 73 Dart_Port port() { return port_; } | 78 Dart_Port port() { return port_; } |
| 74 intptr_t mask() { return mask_; } | 79 intptr_t mask() { return mask_; } |
| 75 | 80 |
| 76 private: | 81 private: |
| 77 intptr_t fd_; | 82 intptr_t fd_; |
| 78 Dart_Port port_; | 83 Dart_Port port_; |
| 79 intptr_t mask_; | 84 intptr_t mask_; |
| 80 intptr_t flags_; | 85 intptr_t flags_; |
| 81 }; | 86 }; |
| 82 | 87 |
| 83 | 88 |
| 84 class EventHandlerImplementation { | 89 class EventHandlerImplementation { |
| 85 public: | 90 public: |
| 86 EventHandlerImplementation(); | 91 EventHandlerImplementation(); |
| 87 ~EventHandlerImplementation(); | 92 ~EventHandlerImplementation(); |
| 88 | 93 |
| 94 // Gets the socket data structure for a given file |
| 95 // descriptor. Creates a new one of one is not found. |
| 89 SocketData* GetSocketData(intptr_t fd); | 96 SocketData* GetSocketData(intptr_t fd); |
| 90 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); |
| 91 void StartEventHandler(); | 98 void StartEventHandler(); |
| 92 | 99 |
| 93 private: | 100 private: |
| 94 intptr_t GetTimeout(); | 101 intptr_t GetTimeout(); |
| 95 bool GetInterruptMessage(InterruptMessage* msg); | 102 bool GetInterruptMessage(InterruptMessage* msg); |
| 96 struct pollfd* GetPollFds(intptr_t* size); | 103 struct pollfd* GetPollFds(intptr_t* size); |
| 97 void HandleEvents(struct pollfd* pollfds, int pollfds_size, int result_size); | 104 void HandleEvents(struct pollfd* pollfds, int pollfds_size, int result_size); |
| 98 void HandleTimeout(); | 105 void HandleTimeout(); |
| 99 static void* Poll(void* args); | 106 static void* Poll(void* args); |
| 100 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); |
| 101 void HandleInterruptFd(); | 108 void HandleInterruptFd(); |
| 102 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); |
| 103 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); |
| 104 | 113 |
| 105 SocketData* socket_map_; | 114 HashMap socket_map_; |
| 106 intptr_t socket_map_size_; | |
| 107 int64_t timeout_; // Time for next timeout. | 115 int64_t timeout_; // Time for next timeout. |
| 108 Dart_Port timeout_port_; | 116 Dart_Port timeout_port_; |
| 109 int interrupt_fds_[2]; | 117 int interrupt_fds_[2]; |
| 110 }; | 118 }; |
| 111 | 119 |
| 112 | 120 |
| 113 #endif // BIN_EVENTHANDLER_MACOS_H_ | 121 #endif // BIN_EVENTHANDLER_MACOS_H_ |
| OLD | NEW |