Skip to content

Commit

Permalink
Thrift/Python compact protocol fast extension
Browse files Browse the repository at this point in the history
Summary:
Making use of the c++ classes using cython

This implementation is on par with speeds of fastbinary, and can also preform the work of fast binary.

Test Plan: run unit tests

Reviewed By: davejwatson@fb.com

Subscribers: mshneer, ruibalp, alandau, bmatheny

FB internal diff: D1409955

Tasks: 4605665, 3906100
  • Loading branch information
fried authored and Pavlo Kushnir committed Nov 8, 2014
1 parent a9e46d4 commit 63f59e5
Show file tree
Hide file tree
Showing 4 changed files with 606 additions and 0 deletions.
45 changes: 45 additions & 0 deletions thrift/lib/cpp/transport/TCallbackTransport.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2014 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "thrift/lib/cpp/transport/TCallbackTransport.h"

#include <exception>

using namespace std;

namespace apache { namespace thrift { namespace transport {

uint32_t TCallbackTransport::read(uint8_t* buf, uint32_t len) {
uint32_t rv = read_callback(userData, buf, len);
if (rv < 0) {
throw TTransportException(TTransportException::UNKNOWN,
"TCallbackTransport::read()",
rv);
}
return rv;
}

void TCallbackTransport::write(const uint8_t* buf, uint32_t len) {
uint32_t rv = write_callback(userData, buf, len);

if (rv < 0) {
throw TTransportException(TTransportException::UNKNOWN,
"TCallbackTransport::write()",
rv);
}
}

}}} // apache::thrift::transport
66 changes: 66 additions & 0 deletions thrift/lib/cpp/transport/TCallbackTransport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2014 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef _THRIFT_TRANSPORT_TCALLBACKTRANSPORT_H_
#define _THRIFT_TRANSPORT_TCALLBACKTRANSPORT_H_ 1

#include "TTransport.h"
#include "TVirtualTransport.h"

namespace apache { namespace thrift { namespace transport {

typedef uint32_t (*WriteCallback)(void* userdata, const uint8_t* buf, uint32_t len);
typedef uint32_t (*ReadCallback)(void* userdata, uint8_t* buf, uint32_t len);

/**
* Call some callbacks to do transport
*
*/
class TCallbackTransport : public TVirtualTransport<TCallbackTransport> {
public:

TCallbackTransport() {}

~TCallbackTransport() {}

bool isOpen() { return true; }

void open() {}

void close() {}

uint32_t read(uint8_t* buf, uint32_t len);

void write(const uint8_t* buf, uint32_t len);

void setWriteCallback(WriteCallback callback, void* userdata) {
write_callback = callback;
userData = userdata;
}
void setReadCallback(ReadCallback callback, void* userdata) {
read_callback = callback;
userData = userdata;
}

protected:
void* userData;
WriteCallback write_callback;
ReadCallback read_callback;
};

}}} // apache::thrift::transport

#endif // #ifndef _THRIFT_TRANSPORT_TCALLBACKTRANSPORT_H_

0 comments on commit 63f59e5

Please sign in to comment.