-
Notifications
You must be signed in to change notification settings - Fork 9
/
vtcp_packet.cpp
231 lines (202 loc) · 4.1 KB
/
vtcp_packet.cpp
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
#include "vtcp_packet.h"
unsigned int vtcp_read2bytes(const unsigned char *buffer)
{
unsigned int result;
result = buffer[0];
result |= (buffer[1] << 8);
return(result);
}
void vtcp_write2bytes(unsigned char *buffer, unsigned int value)
{
buffer[0] = value;
buffer[1] = value >> 8;
}
unsigned int vtcp_read4bytes(const unsigned char *buffer)
{
unsigned int result;
result = buffer[0];
result |= (buffer[1] << 8);
result |= (buffer[2] << 16);
result |= (buffer[3] << 24);
return(result);
}
void vtcp_write4bytes(unsigned char *buffer, unsigned int value)
{
buffer[0] = value;
buffer[1] = value >> 8;
buffer[2] = value >> 16;
buffer[3] = value >> 24;
}
void vtcp_packet_initialize(struct vtcp_packet *pp)
{
unsigned int i;
pp->count = 0;
for (i = 0; i < VTCP_PACKET_CACHE_COUNT; i++)
{
// 标记为空包
pp->packets[i].cb = 0;
}
}
void vtcp_packet_uninitialize(struct vtcp_packet *pp)
{
pp->count = 0;
unsigned int i;
pp->count = 0;
for (i = 0; i < VTCP_PACKET_CACHE_COUNT; i++)
{
// 标记为空包
pp->packets[i].cb = 0;
}
}
struct vtcp_pkt_ext *vtcp_packet_alloc(struct vtcp_packet *pp, unsigned int sn)
{
struct vtcp_pkt_ext *ppe;
ppe = &pp->packets[sn & VTCP_PACKET_CACHE_COUNT_MASK];
if (ppe->cb == 0)
{
//初试化为最大包大小
ppe->cb = sizeof(struct vtcp_pkt);
ppe->cbdata = 0;
ppe->cboffset = 0;
ppe->pkt.hdr.cmd = 0;
ppe->pkt.hdr.index = 0;
pp->count++;
}
else
{
ppe = NULL;
}
return(ppe);
}
struct vtcp_pkt_ext *vtcp_packet_get(struct vtcp_packet *pp, unsigned int sn)
{
struct vtcp_pkt_ext *ppe;
ppe = &pp->packets[sn & VTCP_PACKET_CACHE_COUNT_MASK];
if (ppe->pkt.data.sn == sn && ppe->cb)
{
}
else
{
ppe = NULL;
}
return(ppe);
}
// 连接完成之后需要更新之前数据包的信息
unsigned int vtcp_packet_set_index(struct vtcp_packet *pp, unsigned int index, unsigned int sn, unsigned int count)
{
struct vtcp_pkt_ext *ppe;
unsigned int i;
unsigned int result = 0;
for (i = 0; i < count; i++)
{
ppe = &pp->packets[(sn - i) & VTCP_PACKET_CACHE_COUNT_MASK];
if (ppe->cb)
{
if (ppe->pkt.data.sn == sn - i)
{
vtcp_write2bytes((unsigned char *)&ppe->pkt.hdr.index, index);
result++;
}
else
{
break;
}
}
}
return(result);
}
// 只是设置个标志
unsigned int vtcp_packet_free(struct vtcp_packet *pp, unsigned int sn)
{
struct vtcp_pkt_ext *ppe;
unsigned int result = 0;
ppe = &pp->packets[sn & VTCP_PACKET_CACHE_COUNT_MASK];
if (ppe->pkt.data.sn == sn && ppe->cb)
{
result = ppe->cb;
ppe->cb = 0;
pp->count--;
}
return(result);
}
// 释放多个
unsigned int vtcp_packet_free(struct vtcp_packet *pp, unsigned int sn, unsigned int count)
{
struct vtcp_pkt_ext *ppe;
unsigned int i;
unsigned int result = 0;
for (i = 0; i < count; i++)
{
ppe = &pp->packets[(sn - i) & VTCP_PACKET_CACHE_COUNT_MASK];
if (ppe->cb)
{
if (ppe->pkt.data.sn == sn - i)
{
result += ppe->cb;
ppe->cb = 0;
pp->count--;
}
else
{
break;
}
}
}
return(result);
}
unsigned int vtcp_packet_free(struct vtcp_packet *pp, unsigned int sn, uint8_t *bits, unsigned char bitssize)
{
unsigned int i;
unsigned int j;
unsigned int result = 0;
// 有多余的操作
for (j = 0; j < bitssize; j++)
{
for (i = 0; i < 8; i++)
{
if (bits[j] & (1 << i))
{
result += vtcp_packet_free(pp, sn);
}
sn--;
}
}
return(result);
}
unsigned int vtcp_packet_makebits(struct vtcp_packet *pp, unsigned int sn, unsigned int minimum, uint8_t *bits)
{
struct vtcp_pkt_ext *ppe;
unsigned int i;
unsigned int j;
unsigned int index;
for (j = 0;; j++)
{
bits[j] = 0;
for (i = 0; i < 8; i++)
{
index = sn & VTCP_PACKET_CACHE_COUNT_MASK;
ppe = &pp->packets[index];
if (ppe->pkt.data.sn == sn)
{
bits[j] |= (1 << i);
}
else if (ppe->cb == 0)
{
// do nothing
}
else
{
}
if (sn == minimum)
{
if (i)
{
j++;
}
return(j);
}
sn--;
}
}
return(j);
}