Jim King | 5ec805b | 2015-04-26 07:52:40 -0400 | [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 | */ |
| 19 | |
| 20 | #ifndef _THRIFT_SERVER_TCONNECTEDCLIENT_H_ |
| 21 | #define _THRIFT_SERVER_TCONNECTEDCLIENT_H_ 1 |
| 22 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 23 | #include <memory> |
Jim King | 5ec805b | 2015-04-26 07:52:40 -0400 | [diff] [blame] | 24 | #include <thrift/TProcessor.h> |
| 25 | #include <thrift/protocol/TProtocol.h> |
| 26 | #include <thrift/server/TServer.h> |
| 27 | #include <thrift/transport/TTransport.h> |
| 28 | |
| 29 | namespace apache { |
| 30 | namespace thrift { |
| 31 | namespace server { |
| 32 | |
| 33 | /** |
| 34 | * This represents a client connected to a TServer. The |
| 35 | * processing loop for a client must provide some required |
| 36 | * functionality common to all implementations so it is |
| 37 | * encapsulated here. |
| 38 | */ |
| 39 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 40 | class TConnectedClient : public apache::thrift::concurrency::Runnable { |
| 41 | public: |
| 42 | /** |
| 43 | * Constructor. |
| 44 | * |
| 45 | * @param[in] processor the TProcessor |
| 46 | * @param[in] inputProtocol the input TProtocol |
| 47 | * @param[in] outputProtocol the output TProtocol |
| 48 | * @param[in] eventHandler the server event handler |
| 49 | * @param[in] client the TTransport representing the client |
| 50 | */ |
| 51 | TConnectedClient( |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 52 | const std::shared_ptr<apache::thrift::TProcessor>& processor, |
| 53 | const std::shared_ptr<apache::thrift::protocol::TProtocol>& inputProtocol, |
| 54 | const std::shared_ptr<apache::thrift::protocol::TProtocol>& outputProtocol, |
| 55 | const std::shared_ptr<apache::thrift::server::TServerEventHandler>& eventHandler, |
| 56 | const std::shared_ptr<apache::thrift::transport::TTransport>& client); |
Jim King | 5ec805b | 2015-04-26 07:52:40 -0400 | [diff] [blame] | 57 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 58 | /** |
| 59 | * Destructor. |
| 60 | */ |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 61 | ~TConnectedClient() override; |
Jim King | 5ec805b | 2015-04-26 07:52:40 -0400 | [diff] [blame] | 62 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 63 | /** |
| 64 | * Drive the client until it is done. |
| 65 | * The client processing loop is: |
| 66 | * |
| 67 | * [optional] call eventHandler->createContext once |
| 68 | * [optional] call eventHandler->processContext per request |
| 69 | * call processor->process per request |
| 70 | * handle expected transport exceptions: |
| 71 | * END_OF_FILE means the client is gone |
| 72 | * INTERRUPTED means the client was interrupted |
| 73 | * by TServerTransport::interruptChildren() |
| 74 | * handle unexpected transport exceptions by logging |
| 75 | * handle standard exceptions by logging |
| 76 | * handle unexpected exceptions by logging |
| 77 | * cleanup() |
| 78 | */ |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 79 | void run() override /* override */; |
Jim King | 5ec805b | 2015-04-26 07:52:40 -0400 | [diff] [blame] | 80 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 81 | protected: |
| 82 | /** |
| 83 | * Cleanup after a client. This happens if the client disconnects, |
| 84 | * or if the server is stopped, or if an exception occurs. |
| 85 | * |
| 86 | * The cleanup processing is: |
| 87 | * [optional] call eventHandler->deleteContext once |
| 88 | * close the inputProtocol's TTransport |
| 89 | * close the outputProtocol's TTransport |
| 90 | * close the client |
| 91 | */ |
| 92 | virtual void cleanup(); |
Jim King | 5ec805b | 2015-04-26 07:52:40 -0400 | [diff] [blame] | 93 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 94 | private: |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 95 | std::shared_ptr<apache::thrift::TProcessor> processor_; |
| 96 | std::shared_ptr<apache::thrift::protocol::TProtocol> inputProtocol_; |
| 97 | std::shared_ptr<apache::thrift::protocol::TProtocol> outputProtocol_; |
| 98 | std::shared_ptr<apache::thrift::server::TServerEventHandler> eventHandler_; |
| 99 | std::shared_ptr<apache::thrift::transport::TTransport> client_; |
Jim King | 5ec805b | 2015-04-26 07:52:40 -0400 | [diff] [blame] | 100 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 101 | /** |
| 102 | * Context acquired from the eventHandler_ if one exists. |
| 103 | */ |
| 104 | void* opaqueContext_; |
Jim King | 5ec805b | 2015-04-26 07:52:40 -0400 | [diff] [blame] | 105 | }; |
Jim King | 5ec805b | 2015-04-26 07:52:40 -0400 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | #endif // #ifndef _THRIFT_SERVER_TCONNECTEDCLIENT_H_ |