Skip to content

Commit 729d62d

Browse files
committed
add heartbeat
1 parent fdf8ff2 commit 729d62d

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/main.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,17 @@ class AMQT {
186186
}
187187
}
188188

189-
while (isConnected()) {
189+
for (;;) {
190190
amqp_envelope_t envelope;
191191
amqp_maybe_release_buffers(m_connection);
192192
//struct timeval timeout = {1, 0};
193193
auto ret = amqp_consume_message(m_connection, &envelope, NULL, 0);
194194
if (AMQP_RESPONSE_NORMAL != ret.reply_type) {
195195
log::error("Response wasn't normal! {}", AMQErrorToString(ret));
196-
if (AMQP_RESPONSE_LIBRARY_EXCEPTION == ret.reply_type && AMQP_STATUS_UNEXPECTED_STATE == ret.library_error) {
196+
if (AMQP_RESPONSE_LIBRARY_EXCEPTION == ret.reply_type && AMQP_STATUS_HEARTBEAT_TIMEOUT == ret.library_error) {
197+
log::error("Heartbeat failed, initiating a reconnection!");
198+
break;
199+
} else if (AMQP_RESPONSE_LIBRARY_EXCEPTION == ret.reply_type && AMQP_STATUS_UNEXPECTED_STATE == ret.library_error) {
197200
amqp_frame_t frame;
198201
if (AMQP_STATUS_OK != amqp_simple_wait_frame(m_connection, &frame)) {
199202
log::error("Failed to wait for frame after unexpected state: {}", AMQErrorToString(ret));
@@ -246,7 +249,7 @@ class AMQT {
246249
int delayMs = 3000 * ((m_remainingAttempts + 1) - m_remainingAttempts);
247250
//TODO: fix it being stuck on 3 seconds
248251
log::warn("Disconnected from server. Attempting to reconnect... ({} left, reconnecting in {} seconds)", m_remainingAttempts, (float)((delayMs / 1000.F)));
249-
std::this_thread::sleep_for(std::chrono::seconds(delayMs / 1000));
252+
std::this_thread::sleep_for(std::chrono::milliseconds(delayMs));
250253
}
251254
m_connection = amqp_new_connection();
252255
amqp_socket_t* socket = amqp_tcp_socket_new(m_connection);
@@ -261,7 +264,8 @@ class AMQT {
261264
continue;
262265
}
263266
if (socket && m_connection) {
264-
amqp_rpc_reply_t reply = amqp_login(m_connection, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "gd", "GeometryDashisahorizontalrunnerstylegamedevelopedandpublishedbyRobTopGames");
267+
// heartbeat every 10 seconds!
268+
amqp_rpc_reply_t reply = amqp_login(m_connection, "/", 0, 131072, 10, AMQP_SASL_METHOD_PLAIN, "gd", "GeometryDashisahorizontalrunnerstylegamedevelopedandpublishedbyRobTopGames");
265269
if (reply.reply_type != AMQP_RESPONSE_NORMAL) {
266270
const char* errorStr = nullptr;
267271
log::error("Couldn't login to AMQP server! {}", AMQErrorToString(reply));

0 commit comments

Comments
 (0)