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