KDIS  2-8-x
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
KDataStream.h
Go to the documentation of this file.
1 /*********************************************************************
2 Copyright 2013 Karl Jones
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation
12  and/or other materials provided with the distribution.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 
25 For Further Information Please Contact me at
26 Karljj1@yahoo.com
27 http://p.sf.net/kdis/UserGuide
28 *********************************************************************/
29 
30 /********************************************************************
31  class: KDataStream
32  created: 2008/09/22
33  author: Karl Jones
34 
35  purpose: Converts data types into a buffer stream for sending
36  via a network/writing to file.
37 *********************************************************************/
38 
39 #pragma once
40 
41 #include "./KDefines.h"
42 #include "./KEncodersDecoders.h"
43 #include "./Extras/KUtils.h"
44 #include <vector>
45 
46 namespace KDIS {
47 
49 {
50 private:
51 
52  Endian m_MachineEndian;
53 
54  Endian m_NetEndian;
55 
56  std::vector<KUOCTET> m_vBuffer;
57  KUINT16 m_ui16CurrentWritePos;
58 
59 public:
60 
61  // All DIS data is sent in Big Endian format
62  KDataStream( Endian NetworkEndian = Big_Endian );
63 
64  KDataStream( KOCTET * SerialData, KUINT16 DataSize, Endian NetworkEndian = Big_Endian );
65 
66  ~KDataStream();
67 
68  //************************************
69  // FullName: KDIS::KDataStream::GetMachineEndian
70  //!Description: Returns the machine endian. Calculated automatically.
71  //************************************
72  Endian GetMachineEndian() const;
73 
74  //************************************
75  // FullName: KDIS::KDataStream::GetNetWorkEndian
76  //!Description: Returns the network endian, set by user in
77  //! constructor, Big_Endian by default.
78  //************************************
79  Endian GetNetWorkEndian() const;
80 
81  //************************************
82  // FullName: KDIS::KDataStream::GetBufferSize
83  //!Description: Returns current buffer size in Octets/Bytes
84  //************************************
85  KUINT16 GetBufferSize() const;
86 
87  //************************************
88  // FullName: KDIS::KDataStream::CopyIntoBuffer
89  //!Description: Copy the stream into an Octet buffer,
90  //! Returns total bytes copied into buffer.
91  //! Throws exception if the buffer is too small.
92  // Parameter: KOCTET * Buffer
93  // Parameter: KUINT16 BufferSize
94  // Parameter: KUINT16 WritePos - Where to start writing into the buffer
95  //************************************
96  KUINT16 CopyIntoBuffer( KOCTET * Buffer, KUINT16 BufferSize, KUINT16 WritePos = 0 ) const throw( KException );
97 
98  //************************************
99  // FullName: KDIS::KDataStream::CopyFromBuffer
100  //!Description: Copy data from a buffer/array into the data stream
101  // Parameter: KOCTET * SerialData
102  // Parameter: KUINT16 DataSize
103  // Parameter: Endian NetworkEndian = Big_Endian
104  //************************************
105  void CopyFromBuffer( const KOCTET * SerialData, KUINT16 DataSize, Endian NetworkEndian = Big_Endian );
106 
107  //************************************
108  // FullName: KDIS::KDataStream::GetBufferPtr
109  //!Description: Returns a pointer to the buffer.
110  //! This is a more efficient way of sending data than using CopyIntoBuffer.
111  //************************************
112  const KOCTET * GetBufferPtr() const;
113 
114  //************************************
115  // FullName: KDIS::KDataStream::GetBuffer
116  //!Description: Returns a constant reference to the internal buffer.
117  //! Useful if you need lower-level access to the data.
118  //************************************
119  const std::vector<KUOCTET> & GetBuffer() const;
120 
121  //************************************
122  // FullName: KDIS::KDataStream::ResetWritePosition
123  //!Description: Moves the write position back to the start of the buffer.
124  //************************************
125  void ResetWritePosition();
126 
127  //************************************
128  // FullName: KDIS::KDataStream::SetCurrentWritePosition
129  // KDIS::KDataStream::GetCurrentWritePosition
130  //!Description: The write position is the current position in the buffer that we are reading data from.
131  //! Using these 2 functions it is possible to "peak" at data and then restore the buffers
132  //! write position. For example if we wanted to read the next 4 bytes to determine what the
133  //! next data type is we could peak and then reset the write position back by 4 so that the
134  //! data can be re-read by the data types decode function.
135  // Parameter: KUINT16 WP
136  //************************************
137  void SetCurrentWritePosition( KUINT16 WP );
138  KUINT16 GetCurrentWritePosition() const;
139 
140  //************************************
141  // FullName: KDIS::KDataStream::Clear
142  //!Description: Clears contents
143  //************************************
144  void Clear();
145 
146  //************************************
147  // FullName: KDIS::KDataStream::GetAsString
148  //!Description: Returns string representation of the stream, values are in hex.
149  //************************************
150  KString GetAsString() const;
151 
152  //************************************
153  // FullName: KDIS::KDataStream::GetAsString
154  //!Description: Read a string of hex octets and convert into
155  //! a data stream.
156  //! This function works in conjunction with GetAsString, each PDU could be saved to a
157  //! file and then read back in using this function. Good for debugging or logging data.
158  // Parameter: const KString & S
159  //************************************
160  void ReadFromString( const KString & S );
161 
162  //************************************
163  // FullName: KDIS::KDataStream<Type>::Write
164  //!Description: Write data into stream.
165  // Parameter: Type T, KUOCTET V, KOCTET V
166  //************************************
167  template<class Type>
168  void Write( Type T );
169  void Write( KUOCTET V );
170  void Write( KOCTET V );
171 
172  //************************************
173  // FullName: KDIS::KDataStream<Type>::Write
174  //!Description: Read data from stream.
175  // Parameter: Type & T, KUOCTET & V, KOCTET & V
176  //************************************
177  template<class Type>
178  void Read( Type & T );
179  void Read( KUOCTET & V );
180  void Read( KOCTET & V );
181 
182  // Write into stream
183  template<class Type>
184  KDataStream & operator << ( Type T );
185  KDataStream & operator << ( KDataStream val );
186 
187  // Read from stream
188  template<class Type>
189  KDataStream & operator >> ( Type & T );
190 
191  KBOOL operator == ( const KDataStream & Value ) const;
192  KBOOL operator != ( const KDataStream & Value ) const;
193 };
194 
195 //////////////////////////////////////////////////////////////////////////
196 // Template Operators
197 //////////////////////////////////////////////////////////////////////////
198 
199 template<class Type>
200 void KDataStream::Write( Type T )
201 {
202  KBOOL bSwapBytes;
203  if( m_MachineEndian == m_NetEndian )bSwapBytes = false;
204  else bSwapBytes = true;
205 
206  NetToDataType<Type> OctArray( T, bSwapBytes );
207 
208  for( KUINT8 i = 0; i < sizeof T; ++i )
209  {
210  m_vBuffer.push_back( OctArray.m_Octs[i] );
211  }
212 }
213 
214 //////////////////////////////////////////////////////////////////////////
215 
216 template<class Type>
217 void KDataStream::Read( Type & T )
218 {
219  NetToDataType<Type> OctArray( T, false );
220 
221  // Copy octets into data type
222  for( KUINT8 i = 0; i < sizeof T; ++i, ++m_ui16CurrentWritePos )
223  {
224  OctArray.m_Octs[i] = m_vBuffer[m_ui16CurrentWritePos];
225  }
226 
227  if( m_MachineEndian != m_NetEndian )
228  {
229  OctArray.SwapBytes();
230  }
231 
232  T = OctArray.m_Value;
233 }
234 
235 //////////////////////////////////////////////////////////////////////////
236 
237 template<class Type>
239 {
240  Write( T );
241  return *this;
242 }
243 
244 //////////////////////////////////////////////////////////////////////////
245 
246 template<class Type>
248 {
249  Read( T );
250  return *this;
251 }
252 
253 //////////////////////////////////////////////////////////////////////////
254 
255 } // END namespace KDIS
256 
KDataStream & operator>>(Type &T)
Definition: KDataStream.h:247
void SwapBytes()
Definition: KEncodersDecoders.h:61
unsigned short int KUINT16
Definition: KDefines.h:101
Definition: KEncodersDecoders.h:51
void Read(Type &T)
Description: Read data from stream.
Definition: KDataStream.h:217
DataType m_Value
Definition: KEncodersDecoders.h:57
unsigned char KUOCTET
Definition: KDefines.h:109
Definition: EnumEntityInfoInteraction.h:773
Definition: KDefines.h:182
Definition: KDefines.h:228
Definition: KDataStream.h:48
bool KBOOL
Definition: KDefines.h:119
KOCTET m_Octs[sizeof(DataType)]
Definition: KEncodersDecoders.h:55
std::string KString
Definition: KDefines.h:116
KDataStream & operator<<(Type T)
Definition: KDataStream.h:238
Endian
Definition: KDefines.h:225
unsigned char KUINT8
Definition: KDefines.h:99
char KOCTET
Definition: KDefines.h:108
#define KDIS_EXPORT
Definition: KDefines.h:82
void Write(Type T)
Description: Write data into stream.
Definition: KDataStream.h:200