1
|
#pragma once
|
2
|
#include "mongoose.h"
|
3
|
#include "ZJ_ACSclient.h"
|
4
|
|
5
|
unsigned long GetTimetmp()
|
6
|
{
|
7
|
unsigned long stampNow = 0;
|
8
|
time_t timer;
|
9
|
timer = time(NULL);
|
10
|
stampNow = timer;
|
11
|
|
12
|
return stampNow;
|
13
|
}
|
14
|
|
15
|
|
16
|
static void event_handle(struct mg_connection* connection, int event_type, void* event_data, void* pUser)
|
17
|
{
|
18
|
struct http_message* hm = (struct http_message*)event_data;
|
19
|
int connect_status;
|
20
|
|
21
|
switch (event_type)
|
22
|
{
|
23
|
case MG_EV_CONNECT:
|
24
|
{
|
25
|
|
26
|
connect_status = *(int*)event_data;
|
27
|
if (connect_status != 0)
|
28
|
{
|
29
|
char char_tmp[64] = { 0 };
|
30
|
sprintf(char_tmp, "{\"connect failed, error code: %u\":%u}", connect_status);
|
31
|
string* str_tmp = (string*)pUser;
|
32
|
*str_tmp = char_tmp;
|
33
|
|
34
|
|
35
|
}
|
36
|
}
|
37
|
break;
|
38
|
|
39
|
case MG_EV_HTTP_REPLY:
|
40
|
{
|
41
|
connection->flags |= MG_F_SEND_AND_CLOSE;
|
42
|
|
43
|
string* str_tmp = (string*)pUser;
|
44
|
*str_tmp = string(hm->body.p, hm->body.len);
|
45
|
|
46
|
|
47
|
}
|
48
|
break;
|
49
|
|
50
|
case MG_EV_CLOSE:
|
51
|
|
52
|
break;
|
53
|
|
54
|
case MG_EV_HTTP_CHUNK:
|
55
|
|
56
|
break;
|
57
|
|
58
|
default:
|
59
|
break;
|
60
|
|
61
|
}
|
62
|
|
63
|
}
|
64
|
|
65
|
int Fclient::Sendreq(string& sendUrl, string& sendHeader, string& reqData, string& req_callback, int type)
|
66
|
{
|
67
|
int flag = 0; //????-1????ͨ??ʧ?ܣ? 0??ʾ??ʱ?? 1????????????????
|
68
|
|
69
|
string str_url = sendUrl;
|
70
|
|
71
|
struct mg_mgr mgr = { 0 };
|
72
|
struct mg_connection* nc = NULL;
|
73
|
|
74
|
mg_mgr_init(&mgr, NULL);
|
75
|
|
76
|
switch (type) //type 1??post or get 2??delete 3??put 4??post
|
77
|
{
|
78
|
case 1:
|
79
|
nc = mg_connect_http(&mgr, event_handle, &req_callback, str_url.c_str(), sendHeader.c_str(), reqData.c_str());
|
80
|
break;
|
81
|
|
82
|
case 2:
|
83
|
nc = mg_connect_httpDEL(&mgr, event_handle, &req_callback, str_url.c_str(), sendHeader.c_str(), reqData.c_str());
|
84
|
break;
|
85
|
|
86
|
case 3:
|
87
|
nc = mg_connect_httpPUT(&mgr, event_handle, &req_callback, str_url.c_str(), sendHeader.c_str(), reqData.c_str());
|
88
|
break;
|
89
|
|
90
|
case 4:
|
91
|
nc = mg_connect_httpPOST(&mgr, event_handle, &req_callback, str_url.c_str(), sendHeader.c_str(), reqData.c_str());
|
92
|
break;
|
93
|
|
94
|
default:
|
95
|
nc = mg_connect_http(&mgr, event_handle, &req_callback, str_url.c_str(), sendHeader.c_str(), reqData.c_str());
|
96
|
break;
|
97
|
}
|
98
|
|
99
|
|
100
|
mg_set_protocol_http_websocket(nc);
|
101
|
|
102
|
|
103
|
unsigned long preTime = 0, nowTime = 0;
|
104
|
preTime = GetTimetmp();
|
105
|
|
106
|
while (1)
|
107
|
{
|
108
|
if (req_callback.length() > 0)
|
109
|
{
|
110
|
flag = 1;
|
111
|
if (req_callback.find("connect failed"))
|
112
|
flag = -1;
|
113
|
break;
|
114
|
}
|
115
|
|
116
|
|
117
|
mg_mgr_poll(&mgr, 1000);
|
118
|
|
119
|
nowTime = GetTimetmp();
|
120
|
|
121
|
if (nowTime - preTime >= 15)
|
122
|
{
|
123
|
flag = 0;
|
124
|
break;
|
125
|
}
|
126
|
|
127
|
}
|
128
|
|
129
|
mg_mgr_free(&mgr);
|
130
|
|
131
|
return flag;
|
132
|
}
|
133
|
|