Project

General

Profile

Feature #3002 » Axiomclient.cpp

yan hong, 08/22/2023 03:18 PM

 
1
#pragma once
2
//#include "mongoose.h"
3
#include "Axiomclient.h"
4

    
5
unsigned long GetTimetmp() {
6
	unsigned long stampNow = 0;
7
	time_t timer;
8
	timer = time(NULL);
9
	stampNow = timer;
10

    
11
	return stampNow;
12
}
13

    
14

    
15
static void event_handle(struct mg_connection* connection, int event_type, void* event_data, void* pUser) {
16
	struct http_message* hm = (struct http_message*)event_data;
17
	int connect_status;
18

    
19
	switch (event_type) {
20
	case MG_EV_CONNECT:
21
	{
22

    
23
						  connect_status = *(int*)event_data;
24
						  if (connect_status != 0) {
25
							  char char_tmp[64] = { 0 };
26
							  sprintf(char_tmp, "{\"connect failed, error code: %u\":%u}", connect_status);
27
							  string* str_tmp = (string*)pUser;
28
							  *str_tmp = char_tmp;
29

    
30

    
31
						  }
32
	}
33
		break;
34

    
35
	case MG_EV_HTTP_REPLY:
36
	{
37
//							 connection->flags |= MG_F_SEND_AND_CLOSE;
38

    
39
							 string* str_tmp = (string*)pUser;
40
							 *str_tmp = string(hm->body.p, hm->body.len);
41

    
42

    
43
	}
44
		break;
45

    
46
	case MG_EV_CLOSE:
47

    
48
		break;
49

    
50
	case MG_EV_HTTP_CHUNK:
51

    
52
		break;
53

    
54
	default:
55
		break;
56

    
57
	}
58

    
59
}
60

    
61

    
62
static void event_handlelong(struct mg_connection* connection, int event_type, void* event_data, void* pUser) {
63
	struct http_message* hm = (struct http_message*)event_data;
64
	int connect_status;
65
//	if (string(hm->body.p, hm->body.len).find(":[]")) {sleep(1000000);}
66

    
67
	switch (event_type) {
68
	case MG_EV_CONNECT:
69
	{
70

    
71
						  connect_status = *(int*)event_data;
72
						  if (connect_status != 0) {
73
							  char char_tmp[64] = { 0 };
74
							  sprintf(char_tmp, "{abcdefg, error code}");
75
							  string* str_tmp = (string*)pUser;
76
							  *str_tmp = char_tmp;
77
						  }
78
	}
79
		break;
80

    
81
	case MG_EV_HTTP_REPLY:
82
	{
83

    
84
//							 connection->flags |= MG_F_SEND_AND_CLOSE;
85

    
86
							 string* str_tmp = (string*)pUser;
87
							 *str_tmp = string(hm->body.p, hm->body.len);
88
//							 *str_tmp = string(connection->recv_mbuf.buf, connection->recv_mbuf.len);
89

    
90

    
91
	}
92
		break;
93

    
94
	case MG_EV_CLOSE:
95
	{
96
						char char_tmp[64] = { 0 };
97
						sprintf(char_tmp, "{abcdefg, error code}");
98
						string* str_tmp = (string*)pUser;
99
						*str_tmp = char_tmp;
100
	}
101
		break;
102

    
103
	case MG_EV_HTTP_CHUNK:
104

    
105
		break;
106

    
107
	default:
108
		break;
109

    
110
	}
111

    
112
}
113

    
114
int Fclient::Sendreq(string& sendUrl, string& sendHeader, string& reqData, string& req_callback, int type) {
115
	int flag = 0;			//????-1????ͨ??ʧ?ܣ? 0??ʾ??ʱ?? 1????????????????
116

    
117
	string str_url = sendUrl;
118

    
119
	struct mg_mgr mgr = { 0 };
120
	struct mg_connection* nc = NULL;
121

    
122
	mg_mgr_init(&mgr, NULL);
123

    
124
	switch (type)   //type 1??post or get  2??delete  3??put  4??post
125
	{
126
	case 1:
127
		nc = mg_connect_http(&mgr, event_handle, &req_callback, str_url.c_str(), sendHeader.c_str(), reqData.c_str());
128
		break;
129

    
130
	case 2:
131
		nc = mg_connect_http(&mgr, event_handle, &req_callback, str_url.c_str(), sendHeader.c_str(), reqData.c_str());
132
		break;
133

    
134
	case 3:
135
		nc = mg_connect_httpPUT(&mgr, event_handle, &req_callback, str_url.c_str(), sendHeader.c_str(), reqData.c_str());
136
		break;
137

    
138
	case 4:
139
		nc = mg_connect_httpPOST(&mgr, event_handle, &req_callback, str_url.c_str(), sendHeader.c_str(), reqData.c_str());
140
		break;
141

    
142
	default:
143
		nc = mg_connect_http(&mgr, event_handle, &req_callback, str_url.c_str(), sendHeader.c_str(), reqData.c_str());
144
		break;
145
	}
146

    
147

    
148
	mg_set_protocol_http_websocket(nc);
149

    
150

    
151
	unsigned long preTime = 0, nowTime = 0;
152
	preTime = GetTimetmp();
153
	int findResult = -1;
154
	while (1) {
155

    
156

    
157
		if (req_callback.length() > 0) {
158
			findResult = -1;
159
			findResult = req_callback.find("connect failed, error code");
160
			if (findResult < 0)
161
				flag = 1;//Rx ok ret=1
162
			else
163
				return -1;// -1 = Socket Error
164

    
165
			break;
166
		}
167

    
168
		mg_mgr_poll(&mgr, 1000);
169

    
170
		nowTime = GetTimetmp();
171

    
172
		if (nowTime - preTime >= 15) {
173
			flag = 0;
174
			break;
175
		}
176

    
177
	}
178

    
179
	mg_mgr_free(&mgr);
180
//	if (type == 2) sleep(10000000);
181

    
182
	return flag;
183
}
184

    
185

    
186
void Fclient::init_mg() {
187
	this->mgr_long = { 0 };
188
	this->nc_long = NULL;
189
	mg_mgr_init(&this->mgr_long, NULL);
190

    
191
}
192

    
193
void Fclient::free_mg() {
194
	mg_mgr_free(&this->mgr_long);
195
}
196

    
197
int Fclient::SendreqLong(string& sendUrl, string& sendHeader, string& reqData, int type, bool& flag1, string path, string host) {
198
	int flag = 0;			//????-1????ͨ??ʧ?ܣ? 0??ʾ??ʱ?? 1????????????????
199

    
200
	string str_url = sendUrl;
201
	string callback;
202

    
203
	switch (type)   //type 1??post or get  2??delete  3??put  4??post
204
	{
205
	case 1:
206
		if (flag1) {
207
			nc_long = mg_connect_http(&mgr_long, event_handlelong, &callback, str_url.c_str(), sendHeader.c_str(), reqData.c_str());
208
//			nc_long = mg_connect_ws(&mgr_long, host.c_str(), event_handlelong, &call_back);
209
			flag1 = false;
210

    
211
		} else {
212

    
213
//			sleep(1000000);
214
			mg_printf(nc_long, "%s %s HTTP/1.1\r\nHost: %s\r\nContent-Length: %" SIZE_T_FMT
215
				"\r\n%s\r\n%s",
216
				(reqData[0] == '\0' ? "GET" : "POST"), path.c_str(),
217
				host.c_str(), reqData.length(), sendHeader.c_str(), reqData.c_str());
218
//			sleep(100000000);
219
			nc_long->mgr = &mgr_long;
220
		}
221

    
222
		break;
223

    
224
	case 2:{
225
			   mg_printf(mgr_long.active_connections, "%s %s HTTP/1.1\r\nHost: %s\r\nContent-Length: %" SIZE_T_FMT
226
				   "\r\n%s\r\n%s",
227
				   (reqData[0] == '\0' ? "GET" : "POST"), path.c_str(),
228
				   host.c_str(), reqData.length(), sendHeader.c_str(), reqData.c_str());
229

    
230
			   break;
231
	}
232
	case 3:
233
		nc_long = mg_connect_httpPUT(&mgr_long, event_handle, &callback, str_url.c_str(), sendHeader.c_str(), reqData.c_str());
234
		break;
235

    
236
	case 4:
237
		nc_long = mg_connect_httpPOST(&mgr_long, event_handle, &callback, str_url.c_str(), sendHeader.c_str(), reqData.c_str());
238
		break;
239

    
240
	default:
241
		nc_long = mg_connect_http(&mgr_long, event_handle, &callback, str_url.c_str(), sendHeader.c_str(), reqData.c_str());
242
		break;
243
	}
244

    
245

    
246
//	mg_set_protocol_http_websocket(mgr_long.active_connections);
247
	
248

    
249
	unsigned long preTime = 0, nowTime = 0;
250
	preTime = GetTimetmp();
251
	int findResult = -1;
252
//	int aaaaaaa = 0;
253
	while (1) {
254

    
255

    
256
		if (callback.length() > 0) {
257
			findResult = -1;
258
			findResult = callback.find("abcdefg");
259
			if (findResult < 0)
260
				flag = 1;//Rx ok ret=1
261
			else
262
				return -1;// -1 = Socket Error
263

    
264
			break;
265
		}
266
//		aaaaaaa++;
267

    
268

    
269

    
270
		mg_mgr_poll(nc_long->mgr, 1000);
271
//		if (type == 2 && aaaaaaa == 2) {sleep(1000000);}
272
//		if (!flag1) { sleep(1000000); }
273
//		if (iii == 555) { sleep(10000000); }
274
		nowTime = GetTimetmp();
275

    
276
		if (nowTime - preTime >= 15) {
277
			flag = 0;
278
			break;
279
		}
280

    
281
	}
282

    
283
//	mg_mgr_free(&mgr_long);
284
	call_back = callback;
285

    
286
	return flag;
287
}
288

    
(8-8/9)