David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. 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, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 20 | #ifndef _THRIFT_SERVER_TNONBLOCKINGSERVER_H_ |
| 21 | #define _THRIFT_SERVER_TNONBLOCKINGSERVER_H_ 1 |
| 22 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 23 | #include <Thrift.h> |
| 24 | #include <server/TServer.h> |
David Reiss | 28f298d | 2008-05-01 06:17:36 +0000 | [diff] [blame] | 25 | #include <transport/TBufferTransports.h> |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 26 | #include <transport/TSocket.h> |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 27 | #include <concurrency/ThreadManager.h> |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 28 | #include <climits> |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 29 | #include <stack> |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 30 | #include <string> |
| 31 | #include <errno.h> |
David Reiss | d7a16f4 | 2008-02-19 22:47:29 +0000 | [diff] [blame] | 32 | #include <cstdlib> |
David Reiss | 5105b2e | 2009-05-21 02:28:27 +0000 | [diff] [blame] | 33 | #include <unistd.h> |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 34 | #include <event.h> |
| 35 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 36 | namespace apache { namespace thrift { namespace server { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 37 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 38 | using apache::thrift::transport::TMemoryBuffer; |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 39 | using apache::thrift::transport::TSocket; |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 40 | using apache::thrift::protocol::TProtocol; |
| 41 | using apache::thrift::concurrency::Runnable; |
| 42 | using apache::thrift::concurrency::ThreadManager; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 43 | |
| 44 | // Forward declaration of class |
| 45 | class TConnection; |
| 46 | |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 47 | #ifdef LIBEVENT_VERSION_NUMBER |
| 48 | #define LIBEVENT_VERSION_MAJOR (LIBEVENT_VERSION_NUMBER >> 24) |
| 49 | #define LIBEVENT_VERSION_MINOR ((LIBEVENT_VERSION_NUMBER >> 16) & 0xFF) |
| 50 | #define LIBEVENT_VERSION_REL ((LIBEVENT_VERSION_NUMBER >> 8) & 0xFF) |
| 51 | #else |
| 52 | // assume latest version 1 series |
| 53 | #define LIBEVENT_VERSION_MAJOR 1 |
| 54 | #define LIBEVENT_VERSION_MINOR 14 |
| 55 | #define LIBEVENT_VERSION_REL 13 |
| 56 | #define LIBEVENT_VERSION_NUMBER ((LIBEVENT_VERSION_MAJOR << 24) | (LIBEVENT_VERSION_MINOR << 16) | (LIBEVENT_VERSION_REL << 8)) |
| 57 | #endif |
| 58 | |
| 59 | #if LIBEVENT_VERSION_NUMBER < 0x02000000 |
| 60 | typedef int evutil_socket_t; |
| 61 | #endif |
| 62 | |
| 63 | #ifndef SOCKOPT_CAST_T |
| 64 | #define SOCKOPT_CAST_T void |
| 65 | #endif |
| 66 | |
| 67 | template<class T> |
| 68 | inline const SOCKOPT_CAST_T* const_cast_sockopt(const T* v) { |
| 69 | return reinterpret_cast<const SOCKOPT_CAST_T*>(v); |
| 70 | } |
| 71 | |
| 72 | template<class T> |
| 73 | inline SOCKOPT_CAST_T* cast_sockopt(T* v) { |
| 74 | return reinterpret_cast<SOCKOPT_CAST_T*>(v); |
| 75 | } |
| 76 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 77 | /** |
| 78 | * This is a non-blocking server in C++ for high performance that operates a |
| 79 | * single IO thread. It assumes that all incoming requests are framed with a |
| 80 | * 4 byte length indicator and writes out responses using the same framing. |
| 81 | * |
| 82 | * It does not use the TServerTransport framework, but rather has socket |
| 83 | * operations hardcoded for use with select. |
| 84 | * |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 85 | */ |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 86 | |
| 87 | |
| 88 | /// Overload condition actions. |
| 89 | enum TOverloadAction { |
| 90 | T_OVERLOAD_NO_ACTION, ///< Don't handle overload */ |
| 91 | T_OVERLOAD_CLOSE_ON_ACCEPT, ///< Drop new connections immediately */ |
| 92 | T_OVERLOAD_DRAIN_TASK_QUEUE ///< Drop some tasks from head of task queue */ |
| 93 | }; |
| 94 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 95 | class TNonblockingServer : public TServer { |
| 96 | private: |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 97 | /// Listen backlog |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 98 | static const int LISTEN_BACKLOG = 1024; |
| 99 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 100 | /// Default limit on size of idle connection pool |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 101 | static const size_t CONNECTION_STACK_LIMIT = 1024; |
| 102 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 103 | /// Default limit on total number of connected sockets |
| 104 | static const int MAX_CONNECTIONS = INT_MAX; |
| 105 | |
| 106 | /// Default limit on connections in handler/task processing |
| 107 | static const int MAX_ACTIVE_PROCESSORS = INT_MAX; |
| 108 | |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 109 | /// Default size of write buffer |
| 110 | static const int WRITE_BUFFER_DEFAULT_SIZE = 1024; |
| 111 | |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 112 | /// Maximum size of read buffer allocated to idle connection (0 = unlimited) |
| 113 | static const int IDLE_READ_BUFFER_LIMIT = 1024; |
| 114 | |
| 115 | /// Maximum size of write buffer allocated to idle connection (0 = unlimited) |
| 116 | static const int IDLE_WRITE_BUFFER_LIMIT = 1024; |
| 117 | |
| 118 | /// # of calls before resizing oversized buffers (0 = check only on close) |
| 119 | static const int RESIZE_BUFFER_EVERY_N = 512; |
| 120 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 121 | /// Server socket file descriptor |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 122 | int serverSocket_; |
| 123 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 124 | /// Port server runs on |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 125 | int port_; |
| 126 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 127 | /// For processing via thread pool, may be NULL |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 128 | boost::shared_ptr<ThreadManager> threadManager_; |
| 129 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 130 | /// Is thread pool processing? |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 131 | bool threadPoolProcessing_; |
| 132 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 133 | /// The event base for libevent |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 134 | event_base* eventBase_; |
| 135 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 136 | /// Event struct, used with eventBase_ for connection events |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 137 | struct event serverEvent_; |
| 138 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 139 | /// Event struct, used with eventBase_ for task completion notification |
| 140 | struct event notificationEvent_; |
| 141 | |
| 142 | /// Number of TConnection object we've created |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 143 | size_t numTConnections_; |
| 144 | |
David Reiss | 9e8073c | 2010-03-09 05:19:39 +0000 | [diff] [blame] | 145 | /// Number of Connections processing or waiting to process |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 146 | size_t numActiveProcessors_; |
| 147 | |
| 148 | /// Limit for how many TConnection objects to cache |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 149 | size_t connectionStackLimit_; |
| 150 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 151 | /// Limit for number of connections processing or waiting to process |
| 152 | size_t maxActiveProcessors_; |
| 153 | |
| 154 | /// Limit for number of open connections |
| 155 | size_t maxConnections_; |
| 156 | |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 157 | /// Time in milliseconds before an unperformed task expires (0 == infinite). |
| 158 | int64_t taskExpireTime_; |
| 159 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 160 | /** |
| 161 | * Hysteresis for overload state. This is the fraction of the overload |
| 162 | * value that needs to be reached before the overload state is cleared; |
| 163 | * must be <= 1.0. |
| 164 | */ |
| 165 | double overloadHysteresis_; |
| 166 | |
| 167 | /// Action to take when we're overloaded. |
| 168 | TOverloadAction overloadAction_; |
| 169 | |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 170 | /** |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 171 | * The write buffer is initialized (and when idleWriteBufferLimit_ is checked |
| 172 | * and found to be exceeded, reinitialized) to this size. |
| 173 | */ |
| 174 | size_t writeBufferDefaultSize_; |
| 175 | |
| 176 | /** |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 177 | * Max read buffer size for an idle TConnection. When we place an idle |
| 178 | * TConnection into connectionStack_ or on every resizeBufferEveryN_ calls, |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 179 | * we will free the buffer (such that it will be reinitialized by the next |
| 180 | * received frame) if it has exceeded this limit. 0 disables this check. |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 181 | */ |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 182 | size_t idleReadBufferLimit_; |
| 183 | |
| 184 | /** |
| 185 | * Max write buffer size for an idle connection. When we place an idle |
| 186 | * TConnection into connectionStack_ or on every resizeBufferEveryN_ calls, |
| 187 | * we insure that its write buffer is <= to this size; otherwise we |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 188 | * replace it with a new one of writeBufferDefaultSize_ bytes to insure that |
| 189 | * idle connections don't hog memory. 0 disables this check. |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 190 | */ |
| 191 | size_t idleWriteBufferLimit_; |
| 192 | |
| 193 | /** |
| 194 | * Every N calls we check the buffer size limits on a connected TConnection. |
| 195 | * 0 disables (i.e. the checks are only done when a connection closes). |
| 196 | */ |
| 197 | int32_t resizeBufferEveryN_; |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 198 | |
| 199 | /// Set if we are currently in an overloaded state. |
| 200 | bool overloaded_; |
| 201 | |
| 202 | /// Count of connections dropped since overload started |
| 203 | uint32_t nConnectionsDropped_; |
| 204 | |
| 205 | /// Count of connections dropped on overload since server started |
| 206 | uint64_t nTotalConnectionsDropped_; |
| 207 | |
| 208 | /// File descriptors for pipe used for task completion notification. |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 209 | evutil_socket_t notificationPipeFDs_[2]; |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 210 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 211 | /** |
| 212 | * This is a stack of all the objects that have been created but that |
| 213 | * are NOT currently in use. When we close a connection, we place it on this |
| 214 | * stack so that the object can be reused later, rather than freeing the |
| 215 | * memory and reallocating a new object later. |
| 216 | */ |
| 217 | std::stack<TConnection*> connectionStack_; |
| 218 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 219 | /** |
| 220 | * Called when server socket had something happen. We accept all waiting |
| 221 | * client connections on listen socket fd and assign TConnection objects |
| 222 | * to handle those requests. |
| 223 | * |
| 224 | * @param fd the listen socket. |
| 225 | * @param which the event flag that triggered the handler. |
| 226 | */ |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 227 | void handleEvent(int fd, short which); |
| 228 | |
| 229 | public: |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 230 | TNonblockingServer(boost::shared_ptr<TProcessor> processor, |
Mark Slee | f937339 | 2007-01-24 19:41:57 +0000 | [diff] [blame] | 231 | int port) : |
| 232 | TServer(processor), |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 233 | serverSocket_(-1), |
Mark Slee | f937339 | 2007-01-24 19:41:57 +0000 | [diff] [blame] | 234 | port_(port), |
dweatherford | 5898599 | 2007-06-19 23:10:19 +0000 | [diff] [blame] | 235 | threadPoolProcessing_(false), |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 236 | eventBase_(NULL), |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 237 | numTConnections_(0), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 238 | numActiveProcessors_(0), |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 239 | connectionStackLimit_(CONNECTION_STACK_LIMIT), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 240 | maxActiveProcessors_(MAX_ACTIVE_PROCESSORS), |
| 241 | maxConnections_(MAX_CONNECTIONS), |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 242 | taskExpireTime_(0), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 243 | overloadHysteresis_(0.8), |
| 244 | overloadAction_(T_OVERLOAD_NO_ACTION), |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 245 | writeBufferDefaultSize_(WRITE_BUFFER_DEFAULT_SIZE), |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 246 | idleReadBufferLimit_(IDLE_READ_BUFFER_LIMIT), |
| 247 | idleWriteBufferLimit_(IDLE_WRITE_BUFFER_LIMIT), |
| 248 | resizeBufferEveryN_(RESIZE_BUFFER_EVERY_N), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 249 | overloaded_(false), |
| 250 | nConnectionsDropped_(0), |
| 251 | nTotalConnectionsDropped_(0) {} |
Mark Slee | f937339 | 2007-01-24 19:41:57 +0000 | [diff] [blame] | 252 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 253 | TNonblockingServer(boost::shared_ptr<TProcessor> processor, |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 254 | boost::shared_ptr<TProtocolFactory> protocolFactory, |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 255 | int port, |
| 256 | boost::shared_ptr<ThreadManager> threadManager = boost::shared_ptr<ThreadManager>()) : |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 257 | TServer(processor), |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 258 | serverSocket_(-1), |
Mark Slee | 92f00fb | 2006-10-25 01:28:17 +0000 | [diff] [blame] | 259 | port_(port), |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 260 | threadManager_(threadManager), |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 261 | eventBase_(NULL), |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 262 | numTConnections_(0), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 263 | numActiveProcessors_(0), |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 264 | connectionStackLimit_(CONNECTION_STACK_LIMIT), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 265 | maxActiveProcessors_(MAX_ACTIVE_PROCESSORS), |
| 266 | maxConnections_(MAX_CONNECTIONS), |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 267 | taskExpireTime_(0), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 268 | overloadHysteresis_(0.8), |
| 269 | overloadAction_(T_OVERLOAD_NO_ACTION), |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 270 | writeBufferDefaultSize_(WRITE_BUFFER_DEFAULT_SIZE), |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 271 | idleReadBufferLimit_(IDLE_READ_BUFFER_LIMIT), |
| 272 | idleWriteBufferLimit_(IDLE_WRITE_BUFFER_LIMIT), |
| 273 | resizeBufferEveryN_(RESIZE_BUFFER_EVERY_N), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 274 | overloaded_(false), |
| 275 | nConnectionsDropped_(0), |
| 276 | nTotalConnectionsDropped_(0) { |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 277 | setInputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory())); |
| 278 | setOutputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory())); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 279 | setInputProtocolFactory(protocolFactory); |
| 280 | setOutputProtocolFactory(protocolFactory); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 281 | setThreadManager(threadManager); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 282 | } |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 283 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 284 | TNonblockingServer(boost::shared_ptr<TProcessor> processor, |
| 285 | boost::shared_ptr<TTransportFactory> inputTransportFactory, |
| 286 | boost::shared_ptr<TTransportFactory> outputTransportFactory, |
| 287 | boost::shared_ptr<TProtocolFactory> inputProtocolFactory, |
| 288 | boost::shared_ptr<TProtocolFactory> outputProtocolFactory, |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 289 | int port, |
| 290 | boost::shared_ptr<ThreadManager> threadManager = boost::shared_ptr<ThreadManager>()) : |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 291 | TServer(processor), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 292 | serverSocket_(-1), |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 293 | port_(port), |
Mark Slee | 5d1784a | 2007-12-05 23:20:54 +0000 | [diff] [blame] | 294 | threadManager_(threadManager), |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 295 | eventBase_(NULL), |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 296 | numTConnections_(0), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 297 | numActiveProcessors_(0), |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 298 | connectionStackLimit_(CONNECTION_STACK_LIMIT), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 299 | maxActiveProcessors_(MAX_ACTIVE_PROCESSORS), |
| 300 | maxConnections_(MAX_CONNECTIONS), |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 301 | taskExpireTime_(0), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 302 | overloadHysteresis_(0.8), |
| 303 | overloadAction_(T_OVERLOAD_NO_ACTION), |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 304 | writeBufferDefaultSize_(WRITE_BUFFER_DEFAULT_SIZE), |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 305 | idleReadBufferLimit_(IDLE_READ_BUFFER_LIMIT), |
| 306 | idleWriteBufferLimit_(IDLE_WRITE_BUFFER_LIMIT), |
| 307 | resizeBufferEveryN_(RESIZE_BUFFER_EVERY_N), |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 308 | overloaded_(false), |
| 309 | nConnectionsDropped_(0), |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 310 | nTotalConnectionsDropped_(0) { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 311 | setInputTransportFactory(inputTransportFactory); |
| 312 | setOutputTransportFactory(outputTransportFactory); |
| 313 | setInputProtocolFactory(inputProtocolFactory); |
| 314 | setOutputProtocolFactory(outputProtocolFactory); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 315 | setThreadManager(threadManager); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 316 | } |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 317 | |
David Reiss | 8ede818 | 2010-09-02 15:26:28 +0000 | [diff] [blame] | 318 | ~TNonblockingServer(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 319 | |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 320 | void setThreadManager(boost::shared_ptr<ThreadManager> threadManager); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 321 | |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 322 | boost::shared_ptr<ThreadManager> getThreadManager() { |
| 323 | return threadManager_; |
| 324 | } |
| 325 | |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 326 | /** |
| 327 | * Get the maximum number of unused TConnection we will hold in reserve. |
| 328 | * |
| 329 | * @return the current limit on TConnection pool size. |
| 330 | */ |
David Reiss | 260fa93 | 2009-04-02 23:51:39 +0000 | [diff] [blame] | 331 | size_t getConnectionStackLimit() const { |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 332 | return connectionStackLimit_; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Set the maximum number of unused TConnection we will hold in reserve. |
| 337 | * |
| 338 | * @param sz the new limit for TConnection pool size. |
| 339 | */ |
David Reiss | 260fa93 | 2009-04-02 23:51:39 +0000 | [diff] [blame] | 340 | void setConnectionStackLimit(size_t sz) { |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 341 | connectionStackLimit_ = sz; |
| 342 | } |
| 343 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 344 | bool isThreadPoolProcessing() const { |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 345 | return threadPoolProcessing_; |
| 346 | } |
| 347 | |
| 348 | void addTask(boost::shared_ptr<Runnable> task) { |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 349 | threadManager_->add(task, 0LL, taskExpireTime_); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 352 | event_base* getEventBase() const { |
| 353 | return eventBase_; |
| 354 | } |
| 355 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 356 | /// Increment our count of the number of connected sockets. |
David Reiss | c17fe6b | 2008-04-29 00:29:43 +0000 | [diff] [blame] | 357 | void incrementNumConnections() { |
| 358 | ++numTConnections_; |
| 359 | } |
| 360 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 361 | /// Decrement our count of the number of connected sockets. |
David Reiss | c17fe6b | 2008-04-29 00:29:43 +0000 | [diff] [blame] | 362 | void decrementNumConnections() { |
| 363 | --numTConnections_; |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 364 | } |
| 365 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 366 | /** |
| 367 | * Return the count of sockets currently connected to. |
| 368 | * |
| 369 | * @return count of connected sockets. |
| 370 | */ |
| 371 | size_t getNumConnections() const { |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 372 | return numTConnections_; |
| 373 | } |
| 374 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 375 | /** |
| 376 | * Return the count of connection objects allocated but not in use. |
| 377 | * |
| 378 | * @return count of idle connection objects. |
| 379 | */ |
| 380 | size_t getNumIdleConnections() const { |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 381 | return connectionStack_.size(); |
| 382 | } |
| 383 | |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 384 | /** |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 385 | * Return count of number of connections which are currently processing. |
| 386 | * This is defined as a connection where all data has been received and |
| 387 | * either assigned a task (when threading) or passed to a handler (when |
| 388 | * not threading), and where the handler has not yet returned. |
| 389 | * |
| 390 | * @return # of connections currently processing. |
| 391 | */ |
| 392 | size_t getNumActiveProcessors() const { |
| 393 | return numActiveProcessors_; |
| 394 | } |
| 395 | |
| 396 | /// Increment the count of connections currently processing. |
| 397 | void incrementActiveProcessors() { |
| 398 | ++numActiveProcessors_; |
| 399 | } |
| 400 | |
| 401 | /// Decrement the count of connections currently processing. |
| 402 | void decrementActiveProcessors() { |
| 403 | if (numActiveProcessors_ > 0) { |
| 404 | --numActiveProcessors_; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Get the maximum # of connections allowed before overload. |
| 410 | * |
| 411 | * @return current setting. |
| 412 | */ |
| 413 | size_t getMaxConnections() const { |
| 414 | return maxConnections_; |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * Set the maximum # of connections allowed before overload. |
| 419 | * |
| 420 | * @param maxConnections new setting for maximum # of connections. |
| 421 | */ |
| 422 | void setMaxConnections(size_t maxConnections) { |
| 423 | maxConnections_ = maxConnections; |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Get the maximum # of connections waiting in handler/task before overload. |
| 428 | * |
| 429 | * @return current setting. |
| 430 | */ |
| 431 | size_t getMaxActiveProcessors() const { |
| 432 | return maxActiveProcessors_; |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Set the maximum # of connections waiting in handler/task before overload. |
| 437 | * |
| 438 | * @param maxActiveProcessors new setting for maximum # of active processes. |
| 439 | */ |
| 440 | void setMaxActiveProcessors(size_t maxActiveProcessors) { |
| 441 | maxActiveProcessors_ = maxActiveProcessors; |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Get fraction of maximum limits before an overload condition is cleared. |
| 446 | * |
| 447 | * @return hysteresis fraction |
| 448 | */ |
| 449 | double getOverloadHysteresis() const { |
| 450 | return overloadHysteresis_; |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Set fraction of maximum limits before an overload condition is cleared. |
| 455 | * A good value would probably be between 0.5 and 0.9. |
| 456 | * |
| 457 | * @param hysteresisFraction fraction <= 1.0. |
| 458 | */ |
| 459 | void setOverloadHysteresis(double hysteresisFraction) { |
| 460 | if (hysteresisFraction <= 1.0 && hysteresisFraction > 0.0) { |
| 461 | overloadHysteresis_ = hysteresisFraction; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * Get the action the server will take on overload. |
| 467 | * |
| 468 | * @return a TOverloadAction enum value for the currently set action. |
| 469 | */ |
| 470 | TOverloadAction getOverloadAction() const { |
| 471 | return overloadAction_; |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * Set the action the server is to take on overload. |
| 476 | * |
| 477 | * @param overloadAction a TOverloadAction enum value for the action. |
| 478 | */ |
| 479 | void setOverloadAction(TOverloadAction overloadAction) { |
| 480 | overloadAction_ = overloadAction; |
| 481 | } |
| 482 | |
| 483 | /** |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 484 | * Get the time in milliseconds after which a task expires (0 == infinite). |
| 485 | * |
| 486 | * @return a 64-bit time in milliseconds. |
| 487 | */ |
| 488 | int64_t getTaskExpireTime() const { |
| 489 | return taskExpireTime_; |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * Set the time in milliseconds after which a task expires (0 == infinite). |
| 494 | * |
| 495 | * @param taskExpireTime a 64-bit time in milliseconds. |
| 496 | */ |
| 497 | void setTaskExpireTime(int64_t taskExpireTime) { |
| 498 | taskExpireTime_ = taskExpireTime; |
| 499 | } |
| 500 | |
| 501 | /** |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 502 | * Determine if the server is currently overloaded. |
| 503 | * This function checks the maximums for open connections and connections |
| 504 | * currently in processing, and sets an overload condition if they are |
| 505 | * exceeded. The overload will persist until both values are below the |
| 506 | * current hysteresis fraction of their maximums. |
| 507 | * |
| 508 | * @return true if an overload condition exists, false if not. |
| 509 | */ |
| 510 | bool serverOverloaded(); |
| 511 | |
| 512 | /** Pop and discard next task on threadpool wait queue. |
| 513 | * |
| 514 | * @return true if a task was discarded, false if the wait queue was empty. |
| 515 | */ |
| 516 | bool drainPendingTask(); |
| 517 | |
| 518 | /** |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 519 | * Get the starting size of a TConnection object's write buffer. |
| 520 | * |
| 521 | * @return # bytes we initialize a TConnection object's write buffer to. |
| 522 | */ |
| 523 | size_t getWriteBufferDefaultSize() const { |
| 524 | return writeBufferDefaultSize_; |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Set the starting size of a TConnection object's write buffer. |
| 529 | * |
| 530 | * @param size # bytes we initialize a TConnection object's write buffer to. |
| 531 | */ |
| 532 | void setWriteBufferDefaultSize(size_t size) { |
| 533 | writeBufferDefaultSize_ = size; |
| 534 | } |
| 535 | |
| 536 | /** |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 537 | * Get the maximum size of read buffer allocated to idle TConnection objects. |
| 538 | * |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 539 | * @return # bytes beyond which we will dealloc idle buffer. |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 540 | */ |
| 541 | size_t getIdleReadBufferLimit() const { |
| 542 | return idleReadBufferLimit_; |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * [NOTE: This is for backwards compatibility, use getIdleReadBufferLimit().] |
| 547 | * Get the maximum size of read buffer allocated to idle TConnection objects. |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 548 | * |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 549 | * @return # bytes beyond which we will dealloc idle buffer. |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 550 | */ |
| 551 | size_t getIdleBufferMemLimit() const { |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 552 | return idleReadBufferLimit_; |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | /** |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 556 | * Set the maximum size read buffer allocated to idle TConnection objects. |
| 557 | * If a TConnection object is found (either on connection close or between |
| 558 | * calls when resizeBufferEveryN_ is set) with more than this much memory |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 559 | * allocated to its read buffer, we free it and allow it to be reinitialized |
| 560 | * on the next received frame. |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 561 | * |
| 562 | * @param limit of bytes beyond which we will shrink buffers when checked. |
| 563 | */ |
| 564 | void setIdleReadBufferLimit(size_t limit) { |
| 565 | idleReadBufferLimit_ = limit; |
| 566 | } |
| 567 | |
| 568 | /** |
| 569 | * [NOTE: This is for backwards compatibility, use setIdleReadBufferLimit().] |
| 570 | * Set the maximum size read buffer allocated to idle TConnection objects. |
| 571 | * If a TConnection object is found (either on connection close or between |
| 572 | * calls when resizeBufferEveryN_ is set) with more than this much memory |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 573 | * allocated to its read buffer, we free it and allow it to be reinitialized |
| 574 | * on the next received frame. |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 575 | * |
| 576 | * @param limit of bytes beyond which we will shrink buffers when checked. |
| 577 | */ |
| 578 | void setIdleBufferMemLimit(size_t limit) { |
| 579 | idleReadBufferLimit_ = limit; |
| 580 | } |
| 581 | |
| 582 | |
| 583 | |
| 584 | /** |
| 585 | * Get the maximum size of write buffer allocated to idle TConnection objects. |
| 586 | * |
| 587 | * @return # bytes beyond which we will reallocate buffers when checked. |
| 588 | */ |
| 589 | size_t getIdleWriteBufferLimit() const { |
| 590 | return idleWriteBufferLimit_; |
| 591 | } |
| 592 | |
| 593 | /** |
| 594 | * Set the maximum size write buffer allocated to idle TConnection objects. |
| 595 | * If a TConnection object is found (either on connection close or between |
| 596 | * calls when resizeBufferEveryN_ is set) with more than this much memory |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 597 | * allocated to its write buffer, we destroy and construct that buffer with |
| 598 | * writeBufferDefaultSize_ bytes. |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 599 | * |
| 600 | * @param limit of bytes beyond which we will shrink buffers when idle. |
| 601 | */ |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 602 | void setIdleWriteBufferLimit(size_t limit) { |
| 603 | idleWriteBufferLimit_ = limit; |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 604 | } |
| 605 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 606 | /** |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 607 | * Get # of calls made between buffer size checks. 0 means disabled. |
| 608 | * |
| 609 | * @return # of calls between buffer size checks. |
| 610 | */ |
| 611 | int32_t getResizeBufferEveryN() const { |
| 612 | return resizeBufferEveryN_; |
| 613 | } |
| 614 | |
| 615 | /** |
| 616 | * Check buffer sizes every "count" calls. This allows buffer limits |
| 617 | * to be enforced for persistant connections with a controllable degree |
| 618 | * of overhead. 0 disables checks except at connection close. |
| 619 | * |
| 620 | * @param count the number of calls between checks, or 0 to disable |
| 621 | */ |
| 622 | void setResizeBufferEveryN(int32_t count) { |
| 623 | resizeBufferEveryN_ = count; |
| 624 | } |
| 625 | |
| 626 | |
| 627 | |
| 628 | /** |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 629 | * Return an initialized connection object. Creates or recovers from |
| 630 | * pool a TConnection and initializes it with the provided socket FD |
| 631 | * and flags. |
| 632 | * |
| 633 | * @param socket FD of socket associated with this connection. |
| 634 | * @param flags initial lib_event flags for this connection. |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 635 | * @param addr the sockaddr of the client |
| 636 | * @param addrLen the length of addr |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 637 | * @return pointer to initialized TConnection object. |
| 638 | */ |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 639 | TConnection* createConnection(int socket, short flags, |
| 640 | const sockaddr* addr, socklen_t addrLen); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 641 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 642 | /** |
| 643 | * Returns a connection to pool or deletion. If the connection pool |
| 644 | * (a stack) isn't full, place the connection object on it, otherwise |
| 645 | * just delete it. |
| 646 | * |
| 647 | * @param connection the TConection being returned. |
| 648 | */ |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 649 | void returnConnection(TConnection* connection); |
| 650 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 651 | /** |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 652 | * Callback function that the threadmanager calls when a task reaches |
| 653 | * its expiration time. It is needed to clean up the expired connection. |
| 654 | * |
| 655 | * @param task the runnable associated with the expired task. |
| 656 | */ |
| 657 | void expireClose(boost::shared_ptr<Runnable> task); |
| 658 | |
| 659 | /** |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 660 | * C-callable event handler for listener events. Provides a callback |
| 661 | * that libevent can understand which invokes server->handleEvent(). |
| 662 | * |
| 663 | * @param fd the descriptor the event occured on. |
| 664 | * @param which the flags associated with the event. |
| 665 | * @param v void* callback arg where we placed TNonblockingServer's "this". |
| 666 | */ |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 667 | static void eventHandler(evutil_socket_t fd, short which, void* v) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 668 | ((TNonblockingServer*)v)->handleEvent(fd, which); |
| 669 | } |
| 670 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 671 | /// Creates a socket to listen on and binds it to the local port. |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 672 | void listenSocket(); |
| 673 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 674 | /** |
| 675 | * Takes a socket created by listenSocket() and sets various options on it |
| 676 | * to prepare for use in the server. |
| 677 | * |
| 678 | * @param fd descriptor of socket to be initialized/ |
| 679 | */ |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 680 | void listenSocket(int fd); |
| 681 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 682 | /// Create the pipe used to notify I/O process of task completion. |
| 683 | void createNotificationPipe(); |
| 684 | |
| 685 | /** |
| 686 | * Get notification pipe send descriptor. |
| 687 | * |
| 688 | * @return write fd for pipe. |
| 689 | */ |
| 690 | int getNotificationSendFD() const { |
| 691 | return notificationPipeFDs_[1]; |
| 692 | } |
| 693 | |
| 694 | /** |
| 695 | * Get notification pipe receive descriptor. |
| 696 | * |
| 697 | * @return read fd of pipe. |
| 698 | */ |
| 699 | int getNotificationRecvFD() const { |
| 700 | return notificationPipeFDs_[0]; |
| 701 | } |
| 702 | |
| 703 | /** |
| 704 | * Register the core libevent events onto the proper base. |
| 705 | * |
| 706 | * @param base pointer to the event base to be initialized. |
| 707 | */ |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 708 | void registerEvents(event_base* base); |
| 709 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 710 | /** |
| 711 | * Main workhorse function, starts up the server listening on a port and |
| 712 | * loops over the libevent handler. |
| 713 | */ |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 714 | void serve(); |
| 715 | }; |
| 716 | |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 717 | /// Three states for sockets: recv frame size, recv data, and send mode |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 718 | enum TSocketState { |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 719 | SOCKET_RECV_FRAMING, |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 720 | SOCKET_RECV, |
| 721 | SOCKET_SEND |
| 722 | }; |
| 723 | |
| 724 | /** |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 725 | * Five states for the nonblocking servr: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 726 | * 1) initialize |
| 727 | * 2) read 4 byte frame size |
| 728 | * 3) read frame of data |
| 729 | * 4) send back data (if any) |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 730 | * 5) force immediate connection close |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 731 | */ |
| 732 | enum TAppState { |
| 733 | APP_INIT, |
| 734 | APP_READ_FRAME_SIZE, |
| 735 | APP_READ_REQUEST, |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 736 | APP_WAIT_TASK, |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 737 | APP_SEND_RESULT, |
| 738 | APP_CLOSE_CONNECTION |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 739 | }; |
| 740 | |
| 741 | /** |
| 742 | * Represents a connection that is handled via libevent. This connection |
| 743 | * essentially encapsulates a socket that has some associated libevent state. |
| 744 | */ |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 745 | class TConnection { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 746 | private: |
| 747 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 748 | /// Server handle |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 749 | TNonblockingServer* server_; |
| 750 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 751 | /// Object wrapping network socket |
| 752 | boost::shared_ptr<TSocket> tSocket_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 753 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 754 | /// Libevent object |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 755 | struct event event_; |
| 756 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 757 | /// Libevent flags |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 758 | short eventFlags_; |
| 759 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 760 | /// Socket mode |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 761 | TSocketState socketState_; |
| 762 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 763 | /// Application state |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 764 | TAppState appState_; |
| 765 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 766 | /// How much data needed to read |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 767 | uint32_t readWant_; |
| 768 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 769 | /// Where in the read buffer are we |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 770 | uint32_t readBufferPos_; |
| 771 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 772 | /// Read buffer |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 773 | uint8_t* readBuffer_; |
| 774 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 775 | /// Read buffer size |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 776 | uint32_t readBufferSize_; |
| 777 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 778 | /// Write buffer |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 779 | uint8_t* writeBuffer_; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 780 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 781 | /// Write buffer size |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 782 | uint32_t writeBufferSize_; |
| 783 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 784 | /// How far through writing are we? |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 785 | uint32_t writeBufferPos_; |
| 786 | |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 787 | /// Largest size of write buffer seen since buffer was constructed |
| 788 | size_t largestWriteBufferSize_; |
| 789 | |
| 790 | /// Count of the number of calls for use with getResizeBufferEveryN(). |
| 791 | int32_t callsForResize_; |
| 792 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 793 | /// Task handle |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 794 | int taskHandle_; |
| 795 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 796 | /// Task event |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 797 | struct event taskEvent_; |
| 798 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 799 | /// Transport to read from |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 800 | boost::shared_ptr<TMemoryBuffer> inputTransport_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 801 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 802 | /// Transport that processor writes to |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 803 | boost::shared_ptr<TMemoryBuffer> outputTransport_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 804 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 805 | /// extra transport generated by transport factory (e.g. BufferedRouterTransport) |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 806 | boost::shared_ptr<TTransport> factoryInputTransport_; |
| 807 | boost::shared_ptr<TTransport> factoryOutputTransport_; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 808 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 809 | /// Protocol decoder |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 810 | boost::shared_ptr<TProtocol> inputProtocol_; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 811 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 812 | /// Protocol encoder |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 813 | boost::shared_ptr<TProtocol> outputProtocol_; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 814 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 815 | /// Server event handler, if any |
| 816 | boost::shared_ptr<TServerEventHandler> serverEventHandler_; |
| 817 | |
| 818 | /// Thrift call context, if any |
| 819 | void *connectionContext_; |
| 820 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 821 | /// Go into read mode |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 822 | void setRead() { |
| 823 | setFlags(EV_READ | EV_PERSIST); |
| 824 | } |
| 825 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 826 | /// Go into write mode |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 827 | void setWrite() { |
| 828 | setFlags(EV_WRITE | EV_PERSIST); |
| 829 | } |
| 830 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 831 | /// Set socket idle |
Mark Slee | 402ee28 | 2007-08-23 01:43:20 +0000 | [diff] [blame] | 832 | void setIdle() { |
| 833 | setFlags(0); |
| 834 | } |
| 835 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 836 | /** |
| 837 | * Set event flags for this connection. |
| 838 | * |
| 839 | * @param eventFlags flags we pass to libevent for the connection. |
| 840 | */ |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 841 | void setFlags(short eventFlags); |
| 842 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 843 | /** |
| 844 | * Libevent handler called (via our static wrapper) when the connection |
| 845 | * socket had something happen. Rather than use the flags libevent passed, |
| 846 | * we use the connection state to determine whether we need to read or |
| 847 | * write the socket. |
| 848 | */ |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 849 | void workSocket(); |
| 850 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 851 | /// Close this connection and free or reset its resources. |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 852 | void close(); |
| 853 | |
| 854 | public: |
| 855 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 856 | class Task; |
| 857 | |
| 858 | /// Constructor |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 859 | TConnection(int socket, short eventFlags, TNonblockingServer *s, |
| 860 | const sockaddr* addr, socklen_t addrLen) { |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 861 | readBuffer_ = NULL; |
| 862 | readBufferSize_ = 0; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 863 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 864 | // Allocate input and output tranpsorts |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 865 | // these only need to be allocated once per TConnection (they don't need to be |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 866 | // reallocated on init() call) |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 867 | inputTransport_ = boost::shared_ptr<TMemoryBuffer>(new TMemoryBuffer(readBuffer_, readBufferSize_)); |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 868 | outputTransport_ = boost::shared_ptr<TMemoryBuffer>(new TMemoryBuffer(s->getWriteBufferDefaultSize())); |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 869 | tSocket_.reset(new TSocket()); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 870 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 871 | init(socket, eventFlags, s, addr, addrLen); |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 872 | server_->incrementNumConnections(); |
| 873 | } |
| 874 | |
| 875 | ~TConnection() { |
David Reiss | 472fffb | 2010-03-09 05:20:24 +0000 | [diff] [blame] | 876 | std::free(readBuffer_); |
David Reiss | c17fe6b | 2008-04-29 00:29:43 +0000 | [diff] [blame] | 877 | server_->decrementNumConnections(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 878 | } |
| 879 | |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 880 | /** |
| 881 | * Check buffers against any size limits and shrink it if exceeded. |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 882 | * |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 883 | * @param readLimit we reduce read buffer size to this (if nonzero). |
| 884 | * @param writeLimit if nonzero and write buffer is larger, replace it. |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 885 | */ |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 886 | void checkIdleBufferMemLimit(size_t readLimit, size_t writeLimit); |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 887 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 888 | /// Initialize |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 889 | void init(int socket, short eventFlags, TNonblockingServer *s, |
| 890 | const sockaddr* addr, socklen_t addrLen); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 891 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 892 | /** |
| 893 | * This is called when the application transitions from one state into |
| 894 | * another. This means that it has finished writing the data that it needed |
| 895 | * to, or finished receiving the data that it needed to. |
| 896 | */ |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 897 | void transition(); |
| 898 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 899 | /** |
| 900 | * C-callable event handler for connection events. Provides a callback |
| 901 | * that libevent can understand which invokes connection_->workSocket(). |
| 902 | * |
| 903 | * @param fd the descriptor the event occured on. |
| 904 | * @param which the flags associated with the event. |
| 905 | * @param v void* callback arg where we placed TConnection's "this". |
| 906 | */ |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 907 | static void eventHandler(evutil_socket_t fd, short /* which */, void* v) { |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 908 | assert(fd == ((TConnection*)v)->getTSocket()->getSocketFD()); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 909 | ((TConnection*)v)->workSocket(); |
| 910 | } |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 911 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 912 | /** |
| 913 | * C-callable event handler for signaling task completion. Provides a |
| 914 | * callback that libevent can understand that will read a connection |
| 915 | * object's address from a pipe and call connection->transition() for |
| 916 | * that object. |
| 917 | * |
| 918 | * @param fd the descriptor the event occured on. |
| 919 | */ |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 920 | static void taskHandler(evutil_socket_t fd, short /* which */, void* /* v */) { |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 921 | TConnection* connection; |
David Reiss | 83b8fda | 2010-03-09 05:19:34 +0000 | [diff] [blame] | 922 | ssize_t nBytes; |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 923 | while ((nBytes = recv(fd, cast_sockopt(&connection), sizeof(TConnection*), 0)) |
David Reiss | 83b8fda | 2010-03-09 05:19:34 +0000 | [diff] [blame] | 924 | == sizeof(TConnection*)) { |
| 925 | connection->transition(); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 926 | } |
David Reiss | 83b8fda | 2010-03-09 05:19:34 +0000 | [diff] [blame] | 927 | if (nBytes > 0) { |
| 928 | throw TException("TConnection::taskHandler unexpected partial read"); |
| 929 | } |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 930 | if (errno && errno != EWOULDBLOCK && errno != EAGAIN) { |
David Reiss | 83b8fda | 2010-03-09 05:19:34 +0000 | [diff] [blame] | 931 | GlobalOutput.perror("TConnection::taskHandler read failed, resource leak", errno); |
| 932 | } |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 933 | } |
| 934 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 935 | /** |
| 936 | * Notification to server that processing has ended on this request. |
| 937 | * Can be called either when processing is completed or when a waiting |
| 938 | * task has been preemptively terminated (on overload). |
| 939 | * |
David Reiss | 9e8073c | 2010-03-09 05:19:39 +0000 | [diff] [blame] | 940 | * @return true if successful, false if unable to notify (check errno). |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 941 | */ |
| 942 | bool notifyServer() { |
| 943 | TConnection* connection = this; |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 944 | if (send(server_->getNotificationSendFD(), const_cast_sockopt(&connection), |
| 945 | sizeof(TConnection*), 0) != sizeof(TConnection*)) { |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 946 | return false; |
| 947 | } |
| 948 | |
| 949 | return true; |
| 950 | } |
| 951 | |
| 952 | /// Force connection shutdown for this connection. |
| 953 | void forceClose() { |
| 954 | appState_ = APP_CLOSE_CONNECTION; |
| 955 | if (!notifyServer()) { |
| 956 | throw TException("TConnection::forceClose: failed write on notify pipe"); |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | /// return the server this connection was initialized for. |
| 961 | TNonblockingServer* getServer() { |
| 962 | return server_; |
| 963 | } |
| 964 | |
| 965 | /// get state of connection. |
| 966 | TAppState getState() { |
| 967 | return appState_; |
| 968 | } |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 969 | |
| 970 | /// return the TSocket transport wrapping this network connection |
| 971 | boost::shared_ptr<TSocket> getTSocket() const { |
| 972 | return tSocket_; |
| 973 | } |
| 974 | |
| 975 | /// return the server event handler if any |
| 976 | boost::shared_ptr<TServerEventHandler> getServerEventHandler() { |
| 977 | return serverEventHandler_; |
| 978 | } |
| 979 | |
| 980 | /// return the Thrift connection context if any |
| 981 | void* getConnectionContext() { |
| 982 | return connectionContext_; |
| 983 | } |
| 984 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 985 | }; |
| 986 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 987 | }}} // apache::thrift::server |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 988 | |
| 989 | #endif // #ifndef _THRIFT_SERVER_TSIMPLESERVER_H_ |