From 96fad648096614c13e784f11f0e90a583e567de1 Mon Sep 17 00:00:00 2001 From: Oskari Timperi Date: Wed, 15 Feb 2017 22:35:52 +0200 Subject: Make the code compile using MinGW - implemented GetCurrentTime() for Windows - had to rename GetCurrentTime() to MqttGetCurrentTime() because of name clashes - setup headers correctly --- src/client.c | 6 +++--- src/misc.c | 18 ++++++++++++++++-- src/misc.h | 2 +- src/socket.c | 4 +--- src/socketstream.c | 4 ++++ src/win32.h | 10 ++++++++++ 6 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 src/win32.h (limited to 'src') diff --git a/src/client.c b/src/client.c index 56dbe1a..6839867 100644 --- a/src/client.c +++ b/src/client.c @@ -367,7 +367,7 @@ int MqttClientRunOnce(MqttClient *client) } else if (SIMPLEQ_EMPTY(&client->sendQueue)) { - int64_t elapsed = GetCurrentTime() - client->lastPacketSentTime; + int64_t elapsed = MqttGetCurrentTime() - client->lastPacketSentTime; if (elapsed/1000 >= client->keepAlive) { MqttClientQueueSimplePacket(client, MqttPacketTypePingReq); @@ -582,7 +582,7 @@ static int MqttClientSendPacket(MqttClient *client, MqttPacket *packet) if (MqttPacketSerialize(packet, &client->stream.base) == -1) return -1; - packet->sentAt = GetCurrentTime(); + packet->sentAt = MqttGetCurrentTime(); client->lastPacketSentTime = packet->sentAt; if (packet->type == MqttPacketTypeDisconnect) @@ -968,7 +968,7 @@ static uint16_t MqttClientNextPacketId(MqttClient *client) static int64_t MqttPacketTimeSinceSent(MqttPacket *packet) { - int64_t now = GetCurrentTime(); + int64_t now = MqttGetCurrentTime(); return now - packet->sentAt; } diff --git a/src/misc.c b/src/misc.c index f276020..d6f9e64 100644 --- a/src/misc.c +++ b/src/misc.c @@ -4,9 +4,23 @@ #include #if defined(_WIN32) -#error GetCurrentTime implementation missing +#include "win32.h" +int64_t MqttGetCurrentTime() +{ + static volatile long once = 0; + static LARGE_INTEGER frequency; + LARGE_INTEGER counter; + if (InterlockedCompareExchange(&once, 1, 0) == 0) + { + QueryPerformanceFrequency(&frequency); + } + QueryPerformanceCounter(&counter); + counter.QuadPart *= 1000; + counter.QuadPart /= frequency.QuadPart; + return counter.QuadPart; +} #else -int64_t GetCurrentTime() +int64_t MqttGetCurrentTime() { struct timespec t; diff --git a/src/misc.h b/src/misc.h index 8cea322..0634bb6 100644 --- a/src/misc.h +++ b/src/misc.h @@ -10,7 +10,7 @@ Returns the current time as milliseconds. The return value can only be compared to another return value of the function. */ -int64_t GetCurrentTime(); +int64_t MqttGetCurrentTime(); /* Simple hexdump to stdout. diff --git a/src/socket.c b/src/socket.c index 38c968e..dfe4506 100644 --- a/src/socket.c +++ b/src/socket.c @@ -5,9 +5,7 @@ #include #if defined(_WIN32) -#error not implemented yet -#define WIN32_MEAN_AND_LEAN 1 -#include +#include "win32.h" #else #include #include diff --git a/src/socketstream.c b/src/socketstream.c index bef1803..73da239 100644 --- a/src/socketstream.c +++ b/src/socketstream.c @@ -3,7 +3,11 @@ #include #include +#if !defined(_WIN32) #include +#else +#include "win32.h" +#endif /* close */ #include diff --git a/src/win32.h b/src/win32.h new file mode 100644 index 0000000..b12ca23 --- /dev/null +++ b/src/win32.h @@ -0,0 +1,10 @@ +#ifndef MQTT_WIN32_H +#define MQTT_WIN32_H + +#if defined(_WIN32) +#include +#include +#include +#endif + +#endif -- cgit v1.2.3