1 /*
2    Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 #ifndef PACKER_HPP
26 #define PACKER_HPP
27 
28 #include <TransporterDefinitions.hpp>
29 #include "TransporterInternalDefinitions.hpp"
30 
31 #ifdef WORDS_BIGENDIAN
32   #define MY_OWN_BYTE_ORDER 1
33 #else
34   #define MY_OWN_BYTE_ORDER 0
35 #endif
36 
37 class Packer {
38   Uint32 preComputedWord1;
39   Uint32 checksumUsed;     // Checksum shall be included in the message
40   Uint32 signalIdUsed;     // Senders signal id shall be included in the message
41 public:
42   Packer(bool signalId, bool checksum);
43 
44   Uint32 getMessageLength(const SignalHeader* header,
45 			  const LinearSectionPtr ptr[3]) const ;
46 
47 
48   Uint32 getMessageLength(const SignalHeader* header,
49 			  const SegmentedSectionPtr ptr[3]) const ;
50 
51 
52   Uint32 getMessageLength(const SignalHeader* header,
53 			  const GenericSectionPtr ptr[3]) const ;
54 
55   void pack(Uint32 * insertPtr,
56 	    Uint32 prio,
57 	    const SignalHeader* header,
58 	    const Uint32* data,
59 	    const LinearSectionPtr ptr[3]) const ;
60 
61   void pack(Uint32 * insertPtr,
62 	    Uint32 prio,
63 	    const SignalHeader* header,
64 	    const Uint32* data,
65 	    class SectionSegmentPool & thePool,
66 	    const SegmentedSectionPtr ptr[3]) const ;
67 
68   void pack(Uint32 * insertPtr,
69 	    Uint32 prio,
70 	    const SignalHeader* header,
71 	    const Uint32* data,
72 	    const GenericSectionPtr ptr[3]) const ;
73 };
74 
75 inline
76 Uint32
getMessageLength(const SignalHeader * header,const LinearSectionPtr ptr[3]) const77 Packer::getMessageLength(const SignalHeader* header,
78 			 const LinearSectionPtr ptr[3]) const {
79   Uint32 tLen32 = header->theLength;
80   Uint32 no_seg = header->m_noOfSections;
81   tLen32 += checksumUsed;
82   tLen32 += signalIdUsed;
83   tLen32 += no_seg;
84 
85   for(Uint32 i = 0; i<no_seg; i++){
86     tLen32 += ptr[i].sz;
87   }
88 
89   return (tLen32 * 4) + sizeof(Protocol6);
90 }
91 
92 inline
93 Uint32
getMessageLength(const SignalHeader * header,const SegmentedSectionPtr ptr[3]) const94 Packer::getMessageLength(const SignalHeader* header,
95 			 const SegmentedSectionPtr ptr[3]) const {
96   Uint32 tLen32 = header->theLength;
97   Uint32 no_seg = header->m_noOfSections;
98   tLen32 += checksumUsed;
99   tLen32 += signalIdUsed;
100   tLen32 += no_seg;
101 
102   for(Uint32 i = 0; i<no_seg; i++){
103     tLen32 += ptr[i].sz;
104   }
105 
106   return (tLen32 * 4) + sizeof(Protocol6);
107 }
108 
109 inline
110 Uint32
getMessageLength(const SignalHeader * header,const GenericSectionPtr ptr[3]) const111 Packer::getMessageLength(const SignalHeader* header,
112 			 const GenericSectionPtr ptr[3]) const {
113   Uint32 tLen32 = header->theLength;
114   Uint32 no_seg = header->m_noOfSections;
115   tLen32 += checksumUsed;
116   tLen32 += signalIdUsed;
117   tLen32 += no_seg;
118 
119   for(Uint32 i = 0; i<no_seg; i++){
120     tLen32 += ptr[i].sz;
121   }
122 
123   return (tLen32 * 4) + sizeof(Protocol6);
124 }
125 
126 #endif
127