From b9d5ab00b67bd598ab26a31987df8a9404ab0af9 Mon Sep 17 00:00:00 2001 From: fpagliughi Date: Mon, 21 Dec 2020 21:30:12 -0500 Subject: [PATCH] Fixed windows build and warnings --- CMakeLists.txt | 1 + src/async_client.cpp | 3 ++- src/iclient_persistence.cpp | 6 ++++-- src/samples/data_publish.cpp | 33 +++++++++++++++++++------------- src/samples/multithr_pub_sub.cpp | 8 +++++--- 5 files changed, 32 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ed564a6..a892050 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/src/async_client.cpp b/src/async_client.cpp index 49ef0f3..b903ef2 100644 --- a/src/async_client.cpp +++ b/src/async_client.cpp @@ -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(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 diff --git a/src/iclient_persistence.cpp b/src/iclient_persistence.cpp index 76222cf..0234795 100644 --- a/src/iclient_persistence.cpp +++ b/src/iclient_persistence.cpp @@ -122,8 +122,10 @@ int iclient_persistence::persistence_keys(void* handle, char*** keys, int* nkeys else { *keys = static_cast(MQTTAsync_malloc(n*sizeof(char*))); for (size_t i=0; i(MQTTAsync_malloc(k[i].size()+1)); - strcpy(buf, k[i].c_str()); + auto sz = k[i].size(); + char* buf = static_cast(MQTTAsync_malloc(sz+1)); + strncpy(buf, k[i].c_str(), sz+1); + buf[sz] = '\0'; (*keys)[i] = buf; } } diff --git a/src/samples/data_publish.cpp b/src/samples/data_publish.cpp index 7e95de5..f11055e 100644 --- a/src/samples/data_publish.cpp +++ b/src/samples/data_publish.cpp @@ -41,6 +41,14 @@ * Frank Pagliughi - initial implementation and documentation *******************************************************************************/ +#if !defined(_WIN32) + #include + #include + #include + #include + #include +#endif + #include #include #include @@ -52,17 +60,6 @@ #include #include "mqtt/async_client.h" -// Don't worry about localtime() in this context -#if defined(_WIN32) - #define _CRT_SECURE_NO_WARNINGS -#else - #include - #include - #include - #include - #include -#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) diff --git a/src/samples/multithr_pub_sub.cpp b/src/samples/multithr_pub_sub.cpp index ebf2f1c..3ee6bc1 100644 --- a/src/samples/multithr_pub_sub.cpp +++ b/src/samples/multithr_pub_sub.cpp @@ -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(SERVER_ADDRESS, CLIENT_ID); + auto cli = std::make_shared(address, CLIENT_ID); // Make a counter object also with a shared pointer. auto counter = std::make_shared (); @@ -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;