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