diff --git a/client.cpp b/client.cpp
index 709a53a..18d6c52 100644
--- a/client.cpp
+++ b/client.cpp
@@ -21,6 +21,7 @@ License along with FlashMQ. If not, see .
#include
#include
#include
+#include
#include "logger.h"
@@ -136,7 +137,7 @@ bool Client::readFdIntoBuffer()
return false;
}
- lastActivity = time(NULL);
+ lastActivity = std::chrono::steady_clock::now();
if (session)
session->touch(lastActivity);
@@ -273,17 +274,22 @@ bool Client::keepAliveExpired()
if (keepalive == 0)
return false;
+ const std::chrono::time_point now = std::chrono::steady_clock::now();
+
if (!authenticated)
- return lastActivity + 20 < time(NULL);
+ return lastActivity + std::chrono::seconds(20) < now;
- bool result = (lastActivity + (keepalive*10/5)) < time(NULL);
+ std::chrono::seconds x(keepalive*10/5);
+ bool result = (lastActivity + x) < now;
return result;
}
std::string Client::getKeepAliveInfoString() const
{
- std::string s = "authenticated: " + std::to_string(authenticated) + ", keep-alive: " + std::to_string(keepalive) + "s, last activity "
- + std::to_string(time(NULL) - lastActivity) + " seconds ago.";
+ std::chrono::seconds secondsSinceLastActivity = std::chrono::duration_cast(std::chrono::steady_clock::now() - lastActivity);
+
+ std::string s = formatString("authenticated=%s, keep-alive=%ss, last activity=%s seconds ago.", std::to_string(authenticated).c_str(), std::to_string(keepalive).c_str(),
+ std::to_string(secondsSinceLastActivity.count()).c_str());
return s;
}
diff --git a/client.h b/client.h
index 832b166..1bdf858 100644
--- a/client.h
+++ b/client.h
@@ -65,7 +65,7 @@ class Client
bool disconnectWhenBytesWritten = false;
bool disconnecting = false;
std::string disconnectReason;
- time_t lastActivity = time(NULL);
+ std::chrono::time_point lastActivity;
std::string clientid;
std::string username;
diff --git a/session.cpp b/session.cpp
index 92dda1a..10e57a3 100644
--- a/session.cpp
+++ b/session.cpp
@@ -164,15 +164,19 @@ uint64_t Session::sendPendingQosMessages()
return count;
}
-void Session::touch(time_t val)
+std::chrono::time_point zeroTime;
+std::chrono::seconds expireAfter(EXPIRE_SESSION_AFTER);
+
+void Session::touch(std::chrono::time_point val)
{
- time_t newval = val > 0 ? val : time(NULL);
+ std::chrono::time_point newval = val > zeroTime ? val : std::chrono::steady_clock::now();
lastTouched = newval;
}
bool Session::hasExpired()
{
- return clientDisconnected() && (lastTouched + EXPIRE_SESSION_AFTER) < time(NULL);
+ std::chrono::time_point now = std::chrono::steady_clock::now();
+ return clientDisconnected() && (lastTouched + expireAfter) < now;
}
void Session::addIncomingQoS2MessageId(uint16_t packet_id)
diff --git a/session.h b/session.h
index 94851e1..b9742f2 100644
--- a/session.h
+++ b/session.h
@@ -51,7 +51,7 @@ class Session
std::mutex qosQueueMutex;
uint16_t nextPacketId = 0;
ssize_t qosQueueBytes = 0;
- time_t lastTouched = time(NULL);
+ std::chrono::time_point lastTouched;
Logger *logger = Logger::getInstance();
public:
@@ -67,7 +67,7 @@ public:
void writePacket(const MqttPacket &packet, char max_qos, uint64_t &count);
void clearQosMessage(uint16_t packet_id);
uint64_t sendPendingQosMessages();
- void touch(time_t val = 0);
+ void touch(std::chrono::time_point val = std::chrono::time_point());
bool hasExpired();
void addIncomingQoS2MessageId(uint16_t packet_id);