THRIFT-1348 C++ Qt bindings

Patch: Doug Rosvick
qt-cleanup.patch applied

git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1243124 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Roger Meier 2012-02-11 19:09:30 +00:00
parent 0bab154d9f
commit 19a991528f
4 changed files with 177 additions and 174 deletions

View File

@ -1,13 +1,13 @@
#include "TQIODeviceTransport.h"
#include <QAbstractSocket>
#include <QIODevice>
#include <transport/TBufferTransports.h>
#include <QAbstractSocket>
#include <iostream>
using boost::shared_ptr;
namespace apache { namespace thrift { namespace transport {
using boost::shared_ptr;
TQIODeviceTransport::TQIODeviceTransport(shared_ptr<QIODevice> dev)
: dev_(dev)
@ -42,7 +42,8 @@ namespace apache { namespace thrift { namespace transport {
dev_->close();
}
uint32_t TQIODeviceTransport::readAll(uint8_t* buf, uint32_t len) {
uint32_t TQIODeviceTransport::readAll(uint8_t* buf, uint32_t len)
{
uint32_t requestLen = len;
while (len) {
uint32_t readSize;
@ -57,7 +58,7 @@ namespace apache { namespace thrift { namespace transport {
throw;
}
if (readSize == 0) {
// dev_->waitForReadyRead(50);
dev_->waitForReadyRead(50);
} else {
buf += readSize;
len -= readSize;
@ -77,7 +78,7 @@ namespace apache { namespace thrift { namespace transport {
}
actualSize = (uint32_t)std::min((qint64)len, dev_->bytesAvailable());
readSize = dev_->read(reinterpret_cast<char *>(buf), len);
readSize = dev_->read(reinterpret_cast<char *>(buf), actualSize);
if (readSize < 0) {
QAbstractSocket* socket;
@ -98,7 +99,7 @@ namespace apache { namespace thrift { namespace transport {
while (len) {
uint32_t written = write_partial(buf, len);
len -= written;
// dev_->waitForBytesWritten(50);
dev_->waitForBytesWritten(50);
}
}
@ -151,5 +152,6 @@ namespace apache { namespace thrift { namespace transport {
{
throw TTransportException(TTransportException::UNKNOWN);
}
}}} // apache::thrift::transport

View File

@ -1,37 +1,31 @@
#ifndef _THRIFT_ASYNC_TQIODEVICE_TRANSPORT_H_
#define _THRIFT_ASYNC_TQIODEVICE_TRANSPORT_H_ 1
#include <QIODevice>
#include <boost/shared_ptr.hpp>
#include <transport/TVirtualTransport.h>
namespace apache { namespace thrift { namespace protocol {
class TProtocol;
}}} // apache::thrift::protocol
class QIODevice;
namespace apache { namespace thrift { namespace transport {
/**
* Transport that operates on a QIODevice (socket, file, etc).
*/
class TQIODeviceTransport : public apache::thrift::transport::TVirtualTransport<TQIODeviceTransport> {
public:
explicit TQIODeviceTransport(boost::shared_ptr<QIODevice> dev);
~TQIODeviceTransport();
virtual ~TQIODeviceTransport();
void open();
bool isOpen();
bool peek();
void close();
uint32_t readAll(uint8_t *buf, uint32_t len);
uint32_t read(uint8_t* buf, uint32_t len);
void write(const uint8_t* buf, uint32_t len);
uint32_t write_partial(const uint8_t* buf, uint32_t len);
void flush();
@ -40,6 +34,9 @@ namespace apache { namespace thrift { namespace transport {
void consume(uint32_t len);
private:
TQIODeviceTransport(const TQIODeviceTransport&);
TQIODeviceTransport& operator=(const TQIODeviceTransport&);
boost::shared_ptr<QIODevice> dev_;
};
}}} // apache::thrift::transport

View File

@ -1,11 +1,13 @@
#include "TQTcpServer.h"
#include <protocol/TProtocol.h>
#include <async/TAsyncProcessor.h>
#include "TQTcpServer.h"
#include "TQIODeviceTransport.h"
#include <QTcpSocket>
#include "TQIODeviceTransport.h"
#include <tr1/functional>
#include <protocol/TProtocol.h>
#include <async/TAsyncProcessor.h>
using boost::shared_ptr;
using apache::thrift::protocol::TProtocol;
@ -91,8 +93,7 @@ void TQTcpServer::beginDecode()
QTcpSocket* connection(qobject_cast<QTcpSocket*>(sender()));
Q_ASSERT(connection);
if (ctxMap_.find(connection) == ctxMap_.end())
{
if (ctxMap_.find(connection) == ctxMap_.end()) {
qWarning("[TQTcpServer] Got data on an unknown QTcpSocket");
return;
}
@ -119,8 +120,7 @@ void TQTcpServer::socketClosed()
QTcpSocket* connection(qobject_cast<QTcpSocket*>(sender()));
Q_ASSERT(connection);
if (ctxMap_.find(connection) == ctxMap_.end())
{
if (ctxMap_.find(connection) == ctxMap_.end()) {
qWarning("[TQTcpServer] Unknown QTcpSocket closed");
return;
}
@ -130,8 +130,7 @@ void TQTcpServer::socketClosed()
void TQTcpServer::finish(shared_ptr<ConnectionContext> ctx, bool healthy)
{
if (!healthy)
{
if (!healthy) {
qWarning("[TQTcpServer] Processor failed to process data successfully");
ctxMap_.erase(ctx->connection_.get());
}

View File

@ -6,10 +6,7 @@
#include <boost/shared_ptr.hpp>
#include <tr1/functional>
namespace apache { namespace thrift { namespace protocol {
class TProtocol;
class TProtocolFactory;
}}} // apache::thrift::protocol
@ -17,6 +14,11 @@ namespace apache { namespace thrift { namespace async {
class TAsyncProcessor;
/**
* Server that uses Qt to listen for connections.
* Simply give it a QTcpServer that is listening, along with an async
* processor and a protocol factory, and then run the Qt event loop.
*/
class TQTcpServer : public QObject {
Q_OBJECT
public:
@ -32,6 +34,9 @@ class TQTcpServer : public QObject {
void socketClosed();
private:
TQTcpServer(const TQTcpServer&);
TQTcpServer& operator=(const TQTcpServer&);
class ConnectionContext;
void finish(boost::shared_ptr<ConnectionContext> ctx, bool healthy);