Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 7 | #include "TNonblockingServer.h" |
| 8 | |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 9 | #include <iostream> |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 10 | #include <sys/socket.h> |
| 11 | #include <netinet/in.h> |
| 12 | #include <netinet/tcp.h> |
Mark Slee | fb4b514 | 2007-11-20 01:27:08 +0000 | [diff] [blame] | 13 | #include <netdb.h> |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 14 | #include <fcntl.h> |
| 15 | #include <errno.h> |
| 16 | #include <assert.h> |
| 17 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 18 | namespace facebook { namespace thrift { namespace server { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 19 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 20 | using namespace facebook::thrift::protocol; |
| 21 | using namespace facebook::thrift::transport; |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 22 | using namespace std; |
| 23 | |
| 24 | class TConnection::Task: public Runnable { |
| 25 | public: |
| 26 | Task(boost::shared_ptr<TProcessor> processor, |
| 27 | boost::shared_ptr<TProtocol> input, |
| 28 | boost::shared_ptr<TProtocol> output, |
| 29 | int taskHandle) : |
| 30 | processor_(processor), |
| 31 | input_(input), |
| 32 | output_(output), |
| 33 | taskHandle_(taskHandle) {} |
| 34 | |
| 35 | void run() { |
| 36 | try { |
| 37 | while (processor_->process(input_, output_)) { |
| 38 | if (!input_->getTransport()->peek()) { |
| 39 | break; |
| 40 | } |
| 41 | } |
| 42 | } catch (TTransportException& ttx) { |
David Reiss | a79e488 | 2008-03-05 07:51:47 +0000 | [diff] [blame] | 43 | cerr << "TNonblockingServer client died: " << ttx.what() << endl; |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 44 | } catch (TException& x) { |
David Reiss | a79e488 | 2008-03-05 07:51:47 +0000 | [diff] [blame] | 45 | cerr << "TNonblockingServer exception: " << x.what() << endl; |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 46 | } catch (...) { |
David Reiss | a79e488 | 2008-03-05 07:51:47 +0000 | [diff] [blame] | 47 | cerr << "TNonblockingServer uncaught exception." << endl; |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 48 | } |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 49 | |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 50 | // Signal completion back to the libevent thread via a socketpair |
| 51 | int8_t b = 0; |
| 52 | if (-1 == send(taskHandle_, &b, sizeof(int8_t), 0)) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 53 | GlobalOutput.perror("TNonblockingServer::Task: send ", errno); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 54 | } |
| 55 | if (-1 == ::close(taskHandle_)) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 56 | GlobalOutput.perror("TNonblockingServer::Task: close, possible resource leak ", errno); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | boost::shared_ptr<TProcessor> processor_; |
| 62 | boost::shared_ptr<TProtocol> input_; |
| 63 | boost::shared_ptr<TProtocol> output_; |
| 64 | int taskHandle_; |
| 65 | }; |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 66 | |
| 67 | void TConnection::init(int socket, short eventFlags, TNonblockingServer* s) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 68 | socket_ = socket; |
| 69 | server_ = s; |
| 70 | appState_ = APP_INIT; |
| 71 | eventFlags_ = 0; |
| 72 | |
| 73 | readBufferPos_ = 0; |
| 74 | readWant_ = 0; |
| 75 | |
| 76 | writeBuffer_ = NULL; |
| 77 | writeBufferSize_ = 0; |
| 78 | writeBufferPos_ = 0; |
| 79 | |
| 80 | socketState_ = SOCKET_RECV; |
| 81 | appState_ = APP_INIT; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 82 | |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 83 | taskHandle_ = -1; |
| 84 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 85 | // Set flags, which also registers the event |
| 86 | setFlags(eventFlags); |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 87 | |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 88 | // get input/transports |
| 89 | factoryInputTransport_ = s->getInputTransportFactory()->getTransport(inputTransport_); |
| 90 | factoryOutputTransport_ = s->getOutputTransportFactory()->getTransport(outputTransport_); |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 91 | |
| 92 | // Create protocol |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 93 | inputProtocol_ = s->getInputProtocolFactory()->getProtocol(factoryInputTransport_); |
| 94 | outputProtocol_ = s->getOutputProtocolFactory()->getProtocol(factoryOutputTransport_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void TConnection::workSocket() { |
Mark Slee | aaa23ed | 2007-01-30 19:52:05 +0000 | [diff] [blame] | 98 | int flags=0, got=0, left=0, sent=0; |
| 99 | uint32_t fetch = 0; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 100 | |
| 101 | switch (socketState_) { |
| 102 | case SOCKET_RECV: |
| 103 | // It is an error to be in this state if we already have all the data |
| 104 | assert(readBufferPos_ < readWant_); |
| 105 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 106 | // Double the buffer size until it is big enough |
robert | 7951119 | 2006-12-20 19:25:38 +0000 | [diff] [blame] | 107 | if (readWant_ > readBufferSize_) { |
| 108 | while (readWant_ > readBufferSize_) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 109 | readBufferSize_ *= 2; |
| 110 | } |
David Reiss | d7a16f4 | 2008-02-19 22:47:29 +0000 | [diff] [blame] | 111 | readBuffer_ = (uint8_t*)std::realloc(readBuffer_, readBufferSize_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 112 | if (readBuffer_ == NULL) { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 113 | GlobalOutput("TConnection::workSocket() realloc"); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 114 | close(); |
| 115 | return; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // Read from the socket |
Mark Slee | aaa23ed | 2007-01-30 19:52:05 +0000 | [diff] [blame] | 120 | fetch = readWant_ - readBufferPos_; |
| 121 | got = recv(socket_, readBuffer_ + readBufferPos_, fetch, 0); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 122 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 123 | if (got > 0) { |
| 124 | // Move along in the buffer |
| 125 | readBufferPos_ += got; |
| 126 | |
| 127 | // Check that we did not overdo it |
| 128 | assert(readBufferPos_ <= readWant_); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 129 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 130 | // We are done reading, move onto the next state |
| 131 | if (readBufferPos_ == readWant_) { |
| 132 | transition(); |
| 133 | } |
| 134 | return; |
| 135 | } else if (got == -1) { |
| 136 | // Blocking errors are okay, just move on |
| 137 | if (errno == EAGAIN || errno == EWOULDBLOCK) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | if (errno != ECONNRESET) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 142 | GlobalOutput.perror("TConnection::workSocket() recv -1 ", errno); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | // Whenever we get down here it means a remote disconnect |
| 147 | close(); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 148 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 149 | return; |
| 150 | |
| 151 | case SOCKET_SEND: |
| 152 | // Should never have position past size |
| 153 | assert(writeBufferPos_ <= writeBufferSize_); |
| 154 | |
| 155 | // If there is no data to send, then let us move on |
| 156 | if (writeBufferPos_ == writeBufferSize_) { |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 157 | GlobalOutput("WARNING: Send state with no data to send\n"); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 158 | transition(); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | flags = 0; |
| 163 | #ifdef MSG_NOSIGNAL |
| 164 | // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we |
| 165 | // check for the EPIPE return condition and close the socket in that case |
| 166 | flags |= MSG_NOSIGNAL; |
| 167 | #endif // ifdef MSG_NOSIGNAL |
| 168 | |
Mark Slee | aaa23ed | 2007-01-30 19:52:05 +0000 | [diff] [blame] | 169 | left = writeBufferSize_ - writeBufferPos_; |
| 170 | sent = send(socket_, writeBuffer_ + writeBufferPos_, left, flags); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 171 | |
| 172 | if (sent <= 0) { |
| 173 | // Blocking errors are okay, just move on |
| 174 | if (errno == EAGAIN || errno == EWOULDBLOCK) { |
| 175 | return; |
| 176 | } |
| 177 | if (errno != EPIPE) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 178 | GlobalOutput.perror("TConnection::workSocket() send -1 ", errno); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 179 | } |
| 180 | close(); |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | writeBufferPos_ += sent; |
| 185 | |
| 186 | // Did we overdo it? |
| 187 | assert(writeBufferPos_ <= writeBufferSize_); |
| 188 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 189 | // We are done! |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 190 | if (writeBufferPos_ == writeBufferSize_) { |
| 191 | transition(); |
| 192 | } |
| 193 | |
| 194 | return; |
| 195 | |
| 196 | default: |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 197 | GlobalOutput.printf("Shit Got Ill. Socket State %d", socketState_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 198 | assert(0); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * This is called when the application transitions from one state into |
| 204 | * another. This means that it has finished writing the data that it needed |
| 205 | * to, or finished receiving the data that it needed to. |
| 206 | */ |
| 207 | void TConnection::transition() { |
Mark Slee | aaa23ed | 2007-01-30 19:52:05 +0000 | [diff] [blame] | 208 | |
| 209 | int sz = 0; |
| 210 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 211 | // Switch upon the state that we are currently in and move to a new state |
| 212 | switch (appState_) { |
| 213 | |
| 214 | case APP_READ_REQUEST: |
| 215 | // We are done reading the request, package the read buffer into transport |
| 216 | // and get back some data from the dispatch function |
| 217 | inputTransport_->resetBuffer(readBuffer_, readBufferPos_); |
| 218 | outputTransport_->resetBuffer(); |
David Reiss | 52cb7a7 | 2008-06-30 21:40:35 +0000 | [diff] [blame] | 219 | // Prepend four bytes of blank space to the buffer so we can |
| 220 | // write the frame size there later. |
| 221 | outputTransport_->getWritePtr(4); |
| 222 | outputTransport_->wroteBytes(4); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 223 | |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 224 | if (server_->isThreadPoolProcessing()) { |
| 225 | // We are setting up a Task to do this work and we will wait on it |
| 226 | int sv[2]; |
| 227 | if (-1 == socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 228 | GlobalOutput.perror("TConnection::socketpair() failed ", errno); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 229 | // Now we will fall through to the APP_WAIT_TASK block with no response |
| 230 | } else { |
| 231 | // Create task and dispatch to the thread manager |
| 232 | boost::shared_ptr<Runnable> task = |
| 233 | boost::shared_ptr<Runnable>(new Task(server_->getProcessor(), |
| 234 | inputProtocol_, |
| 235 | outputProtocol_, |
| 236 | sv[1])); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 237 | // The application is now waiting on the task to finish |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 238 | appState_ = APP_WAIT_TASK; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 239 | |
| 240 | // Create an event to be notified when the task finishes |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 241 | event_set(&taskEvent_, |
| 242 | taskHandle_ = sv[0], |
| 243 | EV_READ, |
| 244 | TConnection::taskHandler, |
| 245 | this); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 246 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 247 | // Attach to the base |
| 248 | event_base_set(server_->getEventBase(), &taskEvent_); |
| 249 | |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 250 | // Add the event and start up the server |
| 251 | if (-1 == event_add(&taskEvent_, 0)) { |
| 252 | GlobalOutput("TNonblockingServer::serve(): coult not event_add"); |
| 253 | return; |
| 254 | } |
| 255 | server_->addTask(task); |
Mark Slee | 402ee28 | 2007-08-23 01:43:20 +0000 | [diff] [blame] | 256 | |
| 257 | // Set this connection idle so that libevent doesn't process more |
| 258 | // data on it while we're still waiting for the threadmanager to |
| 259 | // finish this task |
| 260 | setIdle(); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 261 | return; |
| 262 | } |
| 263 | } else { |
| 264 | try { |
| 265 | // Invoke the processor |
| 266 | server_->getProcessor()->process(inputProtocol_, outputProtocol_); |
| 267 | } catch (TTransportException &ttx) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 268 | GlobalOutput.printf("TTransportException: Server::process() %s", ttx.what()); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 269 | close(); |
| 270 | return; |
| 271 | } catch (TException &x) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 272 | GlobalOutput.printf("TException: Server::process() %s", x.what()); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 273 | close(); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 274 | return; |
| 275 | } catch (...) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 276 | GlobalOutput.printf("Server::process() unknown exception"); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 277 | close(); |
| 278 | return; |
| 279 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Mark Slee | 402ee28 | 2007-08-23 01:43:20 +0000 | [diff] [blame] | 282 | // Intentionally fall through here, the call to process has written into |
| 283 | // the writeBuffer_ |
| 284 | |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 285 | case APP_WAIT_TASK: |
| 286 | // We have now finished processing a task and the result has been written |
| 287 | // into the outputTransport_, so we grab its contents and place them into |
| 288 | // the writeBuffer_ for actual writing by the libevent thread |
| 289 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 290 | // Get the result of the operation |
| 291 | outputTransport_->getBuffer(&writeBuffer_, &writeBufferSize_); |
| 292 | |
| 293 | // If the function call generated return data, then move into the send |
| 294 | // state and get going |
David Reiss | af78778 | 2008-07-03 20:29:34 +0000 | [diff] [blame] | 295 | // 4 bytes were reserved for frame size |
David Reiss | 52cb7a7 | 2008-06-30 21:40:35 +0000 | [diff] [blame] | 296 | if (writeBufferSize_ > 4) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 297 | |
| 298 | // Move into write state |
| 299 | writeBufferPos_ = 0; |
| 300 | socketState_ = SOCKET_SEND; |
Mark Slee | 92f00fb | 2006-10-25 01:28:17 +0000 | [diff] [blame] | 301 | |
David Reiss | af78778 | 2008-07-03 20:29:34 +0000 | [diff] [blame] | 302 | // Put the frame size into the write buffer |
| 303 | int32_t frameSize = (int32_t)htonl(writeBufferSize_ - 4); |
| 304 | memcpy(writeBuffer_, &frameSize, 4); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 305 | |
| 306 | // Socket into write mode |
David Reiss | 52cb7a7 | 2008-06-30 21:40:35 +0000 | [diff] [blame] | 307 | appState_ = APP_SEND_RESULT; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 308 | setWrite(); |
| 309 | |
| 310 | // Try to work the socket immediately |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 311 | // workSocket(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 312 | |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | // In this case, the request was asynchronous and we should fall through |
| 317 | // right back into the read frame header state |
Mark Slee | 92f00fb | 2006-10-25 01:28:17 +0000 | [diff] [blame] | 318 | goto LABEL_APP_INIT; |
| 319 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 320 | case APP_SEND_RESULT: |
| 321 | |
| 322 | // N.B.: We also intentionally fall through here into the INIT state! |
| 323 | |
Mark Slee | 92f00fb | 2006-10-25 01:28:17 +0000 | [diff] [blame] | 324 | LABEL_APP_INIT: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 325 | case APP_INIT: |
| 326 | |
| 327 | // Clear write buffer variables |
| 328 | writeBuffer_ = NULL; |
| 329 | writeBufferPos_ = 0; |
| 330 | writeBufferSize_ = 0; |
| 331 | |
| 332 | // Set up read buffer for getting 4 bytes |
| 333 | readBufferPos_ = 0; |
| 334 | readWant_ = 4; |
| 335 | |
| 336 | // Into read4 state we go |
| 337 | socketState_ = SOCKET_RECV; |
| 338 | appState_ = APP_READ_FRAME_SIZE; |
| 339 | |
| 340 | // Register read event |
| 341 | setRead(); |
David Reiss | 84e63ab | 2008-03-07 20:12:28 +0000 | [diff] [blame] | 342 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 343 | // Try to work the socket right away |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 344 | // workSocket(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 345 | |
| 346 | return; |
| 347 | |
| 348 | case APP_READ_FRAME_SIZE: |
| 349 | // We just read the request length, deserialize it |
Mark Slee | aaa23ed | 2007-01-30 19:52:05 +0000 | [diff] [blame] | 350 | sz = *(int32_t*)readBuffer_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 351 | sz = (int32_t)ntohl(sz); |
| 352 | |
| 353 | if (sz <= 0) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 354 | GlobalOutput.printf("TConnection:transition() Negative frame size %d, remote side not using TFramedTransport?", sz); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 355 | close(); |
| 356 | return; |
| 357 | } |
| 358 | |
| 359 | // Reset the read buffer |
| 360 | readWant_ = (uint32_t)sz; |
| 361 | readBufferPos_= 0; |
| 362 | |
| 363 | // Move into read request state |
| 364 | appState_ = APP_READ_REQUEST; |
| 365 | |
| 366 | // Work the socket right away |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 367 | // workSocket(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 368 | |
| 369 | return; |
| 370 | |
| 371 | default: |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 372 | GlobalOutput.printf("Totally Fucked. Application State %d", appState_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 373 | assert(0); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | void TConnection::setFlags(short eventFlags) { |
| 378 | // Catch the do nothing case |
| 379 | if (eventFlags_ == eventFlags) { |
| 380 | return; |
| 381 | } |
| 382 | |
| 383 | // Delete a previously existing event |
| 384 | if (eventFlags_ != 0) { |
| 385 | if (event_del(&event_) == -1) { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 386 | GlobalOutput("TConnection::setFlags event_del"); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 387 | return; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | // Update in memory structure |
| 392 | eventFlags_ = eventFlags; |
| 393 | |
Mark Slee | 402ee28 | 2007-08-23 01:43:20 +0000 | [diff] [blame] | 394 | // Do not call event_set if there are no flags |
| 395 | if (!eventFlags_) { |
| 396 | return; |
| 397 | } |
| 398 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 399 | /** |
| 400 | * event_set: |
| 401 | * |
| 402 | * Prepares the event structure &event to be used in future calls to |
| 403 | * event_add() and event_del(). The event will be prepared to call the |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 404 | * eventHandler using the 'sock' file descriptor to monitor events. |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 405 | * |
| 406 | * The events can be either EV_READ, EV_WRITE, or both, indicating |
| 407 | * that an application can read or write from the file respectively without |
| 408 | * blocking. |
| 409 | * |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 410 | * The eventHandler will be called with the file descriptor that triggered |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 411 | * the event and the type of event which will be one of: EV_TIMEOUT, |
| 412 | * EV_SIGNAL, EV_READ, EV_WRITE. |
| 413 | * |
| 414 | * The additional flag EV_PERSIST makes an event_add() persistent until |
| 415 | * event_del() has been called. |
| 416 | * |
| 417 | * Once initialized, the &event struct can be used repeatedly with |
| 418 | * event_add() and event_del() and does not need to be reinitialized unless |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 419 | * the eventHandler and/or the argument to it are to be changed. However, |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 420 | * when an ev structure has been added to libevent using event_add() the |
| 421 | * structure must persist until the event occurs (assuming EV_PERSIST |
| 422 | * is not set) or is removed using event_del(). You may not reuse the same |
| 423 | * ev structure for multiple monitored descriptors; each descriptor needs |
| 424 | * its own ev. |
| 425 | */ |
| 426 | event_set(&event_, socket_, eventFlags_, TConnection::eventHandler, this); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 427 | event_base_set(server_->getEventBase(), &event_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 428 | |
| 429 | // Add the event |
| 430 | if (event_add(&event_, 0) == -1) { |
Mark Slee | 17496a0 | 2007-08-02 06:37:40 +0000 | [diff] [blame] | 431 | GlobalOutput("TConnection::setFlags(): could not event_add"); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Closes a connection |
| 437 | */ |
| 438 | void TConnection::close() { |
| 439 | // Delete the registered libevent |
| 440 | if (event_del(&event_) == -1) { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 441 | GlobalOutput("TConnection::close() event_del"); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | // Close the socket |
| 445 | if (socket_ > 0) { |
| 446 | ::close(socket_); |
| 447 | } |
| 448 | socket_ = 0; |
| 449 | |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 450 | // close any factory produced transports |
| 451 | factoryInputTransport_->close(); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 452 | factoryOutputTransport_->close(); |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 453 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 454 | // Give this object back to the server that owns it |
| 455 | server_->returnConnection(this); |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Creates a new connection either by reusing an object off the stack or |
| 460 | * by allocating a new one entirely |
| 461 | */ |
| 462 | TConnection* TNonblockingServer::createConnection(int socket, short flags) { |
| 463 | // Check the stack |
| 464 | if (connectionStack_.empty()) { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 465 | return new TConnection(socket, flags, this); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 466 | } else { |
| 467 | TConnection* result = connectionStack_.top(); |
| 468 | connectionStack_.pop(); |
| 469 | result->init(socket, flags, this); |
| 470 | return result; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * Returns a connection to the stack |
| 476 | */ |
| 477 | void TNonblockingServer::returnConnection(TConnection* connection) { |
| 478 | connectionStack_.push(connection); |
| 479 | } |
| 480 | |
| 481 | /** |
David Reiss | a79e488 | 2008-03-05 07:51:47 +0000 | [diff] [blame] | 482 | * Server socket had something happen. We accept all waiting client |
| 483 | * connections on fd and assign TConnection objects to handle those requests. |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 484 | */ |
| 485 | void TNonblockingServer::handleEvent(int fd, short which) { |
| 486 | // Make sure that libevent didn't fuck up the socket handles |
| 487 | assert(fd == serverSocket_); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 488 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 489 | // Server socket accepted a new connection |
| 490 | socklen_t addrLen; |
| 491 | struct sockaddr addr; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 492 | addrLen = sizeof(addr); |
| 493 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 494 | // Going to accept a new client socket |
| 495 | int clientSocket; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 496 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 497 | // Accept as many new clients as possible, even though libevent signaled only |
| 498 | // one, this helps us to avoid having to go back into the libevent engine so |
| 499 | // many times |
| 500 | while ((clientSocket = accept(fd, &addr, &addrLen)) != -1) { |
| 501 | |
| 502 | // Explicitly set this socket to NONBLOCK mode |
| 503 | int flags; |
| 504 | if ((flags = fcntl(clientSocket, F_GETFL, 0)) < 0 || |
| 505 | fcntl(clientSocket, F_SETFL, flags | O_NONBLOCK) < 0) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 506 | GlobalOutput.perror("thriftServerEventHandler: set O_NONBLOCK (fcntl) ", errno); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 507 | close(clientSocket); |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | // Create a new TConnection for this client socket. |
| 512 | TConnection* clientConnection = |
| 513 | createConnection(clientSocket, EV_READ | EV_PERSIST); |
| 514 | |
| 515 | // Fail fast if we could not create a TConnection object |
| 516 | if (clientConnection == NULL) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 517 | GlobalOutput.printf("thriftServerEventHandler: failed TConnection factory"); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 518 | close(clientSocket); |
| 519 | return; |
| 520 | } |
| 521 | |
| 522 | // Put this client connection into the proper state |
| 523 | clientConnection->transition(); |
| 524 | } |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 525 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 526 | // Done looping accept, now we have to make sure the error is due to |
| 527 | // blocking. Any other error is a problem |
| 528 | if (errno != EAGAIN && errno != EWOULDBLOCK) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 529 | GlobalOutput.perror("thriftServerEventHandler: accept() ", errno); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
| 533 | /** |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 534 | * Creates a socket to listen on and binds it to the local port. |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 535 | */ |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 536 | void TNonblockingServer::listenSocket() { |
| 537 | int s; |
Mark Slee | fb4b514 | 2007-11-20 01:27:08 +0000 | [diff] [blame] | 538 | struct addrinfo hints, *res, *res0; |
| 539 | int error; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 540 | |
Mark Slee | fb4b514 | 2007-11-20 01:27:08 +0000 | [diff] [blame] | 541 | char port[sizeof("65536") + 1]; |
| 542 | memset(&hints, 0, sizeof(hints)); |
| 543 | hints.ai_family = PF_UNSPEC; |
| 544 | hints.ai_socktype = SOCK_STREAM; |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 545 | hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; |
Mark Slee | fb4b514 | 2007-11-20 01:27:08 +0000 | [diff] [blame] | 546 | sprintf(port, "%d", port_); |
| 547 | |
| 548 | // Wildcard address |
| 549 | error = getaddrinfo(NULL, port, &hints, &res0); |
| 550 | if (error) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 551 | string errStr = "TNonblockingServer::serve() getaddrinfo " + string(gai_strerror(error)); |
| 552 | GlobalOutput(errStr.c_str()); |
Mark Slee | fb4b514 | 2007-11-20 01:27:08 +0000 | [diff] [blame] | 553 | return; |
| 554 | } |
| 555 | |
| 556 | // Pick the ipv6 address first since ipv4 addresses can be mapped |
| 557 | // into ipv6 space. |
| 558 | for (res = res0; res; res = res->ai_next) { |
| 559 | if (res->ai_family == AF_INET6 || res->ai_next == NULL) |
| 560 | break; |
| 561 | } |
| 562 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 563 | // Create the server socket |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 564 | s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
| 565 | if (s == -1) { |
| 566 | freeaddrinfo(res0); |
| 567 | throw TException("TNonblockingServer::serve() socket() -1"); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 568 | } |
| 569 | |
David Reiss | 13aea46 | 2008-06-10 22:56:04 +0000 | [diff] [blame] | 570 | #ifdef IPV6_V6ONLY |
| 571 | int zero = 0; |
| 572 | if (-1 == setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero))) { |
| 573 | GlobalOutput("TServerSocket::listen() IPV6_V6ONLY"); |
| 574 | } |
| 575 | #endif // #ifdef IPV6_V6ONLY |
| 576 | |
| 577 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 578 | int one = 1; |
| 579 | |
| 580 | // Set reuseaddr to avoid 2MSL delay on server restart |
| 581 | setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); |
| 582 | |
| 583 | if (bind(s, res->ai_addr, res->ai_addrlen) == -1) { |
| 584 | close(s); |
| 585 | freeaddrinfo(res0); |
| 586 | throw TException("TNonblockingServer::serve() bind"); |
| 587 | } |
| 588 | |
| 589 | // Done with the addr info |
| 590 | freeaddrinfo(res0); |
| 591 | |
| 592 | // Set up this file descriptor for listening |
| 593 | listenSocket(s); |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * Takes a socket created by listenSocket() and sets various options on it |
| 598 | * to prepare for use in the server. |
| 599 | */ |
| 600 | void TNonblockingServer::listenSocket(int s) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 601 | // Set socket to nonblocking mode |
| 602 | int flags; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 603 | if ((flags = fcntl(s, F_GETFL, 0)) < 0 || |
| 604 | fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0) { |
| 605 | close(s); |
| 606 | throw TException("TNonblockingServer::serve() O_NONBLOCK"); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | int one = 1; |
| 610 | struct linger ling = {0, 0}; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 611 | |
| 612 | // Keepalive to ensure full result flushing |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 613 | setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one)); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 614 | |
| 615 | // Turn linger off to avoid hung sockets |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 616 | setsockopt(s, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling)); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 617 | |
| 618 | // Set TCP nodelay if available, MAC OS X Hack |
| 619 | // See http://lists.danga.com/pipermail/memcached/2005-March/001240.html |
| 620 | #ifndef TCP_NOPUSH |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 621 | setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 622 | #endif |
| 623 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 624 | if (listen(s, LISTEN_BACKLOG) == -1) { |
| 625 | close(s); |
| 626 | throw TException("TNonblockingServer::serve() listen"); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 629 | // Cool, this socket is good to go, set it as the serverSocket_ |
| 630 | serverSocket_ = s; |
| 631 | } |
| 632 | |
| 633 | /** |
| 634 | * Register the core libevent events onto the proper base. |
| 635 | */ |
| 636 | void TNonblockingServer::registerEvents(event_base* base) { |
| 637 | assert(serverSocket_ != -1); |
| 638 | assert(!eventBase_); |
| 639 | eventBase_ = base; |
| 640 | |
| 641 | // Print some libevent stats |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 642 | GlobalOutput.printf("libevent %s method %s", |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 643 | event_get_version(), |
| 644 | event_get_method()); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 645 | |
| 646 | // Register the server event |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 647 | event_set(&serverEvent_, |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 648 | serverSocket_, |
| 649 | EV_READ | EV_PERSIST, |
| 650 | TNonblockingServer::eventHandler, |
| 651 | this); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 652 | event_base_set(eventBase_, &serverEvent_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 653 | |
| 654 | // Add the event and start up the server |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 655 | if (-1 == event_add(&serverEvent_, 0)) { |
| 656 | throw TException("TNonblockingServer::serve(): coult not event_add"); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 657 | } |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | /** |
| 661 | * Main workhorse function, starts up the server listening on a port and |
| 662 | * loops over the libevent handler. |
| 663 | */ |
| 664 | void TNonblockingServer::serve() { |
| 665 | // Init socket |
| 666 | listenSocket(); |
| 667 | |
| 668 | // Initialize libevent core |
| 669 | registerEvents(static_cast<event_base*>(event_init())); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 670 | |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 671 | // Run the preServe event |
| 672 | if (eventHandler_ != NULL) { |
| 673 | eventHandler_->preServe(); |
dweatherford | 5898599 | 2007-06-19 23:10:19 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 676 | // Run libevent engine, never returns, invokes calls to eventHandler |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 677 | event_base_loop(eventBase_, 0); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | }}} // facebook::thrift::server |