blob: 2b94e71c269ef6448c401b8e6e9cf31d4b3a8ea5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
/*!
* \file CNetworkStream.h
* \author S. Eisenhauer
* \date 12.01.2012
* \brief Stream access to network data buffers
*
* This file defines classes for accessing the data stored in networked buffers as streams
*
*/
#ifndef CNETWORKSTREAM_H_
#define CNETWORKSTREAM_H_
#include "global.h"
#include "protocol.h"
/// enumeration of stream errors
enum tenStreamError
{
nenStreamOK = 0, //!< stream is ok
nenErrorValueOutOfRange = -1, //!< a value in the stream is out of the configured min. max. range
nenErrorStreamToShort = -2 //!< no bytes left in stream for reading or writing
};
/*!
* \brief class for read access to a data buffer as stream
*/
class CNetworkInStream
{
public:
/*!
* \brief constructor
* \param[in] pu8Buffer Pointer to the buffer which is handled by the stream
* \param[in] u32BufferSize Size of the buffer in bytes
*/
CNetworkInStream(uint8_t* pu8Buffer, uint32_t u32BufferSize);
/*!
* \brief read a signed 8 bit integer from the stream
* \param[out] i8Val Reference to storage for the value
*/
void vReadInt8(int8_t& i8Val);
/*!
* \brief read a unsigned 8 bit integer from the stream
* \param[out] u8Val Reference to storage for the value
*/
void vReadUint8(uint8_t& u8Val);
/*!
* \brief read a signed 32 bit integer from the stream
* \param[out] i32Val Reference to storage for the value
*/
void vReadInt32(int32_t& i32Val);
/*!
* \brief read a unsigned 32 bit integer from the stream
* \param[out] u32Val Reference to storage for the value
*/
void vReadUint32(uint32_t& u32Val);
/*!
* \brief read a string from the stream
* \param[out] pcVal Reference to storage for the value
* \param[in] u32Length Length of the expected string
*/
void vReadString(char* pcVal, const uint32_t u32Length);
/*!
* \brief get number of bytes used in stream
* \return number of bytes used in stream
*/
uint32_t u32GetByteCount() const;
/*!
* \brief set stream to a position
* \param[in] u32Offset position to set stream to as offset from start position (=0)
*/
void vSetPosition(const uint32_t u32Offset);
/*!
* \brief check if stream is OK
* \return TRUE if no error is set for stream, FALSE otherwise
*/
bool boIsOK() const;
/*!
* \brief get error of stream
* \param[out] enError stream error code
*/
void vGetError(tenStreamError& enError) const;
/*!
* \brief set error code of stream
* \param[in] enError error to set for stream
*/
void vSetError(tenStreamError enError);
/*!
* \brief reset stream. Set position to start and error to nenStreamOK
*/
void vReset();
private:
CNetworkInStream(const CNetworkInStream& rxOther);
CNetworkInStream& operator= (const CNetworkInStream& rxOther);
/*!
* \brief checks that a sufficient number of bytes is left in the stream
* \param[in] u32Length number of bytes to check the stream for
*/
bool boCheck(const uint32_t u32Length);
const uint8_t* m_pu8CurrentPtr;
const uint8_t* const m_pru8StartPtr;
const uint8_t* const m_pru8EndPtr;
tenStreamError m_enError;
};
/*!
* \brief class for write access to a data buffer as stream
*/
class CNetworkOutStream
{
public:
/*!
* \brief constructor
* \param[in] pu8Buffer Pointer to the buffer which is handled by the stream
* \param[in] u32BufferSize Size of the buffer in bytes
*/
CNetworkOutStream(uint8_t* pu8Buffer, uint32_t u32BufferSize);
/*!
* \brief write a signed 8 bit integer to the stream
* \param[in] i8Val Reference to the value to write to the stream
*/
void vWriteInt8(const int8_t i8Val);
/*!
* \brief write a unsigned 8 bit integer to the stream
* \param[in] u8Val Reference to the value to write to the stream
*/
void vWriteUint8(const uint8_t u8Val);
/*!
* \brief write a signed 32 bit integer to the stream
* \param[in] i32Val Reference to the value to write to the stream
*/
void vWriteInt32(const int32_t i32Val);
/*!
* \brief write a unsigned 32 bit integer to the stream
* \param[in] u32Val Reference to the value to write to the stream
*/
void vWriteUint32(const uint32_t u32Val);
/*!
* \brief write a string to the stream
* \param[in] pcVal string to write to the stream
* \param[in] u32Length Length of the string
*/
void vWriteString(const char* pcVal, const uint32_t u32Length);
/*!
* \brief get number of bytes used in stream
* \return number of bytes used in stream
*/
uint32_t u32GetByteCount() const;
/*!
* \brief set stream to a position
* \param[in] u32Offset position to set stream to as offset from start position (=0)
*/
void vSetPosition(const uint32_t u32Offset);
/*!
* \brief check if stream is OK
* \return TRUE if no error is set for stream, FALSE otherwise
*/
bool boIsOK() const;
/*!
* \brief get error of stream
* \param[out] enError stream error code
*/
void vGetError(tenStreamError& enError) const;
/*!
* \brief set error code of stream
* \param[in] enError error to set for stream
*/
void vSetError(tenStreamError enError);
/*!
* \brief reset stream. Set position to start and error to nenStreamOK
*/
void vReset();
private:
CNetworkOutStream(const CNetworkOutStream& rxOther);
CNetworkOutStream& operator= (const CNetworkOutStream& rxOther);
/*!
* \brief checks that a sufficient number of bytes is left in the stream
* \param[in] u32Length number of bytes to check the stream for
*/
bool boCheck(const uint32_t u32Length);
uint8_t* m_pu8CurrentPtr;
const uint8_t* const m_pru8StartPtr;
const uint8_t* const m_pru8EndPtr;
tenStreamError m_enError;
};
#endif /*CNETWORKSTREAM_H_*/
|