]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/cpp/src/thrift/qt/TQIODeviceTransport.cpp
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / cpp / src / thrift / qt / TQIODeviceTransport.cpp
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 #include <thrift/qt/TQIODeviceTransport.h>
21
22 #include <QAbstractSocket>
23 #include <QIODevice>
24
25 #include <thrift/transport/TBufferTransports.h>
26 #include <memory>
27
28 namespace apache {
29 namespace thrift {
30
31 using std::shared_ptr;
32
33 namespace transport {
34
35 TQIODeviceTransport::TQIODeviceTransport(shared_ptr<QIODevice> dev) : dev_(dev) {
36 }
37
38 TQIODeviceTransport::~TQIODeviceTransport() {
39 dev_->close();
40 }
41
42 void TQIODeviceTransport::open() {
43 if (!isOpen()) {
44 throw TTransportException(TTransportException::NOT_OPEN,
45 "open(): underlying QIODevice isn't open");
46 }
47 }
48
49 bool TQIODeviceTransport::isOpen() const {
50 return dev_->isOpen();
51 }
52
53 bool TQIODeviceTransport::peek() {
54 return dev_->bytesAvailable() > 0;
55 }
56
57 void TQIODeviceTransport::close() {
58 dev_->close();
59 }
60
61 uint32_t TQIODeviceTransport::readAll(uint8_t* buf, uint32_t len) {
62 uint32_t requestLen = len;
63 while (len) {
64 uint32_t readSize;
65 try {
66 readSize = read(buf, len);
67 } catch (...) {
68 if (len != requestLen) {
69 // something read already
70 return requestLen - len;
71 }
72 // error but nothing read yet
73 throw;
74 }
75 if (readSize == 0) {
76 dev_->waitForReadyRead(50);
77 } else {
78 buf += readSize;
79 len -= readSize;
80 }
81 }
82 return requestLen;
83 }
84
85 uint32_t TQIODeviceTransport::read(uint8_t* buf, uint32_t len) {
86 uint32_t actualSize;
87 qint64 readSize;
88
89 if (!dev_->isOpen()) {
90 throw TTransportException(TTransportException::NOT_OPEN,
91 "read(): underlying QIODevice is not open");
92 }
93
94 actualSize = (uint32_t)(std::min)((qint64)len, dev_->bytesAvailable());
95 readSize = dev_->read(reinterpret_cast<char*>(buf), actualSize);
96
97 if (readSize < 0) {
98 QAbstractSocket* socket;
99 if ((socket = qobject_cast<QAbstractSocket*>(dev_.get()))) {
100 throw TTransportException(TTransportException::UNKNOWN,
101 "Failed to read() from QAbstractSocket",
102 socket->error());
103 }
104 throw TTransportException(TTransportException::UNKNOWN, "Failed to read from from QIODevice");
105 }
106
107 return (uint32_t)readSize;
108 }
109
110 void TQIODeviceTransport::write(const uint8_t* buf, uint32_t len) {
111 while (len) {
112 uint32_t written = write_partial(buf, len);
113 len -= written;
114 dev_->waitForBytesWritten(50);
115 }
116 }
117
118 uint32_t TQIODeviceTransport::write_partial(const uint8_t* buf, uint32_t len) {
119 qint64 written;
120
121 if (!dev_->isOpen()) {
122 throw TTransportException(TTransportException::NOT_OPEN,
123 "write_partial(): underlying QIODevice is not open");
124 }
125
126 written = dev_->write(reinterpret_cast<const char*>(buf), len);
127 if (written < 0) {
128 QAbstractSocket* socket;
129 if ((socket = qobject_cast<QAbstractSocket*>(dev_.get()))) {
130 throw TTransportException(TTransportException::UNKNOWN,
131 "write_partial(): failed to write to QAbstractSocket",
132 socket->error());
133 }
134
135 throw TTransportException(TTransportException::UNKNOWN,
136 "write_partial(): failed to write to underlying QIODevice");
137 }
138
139 return (uint32_t)written;
140 }
141
142 void TQIODeviceTransport::flush() {
143 if (!dev_->isOpen()) {
144 throw TTransportException(TTransportException::NOT_OPEN,
145 "flush(): underlying QIODevice is not open");
146 }
147
148 QAbstractSocket* socket;
149
150 if ((socket = qobject_cast<QAbstractSocket*>(dev_.get()))) {
151 socket->flush();
152 } else {
153 dev_->waitForBytesWritten(1);
154 }
155 }
156
157 uint8_t* TQIODeviceTransport::borrow(uint8_t* buf, uint32_t* len) {
158 (void)buf;
159 (void)len;
160 return nullptr;
161 }
162
163 void TQIODeviceTransport::consume(uint32_t len) {
164 (void)len;
165 throw TTransportException(TTransportException::UNKNOWN);
166 }
167 }
168 }
169 } // apache::thrift::transport