1 /*
2  * Copyright (c) 2006- Facebook
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 /*
7  * Licensed to the Apache Software Foundation (ASF) under one
8  * or more contributor license agreements. See the NOTICE file
9  * distributed with this work for additional information
10  * regarding copyright ownership. The ASF licenses this file
11  * to you under the Apache License, Version 2.0 (the
12  * "License"); you may not use this file except in compliance
13  * with the License. You may obtain a copy of the License at
14  *
15  *   http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing,
18  * software distributed under the License is distributed on an
19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20  * KIND, either express or implied. See the License for the
21  * specific language governing permissions and limitations
22  * under the License.
23  */
24 
25 #ifndef _THRIFT_SERVER_TCONNECTEDCLIENT_H_
26 #define _THRIFT_SERVER_TCONNECTEDCLIENT_H_ 1
27 
28 #include <memory>
29 #include <thrift/TProcessor.h>
30 #include <thrift/protocol/TProtocol.h>
31 #include <thrift/server/TServer.h>
32 #include <thrift/transport/TTransport.h>
33 
34 namespace apache
35 {
36 namespace thrift
37 {
38 namespace server
39 {
40 
41 /**
42  * This represents a client connected to a TServer.  The
43  * processing loop for a client must provide some required
44  * functionality common to all implementations so it is
45  * encapsulated here.
46  */
47 
48 class TConnectedClient
49 {
50 public:
51 	/**
52 	 * Constructor.
53 	 *
54 	 * @param[in] processor      the TProcessor
55 	 * @param[in] inputProtocol  the input TProtocol
56 	 * @param[in] outputProtocol the output TProtocol
57 	 * @param[in] eventHandler   the server event handler
58 	 * @param[in] client         the TTransport representing the client
59 	 */
60 	TConnectedClient(
61 		const std::shared_ptr<apache::thrift::TProcessor> &processor,
62 		const std::shared_ptr<apache::thrift::protocol::TProtocol> &inputProtocol,
63 		const std::shared_ptr<apache::thrift::protocol::TProtocol> &outputProtocol,
64 		const std::shared_ptr<apache::thrift::server::TServerEventHandler> &eventHandler,
65 		const std::shared_ptr<apache::thrift::transport::TTransport> &client);
66 
67 	/**
68 	 * Destructor.
69 	 */
70 	~TConnectedClient();
71 
72 	/**
73 	 * Drive the client until it is done.
74 	 * The client processing loop is:
75 	 *
76 	 * [optional] call eventHandler->createContext once
77 	 * [optional] call eventHandler->processContext per request
78 	 *            call processor->process per request
79 	 *              handle expected transport exceptions:
80 	 *                END_OF_FILE means the client is gone
81 	 *                INTERRUPTED means the client was interrupted
82 	 *                            by TServerTransport::interruptChildren()
83 	 *              handle unexpected transport exceptions by logging
84 	 *              handle standard exceptions by logging
85 	 *              handle unexpected exceptions by logging
86 	 *            cleanup()
87 	 */
88 	void run();
89 
90 protected:
91 	/**
92 	 * Cleanup after a client.  This happens if the client disconnects,
93 	 * or if the server is stopped, or if an exception occurs.
94 	 *
95 	 * The cleanup processing is:
96 	 * [optional] call eventHandler->deleteContext once
97 	 *            close the inputProtocol's TTransport
98 	 *            close the outputProtocol's TTransport
99 	 *            close the client
100 	 */
101 	virtual void cleanup();
102 
103 private:
104 	std::shared_ptr<apache::thrift::TProcessor> processor_;
105 	std::shared_ptr<apache::thrift::protocol::TProtocol> inputProtocol_;
106 	std::shared_ptr<apache::thrift::protocol::TProtocol> outputProtocol_;
107 	std::shared_ptr<apache::thrift::server::TServerEventHandler> eventHandler_;
108 	std::shared_ptr<apache::thrift::transport::TTransport> client_;
109 
110 	/**
111 	 * Context acquired from the eventHandler_ if one exists.
112 	 */
113 	void *opaqueContext_;
114 };
115 } // namespace server
116 } // namespace thrift
117 } // namespace apache
118 
119 #endif // #ifndef _THRIFT_SERVER_TCONNECTEDCLIENT_H_
120