From 11b16756cb38f27eae61b23e3c5ee26bcb882635 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Thu, 21 Jul 2022 15:51:52 +0100 Subject: [PATCH] Windows fixes. --- client/client_shared.c | 5 +++++ client/client_shared.h | 2 ++ client/sub_client_output.c | 12 ++++++++-- cmake/FindcJSON.cmake | 1 + config.h | 10 ++++++--- lib/actions_publish.c | 1 - lib/packet_datatypes.c | 4 +++- .../mosquitto_payload_size_stats.c | 8 +++---- .../mosquitto_plugin_event_stats.c | 2 +- plugins/persist-sqlite/plugin.c | 7 ++++++ src/database.c | 22 ++++++++++++++++--- 11 files changed, 59 insertions(+), 15 deletions(-) diff --git a/client/client_shared.c b/client/client_shared.c index 2988b0d1..98acb6f2 100644 --- a/client/client_shared.c +++ b/client/client_shared.c @@ -1223,7 +1223,12 @@ int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, c if(pub_or_sub != CLIENT_SUB){ goto unknown_option; } +#ifdef WIN32 + fprintf(stderr, "Error: --watch not supported on Windows.\n\n"); + return 1; +#else cfg->watch = true; +#endif }else if(!strcmp(argv[i], "--will-payload")){ if(i==argc-1){ fprintf(stderr, "Error: --will-payload argument given but no will payload specified.\n\n"); diff --git a/client/client_shared.h b/client/client_shared.h index f0446946..b3aeda5a 100644 --- a/client/client_shared.h +++ b/client/client_shared.h @@ -116,7 +116,9 @@ struct mosq_config { long session_expiry_interval; int random_filter; /* sub */ int transport; +#ifndef WIN32 bool watch; /* sub */ +#endif #ifdef WITH_SOCKS char *socks5_host; int socks5_port; diff --git a/client/sub_client_output.c b/client/sub_client_output.c index eb3313e5..216a1f13 100644 --- a/client/sub_client_output.c +++ b/client/sub_client_output.c @@ -852,7 +852,7 @@ static void rand_init(void) #endif } - +#ifndef WIN32 static void watch_print(const struct mosquitto_message *message) { struct watch_topic *item = NULL; @@ -873,6 +873,7 @@ static void watch_print(const struct mosquitto_message *message) } printf("\e[%d;1H", item->line); } +#endif void print_message(struct mosq_config *lcfg, const struct mosquitto_message *message, const mosquitto_property *properties) @@ -883,9 +884,12 @@ void print_message(struct mosq_config *lcfg, const struct mosquitto_message *mes long r = 0; #endif +#ifndef WIN32 if(lcfg->watch){ watch_print(message); } +#endif + if(lcfg->random_filter < 10000){ #ifdef WIN32 rand_s(&r); @@ -920,20 +924,24 @@ void print_message(struct mosq_config *lcfg, const struct mosquitto_message *mes fflush(stdout); } } +#ifndef WIN32 if(lcfg->watch){ printf("\e[%d;1H\n", watch_max-1); } +#endif } void output_init(struct mosq_config *lcfg) { rand_init(); +#ifndef WIN32 if(lcfg->watch){ printf("\e[2J\e[1;1H"); printf("Broker: %s\n", lcfg->host); } +#endif #ifdef WIN32 /* Disable text translation so binary payloads aren't modified */ - _setmode(_fileno(stdout), _O_BINARY); + (void)_setmode(_fileno(stdout), _O_BINARY); #endif } diff --git a/cmake/FindcJSON.cmake b/cmake/FindcJSON.cmake index 5458e07a..2d89c1a7 100644 --- a/cmake/FindcJSON.cmake +++ b/cmake/FindcJSON.cmake @@ -41,6 +41,7 @@ IF( CJSON_FOUND ) set_target_properties(cJSON PROPERTIES IMPORTED_LOCATION "${CJSON_LIBRARY}" + IMPORTED_IMPLIB "${CJSON_LIBRARY}" ) ELSE() SET( CJSON_DIR "" CACHE STRING diff --git a/config.h b/config.h index 280b78d7..88330035 100644 --- a/config.h +++ b/config.h @@ -45,11 +45,15 @@ #endif #ifdef WIN32 -# ifndef strcasecmp -# define strcasecmp strcmpi -# endif +# define strcasecmp _stricmp +# define strncasecmp _strnicmp # define strtok_r strtok_s # define strerror_r(e, b, l) strerror_s(b, l, e) + +# ifdef _MSC_VER +# include +typedef SSIZE_T ssize_t; +# endif #endif diff --git a/lib/actions_publish.c b/lib/actions_publish.c index a38eecd5..65c4d232 100644 --- a/lib/actions_publish.c +++ b/lib/actions_publish.c @@ -18,7 +18,6 @@ Contributors: #include "config.h" -#include #include #include diff --git a/lib/packet_datatypes.c b/lib/packet_datatypes.c index 68a90ca8..3e3d4e02 100644 --- a/lib/packet_datatypes.c +++ b/lib/packet_datatypes.c @@ -19,9 +19,11 @@ Contributors: #include "config.h" #include -#include #include #include +#ifndef WIN32 +# include +#endif #ifdef WITH_BROKER # include "mosquitto_broker_internal.h" diff --git a/plugins/examples/payload-size-stats/mosquitto_payload_size_stats.c b/plugins/examples/payload-size-stats/mosquitto_payload_size_stats.c index 2c8c3efb..9e7f438a 100644 --- a/plugins/examples/payload-size-stats/mosquitto_payload_size_stats.c +++ b/plugins/examples/payload-size-stats/mosquitto_payload_size_stats.c @@ -77,7 +77,7 @@ static time_t last_report = 0; static int callback_tick(int event, void *event_data, void *userdata) { - struct timespec ts; + time_t sec; char topic[40]; char payload[40]; int slen; @@ -87,9 +87,9 @@ static int callback_tick(int event, void *event_data, void *userdata) UNUSED(event_data); UNUSED(userdata); - clock_gettime(CLOCK_REALTIME, &ts); - if(last_report + 10 < ts.tv_sec){ - last_report = ts.tv_sec; + sec = time(NULL); + if(last_report + 10 < sec){ + last_report = sec; for(i=0; i #include #include +#ifdef WIN32 +# include +#endif #include "mosquitto.h" #include "mosquitto_broker.h" @@ -66,7 +69,11 @@ static int get_db_file(struct mosquitto_opt *options, int option_count) persistence_location = mosquitto_persistence_location(); if(persistence_location){ +#ifdef WIN32 + (void)mkdir(persistence_location); +#else mkdir(persistence_location, 0770); +#endif plg_data.db_file = malloc(strlen(persistence_location) + 1 + strlen("/mosquitto.sqlite3")); if(!plg_data.db_file){ mosquitto_log_printf(MOSQ_LOG_INFO, "Sqlite persistence: Out of memory."); diff --git a/src/database.c b/src/database.c index dff64cf8..6556b9b3 100644 --- a/src/database.c +++ b/src/database.c @@ -886,18 +886,34 @@ int db__messages_easy_queue(struct mosquitto *context, const char *topic, uint8_ */ uint64_t db__new_msg_id(void) { +#ifdef WIN32 + FILETIME ftime; + uint64_t ftime64; +#else struct timespec ts; +#endif uint64_t id; uint64_t tmp; + time_t sec; + long nsec; id = db.node_id_shifted; /* Top 10-bits */ +#ifdef WIN32 + GetSystemTimePreciseAsFileTime(&ftime); + ftime64 = (((uint64_t)ftime.dwHighDateTime)<<32) + ftime.dwLowDateTime; + tmp = ftime64 - 116444736000000000LL; /* Convert offset to unix epoch, still in counts of 100ns */ + sec = tmp / 10000000; /* Convert to seconds */ + nsec = (long)(tmp - sec)*100; /* Remove seconds, convert to counts of 1ns */ +#else clock_gettime(CLOCK_REALTIME, &ts); - - tmp = (ts.tv_sec - MOSQ_UUID_EPOCH) & 0x7FFFFFFF; + sec = ts.tv_sec; + ns = ts.tv_nsec; +#endif + tmp = (sec - MOSQ_UUID_EPOCH) & 0x7FFFFFFF; id = id | (tmp << 23); /* Seconds, 31-bits (68 years) */ - tmp = (ts.tv_nsec & 0x7FFFFF80); /* top 23-bits of the bottom 30 bits (1 billion ns), ~100 ns resolution */ + tmp = (nsec & 0x7FFFFF80); /* top 23-bits of the bottom 30 bits (1 billion ns), ~100 ns resolution */ id = id | (tmp >> 7); while(id <= db.last_db_id){