-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCHttpClient.h
More file actions
144 lines (106 loc) · 3.68 KB
/
CHttpClient.h
File metadata and controls
144 lines (106 loc) · 3.68 KB
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
#pragma once
#ifndef __HTTP_CURL_H__
#define __HTTP_CURL_H__
#include "Singleton.h"
#include "curl/curl.h"
class CHttpRequest;
typedef boost::shared_ptr<CHttpRequest> CHttpRequestPtr;
typedef boost::function<void (const CHttpRequestPtr &) > HttpCallBack;
typedef xHashMap<LONG64, CHttpRequestPtr> CHttpRequestMap;
typedef std::list<CHttpRequestPtr> CHttpRequestCollect;
typedef std::set<LONG64> CHttpRequestIDSet;
class XClass CHttpClient : public Singleton<CHttpClient>
{
public:
CHttpClient(void);
virtual ~CHttpClient(void);
public:
//刷帧函数
void update();
/**
* @brief HTTP 异步Post请求
* @param strUrl 请求的Url地址
* @param strPost Post数据
* @param cb 回调函数
* @return
*/
LONG64 asyncPost(const std::string & strUrl, const std::string & strPost, HttpCallBack cb);
/**
* @brief HTTP GET请求
* @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com
* @param strResponse 输出参数,返回的内容
* @return 返回是否Post成功
*/
/**
* @brief 注销掉回调函数;
* @param obj
*/
bool unregisterCallBack(LONG64 requestUUID);
int Get(const std::string & strUrl, std::string & strResponse);
void SetDebug(bool bDebug);
void setConnectTimeOut(float t){ m_connectTimeOut = t; }
void setRequestTimeOut(float t){ m_requestTimeOut = t; }
private:
void workThreadFunc();
int curl_multi_select(CURLM * curl_m);
CURL* curl_easy_handler(std::string & sUrl, std::string & strPost, CHttpRequestCollect::iterator it, uint32 uiTimeout = 10000);
/**
* @brief HTTP POST请求
* @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com
* @param strPost 输入参数,使用如下格式para1=val¶2=val2&…
* @param strResponse 输出参数,返回的内容
* @return 返回是否Post成功
*/
int Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse);
/**
* @brief HTTPS POST请求,无证书版本
* @param strUrl 输入参数,请求的Url地址,如:https://www.alipay.com
* @param strPost 输入参数,使用如下格式para1=val¶2=val2&…
* @param strResponse 输出参数,返回的内容
* @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性.
* @return 返回是否Post成功
*/
int Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath = NULL);
/**
* @brief HTTPS GET请求,无证书版本
* @param strUrl 输入参数,请求的Url地址,如:https://www.alipay.com
* @param strResponse 输出参数,返回的内容
* @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性.
* @return 返回是否Post成功
*/
int Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath = NULL);
int asyncGet(const std::string & strUrl, HttpCallBack cb);
private:
bool m_bDebug;
float m_connectTimeOut = 20.0f;
float m_requestTimeOut = 180.0f;
uint32 m_atomic;
CHttpRequestCollect m_NewRequestCollect;
boost::mutex m_NewRequestMutex;
CHttpRequestMap m_DoingRequestCollect;
CHttpRequestCollect m_FinishRequestCollect;
boost::mutex m_FinishRequestMutex;
CURLM * m_handMultiCurl;
boost::thread* m_pWorkThread;
bool m_Quit;
CHttpRequestIDSet m_unregisterRequestCollect;
};
class XClass CHttpRequest
{
public:
CHttpRequest(const std::string& url, const std::string& postData, HttpCallBack cb);
~CHttpRequest()
{
};
void setResponse(const std::string& resp)
{
m_response = resp;
};
LONG64 uuid;
std::string m_url;
std::string m_postData;
std::string m_response;
HttpCallBack m_callBack;
CURLcode m_state; //请求结果 返回0成功其他失败
};
#endif