-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYiniuHttp.cpp
executable file
·255 lines (196 loc) · 5.05 KB
/
YiniuHttp.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include "stdafx.h"
#include "YiniuHttp.h"
#include <string>
using namespace std;
const int ARRAY_SIZE = 255;
const int MAXBUF = 4096;
//自定义消息
const int MY_MESSAGE = WM_USER + 0x00000100;
//网络重新请求次数
const int RETRY_TIMES = 3;
UINT __cdecl CYiniuHttp::WorkerThreadProc(LPVOID pParam)
{
CYiniuHttp* pYiniuHttp = static_cast<CYiniuHttp*>(pParam);
while( !(pYiniuHttp->m_bThreadTerminated) )
{
try
{
//发送请求
pYiniuHttp->SendRequest();
PostThreadMessage(pYiniuHttp->m_dwThreadId, MY_MESSAGE, NULL, NULL);
}catch(CInternetException* pEx)
{
//连接失败,尝试重新连接
int iRet = AfxMessageBox( TEXT("网络状况不佳,发送失败,是否重新连接?"),
MB_YESNO | MB_ICONINFORMATION );
if(IDYES == iRet)
{
UINT uTimes = RETRY_TIMES;
while(uTimes--)
{
try
{
//发送请求
pYiniuHttp->SendRequest();
PostThreadMessage( pYiniuHttp->m_dwThreadId, MY_MESSAGE,
NULL, NULL );
break;
}catch(CInternetException* pEx)
{
Sleep(3000);
if(0 == uTimes)
{
pEx->Delete();
AfxMessageBox(TEXT("重新发送失败,请稍后再试!"));
exit(-1);
}
}
}//while
} else exit(0);
pEx->Delete();
}//catch
pYiniuHttp->m_pThread->SuspendThread();
}//while
return 0;
}
CYiniuHttp::CYiniuHttp(void):
m_bHadCreateThread(FALSE),
m_bThreadTerminated(FALSE)
{
m_strPostHeader = TEXT("Content-Type: application/x-www-form-urlencoded;");
m_bIsGet = TRUE;//默认为 get 请求
m_dwThreadId = GetCurrentThreadId();//界面线程ID
}
CYiniuHttp::~CYiniuHttp(void){ }
void CYiniuHttp::SendRequest() {
if(m_bIsGet)
m_pFile->SendRequest();
else
m_pFile->SendRequest( m_strPostHeader,
m_strPostHeader.GetLength(),
m_straFormData.GetBuffer(),
m_straFormData.GetLength() );
}
CYiniuHttp* CYiniuHttp::m_instance = NULL;
CYiniuHttp* CYiniuHttp::GetInstance()
{
if(!m_instance)
{
m_instance = new CYiniuHttp;
m_instance->ConnectToTheServer(serverIPAddr);
}
return m_instance;
}
BOOL CYiniuHttp::ConnectToTheServer(const CString& strIP, const INTERNET_PORT port)
{
//建立 http 连接
try
{
m_pConnection = m_session.GetHttpConnection(strIP, INTERNET_PORT(port));
} catch(CInternetException* pEx)
{
PrintErrorInfo(pEx);
pEx->Delete();
return FALSE;
}
return TRUE;
}
void CYiniuHttp::WaitForThreadCompletedRequest(HANDLE* pHthread)
{
MSG msg;
BOOL bWaitAll = FALSE;
int nWaitCount = 1;
DWORD dwRet;
while(TRUE)
{
dwRet = MsgWaitForMultipleObjects(nWaitCount, pHthread, bWaitAll, INFINITE, QS_ALLINPUT);
if(dwRet == WAIT_OBJECT_0 + nWaitCount)
{
//接收到消息, 进行消息的循环
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(MY_MESSAGE == msg.message)
return;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
//线程完成请求
break;
}
}
DWORD CYiniuHttp::SendRequest(const CString& strURI, CString& strRespondData)
{
try
{
//打开 http 连接
if(m_bIsGet)
m_pFile = m_pConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, strURI);
else
m_pFile = m_pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, strURI);
} catch(CInternetException* pEx)
{
PrintErrorInfo(pEx);
return -1;
}
//创建线程完成请求
if(!m_bHadCreateThread)
{
m_pThread = AfxBeginThread(WorkerThreadProc, this);
m_pThread->m_bAutoDelete = TRUE;
m_bHadCreateThread = TRUE;
}
else
m_pThread->ResumeThread();
DWORD dwRet;
//刷新界面,等待线程完成请求
WaitForThreadCompletedRequest(&(m_pThread->m_hThread));
//获取响应消息的状态和数据
GetRespondData(strRespondData);
m_pFile->QueryInfoStatusCode(dwRet);
return dwRet;
}
DWORD CYiniuHttp::Get(const CString& strURI, CString& strRespondData)
{
m_bIsGet = TRUE;
return SendRequest(strURI, strRespondData);
}
DWORD CYiniuHttp::Post(const CString& strURI, CString& strRespondData, CStringA& straFormData)
{
m_straFormData = straFormData;
m_bIsGet = FALSE;
return SendRequest(strURI, strRespondData);
}
void CYiniuHttp::PrintErrorInfo(CInternetException* ex)
{
TCHAR szCause[ARRAY_SIZE];
CString strFormatted(_T(""));
ex->GetErrorMessage(szCause, ARRAY_SIZE);
strFormatted += szCause;
AfxMessageBox(strFormatted);
ex->Delete();
}
void CYiniuHttp::CloseConnection()
{
m_pFile->Close();
if(m_pFile) delete m_pFile;
m_pConnection->Close();
if(m_pConnection) delete m_pConnection;
m_session.Close();
m_bThreadTerminated = TRUE;
m_pThread->ResumeThread();
Sleep(10);//界面线程让出CPU时间,让工作线程自行结束运行
}
void CYiniuHttp::GetRespondData(CString& strRespondData)
{
char* buffer = new char[MAXBUF];
UINT nBytesRead = 0;
while ( (nBytesRead=m_pFile->Read(buffer, MAXBUF-1)) > 0 )
{
buffer[nBytesRead] = '\0';
CString strTemp(buffer);
strRespondData += strTemp;
}
delete []buffer;
}