ROOT  6.06/09
Reference Guide
MPSendRecv.cxx
Go to the documentation of this file.
1 #include "MPSendRecv.h"
2 #include "TBufferFile.h"
3 #include "MPCode.h"
4 #include <memory> //unique_ptr
5 
6 //////////////////////////////////////////////////////////////////////////
7 /// Send a message with the specified code on the specified socket.
8 /// This standalone function can be used to send a code
9 /// on a given socket. It does not check whether the socket connection is
10 /// in a valid state. The message code can then be retrieved via MPRecv().\n
11 /// **Note:** only objects the headers of which have been parsed by
12 /// cling can be sent by MPSend(). User-defined types can be made available to
13 /// cling via a call like `gSystem->ProcessLine("#include \"header.h\"")`.
14 /// Pointer types are not supported (with the exception of const char*),
15 /// but the user can simply dereference the pointer and send the
16 /// pointed object instead.\n
17 /// **Note:** for readability, codes should be enumerated as in EMPCode.\n
18 /// \param s a pointer to a valid TSocket. No validity checks are performed\n
19 /// \param code the code to be sent
20 /// \return the number of bytes sent, as per TSocket::SendRaw
21 int MPSend(TSocket *s, unsigned code)
22 {
24  wBuf.WriteUInt(code);
25  wBuf.WriteULong(0);
26  return s->SendRaw(wBuf.Buffer(), wBuf.Length());
27 }
28 
29 
30 //////////////////////////////////////////////////////////////////////////
31 /// Receive message from a socket.
32 /// This standalone function can be used to read a message that
33 /// has been sent via MPSend(). The smart pointer contained in the returned
34 /// ::MPCodeBufPair is null if the message does not contain an object,
35 /// otherwise it points to a TBufferFile.
36 /// To retrieve the object from the buffer different methods must be used
37 /// depending on the type of the object to be read:\n
38 /// * non-pointer built-in types: TBufferFile::operator>> must be used\n
39 /// * c-strings: TBufferFile::ReadString must be used\n
40 /// * class types: TBufferFile::ReadObjectAny must be used\n
41 /// \param s a pointer to a valid TSocket. No validity checks are performed\n
42 /// \return ::MPCodeBufPair, i.e. an std::pair containing message code and (possibly) object
44 {
45  char *rawbuf = new char[sizeof(UInt_t)];
46  //receive message code
47  unsigned nBytes = s->RecvRaw(rawbuf, sizeof(UInt_t));
48  if (nBytes == 0) {
49  return std::make_pair(MPCode::kRecvError, nullptr);
50  }
51  //read message code
52  TBufferFile bufReader(TBuffer::kRead, sizeof(UInt_t), rawbuf, false);
53  unsigned code;
54  bufReader.ReadUInt(code);
55  delete [] rawbuf;
56 
57  //receive object size
58  rawbuf = new char[sizeof(ULong_t)];
59  s->RecvRaw(rawbuf, sizeof(ULong_t));
60  bufReader.SetBuffer(rawbuf, sizeof(ULong_t), false);
61  ULong_t classBufSize;
62  bufReader.ReadULong(classBufSize);
63  delete [] rawbuf;
64 
65  //receive object if needed
66  std::unique_ptr<TBufferFile> objBuf; //defaults to nullptr
67  if (classBufSize != 0) {
68  char *classBuf = new char[classBufSize];
69  s->RecvRaw(classBuf, classBufSize);
70  objBuf.reset(new TBufferFile(TBuffer::kRead, classBufSize, classBuf, true)); //the buffer is deleted by TBuffer's dtor
71  }
72 
73  return std::make_pair(code, std::move(objBuf));
74 }
The concrete implementation of TBuffer for writing/reading to/from a ROOT file or socket...
Definition: TBufferFile.h:51
MPCodeBufPair MPRecv(TSocket *s)
Receive message from a socket.
Definition: MPSendRecv.cxx:43
int MPSend(TSocket *s, unsigned code)
Send a message with the specified code on the specified socket.
Definition: MPSendRecv.cxx:21
Error while reading from the socket.
Definition: MPCode.h:34
virtual void ReadUInt(UInt_t &i)
Definition: TBufferFile.h:462
virtual void WriteULong(ULong_t l)
Definition: TBufferFile.h:385
virtual Int_t SendRaw(const void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Send a raw buffer of specified length.
Definition: TSocket.cxx:620
char * Buffer() const
Definition: TBuffer.h:91
virtual void WriteUInt(UInt_t i)
Definition: TBufferFile.h:371
unsigned int UInt_t
Definition: RtypesCore.h:42
std::pair< unsigned, std::unique_ptr< TBufferFile >> MPCodeBufPair
An std::pair that wraps the code and optional object contained in a message.
Definition: MPSendRecv.h:20
unsigned long ULong_t
Definition: RtypesCore.h:51
Int_t Length() const
Definition: TBuffer.h:94
virtual Int_t RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Receive a raw buffer of specified length bytes.
Definition: TSocket.cxx:901
virtual void ReadULong(ULong_t &l)
Definition: TBufferFile.h:476
void SetBuffer(void *buf, UInt_t bufsiz=0, Bool_t adopt=kTRUE, ReAllocCharFun_t reallocfunc=0)
Sets a new buffer in an existing TBuffer object.
Definition: TBuffer.cxx:163