mirror of
https://github.com/eclipse/paho.mqtt.cpp.git
synced 2025-05-09 03:11:23 +08:00
Fixed windows build and warnings
This commit is contained in:
parent
be69d681fc
commit
b9d5ab00b6
@ -64,6 +64,7 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||
include(GNUInstallDirs)
|
||||
|
||||
if(WIN32)
|
||||
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
|
||||
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
|
||||
set(LIBS_SYSTEM ws2_32)
|
||||
elseif(UNIX)
|
||||
|
@ -271,7 +271,8 @@ int async_client::on_update_connection(void* context,
|
||||
auto n = data.get_user_name().length();
|
||||
if (n > 0) {
|
||||
char* username = static_cast<char*>(MQTTAsync_malloc(n+1));
|
||||
strcpy(username, data.get_user_name().c_str());
|
||||
strncpy(username, data.get_user_name().c_str(), n+1);
|
||||
username[n] = '\0';
|
||||
cdata->username = username;
|
||||
}
|
||||
else
|
||||
|
@ -122,8 +122,10 @@ int iclient_persistence::persistence_keys(void* handle, char*** keys, int* nkeys
|
||||
else {
|
||||
*keys = static_cast<char**>(MQTTAsync_malloc(n*sizeof(char*)));
|
||||
for (size_t i=0; i<n; ++i) {
|
||||
char* buf = static_cast<char*>(MQTTAsync_malloc(k[i].size()+1));
|
||||
strcpy(buf, k[i].c_str());
|
||||
auto sz = k[i].size();
|
||||
char* buf = static_cast<char*>(MQTTAsync_malloc(sz+1));
|
||||
strncpy(buf, k[i].c_str(), sz+1);
|
||||
buf[sz] = '\0';
|
||||
(*keys)[i] = buf;
|
||||
}
|
||||
}
|
||||
|
@ -41,6 +41,14 @@
|
||||
* Frank Pagliughi - initial implementation and documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#if !defined(_WIN32)
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <dirent.h>
|
||||
#include <unistd.h>
|
||||
#include <fstream>
|
||||
#endif
|
||||
|
||||
#include <random>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
@ -52,17 +60,6 @@
|
||||
#include <ctime>
|
||||
#include "mqtt/async_client.h"
|
||||
|
||||
// Don't worry about localtime() in this context
|
||||
#if defined(_WIN32)
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#else
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <dirent.h>
|
||||
#include <unistd.h>
|
||||
#include <fstream>
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
using namespace std::chrono;
|
||||
|
||||
@ -80,6 +77,11 @@ const string PERSIST_DIR { "data-persist" };
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// At some point, when the library gets updated to C++17, we can use
|
||||
// std::filesystem to make a portable version of this.
|
||||
|
||||
#if !defined(_WIN32)
|
||||
|
||||
// Example of user-based file persistence with a simple XOR encoding scheme.
|
||||
//
|
||||
// Similar to the built-in file persistence, this just creates a
|
||||
@ -244,6 +246,7 @@ public:
|
||||
::remove(path.c_str());
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@ -251,8 +254,12 @@ int main(int argc, char* argv[])
|
||||
{
|
||||
string address = (argc > 1) ? string(argv[1]) : DFLT_ADDRESS;
|
||||
|
||||
encoded_file_persistence persist("elephant");
|
||||
mqtt::async_client cli(address, CLIENT_ID, MAX_BUFFERED_MSGS, &persist);
|
||||
#if defined(_WIN32)
|
||||
mqtt::async_client cli(address, CLIENT_ID, MAX_BUFFERED_MSGS);
|
||||
#else
|
||||
encoded_file_persistence persist("elephant");
|
||||
mqtt::async_client cli(address, CLIENT_ID, MAX_BUFFERED_MSGS, &persist);
|
||||
#endif
|
||||
|
||||
auto connOpts = mqtt::connect_options_builder()
|
||||
.keep_alive_interval(MAX_BUFFERED_MSGS * PERIOD)
|
||||
|
@ -60,7 +60,7 @@
|
||||
using namespace std;
|
||||
using namespace std::chrono;
|
||||
|
||||
const std::string SERVER_ADDRESS("tcp://localhost:1883");
|
||||
const std::string DFLT_SERVER_ADDRESS("tcp://localhost:1883");
|
||||
const std::string CLIENT_ID("multithr_pub_sub_cpp");
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
@ -139,8 +139,10 @@ void publisher_func(mqtt::async_client_ptr cli, multithr_counter::ptr_t counter)
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
string address = (argc > 1) ? string(argv[1]) : DFLT_SERVER_ADDRESS;
|
||||
|
||||
// Create an MQTT client using a smart pointer to be shared among threads.
|
||||
auto cli = std::make_shared<mqtt::async_client>(SERVER_ADDRESS, CLIENT_ID);
|
||||
auto cli = std::make_shared<mqtt::async_client>(address, CLIENT_ID);
|
||||
|
||||
// Make a counter object also with a shared pointer.
|
||||
auto counter = std::make_shared <multithr_counter>();
|
||||
@ -160,7 +162,7 @@ int main(int argc, char* argv[])
|
||||
// we're using a persistent (non-clean) session with the broker.
|
||||
cli->start_consuming();
|
||||
|
||||
cout << "Connecting to the MQTT server..." << flush;
|
||||
cout << "Connecting to the MQTT server at " << address << "..." << flush;
|
||||
auto rsp = cli->connect(connOpts)->get_connect_response();
|
||||
cout << "OK\n" << endl;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user