Project

General

Profile

Feature #2953 » Line4C3client.cpp

yan hong, 04/18/2023 10:43 AM

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

    
5
static void event_handle(struct mg_connection* connection, int event_type, void* event_data, void* pUser)
6
{
7
	struct http_message* hm = (struct http_message*)event_data;
8
	int connect_status;
9
	string* str_tmp=0;
10
	switch (event_type)
11
	{
12
	case MG_EV_CONNECT:
13
		connect_status = *(int*)event_data;
14
		if (connect_status != 0)//socket error
15
		{
16
			char char_tmp[64] = { 0 };
17
			sprintf(char_tmp, "{\"connect failed, error code: %u\":%u}", connect_status);
18
			str_tmp = (string*)pUser;
19
			*str_tmp = char_tmp;
20
		}//socket ok
21
	break;
22
	case MG_EV_HTTP_REPLY:
23
		connection->flags |= MG_F_SEND_AND_CLOSE;
24
		str_tmp = (string*)pUser;
25
		*str_tmp = string(hm->body.p, hm->body.len);
26
	break;
27
	case MG_EV_CLOSE:
28
		break;
29
	case MG_EV_HTTP_CHUNK:
30
		break;
31
	default:
32
		break;
33
	}
34
}
35

    
36
int Fclient::Sendreq(string& sendUrl, string& sendHeader, string& reqData, string& req_callback)
37
{
38
	int flag = 0;			//????-1????ͨ??ʧ?ܣ? 0??ʾ??ʱ?? 1????????????????
39
	string str_url = sendUrl;
40
	struct mg_mgr mgr={0};
41
	struct mg_connection* nc=NULL;
42

    
43
	mg_mgr_init(&mgr, NULL);
44
//	nc = mg_connect_http(&mgr, event_handle, &req_callback, str_url.c_str(), sendHeader.c_str(), reqData.c_str());
45
	int findResult = -1;
46
	findResult = str_url.find("stop");
47
	if (findResult<0)
48
		nc = mg_connect_http(&mgr, event_handle, &req_callback, str_url.c_str(), sendHeader.c_str(), reqData.c_str());
49
	else
50
		nc = mg_connect_httpDEL(&mgr, event_handle, &req_callback, str_url.c_str(), sendHeader.c_str(), reqData.c_str());
51

    
52
	unsigned long preTime = 0, nowTime = 0;
53
	preTime = GetTimetmp();
54
	while (1)
55
	{
56
		if (req_callback.length() > 0)
57
		{
58
			findResult = -1;
59
			findResult = req_callback.find("connect failed, error code:");
60
			if (findResult < 0)
61
				flag = 1;//Rx ok ret=1
62
			else
63
				return -1;// -1 = Socket Error
64
			break;
65
		}
66
		mg_mgr_poll(&mgr, 1000);
67
		nowTime = GetTimetmp();
68
		if (nowTime - preTime >= 8)
69
		{	break;//RxTimeout ret=0
70
		}			
71
	}
72
	mg_mgr_free(&mgr);
73
	return flag;
74
}
75

    
(4-4/9)