blob: 071571a8867c1c4e91247d672576fe56b5f04aa0 [file] [log] [blame]
Jim King5ec805b2015-04-26 07:52:40 -04001/*
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
cyy316723a2019-01-05 16:35:14 +080023#include <memory>
Jim King5ec805b2015-04-26 07:52:40 -040024#include <thrift/TProcessor.h>
25#include <thrift/protocol/TProtocol.h>
26#include <thrift/server/TServer.h>
27#include <thrift/transport/TTransport.h>
28
29namespace apache {
30namespace thrift {
31namespace 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 Grochowski1f6e3802015-05-18 18:10:06 +020040class TConnectedClient : public apache::thrift::concurrency::Runnable {
41public:
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(
cyy316723a2019-01-05 16:35:14 +080052 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 King5ec805b2015-04-26 07:52:40 -040057
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020058 /**
59 * Destructor.
60 */
Sebastian Zenker042580f2019-01-29 15:48:12 +010061 ~TConnectedClient() override;
Jim King5ec805b2015-04-26 07:52:40 -040062
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020063 /**
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 Zenker042580f2019-01-29 15:48:12 +010079 void run() override /* override */;
Jim King5ec805b2015-04-26 07:52:40 -040080
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020081protected:
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 King5ec805b2015-04-26 07:52:40 -040093
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020094private:
cyy316723a2019-01-05 16:35:14 +080095 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 King5ec805b2015-04-26 07:52:40 -0400100
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200101 /**
102 * Context acquired from the eventHandler_ if one exists.
103 */
104 void* opaqueContext_;
Jim King5ec805b2015-04-26 07:52:40 -0400105};
Jim King5ec805b2015-04-26 07:52:40 -0400106}
107}
108}
109
110#endif // #ifndef _THRIFT_SERVER_TCONNECTEDCLIENT_H_