| 1 | // |
|---|
| 2 | // mp::iothreads::reader |
|---|
| 3 | // |
|---|
| 4 | // Copyright (C) 2008 FURUHASHI Sadayuki |
|---|
| 5 | // |
|---|
| 6 | // Licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 7 | // you may not use this file except in compliance with the License. |
|---|
| 8 | // You may obtain a copy of the License at |
|---|
| 9 | // |
|---|
| 10 | // http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 11 | // |
|---|
| 12 | // Unless required by applicable law or agreed to in writing, software |
|---|
| 13 | // distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|---|
| 15 | // See the License for the specific language governing permissions and |
|---|
| 16 | // limitations under the License. |
|---|
| 17 | // |
|---|
| 18 | |
|---|
| 19 | #ifndef MP_IOTHREADS_READER_H__ |
|---|
| 20 | #define MP_IOTHREADS_READER_H__ |
|---|
| 21 | |
|---|
| 22 | #include "mp/functional.h" |
|---|
| 23 | #include <memory> |
|---|
| 24 | |
|---|
| 25 | namespace mp { |
|---|
| 26 | namespace iothreads { |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | class handler { |
|---|
| 30 | public: |
|---|
| 31 | handler(int fd) : m_fd(fd) {} |
|---|
| 32 | virtual ~handler() { } |
|---|
| 33 | public: |
|---|
| 34 | virtual void read_event() = 0; |
|---|
| 35 | virtual void connected() {} |
|---|
| 36 | public: |
|---|
| 37 | int fd() const |
|---|
| 38 | { return m_fd; } |
|---|
| 39 | void send_data(const void* buf, size_t len) |
|---|
| 40 | { return iothreads::send_data(m_fd, buf, len); } |
|---|
| 41 | void send_datav(const struct iovec* vb, const reqvec* vr, size_t count) |
|---|
| 42 | { return iothreads::send_datav(m_fd, vb, vr, count); } |
|---|
| 43 | private: |
|---|
| 44 | int m_fd; |
|---|
| 45 | private: |
|---|
| 46 | handler(); |
|---|
| 47 | handler(const handler&); |
|---|
| 48 | }; |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | class reader { |
|---|
| 52 | public: |
|---|
| 53 | static void initialize(unsigned int num_threads); |
|---|
| 54 | static void destroy(); |
|---|
| 55 | |
|---|
| 56 | public: |
|---|
| 57 | template <typename Handler> |
|---|
| 58 | static void add(int fd); |
|---|
| 59 | |
|---|
| 60 | MP_ARGS_BEGIN |
|---|
| 61 | template <typename Handler, MP_ARGS_TEMPLATE> |
|---|
| 62 | static void add(int fd, MP_ARGS_PARAMS); |
|---|
| 63 | MP_ARGS_END |
|---|
| 64 | |
|---|
| 65 | static void close(int fd); |
|---|
| 66 | |
|---|
| 67 | typedef function<void (handler&)> message_t; |
|---|
| 68 | static void send_message(int fd, message_t* msg); |
|---|
| 69 | |
|---|
| 70 | private: |
|---|
| 71 | class impl; |
|---|
| 72 | static std::auto_ptr<impl> s_instance; |
|---|
| 73 | static impl& instance(); |
|---|
| 74 | |
|---|
| 75 | private: |
|---|
| 76 | reader(); |
|---|
| 77 | ~reader(); |
|---|
| 78 | reader(const reader&); |
|---|
| 79 | }; |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | class reader::impl { |
|---|
| 83 | public: |
|---|
| 84 | impl(unsigned int num_threads); |
|---|
| 85 | ~impl(); |
|---|
| 86 | |
|---|
| 87 | public: |
|---|
| 88 | template <typename Handler> |
|---|
| 89 | void add(int fd); |
|---|
| 90 | |
|---|
| 91 | MP_ARGS_BEGIN |
|---|
| 92 | template <typename Handler, MP_ARGS_TEMPLATE> |
|---|
| 93 | void add(int fd, MP_ARGS_PARAMS); |
|---|
| 94 | MP_ARGS_END |
|---|
| 95 | |
|---|
| 96 | void close(int fd); |
|---|
| 97 | |
|---|
| 98 | void send_message(int fd, message_t* msg); |
|---|
| 99 | |
|---|
| 100 | private: |
|---|
| 101 | class worker; |
|---|
| 102 | std::vector<worker*> m_workers; |
|---|
| 103 | |
|---|
| 104 | private: |
|---|
| 105 | void add_impl(handler* h); |
|---|
| 106 | worker& worker_of(int fd); |
|---|
| 107 | |
|---|
| 108 | private: |
|---|
| 109 | impl(); |
|---|
| 110 | impl(const impl&); |
|---|
| 111 | }; |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | template <typename Handler> |
|---|
| 115 | inline void reader::add(int fd) |
|---|
| 116 | { |
|---|
| 117 | instance().add<Handler>(fd); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | MP_ARGS_BEGIN |
|---|
| 121 | template <typename Handler, MP_ARGS_TEMPLATE> |
|---|
| 122 | inline void reader::add(int fd, MP_ARGS_PARAMS) |
|---|
| 123 | { |
|---|
| 124 | instance().add<Handler, MP_ARGS_TYPES>(fd, MP_ARGS_FUNC); |
|---|
| 125 | } |
|---|
| 126 | MP_ARGS_END |
|---|
| 127 | |
|---|
| 128 | inline void reader::close(int fd) |
|---|
| 129 | { |
|---|
| 130 | instance().close(fd); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | inline void reader::send_message(int fd, message_t* msg) |
|---|
| 134 | { |
|---|
| 135 | instance().send_message(fd, msg); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | template <typename Handler> |
|---|
| 140 | inline void reader::impl::add(int fd) |
|---|
| 141 | { |
|---|
| 142 | handler* h = NULL; |
|---|
| 143 | try { h = new Handler(fd); } |
|---|
| 144 | catch (...) { ::close(fd); } |
|---|
| 145 | try { add_impl(h); } |
|---|
| 146 | catch (...) { ::close(fd); delete h; throw; } |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | MP_ARGS_BEGIN |
|---|
| 150 | template <typename Handler, MP_ARGS_TEMPLATE> |
|---|
| 151 | inline void reader::impl::add(int fd, MP_ARGS_PARAMS) |
|---|
| 152 | { |
|---|
| 153 | handler* h = NULL; |
|---|
| 154 | try { h = new Handler(fd, MP_ARGS_FUNC); } |
|---|
| 155 | catch (...) { ::close(fd); } |
|---|
| 156 | try { add_impl(h); } |
|---|
| 157 | catch (...) { ::close(fd); delete h; throw; } |
|---|
| 158 | } |
|---|
| 159 | MP_ARGS_END |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | template <typename Handler> |
|---|
| 163 | inline void add(int fd) |
|---|
| 164 | { |
|---|
| 165 | reader::add<Handler>(fd); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | MP_ARGS_BEGIN |
|---|
| 169 | template <typename Handler, MP_ARGS_TEMPLATE> |
|---|
| 170 | inline void add(int fd, MP_ARGS_PARAMS) |
|---|
| 171 | { |
|---|
| 172 | reader::add<Handler>(fd, MP_ARGS_FUNC); |
|---|
| 173 | } |
|---|
| 174 | MP_ARGS_END |
|---|
| 175 | |
|---|
| 176 | inline void close(int fd) |
|---|
| 177 | { |
|---|
| 178 | reader::close(fd); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | MP_ARGS_BEGIN |
|---|
| 182 | template <MP_ARGS_TEMPLATE> |
|---|
| 183 | inline void send_message(int fd, MP_ARGS_PARAMS) |
|---|
| 184 | { |
|---|
| 185 | reader::send_message(fd, |
|---|
| 186 | new reader::message_t(bind(MP_ARGS_FUNC))); |
|---|
| 187 | } |
|---|
| 188 | MP_ARGS_END |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | } // namespace iothreads |
|---|
| 192 | } // namespace mp |
|---|
| 193 | |
|---|
| 194 | #endif /* mp/iothreads/reader.h */ |
|---|
| 195 | |
|---|