26 #include <boost/version.hpp>
27 #if defined (_WIN32) && defined (_MSC_VER) && BOOST_VERSION != 102800 //wierd error in MSVC
28 # define __LINE__NOMSC 0
30 # define __LINE__NOMSC __LINE__
47 if (sz >= header_length) {
51 if (h.data_length > sz) {
52 COUT<<
"Packet not correctly received, not enough data for buffer"<<endl
53 <<
" should be still "<<h.data_length
54 <<
" but buffer has only "<<sz<<endl;
58 if (packet_uncompress( _packet,
59 (
const unsigned char*) buffer,
68 COUT<<
"Parsed a packet with"
69 <<
" cmd="<<
Cmd( h.command )<<
"("<<(
int) h.command<<
")"
72 <<
" len="<<h.data_length
77 COUT<<
"Packet not correctly received, not enough data for header"<<endl;
84 if (buffer.
len() >= header_length) {
86 size_t sz = buffer.
len();
88 if (h.data_length > sz) {
89 COUT<<
"Packet not correctly received, not enough data for buffer"<<endl
90 <<
" should be still "<<h.data_length
91 <<
" but buffer has only "<<sz<<endl;
96 if (packet_uncompress( _packet,
102 COUT<<
"Received compressed packet, but compiled without zlib"<<endl;
109 COUT<<
"Parsed a packet with"
110 <<
" cmd="<<
Cmd( h.command )<<
"("<<(
int) h.command<<
")"
112 <<
" ts="<<h.timestamp
113 <<
" len="<<h.data_length
118 COUT<<
"Packet not correctly received, not enough data for header"<<endl;
138 h.command = a.h.command;
139 h.serial = a.h.serial;
140 h.timestamp = a.h.timestamp;
141 h.data_length = a.h.data_length;
153 const char *caller_file,
157 create( cmd, nserial, buf, length, prio, caller_file, caller_line );
158 return send( sock, dst );
161 void Packet::create(
Cmd cmd,
166 const char *caller_file,
170 unsigned int microtime;
174 microtime = (
unsigned int) ( floor( curtime*1000 ) );
175 h.timestamp = microtime;
178 COUT<<
"enter "<<__PRETTY_FUNCTION__<<endl
179 <<
" *** from "<<caller_file<<
":"<<caller_line<<endl
180 <<
" *** create "<<cmd<<
" ser="<<nserial<<
", "<<length
181 <<
" *** curtime "<<curtime
182 <<
" microtime "<<microtime
183 <<
" timestamp "<<h.timestamp<<endl;
194 bool packet_filled =
false;
197 #ifdef USE_COMPRESSED_PACKETS
200 sz = length+(length/10)+15+header_length;
202 char *
c =
new char[sz];
203 unsigned long clen_l = length;
205 unsigned char *dest = (
unsigned char*) &c[header_length+
sizeof (ulen_i)];
208 zlib_errcode = ::compress2( dest, &clen_l, (
unsigned char*) buf, length, 9 );
209 if (zlib_errcode == Z_OK) {
210 if (clen_l < length+2) {
211 ulen_i = htonl( (
unsigned int) length );
214 h.data_length = clen_l+
sizeof (ulen_i);
218 COUT<<
"Created a compressed packet of length "
219 <<h.data_length+header_length<<
" for sending"<<endl;
222 _packet.
set( c, h.data_length+header_length, PacketMem::TakeOwnership );
223 packet_filled =
true;
226 COUT<<
"Compressing "<<cmd
227 <<
" packet refused - bigger than original"<<std::endl;
240 if (packet_filled ==
false) {
241 h.flags &= (~COMPRESSED);
242 h.data_length = length;
244 char *c =
new char[length+header_length];
247 _packet.
set( c, length+header_length, PacketMem::TakeOwnership );
249 COUT<<
"Created a packet of length "
250 <<length+header_length<<
" for sending"<<endl;
258 cout<<
"*** "<<file<<
":"<<line<<
" "<<endl;
259 cout<<
"*** Packet display -- Command : "<<
Cmd( h.command )
260 <<
" - Serial : "<<h.serial<<
" - Flags : "<<h.flags<<endl;
262 cout<<
"*** Buffer : "<<endl;
263 _packet.
dump( cout, 4 );
269 cout<<
"Packet : "<<h.command<<
" | "<<h.serial<<
" | ";
271 for (
size_t i = 0;
i < _packet.
len();
i++)
276 void Packet::Header::ntoh(
const void *buf )
279 const Header *
h = (
const Header*) buf;
280 command = h->command;
281 serial = ntohs( h->serial );
282 timestamp = ntohl( h->timestamp );
283 data_length = ntohl( h->data_length );
284 flags = ntohs( h->flags );
287 void Packet::Header::hton(
char *buf )
290 Header *
h = (Header*) buf;
291 h->command = command;
292 h->serial = htons( serial );
293 h->timestamp = htonl( timestamp );
294 h->data_length = htonl( data_length );
295 h->flags = htons( flags );
303 COUT<<
"sending "<<
Cmd( h.command )<<
" through "<<dst_s<<
" to "
306 COUT<<
"sending "<<
Cmd( h.command )<<
" through "<<dst_s<<
" to "
312 if ( ( ret = dst_s.
sendbuf(
this, dst_a, h.flags ) ) == -1 ) {
314 perror(
"Error sending packet " );
315 cout<<
Cmd( h.command )<<endl;
318 COUT<<
"After successful sendbuf"<<endl;
339 if ( h.data_length > _packet.
len() )
340 return _packet.
len();
342 return h.data_length;
353 return h.data_length+header_length;
357 bool Packet::packet_uncompress(
PacketMem &outpacket,
const unsigned char *src,
size_t sz, Header &header )
361 unsigned long ulen_l;
364 src += header_length;
365 ulen_i = ntohl( *(
unsigned int*) src );
366 src +=
sizeof (ulen_i);
367 sz -=
sizeof (ulen_i);
371 dest = (
unsigned char*) mem.getVarBuf();
372 dest += header_length;
375 zlib_errcode = ::uncompress( dest, &ulen_l, src, sz );
376 if (zlib_errcode != Z_OK) {
377 COUT<<
"Compressed packet not correctly received, "
378 <<
"decompression failed with zlib errcode "
379 <<zlib_errcode<<endl;
381 }
else if (ulen_l != ulen_i) {
382 COUT<<
"Compressed packet not correctly received, "
383 <<
"expected len "<<ulen_i<<
", "
384 <<
"received len "<<ulen_l<<endl;
388 header.data_length = ulen_i;
391 COUT<<
"Parsed a compressed packet with"
392 <<
" cmd="<<
Cmd( header.command )<<
"("<<(
int) header.command<<
")"
393 <<
" ser="<<header.serial
394 <<
" ts="<<header.timestamp
395 <<
" len="<<header.data_length
402 bool Packet::packet_uncompress(
PacketMem&,
const unsigned char*,
size_t, Header& )