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
58 // Create a AliHLTMessage object for storing objects. The "what" integer
59 // describes the type of message. Predifined ROOT system message types
60 // can be found in MessageTypes.h. Make sure your own message types are
61 // unique from the ROOT defined message types (i.e. 0 - 10000 are
62 // reserved by ROOT). In case you OR "what" with kMESS_ACK, the message
63 // will wait for an acknowledgement from the remote side. This makes
64 // the sending process synchronous. In case you OR "what" with kMESS_ZIP,
65 // the message will be compressed in TSocket using the zip algorithm
66 // (only if message is > 256 bytes).
68 // 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),
92 // Create a AliHLTMessage object for reading objects. The objects will be
93 // read from buf. Use the What() method to get the message type.
95 // skip space at the beginning of the message reserved for the message length
96 fBufCur += sizeof(UInt_t);
100 if (fWhat & kMESS_ZIP) {
101 // if buffer has kMESS_ZIP set, move it to fBufComp and uncompress
103 fBufCompCur = fBuffer + bufsize;
108 if (fWhat == kMESS_OBJECT) {
110 fClass = ReadClass(); // get first the class stored in message
111 SetBufferOffset(sizeof(UInt_t) + sizeof(fWhat));
118 //______________________________________________________________________________
119 AliHLTMessage::~AliHLTMessage()
121 // Clean up compression buffer.
125 //______________________________________________________________________________
126 void AliHLTMessage::Forward()
128 // Change a buffer that was received into one that can be send, i.e.
129 // forward a just received message.
133 SetBufferOffset(fBufSize);
141 //______________________________________________________________________________
142 void AliHLTMessage::Reset()
144 // Reset the message buffer so we can use (i.e. fill) it again.
146 SetBufferOffset(sizeof(UInt_t) + sizeof(fWhat));
157 //______________________________________________________________________________
158 void AliHLTMessage::SetLength() const
160 // Set the message length at the beginning of the message buffer.
163 char *buf = Buffer();
164 *((UInt_t*)buf) = (UInt_t)(Length() - sizeof(UInt_t));
168 *((UInt_t*)buf) = (UInt_t)(Length() - sizeof(UInt_t));
173 //______________________________________________________________________________
174 void AliHLTMessage::SetWhat(UInt_t what)
176 // Using this method one can change the message type a-posteriory.
177 // In case you OR "what" with kMESS_ACK, the message will wait for
178 // an acknowledgement from the remote side. This makes the sending
179 // process synchronous.
183 char *buf = Buffer();
184 buf += sizeof(UInt_t); // skip reserved length space
189 buf += sizeof(UInt_t); // skip reserved length space
190 tobuf(buf, what | kMESS_ZIP);
194 //______________________________________________________________________________
195 void AliHLTMessage::SetCompressionLevel(Int_t level)
197 // Set the message compression level. Can be between 0 and 9 with 0
198 // being no compression and 9 maximum compression. In general the default
199 // level of 1 is the best compromise between achieved compression and
200 // cpu time. Compression will only happen when the message is > 256 bytes.
202 if (level < 0) level = 0;
203 if (level > 9) level = 9;
205 if (level != fCompress && fBufComp) {
214 //______________________________________________________________________________
215 Int_t AliHLTMessage::Compress()
217 // Compress the message. The message will only be compressed if the
218 // compression level > 0 and the if the message is > 256 bytes.
219 // Returns -1 in case of error (when compression fails or
220 // when the message increases in size in some pathological cases),
221 // otherwise returns 0.
223 if (fCompress == 0) {
224 // no compression specified
234 if (fBufComp && fCompPos == fBufCur) {
235 // the message was already compressed
239 // remove any existing compressed buffer before compressing modified message
247 if (Length() <= (Int_t)(256 + 2*sizeof(UInt_t))) {
248 // this message is too small to be compressed
252 Int_t hdrlen = 2*sizeof(UInt_t);
253 Int_t messlen = Length() - hdrlen;
254 Int_t nbuffers = messlen / kMAXBUF;
255 Int_t chdrlen = 3*sizeof(UInt_t); // compressed buffer header length
256 Int_t buflen = TMath::Max(512, chdrlen + messlen + 9*nbuffers);
257 fBufComp = new char[buflen];
258 char *messbuf = Buffer() + hdrlen;
259 char *bufcur = fBufComp + chdrlen;
263 for (Int_t i = 0; i <= nbuffers; i++) {
265 bufmax = messlen - nzip;
268 R__zip(fCompress, &bufmax, messbuf, &bufmax, bufcur, &nout);
269 if (nout == 0 || nout >= messlen) {
270 //this happens when the buffer cannot be compressed
282 fBufCompCur = bufcur;
286 tobuf(bufcur, (UInt_t)(CompLength() - sizeof(UInt_t)));
287 Int_t what = fWhat | kMESS_ZIP;
289 tobuf(bufcur, Length()); // original uncompressed buffer length
294 //______________________________________________________________________________
295 Int_t AliHLTMessage::Uncompress()
297 // Uncompress the message. The message will only be uncompressed when
298 // kMESS_ZIP is set. Returns -1 in case of error, 0 otherwise.
300 if (!fBufComp || !(fWhat & kMESS_ZIP))
304 Int_t hdrlen = 2*sizeof(UInt_t);
305 UChar_t *bufcur = (UChar_t*)fBufComp + hdrlen;
306 frombuf((char *&)bufcur, &buflen);
307 fBuffer = new char[buflen];
309 fBufCur = fBuffer + sizeof(UInt_t) + sizeof(fWhat);
310 fBufMax = fBuffer + fBufSize;
311 char *messbuf = fBuffer + hdrlen;
313 Int_t nin, nout, nbuf;
316 nin = 9 + ((Int_t)bufcur[3] | ((Int_t)bufcur[4] << 8) | ((Int_t)bufcur[5] << 16));
317 nbuf = (Int_t)bufcur[6] | ((Int_t)bufcur[7] << 8) | ((Int_t)bufcur[8] << 16);
318 R__unzip(&nin, bufcur, &nbuf, messbuf, &nout);
321 if (noutot >= buflen - hdrlen) break;