source: trunk/src/testing/QUANTA/include/QUANTAnet_datapack_c.hxx @ 4

Revision 4, 16.5 KB checked in by ajaworski, 13 years ago (diff)

Added modified SAGE sources

Line 
1/******************************************************************************
2 * QUANTA - A toolkit for High Performance Data Sharing
3 * Copyright (C) 2003 Electronic Visualization Laboratory, 
4 * University of Illinois at Chicago
5 *
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 *  * Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 *  * Redistributions in binary form must reproduce the above
14 *    copyright notice, this list of conditions and the following disclaimer
15 *    in the documentation and/or other materials provided with the distribution.
16 *  * Neither the name of the University of Illinois at Chicago nor
17 *    the names of its contributors may be used to endorse or promote
18 *    products derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * Direct questions, comments etc about Quanta to cavern@evl.uic.edu
33 *****************************************************************************/
34
35#ifndef _QUANTA_DATAPACK_C_H
36#define _QUANTA_DATAPACK_C_H
37
38#define FLOAT_SIZE 4
39#define DOUBLE_SIZE 8
40#define INT_SIZE 4
41#if (_MIPS_SZLONG==64)
42#define LONG_SIZE 8
43#define UNSIGNED_LONG_SIZE 8
44#else
45#define LONG_SIZE 4
46#define UNSIGNED_LONG_SIZE 4
47#endif
48#define INT32_SIZE 4
49#define INT64_SIZE 8
50#define CHAR_SIZE 1
51
52typedef int int32;
53
54#ifdef WIN32
55typedef __int64 int64;
56#elif (_MIPS_SZLONG==64)
57typedef long int64;
58#else
59typedef long long int64;
60#endif
61
62#ifdef _WIN32_WCE
63#ifndef __HAVE_WINSOCK2_H
64#include <winsock2.h>
65#define __HAVE_WINSOCK2_H
66#endif /* __HAVE_WINSOCK2_H */
67#endif
68
69/**
70Data packing class. It is basically a glorified memcpy().  The idea is
71that you create an object to help you pack data for transmission over
72networks.
73
74Sending and packing data: First you create a QUANTAnet_datapack_c
75object. Then using the InitPack() method, assign to it a pre-allocated
76memory buffer (SEE BELOW FOR IMPORTANT NOTES.)
77Then using the various QUANTAnet_datapack_c::pack*()
78member functions, you can pack integers, chars, floats, and doubles
79into the buffer. The buffer is now ready for big-endian to
80little-endian transmission. (And vice-versa).
81
82Receiving and unpacking data: Similarly if you receive a buffer of
83data from the network, you assign this buffer to a
84QUANTAnet_datapack_c object using the InitUnpack() method.  Finally,
85we unpack its constituent components using the
86QUANTAnet_datapack_c::unpack*() member functions.
87
88IMPORTANT NOTES:
89
90It is important to compute the length of the buffer using the various
91QUANTAnet_datapack_c::sizeof_*() methods where possible as additional
92buffer space is needed to encode platform-specific information. If in
93doubt always allocate 1 more byte than necessary. The sizeof_() calls
94will make sure that extra byte is included.
95
96To make your application as portable as possible, please take a look at
97packInt32 and packInt64 and counter functions that unpacks the data.
98These functions should be more portable than packInt and packLong functions
99since there is not specification about the size of int and lont int types
100in C or C++ language reference manual.
101
102For example, on SGI, int is going to be treated as 32bits no matter what
103kind of binary format you are using. However, long int is going to be treated
104as 32bits if you use 32 or n32 for your binary format, whereas it would take
10564bits if you use 64 as your binary format. On Win32 and linux running on
106Intel processors, both int and long takes 32bit space.
107
108Finally remember the order in which you packed your data. You need to
109use the same order to unpack them correctly.
110
111@version: 12/1/1999
112
113*/
114
115class QUANTAnet_datapack_c {
116
117
118public:
119        QUANTAnet_datapack_c() {
120                running = 0; size = 0;
121                start = 0;
122        }
123
124       
125        ///Datapack class return values
126        //@{
127        /// Operation went ok
128        static const int OK/* = 0*/;
129        /// Operation failed
130        static const int FAILED/* = -1*/;
131        //@}
132
133        /**
134        Before we do any actual packing, we first call this method to attach the datapack object to some buffer
135
136        @param buffer
137        This buffer provided by the API user is where the packed data will be stored
138
139        @param buffersize
140        The size in bytes of the buffer above
141        */
142        void initPack(char *buffer, int buffersize);
143
144        /**
145        Before we do any actual unpacking, we first call this method to attach the datapack object to some buffer
146
147        @param buffer
148        This buffer where the packed data is stored
149
150        @param buffersize
151        The size in bytes of the buffer above
152        */
153        void initUnpack(char *buffer, int buffersize);
154
155        /**
156        Given a datapack class, this method gives us a pointer to the buffer where the packed data is stored
157
158        @return
159        A pointer to the beginning of the packed data stream
160
161
162        */
163        char *getBuffer() { return (char *)start;}
164
165        /**
166        Gives us the size of the buffer attached to this datapack object
167
168        @return
169        Size in bytes of the attached buffer
170
171        */
172        int getBufferMaxSize() { return size;}
173
174        /**
175        Gives us the size in bytes of available space left in the attached buffer
176
177        @return
178        Size in bytes of the remaining space available in the attached buffer
179        */
180        int getBufferFilledSize() {return running - start;}
181
182        /**
183        Insert a variable of type float into the buffer
184
185        @return
186        OK or FAILED
187
188        @param val
189        The float variable to be packed
190        */
191        int packFloat(float val);
192
193        /**
194        Insert a variable of type int into the buffer
195
196        @return
197        OK or FAILED
198
199        @param val
200        The int variable to be packed
201        */
202        int packInt(int val) ;
203
204        /**
205        Insert a variable of type 32-bit integer into the buffer
206
207        @return
208        OK or FAILED
209
210
211        @param val
212        The 32bit integer variable to be packed
213        */
214        int packInt32(int32 val);
215
216        /**
217        Insert a variable of type long into the buffer. Note that long takes 8
218        bytes when it is compiled with 64-bit compiler on SGI.
219
220        @return
221        OK or FAILED
222
223        @param val
224        The long variable to be packed
225        */
226        int packLong(long val) ;
227
228        /**
229        Insert a variable of type 64-bit integer into the buffer
230
231        @return
232        OK or FAILED
233
234        @param val
235        The 64-bit integer variable to be packed
236        */
237        int packInt64(int64 val);
238
239        /**
240        Insert a variable of type double into the buffer
241
242        @return
243        OK or FAILED
244
245        @param val
246        The double variable to be packed
247        */
248        int packDouble(double val);
249
250
251        #ifdef _WIN32_WCE
252        /**
253        Insert a variable of type char into the buffer
254
255        @return
256        OK or FAILED
257
258        @param val
259        The Unicode char variable to be packed
260        */
261        int packChar(TCHAR val) ;
262        #endif
263
264        /**
265        Insert a variable of type char into the buffer
266
267        @return
268        OK or FAILED
269
270        @param val
271        The char variable to be packed
272        */
273        int packChar(char val) ;
274
275  /** Pack a null-terminated character string.
276      This convenience functions packs a null-terminated character
277      string into the buffer. The length of the string in bytes is
278      internally encoded. This reduces network traffic as only the
279      required number of bytes are sent across the network.
280
281      @param val null-terminated character string to pack
282      @return OK or FAILED
283  */
284  int packString(char* val);
285
286        #ifdef _WIN32_WCE
287        /**
288        Pack raw characters into the buffer
289
290        @return
291        OK or FAILED
292
293        @param val
294        The Unicode char array to be packed
295        @param sz
296        Num chars to pack
297        */
298        int pack(TCHAR* val, int sz) ;
299        #endif 
300
301        /**
302        Pack raw characters into the buffer
303
304        @return
305        OK or FAILED
306
307        @param val
308        The char array to be packed
309        @param sz
310        Num chars to pack
311        */
312        int pack(char* val, int sz) ;
313       
314        /**
315        Pack floats of a float array into the buffer
316
317        @return
318        OK or FAILED
319
320        @param val
321        The float array to be packed
322        @param sz
323        Num floats in the array
324        */
325        int packFloatArray(float* val, int sz) ;
326
327        /**
328        Pack doubles of a double array into the buffer
329
330        @return
331        OK or FAILED
332
333        @param val
334        The double array to be packed
335        @param sz
336        Num doubles in the array
337        */
338        int packDoubleArray(double* val, int sz) ;
339
340        /**
341        Pack an array with int type values into the buffer
342
343        @return
344        OK or FAILED
345
346        @param val
347        The int array to be packed
348        @param sz
349        Num int values in the array
350        */
351        int packIntArray(int* val, int sz) ;
352
353        /**
354        Pack an array with int32 type values into the buffer
355
356        @return
357        OK or FAILED
358
359        @param val
360        The int32 array to be packed
361        @param sz
362        Num int32 values in the array
363        */
364        int packInt32Array(int32* val, int sz) ;
365   
366    /**
367        Pack an array with int64 type values into the buffer
368
369        @return
370        OK or FAILED
371
372        @param val
373        The int64 array to be packed
374        @param sz
375        Num int64 values in the array
376        */
377        int packInt64Array(int64* val, int sz) ;
378
379    /**
380        Pack an array with long type values into the buffer
381
382        @return
383        OK or FAILED
384
385        @param val
386        The long array to be packed
387        @param sz
388        Num long values in the array
389        */
390        int packLongArray(long* val, int sz) ;
391
392    /**
393        Insert a variable of type unsigned long into the buffer. Note that long                 takes 8 bytes when it is compiled with 64-bit compiler on SGI.
394
395        @return
396        OK or FAILED
397
398        @param val
399        The unsigned long variable to be packed
400        */
401
402        int packUnsignedLong(unsigned long val);
403   
404    /**
405        Extract a variable of type float from the buffer
406
407        @return
408        OK or FAILED
409
410        @param Answer
411        A user provided float variable where we store the extracted value
412        */
413        int unpackFloat(float *Answer) ;
414
415        /**
416        Extract a variable of type int from the buffer
417
418        @return
419        OK or FAILED
420
421        @param Answer
422        A user provided int variable where we store the extracted value
423        */
424        int unpackInt(int *Answer) ;
425
426/**
427        Extract a variable of type 32bit integer from the buffer
428
429        @return
430        OK or FAILED
431
432        @param Answer
433        A user provided 32bit integer variable where we store the extracted value
434        */
435        int unpackInt32(int32 *Answer) ;
436
437        /**
438        Extract a variable of type long from the buffer
439
440        @return
441        OK or FAILED
442
443        @param Answer
444        A user provided long variable where we store the extracted value
445        */
446        int unpackLong(long *Answer) ;
447
448        /**
449        Extract a variable of type unsigned long from the buffer
450
451        @return
452        OK or FAILED
453
454        @param Answer
455        A user provided unsigned long variable where we store the extracted value
456        */
457        int unpackUnsignedLong(unsigned long *Answer) ;
458
459
460        /**
461        Extract a variable of type 64-bit integer from the buffer
462
463        @return
464        OK or FAILED
465
466        @param Answer
467        A user provided 64-bit integer variable where we store the extracted value
468        */
469        int unpackInt64(int64 *Answer) ;
470
471        /**
472        Extract a variable of type double from the buffer
473
474        @return
475        OK or FAILED
476
477        @param Answer
478        A user provided double variable where we store the extracted value
479        */
480        int unpackDouble(double *Answer)  ;
481
482
483        #ifdef _WIN32_WCE
484        /**
485        Extract a variable of type char from the buffer
486
487        @return
488        OK or FAILED
489
490        @param Answer
491        A user provided Unicode char variable where we store the extracted value
492        */
493        int unpackChar(TCHAR *Answer) ;
494        #endif
495
496        /**
497        Extract a variable of type char from the buffer
498
499        @return
500        OK or FAILED
501
502        @param Answer
503        A user provided char variable where we store the extracted value
504        */
505        int unpackChar(char *Answer) ;
506
507  /** Pack a null-terminated character string.
508      This convenience functions packs a null-terminated character
509      string into the buffer. The length of the string in bytes is
510      internally encoded when extracting the string. If sz indicates
511      that val does not have enough space to unpack the string, then
512      the unpack function will fail. The unpacked value does include
513      the terminating null character '\\0'.
514
515      @param val character buffer to stored unpacked string.
516      @param sz size in bytes of the val buffer.
517      @return OK or FAILED
518  */
519  int unpackString(char* val, const int& sz);
520
521        #ifdef _WIN32_WCE
522        /**
523        Extract the packed chars from the buffer
524
525        @return
526        OK or FAILED
527
528        @param Answer
529        A user provided Unicode char array where we store the extracted value
530        */
531        int unpack(TCHAR *Answer, int sz) ;
532        #endif
533
534        /**
535        Extract the packed chars from the buffer
536
537        @return
538        OK or FAILED
539
540        @param Answer
541        A user provided char array where we store the extracted value
542  @param sz
543  Size in bytes of the provided char array
544        */
545        int unpack(char *Answer, int sz) ;
546
547        /**
548        Extract the packed float array from the buffer
549
550        @return
551        OK or FAILED
552
553        @param Answer
554        A user provided float array where the extracted floats are stored
555       
556    @param sz
557    The size of the array in which the extracted floats are stored
558    */
559    int unpackFloatArray(float* Answer, int sz);
560   
561        /**
562        Extract the packed double array from the buffer
563
564        @return
565        OK or FAILED
566
567        @param Answer
568        A user provided double array where the extracted doubles are stored
569       
570    @param sz
571    The size of the array in which the extracted doubles are stored
572    */
573    int unpackDoubleArray(double* Answer, int sz);
574   
575        /**
576        Extract the packed int array from the buffer
577
578        @return
579        OK or FAILED
580
581        @param Answer
582        A user provided int array where the extracted int values are stored
583       
584    @param sz
585    The size of the array in which the extracted int values are stored
586    */
587    int unpackIntArray(int* Answer, int sz);
588   
589        /**
590        Extract the packed int32 array from the buffer
591
592        @return
593        OK or FAILED
594
595        @param Answer
596        A user provided array where the extracted int32 values are stored
597       
598    @param sz
599    The size of the array in which the extracted int32 values are stored
600    */
601    int unpackInt32Array(int32* Answer, int sz);
602
603        /**
604        Extract the packed int64 array from the buffer
605
606        @return
607        OK or FAILED
608
609        @param Answer
610        A user provided array where the extracted int64 values are stored
611       
612    @param sz
613    The size of the array in which the extracted int64 values are stored
614    */
615    int unpackInt64Array(int64* Answer, int sz);
616   
617        /**
618        Extract the packed long array from the buffer
619
620        @return
621        OK or FAILED
622
623        @param Answer
624        A user provided array where the extracted long values are stored
625       
626    @param sz
627    The size of the array in which the extracted long values are stored
628    */
629    int unpackLongArray(long* Answer, int sz);
630   
631    /**
632        This function tells us if there is enough incoming_size bytes in the buffer to perform the operation.
633
634        @return OK or FAILED
635
636        @param incoming_size
637        The size in bytes of to be tested.
638        */
639        int checkspace(unsigned int incoming_size);
640
641        /**
642        Gives us a cross-platform safe float size
643       
644        @return
645        The number of bytes in an int (+ 1 byte for machine code storage)
646        */
647        static int sizeof_float(unsigned int cnt=1){return FLOAT_SIZE * cnt;};
648        /**
649        Gives us a cross-platform safe int size
650       
651        @return
652        The number of bytes in an int (+ 1 byte for machine code storage)
653        */
654        static int sizeof_int(unsigned int cnt=1){return INT_SIZE * cnt;};
655
656        /**
657        Gives us a cross-platform safe 64-bit int size
658       
659        @return
660        The number of bytes in an 64-bit int
661        */
662        static int sizeof_int64(unsigned int cnt=1){return INT64_SIZE * cnt;};
663
664        /**
665        Gives us a cross-platform safe 32-bit int size
666       
667        @return
668        The number of bytes in an 32-bit int
669        */
670        static int sizeof_int32(unsigned int cnt=1){return INT32_SIZE * cnt;};
671       
672        /**
673        Gives us a cross-platform safe long size
674       
675        @return
676        The number of bytes in a long (+ 1 byte for machine code storage)
677        */
678        static int sizeof_long(unsigned int cnt=1){return LONG_SIZE * cnt;};
679
680        /**
681        Gives us a cross-platform safe unsigned long size
682       
683        @return
684        The number of bytes in unsigned long (+ 1 byte for machine code storage)
685        */
686        static int sizeof_unsignedLong(unsigned int cnt=1){return UNSIGNED_LONG_SIZE * cnt;};
687
688        /**
689        Gives us a cross-platform safe char size
690       
691        @return
692        The number of bytes in a char 1 byte for machine code storage)
693        */
694        static int sizeof_char(unsigned int cnt=1){return CHAR_SIZE * cnt;};
695
696        /**
697        Gives us a cross-platform safe double size
698       
699        @return
700        The number of bytes in a double (+ 1 byte for machine code storage)
701        */
702        static int sizeof_double(unsigned int cnt=1){return DOUBLE_SIZE * cnt;};
703
704  /** Gives us a cross-platform safe string size encoded with its length.
705      @return The number of bytes used to encode a string.
706  */
707  static int sizeof_string(unsigned int cnt=1)
708    { return sizeof_int() + sizeof_char(cnt); }
709
710
711private:
712        unsigned long size;
713        unsigned char *running;
714        unsigned char *start;
715
716};
717
718#endif
Note: See TracBrowser for help on using the repository browser.