Hi all,
I am new to thrift.
I play a bit with it on PHP.
First, I create a struct like this
struct Book {
1: i32 isbn,
2: string name
}
Then, generate the Book class with thrift 0.6.1
Next, I use the script to test.
<?php
$GLOBALS['THRIFT_ROOT'] = __DIR__ . '/lib/vendor/thrift';
require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';
require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinarySerializer.php';
require_once __DIR__ . '/gen-php/book/book_types.php';
$aBook = new Book(array('isbn' => 123456, 'name' => 'Programming PHP'));
$bin = TBinarySerializer::serialize($aBook);
$theBook = TBinarySerializer::deserialize($bin, 'Book');
print_r($theBook);
echo "\n";
?>
Then I get this error.
PHP Fatal error: Uncaught exception 'TTransportException' with message
'TMemoryBuffer: Could not read 1 bytes from buffer.' in
/Users/nevill/play-thrfit/lib/vendor/thrift/transport/TMemoryBuffer.php:58
After spending some hours on reading source code, transport/*, protocol/*, I
find it's because when using TBufferedTransport to serialize to binary, it
doesn't write to the underlying transport TMemoryBuffer at the end of the
process.
So, I fix it like this in TBinarySerializer.php
public static function serialize($object) {
$transport = new TMemoryBuffer();
$protocol = new TBinaryProtocolAccelerated($transport);
if (function_exists('thrift_protocol_write_binary')) {
...
} else {
$object->write($protocol);
$protocol->flush(); // <----------------------------------- Here is
the fix
}
return $transport->getBuffer();
}
Then, everything works as I expected.
============================================================
My question is
Do I find a bug or I use the library in a wrong way ?
--
Best Regards
- Nevill