OverSim
cnetcommbuffer.cc
Go to the documentation of this file.
1 //=========================================================================
2 // CMEMCOMMBUFFER.CC - part of
3 //
4 // OMNeT++/OMNEST
5 // Discrete System Simulation in C++
6 //
7 // Written by: Andras Varga, 2003
8 //
9 //=========================================================================
10 
11 /*--------------------------------------------------------------*
12  Copyright (C) 2003-2005 Andras Varga
13  Monash University, Dept. of Electrical and Computer Systems Eng.
14  Melbourne, Australia
15 
16  This file is distributed WITHOUT ANY WARRANTY. See the file
17  `license' for details on this and other legal matters.
18 *--------------------------------------------------------------*/
19 #include <string.h>
20 #include <platdep/sockets.h>
21 #include <stdexcept>
22 #include <omnetpp.h>
23 #include "cnetcommbuffer.h"
24 
25 /*
26 #define STORE(type,d) {*(type *)(mBuffer+mMsgSize)=d; mMsgSize+=sizeof(type);}
27 #define EXTRACT(type,d) {d=*(type *)(mBuffer+mPosition); mPosition+=sizeof(type);}
28 */
29 
30 #define STOREARRAY(type,d,size) {memcpy(mBuffer+mMsgSize,d,size*sizeof(type)); mMsgSize+=size*sizeof(type);}
31 #define EXTRACTARRAY(type,d,size) {\
32  if ((mPosition + size*sizeof(type)) <= (uint32_t)mBufferSize) {\
33  memcpy(d,mBuffer+mPosition,size*sizeof(type)); mPosition+=size*sizeof(type);\
34  } else {\
35  throw cRuntimeError("OverSim cnetcommbuffer.cc: EXTRACTARRAY buffer overflow!");\
36  }\
37 }
38 
39 #define STORE(type,d) {memcpy(mBuffer+mMsgSize,(void*)&d,sizeof(type)); mMsgSize+=sizeof(type);}
40 #define EXTRACT(type,d) {\
41  if ((mPosition + sizeof(type)) <= (uint32_t)mBufferSize) {\
42  memcpy((void*)&d,mBuffer+mPosition,sizeof(type)); mPosition+=sizeof(type);\
43  } else {\
44  throw cRuntimeError("OverSim cnetcommbuffer.cc: EXTRACT buffer overflow!");\
45  }\
46 }
47 
49 {
50 }
51 
53 {
54 }
55 
56 
58 {
59  extendBufferFor(sizeof(char));
60  STORE(char,d);
61 }
62 
63 
64 void cNetCommBuffer::pack(unsigned char d)
65 {
66  extendBufferFor(sizeof(unsigned char));
67  STORE(unsigned char,d);
68 }
69 
70 
72 {
73  extendBufferFor(sizeof(bool));
74  STORE(bool,d);
75 }
76 
77 
78 void cNetCommBuffer::pack(short d)
79 {
80  extendBufferFor(sizeof(short));
81  short d_buf = htons(d);
82  STORE(short,d_buf);
83 }
84 
85 
86 void cNetCommBuffer::pack(unsigned short d)
87 {
88  extendBufferFor(sizeof(unsigned short));
89  unsigned short d_buf = htons(d);
90  STORE(unsigned short,d_buf);
91 }
92 
93 
95 {
96  extendBufferFor(sizeof(int));
97  int d_buf = htonl(d);
98  STORE(int,d_buf);
99 }
100 
101 
102 void cNetCommBuffer::pack(unsigned int d)
103 {
104  extendBufferFor(sizeof(unsigned int));
105  unsigned int d_buf = htonl(d);
106  STORE(unsigned int,d_buf);
107 }
108 
109 
111 {
112  extendBufferFor(sizeof(long));
113  long d_buf = htonl(d);
114  STORE(long,d_buf);
115 }
116 
117 
118 void cNetCommBuffer::pack(unsigned long d)
119 {
120  extendBufferFor(sizeof(unsigned long));
121  unsigned long d_buf = htonl(d);
122  STORE(unsigned long,d_buf);
123 }
124 
125 
126 void cNetCommBuffer::pack(opp_long_long d)
127 {
128  extendBufferFor(sizeof(opp_long_long));
129  STORE(opp_long_long,d);
130 }
131 
132 
133 void cNetCommBuffer::pack(opp_unsigned_long_long d)
134 {
135  extendBufferFor(sizeof(opp_unsigned_long_long));
136  STORE(opp_unsigned_long_long,d);
137 }
138 
139 
140 void cNetCommBuffer::pack(float d)
141 {
142  extendBufferFor(sizeof(float));
143  STORE(float,d);
144 }
145 
146 
147 void cNetCommBuffer::pack(double d)
148 {
149  extendBufferFor(sizeof(double));
150  STORE(double,d);
151 }
152 
153 
154 void cNetCommBuffer::pack(long double d)
155 {
156  extendBufferFor(sizeof(long double));
157  STORE(long double,d);
158 }
159 
160 
161 
162 // pack a string
163 void cNetCommBuffer::pack(const char *d)
164 {
165  int len = d ? strlen(d) : 0;
166 
167  pack(len);
168  extendBufferFor(len*sizeof(char));
169  STOREARRAY(char,d,len);
170 }
171 
172 void cNetCommBuffer::pack(const opp_string& d)
173 {
174  pack(d.c_str());
175 }
176 
177 void cNetCommBuffer::pack(const SimTime *d, int size)
178 {
179  for (int i = 0; i < size; i++)
180  pack(d[i]);
181 }
182 
183 void cNetCommBuffer::pack(const char *d, int size)
184 {
185  extendBufferFor(size*sizeof(char));
186  STOREARRAY(char,d,size);
187 }
188 
189 
190 void cNetCommBuffer::pack(const unsigned char *d, int size)
191 {
192  extendBufferFor(size*sizeof(unsigned char));
193  STOREARRAY(unsigned char,d,size);
194 }
195 
196 
197 void cNetCommBuffer::pack(const bool *d, int size)
198 {
199  extendBufferFor(size*sizeof(bool));
200  STOREARRAY(bool,d,size);
201 }
202 
203 //FIXME: not portable!
204 void cNetCommBuffer::pack(const short *d, int size)
205 {
206  extendBufferFor(size*sizeof(short));
207  STOREARRAY(short,d,size);
208 }
209 
210 
211 //FIXME: not portable!
212 void cNetCommBuffer::pack(const unsigned short *d, int size)
213 {
214  extendBufferFor(size*sizeof(unsigned short));
215  STOREARRAY(unsigned short,d,size);
216 }
217 
218 
219 //FIXME: not portable!
220 void cNetCommBuffer::pack(const int *d, int size)
221 {
222  extendBufferFor(size*sizeof(int));
223  STOREARRAY(int,d,size);
224 }
225 
226 
227 //FIXME: not portable!
228 void cNetCommBuffer::pack(const unsigned int *d, int size)
229 {
230  extendBufferFor(size*sizeof(unsigned int));
231  STOREARRAY(unsigned int,d,size);
232 }
233 
234 
235 //FIXME: not portable!
236 void cNetCommBuffer::pack(const long *d, int size)
237 {
238  extendBufferFor(size*sizeof(long));
239  STOREARRAY(long,d,size);
240 }
241 
242 
243 //FIXME: not portable!
244 void cNetCommBuffer::pack(const unsigned long *d, int size)
245 {
246  extendBufferFor(size*sizeof(unsigned long));
247  STOREARRAY(unsigned long,d,size);
248 }
249 
250 
251 void cNetCommBuffer::pack(const opp_long_long *d, int size)
252 {
253  extendBufferFor(size*sizeof(opp_long_long));
254  STOREARRAY(opp_long_long,d,size);
255 }
256 
257 
258 void cNetCommBuffer::pack(const opp_unsigned_long_long *d, int size)
259 {
260  extendBufferFor(size*sizeof(opp_unsigned_long_long));
261  STOREARRAY(opp_unsigned_long_long,d,size);
262 }
263 
264 
265 void cNetCommBuffer::pack(const float *d, int size)
266 {
267  extendBufferFor(size*sizeof(float));
268  STOREARRAY(float,d,size);
269 }
270 
271 
272 void cNetCommBuffer::pack(const double *d, int size)
273 {
274  extendBufferFor(size*sizeof(double));
275  STOREARRAY(double,d,size);
276 }
277 
278 
279 void cNetCommBuffer::pack(const long double *d, int size)
280 {
281  extendBufferFor(size*sizeof(long double));
282  STOREARRAY(long double,d,size);
283 }
284 
285 
286 // pack string array
287 void cNetCommBuffer::pack(const char **d, int size)
288 {
289  for (int i = 0; i < size; i++)
290  pack(d[i]);
291 }
292 
293 void cNetCommBuffer::pack(const opp_string *d, int size)
294 {
295  for (int i = 0; i < size; i++)
296  pack(d[i]);
297 }
298 
299 void cNetCommBuffer::pack(SimTime d)
300 {
301  pack((opp_long_long)d.raw());
302 }
303 
304 //--------------------------------
305 
307 {
308  EXTRACT(char,d);
309 }
310 
311 
312 void cNetCommBuffer::unpack(unsigned char& d)
313 {
314  EXTRACT(unsigned char,d);
315 }
316 
318 {
319  EXTRACT(bool,d);
320 }
321 
323 {
324  EXTRACT(short,d);
325  d = ntohs(d);
326 }
327 
328 
329 void cNetCommBuffer::unpack(unsigned short& d)
330 {
331  EXTRACT(unsigned short,d);
332  d = ntohs(d);
333 }
334 
335 
337 {
338  EXTRACT(int,d);
339  d = ntohl(d);
340 }
341 
342 
343 void cNetCommBuffer::unpack(unsigned int& d)
344 {
345  EXTRACT(unsigned int,d);
346  d = ntohl(d);
347 }
348 
349 
351 {
352  EXTRACT(long,d);
353  d = ntohl(d);
354 }
355 
356 
357 void cNetCommBuffer::unpack(unsigned long& d)
358 {
359  EXTRACT(unsigned long,d);
360  d = ntohl(d);
361 }
362 
363 
364 void cNetCommBuffer::unpack(opp_long_long& d)
365 {
366  EXTRACT(opp_long_long,d);
367 }
368 
369 
370 void cNetCommBuffer::unpack(opp_unsigned_long_long& d)
371 {
372  EXTRACT(opp_unsigned_long_long,d);
373 }
374 
375 
377 {
378  EXTRACT(float,d);
379 }
380 
381 
382 void cNetCommBuffer::unpack(double& d)
383 {
384  EXTRACT(double,d);
385 }
386 
387 
388 void cNetCommBuffer::unpack(long double& d)
389 {
390  EXTRACT(long double,d);
391 }
392 
393 
394 // unpack a string
395 void cNetCommBuffer::unpack(const char *&d)
396 {
397  int len;
398  unpack(len);
399 
400  char *tmp = new char[len+1];
401  EXTRACTARRAY(char,tmp,len);
402  tmp[len] = '\0';
403  d = tmp;
404 }
405 
406 
407 void cNetCommBuffer::unpack(opp_string& d)
408 {
409  int len;
410  unpack(len);
411 
412  d.reserve(len+1);
413  EXTRACTARRAY(char,d.buffer(),len);
414  d.buffer()[len] = '\0';
415 }
416 
417 
418 void cNetCommBuffer::unpack(SimTime& d)
419 {
420  opp_long_long raw;
421  unpack(raw);
422  d.setRaw(raw);
423 }
424 
425 
426 void cNetCommBuffer::unpack(char *d, int size)
427 {
428  EXTRACTARRAY(char,d,size);
429 }
430 
431 void cNetCommBuffer::unpack(unsigned char *d, int size)
432 {
433  EXTRACTARRAY(unsigned char,d,size);
434 }
435 
436 
437 void cNetCommBuffer::unpack(bool *d, int size)
438 {
439  EXTRACTARRAY(bool,d,size);
440 }
441 
442 void cNetCommBuffer::unpack(short *d, int size)
443 {
444  EXTRACTARRAY(short,d,size);
445 }
446 
447 
448 void cNetCommBuffer::unpack(unsigned short *d, int size)
449 {
450  EXTRACTARRAY(unsigned short,d,size);
451 }
452 
453 
454 void cNetCommBuffer::unpack(int *d, int size)
455 {
456  EXTRACTARRAY(int,d,size);
457 }
458 
459 
460 void cNetCommBuffer::unpack(unsigned int *d, int size)
461 {
462  EXTRACTARRAY(unsigned,d,size);
463 }
464 
465 
466 void cNetCommBuffer::unpack(long *d, int size)
467 {
468  EXTRACTARRAY(long,d,size);
469 }
470 
471 
472 void cNetCommBuffer::unpack(unsigned long *d, int size)
473 {
474  EXTRACTARRAY(unsigned long,d,size);
475 }
476 
477 
478 void cNetCommBuffer::unpack(opp_long_long *d, int size)
479 {
480  EXTRACTARRAY(opp_long_long,d,size);
481 }
482 
483 
484 void cNetCommBuffer::unpack(opp_unsigned_long_long *d, int size)
485 {
486  EXTRACTARRAY(opp_unsigned_long_long,d,size);
487 }
488 
489 
490 void cNetCommBuffer::unpack(float *d, int size)
491 {
492  EXTRACTARRAY(float,d,size);
493 }
494 
495 
496 void cNetCommBuffer::unpack(double *d, int size)
497 {
498  EXTRACTARRAY(double,d,size);
499 }
500 
501 
502 void cNetCommBuffer::unpack(long double *d, int size)
503 {
504  EXTRACTARRAY(long double,d,size);
505 }
506 
507 void cNetCommBuffer::unpack(const char **d, int size)
508 {
509  for (int i = 0; i < size; i++)
510  unpack(d[i]);
511 }
512 
513 void cNetCommBuffer::unpack(opp_string *d, int size)
514 {
515  for (int i = 0; i < size; i++)
516  unpack(d[i]);
517 }
518 
519 
520 void cNetCommBuffer::unpack(SimTime *d, int size)
521 {
522  for (int i = 0; i < size; i++)
523  unpack(d[i]);
524 }
525 
526 
528 {
529  return (mMsgSize - mPosition);
530 }
531 
532 void cNetCommBuffer::packObject(cObject *obj)
533 {
534  pack(obj->getClassName());
535  obj->parsimPack(this);
536 }
537 
539 {
540  char *classname = NULL;
541  cObject *obj = NULL;
542  try {
543  unpack(classname);
544  obj = createOne(classname);
545  obj->parsimUnpack(this);
546  } catch (...) {
547  delete [] classname;
548  delete obj;
549  throw std::invalid_argument("Failed to parse received packet");
550  }
551 
552  delete [] classname;
553  return obj;
554 }
555