diff options
| -rw-r--r-- | src/client.c | 6 | ||||
| -rw-r--r-- | src/misc.c | 18 | ||||
| -rw-r--r-- | src/misc.h | 2 | ||||
| -rw-r--r-- | src/socket.c | 4 | ||||
| -rw-r--r-- | src/socketstream.c | 4 | ||||
| -rw-r--r-- | src/win32.h | 10 | ||||
| -rw-r--r-- | tools/amalgamate.py | 1 |
7 files changed, 36 insertions, 9 deletions
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; } @@ -4,9 +4,23 @@ #include <time.h> #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; @@ -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 <assert.h> #if defined(_WIN32) -#error not implemented yet -#define WIN32_MEAN_AND_LEAN 1 -#include <windows.h> +#include "win32.h" #else #include <sys/types.h> #include <sys/socket.h> 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 <assert.h> #include <string.h> +#if !defined(_WIN32) #include <arpa/inet.h> +#else +#include "win32.h" +#endif /* close */ #include <unistd.h> 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 <winsock2.h> +#include <ws2tcpip.h> +#include <windows.h> +#endif + +#endif diff --git a/tools/amalgamate.py b/tools/amalgamate.py index 03d2a14..f3fd6ba 100644 --- a/tools/amalgamate.py +++ b/tools/amalgamate.py @@ -8,6 +8,7 @@ src_dir = os.path.join(this_dir, '..', 'src') sources = ( 'config.h', + 'win32.h', 'queue.h', 'log.h', 'misc.c', |
