3 /** @file AliHLTMessage.cxx
4 @author Matthias Richter (customization of Root TMessage )
6 @brief Serialization of Root objects in the ALICE HLT. */
8 // This is the original Root TMessage implementation with a few minor
9 // modifications, original revision:
10 // root/net: v5-14-00 $: TMessage.cxx,v 1.6 2004/05/07 09:51:58 brun
11 // Author: Fons Rademakers 19/12/96
13 /*************************************************************************
14 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
15 * All rights reserved. *
17 * For the licensing terms see $ROOTSYS/LICENSE. *
18 * For the list of contributors see $ROOTSYS/README/CREDITS. *
19 *************************************************************************/
21 //////////////////////////////////////////////////////////////////////////
25 // Message buffer class used for serializing objects and sending them //
26 // over a network. This class inherits from TBuffer the basic I/O //
29 //////////////////////////////////////////////////////////////////////////
31 #include "AliHLTMessage.h"
35 extern "C" void R__zip (Int_t cxlevel, Int_t *nin, char *bufin, Int_t *lout, char *bufout, Int_t *nout);
36 extern "C" void R__unzip(Int_t *nin, UChar_t *bufin, Int_t *lout, char *bufout, Int_t *nout);
37 const Int_t kMAXBUF = 0xffffff;
40 ClassImp(AliHLTMessage)
42 //______________________________________________________________________________
43 AliHLTMessage::AliHLTMessage(UInt_t what)
45 # ifdef ROOT_TBufferFile
52 // Create a AliHLTMessage object for storing objects. The "what" integer
53 // describes the type of message. Predifined ROOT system message types
54 // can be found in MessageTypes.h. Make sure your own message types are
55 // unique from the ROOT defined message types (i.e. 0 - 10000 are
56 // reserved by ROOT). In case you OR "what" with kMESS_ACK, the message
57 // will wait for an acknowledgement from the remote side. This makes
58 // the sending process synchronous. In case you OR "what" with kMESS_ZIP,
59 // the message will be compressed in TSocket using the zip algorithm
60 // (only if message is > 256 bytes).
62 // space at the beginning of the message reserved for the message length
76 //______________________________________________________________________________
77 AliHLTMessage::AliHLTMessage(void *buf, Int_t bufsize)
79 # if defined(ROOT_TBufferFile) || defined(HAVE_TBUFFERFILE_H)
80 TBufferFile(kRead, bufsize, buf, 0),
82 TBuffer(kRead, bufsize, buf, 0),
86 // Create a AliHLTMessage object for reading objects. The objects will be
87 // read from buf. Use the What() method to get the message type.
89 // skip space at the beginning of the message reserved for the message length
90 fBufCur += sizeof(UInt_t);
99 if (fWhat & kMESS_ZIP) {
100 // if buffer has kMESS_ZIP set, move it to fBufComp and uncompress
102 fBufCompCur = fBuffer + bufsize;
107 if (fWhat == kMESS_OBJECT) {
109 fClass = ReadClass(); // get first the class stored in message
110 SetBufferOffset(sizeof(UInt_t) + sizeof(fWhat));
117 //______________________________________________________________________________
118 AliHLTMessage::~AliHLTMessage()
120 // Clean up compression buffer.
124 //______________________________________________________________________________
125 void AliHLTMessage::Forward()
127 // Change a buffer that was received into one that can be send, i.e.
128 // forward a just received message.
132 SetBufferOffset(fBufSize);
140 //______________________________________________________________________________
141 void AliHLTMessage::Reset()
143 // Reset the message buffer so we can use (i.e. fill) it again.
145 SetBufferOffset(sizeof(UInt_t) + sizeof(fWhat));
156 //______________________________________________________________________________
157 void AliHLTMessage::SetLength() const
159 // Set the message length at the beginning of the message buffer.
162 char *buf = Buffer();
163 *((UInt_t*)buf) = (UInt_t)(Length() - sizeof(UInt_t));
167 *((UInt_t*)buf) = (UInt_t)(Length() - sizeof(UInt_t));
172 //______________________________________________________________________________
173 void AliHLTMessage::SetWhat(UInt_t what)
175 // Using this method one can change the message type a-posteriory.
176 // In case you OR "what" with kMESS_ACK, the message will wait for
177 // an acknowledgement from the remote side. This makes the sending
178 // process synchronous.
182 char *buf = Buffer();
183 buf += sizeof(UInt_t); // skip reserved length space
188 buf += sizeof(UInt_t); // skip reserved length space
189 tobuf(buf, what | kMESS_ZIP);
193 //______________________________________________________________________________
194 void AliHLTMessage::SetCompressionLevel(Int_t level)
196 // Set the message compression level. Can be between 0 and 9 with 0
197 // being no compression and 9 maximum compression. In general the default
198 // level of 1 is the best compromise between achieved compression and
199 // cpu time. Compression will only happen when the message is > 256 bytes.
201 if (level < 0) level = 0;
202 if (level > 9) level = 9;
204 if (level != fCompress && fBufComp) {
213 //______________________________________________________________________________
214 Int_t AliHLTMessage::Compress()
216 // Compress the message. The message will only be compressed if the
217 // compression level > 0 and the if the message is > 256 bytes.
218 // Returns -1 in case of error (when compression fails or
219 // when the message increases in size in some pathological cases),
220 // otherwise returns 0.
222 if (fCompress == 0) {
223 // no compression specified
233 if (fBufComp && fCompPos == fBufCur) {
234 // the message was already compressed
238 // remove any existing compressed buffer before compressing modified message
246 if (Length() <= (Int_t)(256 + 2*sizeof(UInt_t))) {
247 // this message is too small to be compressed
251 Int_t hdrlen = 2*sizeof(UInt_t);
252 Int_t messlen = Length() - hdrlen;
253 Int_t nbuffers = messlen / kMAXBUF;
254 Int_t chdrlen = 3*sizeof(UInt_t); // compressed buffer header length
255 Int_t buflen = TMath::Max(512, chdrlen + messlen + 9*nbuffers);
256 fBufComp = new char[buflen];
257 char *messbuf = Buffer() + hdrlen;
258 char *bufcur = fBufComp + chdrlen;
262 for (Int_t i = 0; i <= nbuffers; i++) {
264 bufmax = messlen - nzip;
267 R__zip(fCompress, &bufmax, messbuf, &bufmax, bufcur, &nout);
268 if (nout == 0 || nout >= messlen) {
269 //this happens when the buffer cannot be compressed
281 fBufCompCur = bufcur;
285 tobuf(bufcur, (UInt_t)(CompLength() - sizeof(UInt_t)));
286 Int_t what = fWhat | kMESS_ZIP;
288 tobuf(bufcur, Length()); // original uncompressed buffer length
293 //______________________________________________________________________________
294 Int_t AliHLTMessage::Uncompress()
296 // Uncompress the message. The message will only be uncompressed when
297 // kMESS_ZIP is set. Returns -1 in case of error, 0 otherwise.
299 if (!fBufComp || !(fWhat & kMESS_ZIP))
303 Int_t hdrlen = 2*sizeof(UInt_t);
304 UChar_t *bufcur = (UChar_t*)fBufComp + hdrlen;
305 frombuf((char *&)bufcur, &buflen);
306 fBuffer = new char[buflen];
308 fBufCur = fBuffer + sizeof(UInt_t) + sizeof(fWhat);
309 fBufMax = fBuffer + fBufSize;
310 char *messbuf = fBuffer + hdrlen;
312 Int_t nin, nout, nbuf;
315 nin = 9 + ((Int_t)bufcur[3] | ((Int_t)bufcur[4] << 8) | ((Int_t)bufcur[5] << 16));
316 nbuf = (Int_t)bufcur[6] | ((Int_t)bufcur[7] << 8) | ((Int_t)bufcur[8] << 16);
317 R__unzip(&nin, bufcur, &nbuf, messbuf, &nout);
320 if (noutot >= buflen - hdrlen) break;