David Reiss | 1ffb61b | 2008-04-08 05:07:26 +0000 | [diff] [blame] | 1 | #include <cstdlib> |
| 2 | #include <stdexcept> |
| 3 | #include <Thrift.h> |
| 4 | #include <transport/TTransportUtils.h> |
David Reiss | 28f298d | 2008-05-01 06:17:36 +0000 | [diff] [blame^] | 5 | #include <transport/TBufferTransports.h> |
David Reiss | 1ffb61b | 2008-04-08 05:07:26 +0000 | [diff] [blame] | 6 | using namespace std; |
| 7 | using boost::shared_ptr; |
| 8 | using facebook::thrift::transport::TTransportException; |
| 9 | using facebook::thrift::transport::TPipedTransport; |
| 10 | using facebook::thrift::transport::TMemoryBuffer; |
| 11 | |
| 12 | int main() { |
| 13 | shared_ptr<TMemoryBuffer> underlying(new TMemoryBuffer); |
| 14 | shared_ptr<TMemoryBuffer> pipe(new TMemoryBuffer); |
| 15 | shared_ptr<TPipedTransport> trans(new TPipedTransport(underlying, pipe)); |
| 16 | |
| 17 | uint8_t buffer[4]; |
| 18 | |
| 19 | underlying->write((uint8_t*)"abcd", 4); |
| 20 | trans->readAll(buffer, 2); |
| 21 | assert( string((char*)buffer, 2) == "ab" ); |
| 22 | trans->readEnd(); |
| 23 | assert( pipe->getBufferAsString() == "ab" ); |
| 24 | pipe->resetBuffer(); |
| 25 | underlying->write((uint8_t*)"ef", 2); |
| 26 | trans->readAll(buffer, 2); |
| 27 | assert( string((char*)buffer, 2) == "cd" ); |
| 28 | trans->readAll(buffer, 2); |
| 29 | assert( string((char*)buffer, 2) == "ef" ); |
| 30 | trans->readEnd(); |
| 31 | assert( pipe->getBufferAsString() == "cdef" ); |
| 32 | |
| 33 | return 0; |
| 34 | |
| 35 | } |