Skip to content

Instantly share code, notes, and snippets.

@eguiraud
Created August 31, 2015 09:31
Show Gist options
  • Select an option

  • Save eguiraud/2a50777d32d89019a221 to your computer and use it in GitHub Desktop.

Select an option

Save eguiraud/2a50777d32d89019a221 to your computer and use it in GitHub Desktop.
some snippets that send and receive C++ objects using ROOT streaming capabilities
#include "TApplication.h"
#include "TClass.h"
#include "TInterpreter.h"
#include "TBufferFile.h"
#include <utility>
#include <iostream>
#include "StreamMe.h"
/* no streaming is done here, I just write a class to a TBufferFile and try to read it
with another TBufferFile. */
int main() {
StreamMe a(123);
a.b = {1,2,3};
//get class info
gInterpreter->ProcessLine("#include \"StreamMe.h\"");
TClass *c = TClass::GetClass("StreamMe");
//write class on buffer
TBufferFile wBuf(TBuffer::kWrite);
wBuf.WriteObjectAny(&a, c);
//read class from buffer
TBufferFile rBuf(TBuffer::kRead, wBuf.BufferSize(), wBuf.Buffer(), false);
auto r = (StreamMe*)rBuf.ReadObjectAny(c);
return 0;
}
#include "TSocket.h"
#include "TBufferFile.h"
#include "TInterpreter.h"
#include "TApplication.h"
#include "TClass.h"
#include "StreamMe.h"
#include <iostream>
int main() {
TApplication app("app", nullptr, nullptr);
gInterpreter->ProcessLine("#include \"StreamMe.h\"");
auto c = TClass::GetClass("StreamMe");
//connect and receive
TSocket *s = new TSocket("localhost", 9090);
void *lengthBuf = malloc(1024);
std::cout << "received " << s->RecvRaw(lengthBuf, sizeof(ULong_t)) << " bytes" << std::endl;
TBufferFile rBuf(TBuffer::kRead, sizeof(ULong_t), lengthBuf);
ULong_t classBufSize;
rBuf.ReadULong(classBufSize);
std::cout << "classBufSize " << classBufSize << std::endl;
void *classBuf = malloc(classBufSize);
std::cout << "received " << s->RecvRaw(classBuf, classBufSize) << " bytes" << std::endl;
TBufferFile rBuf2(TBuffer::kRead, classBufSize, classBuf);
auto r = (StreamMe*)rBuf2.ReadObjectAny(c);
if(!r)
std::cout << "ERROR; object == nullptr";
else {
std::cout << r->a << std::endl;
std::cout << r->b.front() << std::endl;
std::cout << r->b.back() << std::endl;
}
return 0;
}
#include "TSocket.h"
#include "TServerSocket.h"
#include "TBufferFile.h"
#include "TInterpreter.h"
#include "TApplication.h"
#include "TClass.h"
#include "StreamMe.h"
#include <iostream>
int main() {
TApplication app("app", nullptr, nullptr);
gInterpreter->ProcessLine("#include \"StreamMe.h\"");
//connect
TServerSocket ss(9090, kTRUE);
TSocket *s = ss.Accept();
//send object
StreamMe a(123);
for(unsigned i=0; i<2000; ++i)
a.b.push_back(i);
TClass *c = TClass::GetClass("StreamMe");
TBufferFile classBuf(TBuffer::kWrite);
classBuf.WriteObjectAny(&a, c);
TBufferFile wBuf(TBuffer::kWrite);
wBuf.WriteULong(classBuf.BufferSize());
wBuf.WriteBuf(classBuf.Buffer(), classBuf.BufferSize());
std::cout << "sending " << s->SendRaw(wBuf.Buffer(), wBuf.BufferSize()) << " bytes" << std::endl;
return 0;
}
#ifndef _STREAMME_
#define _STREAMME_
struct StreamMe {
explicit StreamMe(int _a) : a(_a) {};
StreamMe() : StreamMe(1) {};
int a;
std::vector<int> b;
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment