Roger Meier | 7699b40 | 2012-04-08 18:18:44 +0000 | [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 | */ |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 19 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame^] | 20 | #include <functional> |
| 21 | #include <memory> |
| 22 | |
Roger Meier | 4285ba2 | 2013-06-10 21:17:23 +0200 | [diff] [blame] | 23 | #include <thrift/qt/TQTcpServer.h> |
| 24 | #include <thrift/qt/TQIODeviceTransport.h> |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 25 | |
Sebastian Zenker | e917a27 | 2016-01-18 08:45:52 +0100 | [diff] [blame] | 26 | #include <QMetaType> |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 27 | #include <QTcpSocket> |
| 28 | |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 29 | #include <thrift/protocol/TProtocol.h> |
| 30 | #include <thrift/async/TAsyncProcessor.h> |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 31 | |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 32 | using apache::thrift::protocol::TProtocol; |
| 33 | using apache::thrift::protocol::TProtocolFactory; |
| 34 | using apache::thrift::transport::TTransport; |
| 35 | using apache::thrift::transport::TTransportException; |
| 36 | using apache::thrift::transport::TQIODeviceTransport; |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame^] | 37 | using std::bind; |
| 38 | using std::function; |
| 39 | using std::placeholders::_1; |
| 40 | using std::shared_ptr; |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 41 | |
| 42 | QT_USE_NAMESPACE |
| 43 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 44 | namespace apache { |
| 45 | namespace thrift { |
| 46 | namespace async { |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 47 | |
| 48 | struct TQTcpServer::ConnectionContext { |
| 49 | shared_ptr<QTcpSocket> connection_; |
| 50 | shared_ptr<TTransport> transport_; |
| 51 | shared_ptr<TProtocol> iprot_; |
| 52 | shared_ptr<TProtocol> oprot_; |
| 53 | |
| 54 | explicit ConnectionContext(shared_ptr<QTcpSocket> connection, |
| 55 | shared_ptr<TTransport> transport, |
| 56 | shared_ptr<TProtocol> iprot, |
| 57 | shared_ptr<TProtocol> oprot) |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 58 | : connection_(connection), transport_(transport), iprot_(iprot), oprot_(oprot) {} |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | TQTcpServer::TQTcpServer(shared_ptr<QTcpServer> server, |
| 62 | shared_ptr<TAsyncProcessor> processor, |
| 63 | shared_ptr<TProtocolFactory> pfact, |
| 64 | QObject* parent) |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 65 | : QObject(parent), server_(server), processor_(processor), pfact_(pfact) { |
Sebastian Zenker | e917a27 | 2016-01-18 08:45:52 +0100 | [diff] [blame] | 66 | qRegisterMetaType<QTcpSocket*>("QTcpSocket*"); |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 67 | connect(server.get(), SIGNAL(newConnection()), SLOT(processIncoming())); |
| 68 | } |
| 69 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 70 | TQTcpServer::~TQTcpServer() { |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 73 | void TQTcpServer::processIncoming() { |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 74 | while (server_->hasPendingConnections()) { |
| 75 | // take ownership of the QTcpSocket; technically it could be deleted |
| 76 | // when the QTcpServer is destroyed, but any real app should delete this |
| 77 | // class before deleting the QTcpServer that we are using |
| 78 | shared_ptr<QTcpSocket> connection(server_->nextPendingConnection()); |
Roger Meier | 8b51bc6 | 2014-07-24 23:33:33 +0200 | [diff] [blame] | 79 | |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 80 | shared_ptr<TTransport> transport; |
| 81 | shared_ptr<TProtocol> iprot; |
| 82 | shared_ptr<TProtocol> oprot; |
Roger Meier | 8b51bc6 | 2014-07-24 23:33:33 +0200 | [diff] [blame] | 83 | |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 84 | try { |
| 85 | transport = shared_ptr<TTransport>(new TQIODeviceTransport(connection)); |
| 86 | iprot = shared_ptr<TProtocol>(pfact_->getProtocol(transport)); |
| 87 | oprot = shared_ptr<TProtocol>(pfact_->getProtocol(transport)); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 88 | } catch (...) { |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 89 | qWarning("[TQTcpServer] Failed to initialize transports/protocols"); |
| 90 | continue; |
| 91 | } |
Roger Meier | 8b51bc6 | 2014-07-24 23:33:33 +0200 | [diff] [blame] | 92 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 93 | ctxMap_[connection.get()] |
| 94 | = shared_ptr<ConnectionContext>(new ConnectionContext(connection, transport, iprot, oprot)); |
Roger Meier | 8b51bc6 | 2014-07-24 23:33:33 +0200 | [diff] [blame] | 95 | |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 96 | connect(connection.get(), SIGNAL(readyRead()), SLOT(beginDecode())); |
Roger Meier | 8b51bc6 | 2014-07-24 23:33:33 +0200 | [diff] [blame] | 97 | |
Sebastian Zenker | e917a27 | 2016-01-18 08:45:52 +0100 | [diff] [blame] | 98 | connect(connection.get(), SIGNAL(disconnected()), SLOT(socketClosed())); |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 102 | void TQTcpServer::beginDecode() { |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 103 | QTcpSocket* connection(qobject_cast<QTcpSocket*>(sender())); |
| 104 | Q_ASSERT(connection); |
| 105 | |
Roger Meier | 19a9915 | 2012-02-11 19:09:30 +0000 | [diff] [blame] | 106 | if (ctxMap_.find(connection) == ctxMap_.end()) { |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 107 | qWarning("[TQTcpServer] Got data on an unknown QTcpSocket"); |
| 108 | return; |
| 109 | } |
Roger Meier | 8b51bc6 | 2014-07-24 23:33:33 +0200 | [diff] [blame] | 110 | |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 111 | shared_ptr<ConnectionContext> ctx = ctxMap_[connection]; |
Roger Meier | 8b51bc6 | 2014-07-24 23:33:33 +0200 | [diff] [blame] | 112 | |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 113 | try { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 114 | processor_ |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 115 | ->process(bind(&TQTcpServer::finish, this, ctx, _1), |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 116 | ctx->iprot_, |
| 117 | ctx->oprot_); |
| 118 | } catch (const TTransportException& ex) { |
| 119 | qWarning("[TQTcpServer] TTransportException during processing: '%s'", ex.what()); |
Sebastian Zenker | e917a27 | 2016-01-18 08:45:52 +0100 | [diff] [blame] | 120 | scheduleDeleteConnectionContext(connection); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 121 | } catch (...) { |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 122 | qWarning("[TQTcpServer] Unknown processor exception"); |
Sebastian Zenker | e917a27 | 2016-01-18 08:45:52 +0100 | [diff] [blame] | 123 | scheduleDeleteConnectionContext(connection); |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 127 | void TQTcpServer::socketClosed() { |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 128 | QTcpSocket* connection(qobject_cast<QTcpSocket*>(sender())); |
| 129 | Q_ASSERT(connection); |
Sebastian Zenker | e917a27 | 2016-01-18 08:45:52 +0100 | [diff] [blame] | 130 | scheduleDeleteConnectionContext(connection); |
| 131 | } |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 132 | |
Sebastian Zenker | e917a27 | 2016-01-18 08:45:52 +0100 | [diff] [blame] | 133 | void TQTcpServer::deleteConnectionContext(QTcpSocket* connection) { |
| 134 | const ConnectionContextMap::size_type deleted = ctxMap_.erase(connection); |
| 135 | if (0 == deleted) { |
| 136 | qWarning("[TQTcpServer] Unknown QTcpSocket"); |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 137 | } |
Sebastian Zenker | e917a27 | 2016-01-18 08:45:52 +0100 | [diff] [blame] | 138 | } |
Roger Meier | 8b51bc6 | 2014-07-24 23:33:33 +0200 | [diff] [blame] | 139 | |
Sebastian Zenker | e917a27 | 2016-01-18 08:45:52 +0100 | [diff] [blame] | 140 | void TQTcpServer::scheduleDeleteConnectionContext(QTcpSocket* connection) { |
| 141 | QMetaObject::invokeMethod(this, "deleteConnectionContext", Qt::QueuedConnection, Q_ARG(QTcpSocket*, connection)); |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 144 | void TQTcpServer::finish(shared_ptr<ConnectionContext> ctx, bool healthy) { |
Roger Meier | 19a9915 | 2012-02-11 19:09:30 +0000 | [diff] [blame] | 145 | if (!healthy) { |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 146 | qWarning("[TQTcpServer] Processor failed to process data successfully"); |
Sebastian Zenker | e917a27 | 2016-01-18 08:45:52 +0100 | [diff] [blame] | 147 | deleteConnectionContext(ctx->connection_.get()); |
Roger Meier | 86e8986 | 2012-02-10 19:53:20 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | } // apache::thrift::async |