-
Notifications
You must be signed in to change notification settings - Fork 9
/
cxpacket.h
315 lines (284 loc) · 9.17 KB
/
cxpacket.h
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#ifndef CXPACKET_H
#define CXPACKET_H
#include "cxglobal.h"
#include "cxinterinfo.h"
template<typename head_t, typename front_t, typename length_t, typename body_t, typename end_t>
class CxPacket5
{
public:
struct PacketData
{
PacketData(head_t h, front_t f, body_t b, end_t e, int l, char * d) : head(h), front(f), body(b), end(e), length(l), data(d) {}
head_t head;
front_t front;
body_t body;
end_t end;
int length;
char * data;
};
typedef end_t (*fn_crc_t)(const char * pData, int iLength);
class ReceivePacket
{
public:
ReceivePacket(const head_t & head, int iBufferSize = 1024 * 16) :
_head(head),
_tmpBuffer(256, 0)
{
_tmpBufferP = & _tmpBuffer.front();
_state = 0;
_dataCurrent = NULL;
_dataBegin = NULL;
_dataEnd = NULL;
setBufferSize(iBufferSize);
}
~ReceivePacket()
{
}
void setBufferSize(int iBufferSize)
{
_dataBuffer.resize(iBufferSize, 0);
_dataBufferP = & _dataBuffer.front();
}
void push(const char *pData, int iLength)
{
const char * pIndex = pData;
const char * pEnd = pData + iLength;
while (pIndex < pEnd)
{
switch (_state)
{
case 0:
{
head_t h = *(head_t *)(pIndex);
if (h == _head)
{
pIndex += sizeof(head_t)-1;
_state = 1;
_tmpIndex = 0;
}
}
break;
case 1:
{
_tmpBuffer[_tmpIndex] = *pIndex;
++_tmpIndex;
if (_tmpIndex == sizeof(front_t))
{
_front = *((front_t*)_tmpBufferP/*_tmpBuffer.data()*/);
_state = 2;
_tmpIndex = 0;
}
}
break;
case 2:
{
_tmpBuffer[_tmpIndex] = *pIndex;
++_tmpIndex;
if (_tmpIndex == sizeof(length_t))
{
_dataLength = *((length_t*)_tmpBufferP/*_tmpBuffer.data()*/);
_state = 3;
_tmpIndex = 0;
}
}
break;
case 3:
{
_tmpBuffer[_tmpIndex] = *pIndex;
++_tmpIndex;
if (_tmpIndex == sizeof(body_t))
{
_body = *((body_t*)_tmpBufferP/*_tmpBuffer.data()*/);
if (_dataLength == 0)
{
_state = 5;
_tmpIndex = 0;
break;
}
if (_packets.size() > 0)
{
const PacketData & packet = _packets.back();
_dataBegin = packet.data + packet.length;
}
else
{
_dataBegin = _dataBufferP/*_dataBuffer.data()*/;
}
if (! _dataCurrent) _dataCurrent = _dataBegin;
_dataEnd = _dataCurrent + _dataLength;
if (_dataEnd > _dataBufferP/*_dataBuffer.data()*/ + _dataBuffer.size())
{
cxPrompt() << "CxPacket : out buffer size !!!" << cxEnd;
_state = 0;
break;
}
_state = 4;
}
}
break;
case 4:
{
* _dataCurrent = * pIndex;
++_dataCurrent;
if (_dataCurrent >= _dataEnd)
{
_state = 5;
_tmpIndex = 0;
}
}
break;
case 5:
{
_tmpBuffer[_tmpIndex] = *pIndex;
++_tmpIndex;
if (_tmpIndex == sizeof(end_t))
{
_end = *((end_t*)_tmpBufferP/*_tmpBuffer.data()*/);
appendPacket();
_state = 0;
}
}
break;
default:
break;
}
++pIndex;
}
}
void appendPacket()
{
PacketData packet(_head, _front, _body, _end, _dataLength, _dataBegin);
_packets.push_back(packet);
}
inline size_t count() const { return _packets.size(); }
inline const std::vector<PacketData> & packets() const { return _packets; }
inline void clear()
{
_packets.clear();
if (_dataCurrent < _dataEnd)
{
memmove(_dataBufferP/*(char *)_dataBuffer.data()*/, _dataBegin, _dataCurrent - _dataBegin);
_dataEnd = _dataBufferP/*_dataBuffer.data()*/ + _dataLength - 1;
_dataCurrent = _dataBufferP/*_dataBuffer.data()*/ + ( _dataCurrent - _dataBegin );
_dataBegin = _dataBufferP/*_dataBuffer.data()*/;
}
else
{
_dataCurrent = NULL;
_dataEnd = NULL;
_dataBegin = NULL;
}
}
private:
head_t _head;
front_t _front;
// length_t _length;
body_t _body;
end_t _end;
int _state;
std::vector<char> _tmpBuffer;
char * _tmpBufferP;
int _tmpIndex;
int _dataLength;
int _dataIndex;
std::vector<char> _dataBuffer;
char * _dataBufferP;
char * _dataCurrent;
char * _dataEnd;
char * _dataBegin;
std::vector<PacketData> _packets;
};
class SendPacket
{
public:
SendPacket(const head_t & head, front_t front, fn_crc_t fn_crc)
{
_head = head;
_front = front;
_fn_crc = fn_crc;
_size = 4096;
}
~SendPacket()
{
}
int setPack(const body_t & b, const char * d, int len)
{
if (len<0) return -1;
int iSize = sizeof(head_t)+sizeof(front_t)+sizeof(length_t)+sizeof(body_t)+sizeof(end_t) + len;
if (iSize > 4096) return -1;
char * data = _data;
*((head_t *)data) = _head; data += sizeof(head_t);
*((front_t *)data) = _front; data += sizeof(front_t);
*((length_t *)data) = len; data += sizeof(length_t);
*((body_t *)data) = b; data += sizeof(body_t);
if (d && len > 0)
{
memcpy(data, d, len); data += len;
}
*((end_t *)data) = _fn_crc(_data+sizeof(head_t)+sizeof(front_t)+sizeof(length_t), sizeof(body_t)+len);
_size = iSize;
return iSize;
}
inline const char * data() { return _data; }
inline int size() { return _size; }
private:
fn_crc_t _fn_crc;
head_t _head;
front_t _front;
char _data[4096];
int _size;
};
};
//do not delete
//
//void ::receivedData(const char *pData, int iLength)
//{
// if (iLength>0 && iLength<4097)
// {
// memcpy(_recPch, pData, iLength);
// _recPch += iLength;
// const char * pchIndex = takeBuffer(_recBuffer, _recPch);
// if (pchIndex != _recBuffer)
// {
// int iSize = 0;
// if (pchIndex != _recPch ) // (pchIndex < pchEnd)
// {
// iSize = pchEnd-pchIndex;
// memmove(_recBuffer, pchIndex, iSize);
// }
// _recPch = _recBuffer + iSize;
// }
// }
//}
//const char * ::takeBuffer(const char * pchBegin, const char * pchEnd)
//{
// const char * pchIndex = pchBegin;
// while (pchIndex < pchEnd)
// {
// int h = *(int *)(pchIndex);
// if (h == c_psm_head)
// {
// if (pchEnd - pchIndex < sizeof(PacketData))
// {
// break;
// }
// PacketData frame;
// frame.front = *(short *)(pchIndex+4);
// frame.length = *(short *)(pchIndex+4+2);
// if (l <0 || l > 4096)
// {
// pchIndex+=6;
// continue;
// }
// frame.body = *(body_t *)(pchIndex+4+2+2);
// frame.data = std::string((pchIndex+4+2+2+sizeof(psm_packet_body_t)), frame.dataLength);
// frame.crc = *(short *)(pchIndex+4+2+2+sizeof(psm_packet_body_t)+frame.dataLength);
// }
// else
// {
// pchIndex++;
// }
// }
// return pchIndex;
//}
#endif // CXPACKET_H