我無法找到一個關於如何使用apache thrift通過共享內存進行ipc通信的示例。我的目標是在節儉的幫助下序列化一個存在的類,然後通過共享內存發送到另一個過程,在節儉的幫助下我再次反序列化它。現在我正在使用TMemoryBuffer和TBinaryProtocol來序列化數據。雖然這有效,但我不知道如何將它寫入共享內存。通過共享內存使用Thrift進行IPC通信
這是到目前爲止我的代碼:
#include "test_types.h"
#include "test_constants.h"
#include "thrift/protocol/TBinaryProtocol.h"
#include "thrift/transport/TBufferTransports.h"
int main(int argc, char** argv)
{
int shID;
char* myPtr;
Person* dieter = new Person("Dieter", "Neuer");
//Person* johann = new Person("Johann", "Liebert");
//Car* ford = new Car("KLENW", 4, 4);
PersonThrift dieterThrift;
dieterThrift.nachName = dieter->getNachname();
dieterThrift.vorName = dieter->getVorname();
boost::shared_ptr<apache::thrift::transport::TMemoryBuffer> transport(new apache::thrift::transport::TMemoryBuffer);
boost::shared_ptr<apache::thrift::protocol::TBinaryProtocol> protocol(new apache::thrift::protocol::TBinaryProtocol(transport));
test thriftTest;
thriftTest.personSet.insert(dieterThrift);
u_int32_t size = thriftTest.write(protocol.get());
std::cout << transport.get()->getBufferAsString();
shID = shmget(1000, 100, IPC_CREAT | 0666);
if (shID >= 0)
{
myPtr = (char*)shmat(shID, 0, 0);
if (myPtr==(char *)-1)
{
perror("shmat");
}
else
{
//myPtr = protocol.get();
}
}
getchar();
shmdt(myPtr);
}
的主要問題是部分
//myPtr = protocol.get();
如何使用節儉,這樣我可以寫我的反序列化的數據到myPtr(並由此進入共享內存)。我想TMemoryBuffer可能已經不是個好主意了。正如你可能看到的,我對此並不是很有經驗。
親切的問候和感謝提前
邁克爾