FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TTServer/cpp/src/msg_server/HttpConn.cpp at master · codeindeep/TTServer · GitHub
codeindeep
TTServer
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
TTServer
/
cpp
/
src
/
msg_server
/
HttpConn.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
221 lines (178 loc) · 4.83 KB
Breadcrumbs
TTServer
/
cpp
/
src
/
msg_server
/
HttpConn.cpp
Copy path
File metadata and controls
221 lines (178 loc) · 4.83 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* HttpConn.cpp
*
* Created on: 2013-9-29
* Author: ziteng@mogujie.com
*/
#
include
"
HttpParserWrapper.h
"
#
include
"
HttpConn.h
"
#
include
"
HttpQuery.h
"
static
HttpConnMap_t g_http_conn_map;
//
conn_handle 从0开始递增,可以防止因socket handle重用引起的一些冲突
static
uint32_t
g_conn_handle_generator =
0
;
CHttpConn*
FindHttpConnByHandle
(
uint32_t
conn_handle)
{
CHttpConn* pConn =
NULL
;
HttpConnMap_t::iterator it = g_http_conn_map.
find
(conn_handle);
if
(it != g_http_conn_map.
end
()) {
pConn = it->
second
;
}
return
pConn;
}
void
httpconn_callback
(
void
* callback_data,
uint8_t
msg,
uint32_t
handle,
uint32_t
uParam,
void
* pParam)
{
NOTUSED_ARG
(uParam);
NOTUSED_ARG
(pParam);
//
convert void* to uint32_t, oops
uint32_t
conn_handle = *((
uint32_t
*)(&callback_data));
CHttpConn* pConn =
FindHttpConnByHandle
(conn_handle);
if
(!pConn) {
return
;
}
switch
(msg)
{
case
NETLIB_MSG_READ
:
pConn->
OnRead
();
break
;
case
NETLIB_MSG_WRITE
:
pConn->
OnWrite
();
break
;
case
NETLIB_MSG_CLOSE
:
pConn->
OnClose
();
break
;
default
:
log
(
"
!!!httpconn_callback error msg: %d
\n
"
, msg);
break
;
}
}
void
http_conn_timer_callback
(
void
* callback_data,
uint8_t
msg,
uint32_t
handle,
void
* pParam)
{
CHttpConn* pConn =
NULL
;
HttpConnMap_t::iterator it, it_old;
uint64_t
cur_time =
get_tick_count
();
for
(it = g_http_conn_map.
begin
(); it != g_http_conn_map.
end
(); ) {
it_old = it;
it++;
pConn = it_old->
second
;
pConn->
OnTimer
(cur_time);
}
}
void
init_http_conn
()
{
netlib_register_timer
(http_conn_timer_callback,
NULL
,
1000
);
}
//
////////////////////////
CHttpConn::CHttpConn
()
{
m_busy =
false
;
m_sock_handle =
NETLIB_INVALID_HANDLE
;
m_state =
CONN_STATE_IDLE
;
m_last_send_tick = m_last_recv_tick =
get_tick_count
();
m_conn_handle = ++g_conn_handle_generator;
if
(m_conn_handle ==
0
) {
m_conn_handle = ++g_conn_handle_generator;
}
//
log("CHttpConn, handle=%u\n", m_conn_handle);
}
CHttpConn::~CHttpConn
()
{
//
log("~CHttpConn, handle=%u\n", m_conn_handle);
}
int
CHttpConn::Send
(
void
* data,
int
len)
{
m_last_send_tick =
get_tick_count
();
if
(m_busy)
{
m_out_buf.
Write
(data, len);
return
len;
}
int
ret =
netlib_send
(m_sock_handle, data, len);
if
(ret <
0
)
ret =
0
;
if
(ret < len)
{
m_out_buf.
Write
((
char
*)data + ret, len - ret);
m_busy =
true
;
//
log("not send all, remain=%d\n", m_out_buf.GetWriteOffset());
}
return
len;
}
void
CHttpConn::Close
()
{
m_state =
CONN_STATE_CLOSED
;
g_http_conn_map.
erase
(m_conn_handle);
netlib_close
(m_sock_handle);
ReleaseRef
();
}
void
CHttpConn::OnConnect
(
net_handle_t
handle)
{
//
printf("OnConnect, handle=%d\n", handle);
m_sock_handle = handle;
m_state =
CONN_STATE_CONNECTED
;
g_http_conn_map.
insert
(
make_pair
(m_conn_handle,
this
));
netlib_option
(handle,
NETLIB_OPT_SET_CALLBACK
, (
void
*)httpconn_callback);
netlib_option
(handle,
NETLIB_OPT_SET_CALLBACK_DATA
,
reinterpret_cast
<
void
*>(m_conn_handle) );
netlib_option
(handle,
NETLIB_OPT_GET_REMOTE_IP
, (
void
*)&m_peer_ip);
}
void
CHttpConn::OnRead
()
{
for
(;;)
{
uint32_t
free_buf_len = m_in_buf.
GetAllocSize
() - m_in_buf.
GetWriteOffset
();
if
(free_buf_len <
READ_BUF_SIZE
+
1
)
m_in_buf.
Extend
(
READ_BUF_SIZE
+
1
);
int
ret =
netlib_recv
(m_sock_handle, m_in_buf.
GetBuffer
() + m_in_buf.
GetWriteOffset
(),
READ_BUF_SIZE
);
if
(ret <=
0
)
break
;
m_in_buf.
IncWriteOffset
(ret);
m_last_recv_tick =
get_tick_count
();
}
//
每次请求对应一个HTTP连接,所以读完数据后,不用在同一个连接里面准备读取下个请求
char
* in_buf = (
char
*)m_in_buf.
GetBuffer
();
uint32_t
buf_len = m_in_buf.
GetWriteOffset
();
in_buf[buf_len] =
'
\0
'
;
//
log("OnRead, buf_len=%u, conn_handle=%u\n", buf_len, m_conn_handle); // for debug
CHttpParserWrapper* pHttpParser =
CHttpParserWrapper::GetInstance
();
pHttpParser->
ParseHttpContent
(in_buf, buf_len);
if
(pHttpParser->
IsReadAll
()) {
string url = pHttpParser->
GetUrl
();
if
(
strncmp
(url.
c_str
(),
"
/query/
"
,
7
) ==
0
) {
string content = pHttpParser->
GetBodyContent
();
CHttpQuery* pQueryInstance =
CHttpQuery::GetInstance
();
pQueryInstance->
DispatchQuery
(url, content,
this
);
}
else
{
log
(
"
url unknown, url=%s
\n
"
, url.
c_str
());
Close
();
}
}
}
void
CHttpConn::OnWrite
()
{
if
(!m_busy)
return
;
int
ret =
netlib_send
(m_sock_handle, m_out_buf.
GetBuffer
(), m_out_buf.
GetWriteOffset
());
if
(ret <
0
)
ret =
0
;
int
out_buf_size = (
int
)m_out_buf.
GetWriteOffset
();
m_out_buf.
Read
(
NULL
, ret);
if
(ret < out_buf_size)
{
m_busy =
true
;
log
(
"
not send all, remain=%d
\n
"
, m_out_buf.
GetWriteOffset
());
}
else
{
m_busy =
false
;
}
}
void
CHttpConn::OnClose
()
{
Close
();
}
void
CHttpConn::OnTimer
(
uint64_t
curr_tick)
{
if
(curr_tick > m_last_recv_tick +
HTTP_CONN_TIMEOUT
) {
log
(
"
HttpConn timeout, handle=%d
\n
"
, m_conn_handle);
Close
();
}
}
Back
|
FazBrowse Home
|
New Git URL