| 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 |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | #ifndef RUNTIME_BIN_EVENTHANDLER_LINUX_H_ |
| 6 | #define RUNTIME_BIN_EVENTHANDLER_LINUX_H_ |
| 7 | |
| 8 | #if !defined(RUNTIME_BIN_EVENTHANDLER_H_) |
| 9 | #error Do not include eventhandler_linux.h directly; use eventhandler.h instead. |
| 10 | #endif |
| 11 | |
| 12 | #include <errno.h> |
| 13 | #include <sys/epoll.h> |
| 14 | #include <sys/socket.h> |
| 15 | #include <unistd.h> |
| 16 | |
| 17 | #include "platform/hashmap.h" |
| 18 | #include "platform/signal_blocker.h" |
| 19 | |
| 20 | namespace dart { |
| 21 | namespace bin { |
| 22 | |
| 23 | class DescriptorInfo : public DescriptorInfoBase { |
| 24 | public: |
| 25 | explicit DescriptorInfo(intptr_t fd) : DescriptorInfoBase(fd) {} |
| 26 | |
| 27 | virtual ~DescriptorInfo() {} |
| 28 | |
| 29 | intptr_t GetPollEvents(); |
| 30 | |
| 31 | virtual void Close() { |
| 32 | close(fd_); |
| 33 | fd_ = -1; |
| 34 | } |
| 35 | |
| 36 | private: |
| 37 | DISALLOW_COPY_AND_ASSIGN(DescriptorInfo); |
| 38 | }; |
| 39 | |
| 40 | class DescriptorInfoSingle : public DescriptorInfoSingleMixin<DescriptorInfo> { |
| 41 | public: |
| 42 | explicit DescriptorInfoSingle(intptr_t fd) |
| 43 | : DescriptorInfoSingleMixin(fd, false) {} |
| 44 | virtual ~DescriptorInfoSingle() {} |
| 45 | |
| 46 | private: |
| 47 | DISALLOW_COPY_AND_ASSIGN(DescriptorInfoSingle); |
| 48 | }; |
| 49 | |
| 50 | class DescriptorInfoMultiple |
| 51 | : public DescriptorInfoMultipleMixin<DescriptorInfo> { |
| 52 | public: |
| 53 | explicit DescriptorInfoMultiple(intptr_t fd) |
| 54 | : DescriptorInfoMultipleMixin(fd, false) {} |
| 55 | virtual ~DescriptorInfoMultiple() {} |
| 56 | |
| 57 | private: |
| 58 | DISALLOW_COPY_AND_ASSIGN(DescriptorInfoMultiple); |
| 59 | }; |
| 60 | |
| 61 | class EventHandlerImplementation { |
| 62 | public: |
| 63 | EventHandlerImplementation(); |
| 64 | ~EventHandlerImplementation(); |
| 65 | |
| 66 | void UpdateEpollInstance(intptr_t old_mask, DescriptorInfo* di); |
| 67 | |
| 68 | // Gets the socket data structure for a given file |
| 69 | // descriptor. Creates a new one if one is not found. |
| 70 | DescriptorInfo* GetDescriptorInfo(intptr_t fd, bool is_listening); |
| 71 | void SendData(intptr_t id, Dart_Port dart_port, int64_t data); |
| 72 | void Start(EventHandler* handler); |
| 73 | void Shutdown(); |
| 74 | |
| 75 | private: |
| 76 | void HandleEvents(struct epoll_event* events, int size); |
| 77 | static void Poll(uword args); |
| 78 | void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); |
| 79 | void HandleInterruptFd(); |
| 80 | void UpdateTimerFd(); |
| 81 | void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); |
| 82 | intptr_t GetPollEvents(intptr_t events, DescriptorInfo* di); |
| 83 | static void* GetHashmapKeyFromFd(intptr_t fd); |
| 84 | static uint32_t GetHashmapHashFromFd(intptr_t fd); |
| 85 | |
| 86 | SimpleHashMap socket_map_; |
| 87 | TimeoutQueue timeout_queue_; |
| 88 | bool shutdown_; |
| 89 | int interrupt_fds_[2]; |
| 90 | int epoll_fd_; |
| 91 | int timer_fd_; |
| 92 | |
| 93 | DISALLOW_COPY_AND_ASSIGN(EventHandlerImplementation); |
| 94 | }; |
| 95 | |
| 96 | } // namespace bin |
| 97 | } // namespace dart |
| 98 | |
| 99 | #endif // RUNTIME_BIN_EVENTHANDLER_LINUX_H_ |
| 100 | |