资源简介
数据上传至mqtt服务器,或订阅mqtt 服务器消息来消费
代码片段和文件信息
#include “mqtt.h“
#include “global.h“
MQTTClient g_client;
volatile MQTTClient_deliveryToken deliveredtoken;
bool g_bcontent = true;
//mutexQueue g_OperationInfoque;
void G2U(const char* gb2312 string &strContent)
{
int len = MultiByteToWideChar(CP_ACP 0 gb2312 -1 NULL 0);
wchar_t* wstr = new wchar_t[len + 1];
memset(wstr 0 len + 1);
MultiByteToWideChar(CP_ACP 0 gb2312 -1 wstr len);
len = WideCharToMultiByte(CP_UTF8 0 wstr -1 NULL 0 NULL NULL);
char* str = new char[len + 1];
memset(str 0 len + 1);
WideCharToMultiByte(CP_UTF8 0 wstr -1 str len NULL NULL);
if (wstr) delete[] wstr;
strContent = str;
if (str) delete[] str;
}
void delivered(void *context MQTTClient_deliveryToken dt)
{
printf( “Message with token value %d delivery confirmed\n“ dt );
deliveredtoken = dt;
}
int msgarrvd(void *context char *topicName int topicLen MQTTClient_message *message)
{
int i;
char* payloadptr;
//CLog::WriteLogFormat(“Message arrived\n topic: %s\n“ topicName);
payloadptr = (char*)message->payload;
if (strcmp(payloadptr DISCONNECT) == 0) {
printf(“ \n out!!“);
g_bcontent = false;
}
string strTmpMsg = ““;
payloadptr = (char*)message->payload;
for (i = 0; i < message->payloadlen; i++)
{
strTmpMsg += (*payloadptr++);
}
//string strResult = ““;
//U2G( strTmpMsg.c_str() strResult );
//判断处理topic
MQTTClient_freeMessage(&message);
MQTTClient_free(topicName);
return 1;
}
void connlost(void *context char *cause)
{
//CLog::WriteLogFormat(“Connection lost!! cause: [%s]\n“ cause);
printf(“Connection lost!!cause: [%s]\n“ cause);
g_bcontent = false;
}
//********************************************* 线程 ******************************************************
DWORD WINAPI doHandMessage(PVOID lpParam)
{
mqtt *q = (mqtt*)lpParam;
while (true)
{
if (g_OperationInfoque.size() > 0 && g_bcontent)
{
OPERATIONINFO stOperationInfo;
if (!g_OperationInfoque.top(stOperationInfo))
{
//CLog::WriteLogFormat(“队列取出失败!\n“);
printf(“队列取出失败!\n“);
break;
}
string strJson = stOperationInfo.strJson;
string strTOPIC = stOperationInfo.strTOPIC;
if (q->publishMessage(strTOPIC strJson.c_str()))
{
g_OperationInfoque.pop();
}
else
{
g_bcontent = false;
}
Sleep(200);
}
}
return 0;
}
DWORD WINAPI MQTT_ReConnect(PVOID lpParam)
{
mqtt *q = (mqtt*)lpParam;
while (true)
{
if (!g_bcontent)
{
MQTTClient_destroy(&g_client);
if (q->initMQTTconnect())
{
g_bcontent = true;
}
}
Sleep(1000);
}
return 0;
}
mqtt::mqtt()
{
//----------------------------------------处理MQTT断线重连------------------------------------------------------------
HANDLE hDoHandThread = CreateThread(NULL 0 doHandMessage this 0 NULL);
Sleep(1000);
HANDLE hMQTTReConnectThread = CreateThread(NULL 0 MQTT_ReConnect this 0 N
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 274026 2021-06-24 17:04 MqttClass\Debug\mqtt.obj
文件 80 2021-06-24 17:04 MqttClass\Debug\MqttClass.log
文件 289938 2021-06-24 16:33 MqttClass\Debug\MqttClass.obj
文件 1398 2021-06-24 17:04 MqttClass\Debug\MqttClass.tlog\CL.command.1.tlog
文件 90104 2021-06-24 17:04 MqttClass\Debug\MqttClass.tlog\CL.read.1.tlog
文件 1126 2021-06-24 17:04 MqttClass\Debug\MqttClass.tlog\CL.write.1.tlog
文件 1200 2021-06-24 17:04 MqttClass\Debug\MqttClass.tlog\li
文件 3776 2021-06-24 17:04 MqttClass\Debug\MqttClass.tlog\li
文件 452 2021-06-24 17:04 MqttClass\Debug\MqttClass.tlog\li
文件 196 2021-06-24 17:04 MqttClass\Debug\MqttClass.tlog\MqttClass.lastbuildstate
文件 551936 2021-06-24 17:04 MqttClass\Debug\vc141.idb
文件 585728 2021-06-24 17:04 MqttClass\Debug\vc141.pdb
文件 267 2021-06-24 16:28 MqttClass\global.h
文件 89088 2019-05-29 16:47 MqttClass\lib\paho-mqtt3a.dll
文件 9824 2020-06-01 16:53 MqttClass\lib\paho-mqtt3a.lib
文件 77312 2018-02-21 05:11 MqttClass\lib\paho-mqtt3c.dll
文件 11252 2020-06-01 16:55 MqttClass\lib\paho-mqtt3c.lib
文件 15872 2019-05-29 16:47 MqttClass\lib\paho-mqttpp3.dll
文件 1080688 2019-05-29 16:47 MqttClass\lib\paho-mqttpp3.lib
文件 5240 2021-06-24 17:03 MqttClass\mqtt.cpp
文件 399 2021-06-24 16:00 MqttClass\mqtt.h
文件 1118 2021-06-24 16:33 MqttClass\MqttClass.cpp
文件 7982 2021-06-24 16:01 MqttClass\MqttClass.vcxproj
文件 1529 2021-06-24 16:01 MqttClass\MqttClass.vcxproj.filters
文件 165 2021-06-24 14:37 MqttClass\MqttClass.vcxproj.user
文件 61062 2018-02-21 05:11 MqttClass\MQTTClient.h
文件 10437 2018-02-21 05:11 MqttClass\MQTTClientPersistence.h
文件 949 2020-09-28 10:16 MqttClass\queue.h
文件 1441 2021-06-24 14:38 MqttClass.sln
目录 0 2021-06-24 17:04 MqttClass\Debug\MqttClass.tlog
............此处省略6个文件信息
相关资源
- 商品库存管理系统 C++ MFC
- c++ 多功能计算器
- C++17 In Detail
- 嵌入式QtC++编程课件
- STM32连接EC20使用内置MQTT协议接入阿里
- 颜色识别形状识别STM103嵌入式代码
- c++ 邮件多附件群发
- c++ 透明代理(hookproxy)
- mfc 调用redis
- C语言封装的HttpClient接口
- FTP客户端源码(c++)
- c++ 画图(14Qt-XPS)
- c++多边形交并差运算
- VC++基于OpenGL模拟的一个3维空间模型
- c++ 虚拟摄像头
- hook,捕获所有案件,查找所有窗口,
- C语言课设计算器
- c++ 简易贪吃蛇源码
- 高精度加法(c++代码)
- C++调用百度地图案例
- 北京化工大学计算方法(C/C++)讲义
- 基于VC++的SolidWorks二次开发SolidWorks
- c++ 模拟鼠标按键
- OFD编辑器
- Beginning C++17 From Novice to Professional
- C++ STL实现
- opencv手部轮廓识别以及轨迹识别
- 百度C++编码规范
- C++ sql2008 WebServer通讯.docx
- c++ 定时关机程序源码
评论
共有 条评论