blob: 9a78e3e9b180ec3b69345d866674e06dea24c452 [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#include <thrift/server/TConnectedClient.h>
21
22namespace apache {
23namespace thrift {
24namespace server {
25
26using apache::thrift::TProcessor;
27using apache::thrift::protocol::TProtocol;
28using apache::thrift::server::TServerEventHandler;
29using apache::thrift::transport::TTransport;
30using apache::thrift::transport::TTransportException;
cyy316723a2019-01-05 16:35:14 +080031using std::shared_ptr;
Jim King5ec805b2015-04-26 07:52:40 -040032using std::string;
33
Jim King21b68522015-04-26 18:30:26 -040034TConnectedClient::TConnectedClient(const shared_ptr<TProcessor>& processor,
Jim King5ec805b2015-04-26 07:52:40 -040035 const shared_ptr<TProtocol>& inputProtocol,
36 const shared_ptr<TProtocol>& outputProtocol,
37 const shared_ptr<TServerEventHandler>& eventHandler,
38 const shared_ptr<TTransport>& client)
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020039
Jim King21b68522015-04-26 18:30:26 -040040 : processor_(processor),
Jim King5ec805b2015-04-26 07:52:40 -040041 inputProtocol_(inputProtocol),
42 outputProtocol_(outputProtocol),
43 eventHandler_(eventHandler),
44 client_(client),
Sebastian Zenker042580f2019-01-29 15:48:12 +010045 opaqueContext_(nullptr) {
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020046}
Jim King5ec805b2015-04-26 07:52:40 -040047
Sebastian Zenker042580f2019-01-29 15:48:12 +010048TConnectedClient::~TConnectedClient() = default;
Jim King5ec805b2015-04-26 07:52:40 -040049
50void TConnectedClient::run() {
51 if (eventHandler_) {
52 opaqueContext_ = eventHandler_->createContext(inputProtocol_, outputProtocol_);
53 }
54
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020055 for (bool done = false; !done;) {
Jim King5ec805b2015-04-26 07:52:40 -040056 if (eventHandler_) {
57 eventHandler_->processContext(opaqueContext_, client_);
58 }
59
60 try {
61 if (!processor_->process(inputProtocol_, outputProtocol_, opaqueContext_)) {
62 break;
63 }
64 } catch (const TTransportException& ttx) {
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020065 switch (ttx.getType()) {
Jim Kingf793c2b2016-11-10 15:08:21 -050066 case TTransportException::END_OF_FILE:
67 case TTransportException::INTERRUPTED:
68 case TTransportException::TIMED_OUT:
69 // Client disconnected or was interrupted or did not respond within the receive timeout.
70 // No logging needed. Done.
71 done = true;
72 break;
Jim King21b68522015-04-26 18:30:26 -040073
Jim Kingf793c2b2016-11-10 15:08:21 -050074 default: {
75 // All other transport exceptions are logged.
76 // State of connection is unknown. Done.
77 string errStr = string("TConnectedClient died: ") + ttx.what();
78 GlobalOutput(errStr.c_str());
79 done = true;
80 break;
81 }
Jim King5ec805b2015-04-26 07:52:40 -040082 }
83 } catch (const TException& tex) {
Jim King21b68522015-04-26 18:30:26 -040084 string errStr = string("TConnectedClient processing exception: ") + tex.what();
Jim King5ec805b2015-04-26 07:52:40 -040085 GlobalOutput(errStr.c_str());
James E. King, III76567932016-11-14 10:30:05 -050086 // Disconnect from client, because we could not process the message.
87 done = true;
Jim King5ec805b2015-04-26 07:52:40 -040088 }
89 }
90
91 cleanup();
92}
93
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020094void TConnectedClient::cleanup() {
Jim King5ec805b2015-04-26 07:52:40 -040095 if (eventHandler_) {
96 eventHandler_->deleteContext(opaqueContext_, inputProtocol_, outputProtocol_);
97 }
98
99 try {
100 inputProtocol_->getTransport()->close();
101 } catch (const TTransportException& ttx) {
Jim King21b68522015-04-26 18:30:26 -0400102 string errStr = string("TConnectedClient input close failed: ") + ttx.what();
Jim King5ec805b2015-04-26 07:52:40 -0400103 GlobalOutput(errStr.c_str());
104 }
Jim King21b68522015-04-26 18:30:26 -0400105
Jim King5ec805b2015-04-26 07:52:40 -0400106 try {
107 outputProtocol_->getTransport()->close();
108 } catch (const TTransportException& ttx) {
Jim King21b68522015-04-26 18:30:26 -0400109 string errStr = string("TConnectedClient output close failed: ") + ttx.what();
Jim King5ec805b2015-04-26 07:52:40 -0400110 GlobalOutput(errStr.c_str());
111 }
Jim King21b68522015-04-26 18:30:26 -0400112
Jim King5ec805b2015-04-26 07:52:40 -0400113 try {
114 client_->close();
115 } catch (const TTransportException& ttx) {
Jim King21b68522015-04-26 18:30:26 -0400116 string errStr = string("TConnectedClient client close failed: ") + ttx.what();
Jim King5ec805b2015-04-26 07:52:40 -0400117 GlobalOutput(errStr.c_str());
118 }
119}
Jim King5ec805b2015-04-26 07:52:40 -0400120}
121}
122} // apache::thrift::server