]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/cpp/src/thrift/qt/TQIODeviceTransport.cpp
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
20 #include <thrift/qt/TQIODeviceTransport.h>
22 #include <QAbstractSocket>
25 #include <thrift/transport/TBufferTransports.h>
31 using std::shared_ptr
;
35 TQIODeviceTransport::TQIODeviceTransport(shared_ptr
<QIODevice
> dev
) : dev_(dev
) {
38 TQIODeviceTransport::~TQIODeviceTransport() {
42 void TQIODeviceTransport::open() {
44 throw TTransportException(TTransportException::NOT_OPEN
,
45 "open(): underlying QIODevice isn't open");
49 bool TQIODeviceTransport::isOpen() const {
50 return dev_
->isOpen();
53 bool TQIODeviceTransport::peek() {
54 return dev_
->bytesAvailable() > 0;
57 void TQIODeviceTransport::close() {
61 uint32_t TQIODeviceTransport::readAll(uint8_t* buf
, uint32_t len
) {
62 uint32_t requestLen
= len
;
66 readSize
= read(buf
, len
);
68 if (len
!= requestLen
) {
69 // something read already
70 return requestLen
- len
;
72 // error but nothing read yet
76 dev_
->waitForReadyRead(50);
85 uint32_t TQIODeviceTransport::read(uint8_t* buf
, uint32_t len
) {
89 if (!dev_
->isOpen()) {
90 throw TTransportException(TTransportException::NOT_OPEN
,
91 "read(): underlying QIODevice is not open");
94 actualSize
= (uint32_t)(std::min
)((qint64
)len
, dev_
->bytesAvailable());
95 readSize
= dev_
->read(reinterpret_cast<char*>(buf
), actualSize
);
98 QAbstractSocket
* socket
;
99 if ((socket
= qobject_cast
<QAbstractSocket
*>(dev_
.get()))) {
100 throw TTransportException(TTransportException::UNKNOWN
,
101 "Failed to read() from QAbstractSocket",
104 throw TTransportException(TTransportException::UNKNOWN
, "Failed to read from from QIODevice");
107 return (uint32_t)readSize
;
110 void TQIODeviceTransport::write(const uint8_t* buf
, uint32_t len
) {
112 uint32_t written
= write_partial(buf
, len
);
114 dev_
->waitForBytesWritten(50);
118 uint32_t TQIODeviceTransport::write_partial(const uint8_t* buf
, uint32_t len
) {
121 if (!dev_
->isOpen()) {
122 throw TTransportException(TTransportException::NOT_OPEN
,
123 "write_partial(): underlying QIODevice is not open");
126 written
= dev_
->write(reinterpret_cast<const char*>(buf
), len
);
128 QAbstractSocket
* socket
;
129 if ((socket
= qobject_cast
<QAbstractSocket
*>(dev_
.get()))) {
130 throw TTransportException(TTransportException::UNKNOWN
,
131 "write_partial(): failed to write to QAbstractSocket",
135 throw TTransportException(TTransportException::UNKNOWN
,
136 "write_partial(): failed to write to underlying QIODevice");
139 return (uint32_t)written
;
142 void TQIODeviceTransport::flush() {
143 if (!dev_
->isOpen()) {
144 throw TTransportException(TTransportException::NOT_OPEN
,
145 "flush(): underlying QIODevice is not open");
148 QAbstractSocket
* socket
;
150 if ((socket
= qobject_cast
<QAbstractSocket
*>(dev_
.get()))) {
153 dev_
->waitForBytesWritten(1);
157 uint8_t* TQIODeviceTransport::borrow(uint8_t* buf
, uint32_t* len
) {
163 void TQIODeviceTransport::consume(uint32_t len
) {
165 throw TTransportException(TTransportException::UNKNOWN
);
169 } // apache::thrift::transport