-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecvOneFrame.c
200 lines (145 loc) · 3.34 KB
/
RecvOneFrame.c
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
/* -- $Id: RecvOneFrame.cpp,v 1.12.2.2.4.3 2004/07/22 11:14:40 yuzhe Exp $ -- */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "RecvOneFrame.h"
#include "NetLog.h"
#include "Debuger.h"
#include "NetServer.h"
#include "CtrlLinkTbl.h"
#include "Protocol.h"
#ifdef DEBUG
static void
PrintPkt (char *p, int len)
{
printf ("\n .... RECEIVE PACKET CONTENT : .... \n");
int i;
for (i = 0; i < len; i++, p++)
{
if (i != 0 && i % 8 == 0)
printf ("\n");
printf ("%02x ", (unsigned char) (*p));
}
printf ("\n");
return;
}
#endif
/**********************************************************************
* recvFrame:
* packet saved in char* pAppFrame,
* return : length of packet
**********************************************************************/
int
RecvOneFrame (int s, char *pAppFrame)
{
assert (s >= 0);
assert (pAppFrame != NULL);
char bufleft[MAX_FRAME_LEN];
char *ptrstart = bufleft, *ptrend = bufleft;
int found_item = 0;
//-- 在连接表中查找表项,其中保存未处理的报文分片 --//
int i;
PCTRL_LINK_DESC pcld = CtrlLinkTbl;
for (i = 0; i < MAX_CONN_NUM; i++, pcld++)
{
if (pcld->socket_fd != s)
continue;
if (pcld->unhandled_buf.len > 0)
{
memcpy (bufleft, pcld->unhandled_buf.buf,
pcld->unhandled_buf.len);
ptrend = ptrend + pcld->unhandled_buf.len;
pthread_mutex_lock (&pcld->mutex);
pcld->unhandled_buf.len = 0;
pthread_mutex_unlock (&pcld->mutex);
}
found_item = 1;
break;
}
if (found_item != 1)
{
TRACE (12, (" socket number is not found!\n "));
return -1;
}
//-- 开始分析报文头 --//
int haslen = 0;
int framelen = 0;
int buflen = ptrend - ptrstart;
logRec (" bufleft, ptrstart, ptrend: %d, %d, %d\n", bufleft, ptrstart,
ptrend);
if (buflen >= 2) //???FIXME
{
// get another packet length
framelen = ((PPACKET_HDR) ptrstart)->len;
haslen = 1;
if (buflen >= framelen)
{
// 包括完整报文
memcpy (pAppFrame, ptrstart, framelen);
// 剩余数据拷回
pthread_mutex_lock (&pcld->mutex);
memcpy (pcld->unhandled_buf.buf, ptrstart + framelen,
buflen - framelen);
pcld->unhandled_buf.len = buflen - framelen;
pthread_mutex_unlock (&pcld->mutex);
#ifdef DEBUG
{
PrintPkt (pAppFrame, framelen);
}
#endif
return framelen;
}
}
int data_received;
for (;;)
{
data_received =
recv (s, ptrend, MAX_FRAME_LEN - (ptrend - bufleft), 0);
if (data_received == -1)
{
perror ("recv");
return -1;
}
//客户端关闭连接
if (data_received == 0)
{
TRACE (6, ("Connection closed by client!\n"));
return 0;
}
ptrend = ptrend + data_received;
buflen = ptrend - ptrstart;
if (haslen == 0 && buflen >= 2)
{
framelen = ((PPACKET_HDR) ptrstart)->len;
haslen = 1;
}
if (haslen == 1 && framelen > MAX_FRAME_LEN)
{
TRACE (12,
(" !!!!!! Frame Len > MAX_FRAME_LEN , EXIT!!!!!! ...\n"));
return -1;
}
if (buflen >= framelen)
{
break;
}
}
// 得到完整报文
memcpy (pAppFrame, ptrstart, framelen);
// 剩余数据拷回
pthread_mutex_lock (&pcld->mutex);
memcpy (pcld->unhandled_buf.buf, ptrstart + framelen,
buflen - framelen);
pcld->unhandled_buf.len = buflen - framelen;
pthread_mutex_unlock (&pcld->mutex);
#ifdef DEBUG
{
PrintPkt (pAppFrame, framelen);
}
#endif
return framelen;
}