Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented heartbeat timer for LibUV example #510

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion include/amqpcpp/libuv.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,30 @@ class LibUvHandler : public TcpHandler
*/
uv_loop_t *_loop;

/**
* timer
* @var uv_timer_t*
*/
uv_timer_t *_timer;

/**
* All I/O watchers that are active, indexed by their filedescriptor
* @var std::map<int,Watcher>
*/
std::map<int,std::unique_ptr<Watcher>> _watchers;

/**
* timer callback
* @param handle Internal timer handle
*/
static void timer_cb(uv_timer_t* handle)
{
// retrieving the connection
TcpConnection* conn = static_cast<TcpConnection*>(handle->data);

// telliing the connection to send a heartbeat to the broker
conn->heartbeat();
}

/**
* Method that is called by AMQP-CPP to register a filedescriptor for readability or writability
Expand Down Expand Up @@ -205,6 +223,30 @@ class LibUvHandler : public TcpHandler
}
}

/**
* Method that is called when the server sends a heartbeat to the client
* @param connection The connection over which the heartbeat was received
* @param interval agreed interval by the broker
* @see ConnectionHandler::onHeartbeat
*/
uint16_t onNegotiate(TcpConnection *connection, uint16_t interval) override
{
if(interval < 60) interval = 60;

// initialization of the timer
_timer = new uv_timer_t();
uv_timer_init(_loop, _timer);

// passing connection to be recieved at the callback
_timer->data = connection;

// starting the timer with callback
uv_timer_start(_timer, timer_cb, 0, interval*1000);

// returning the agreed heartbeat interval to the broker
return interval;
}

public:
/**
* Constructor
Expand All @@ -215,7 +257,18 @@ class LibUvHandler : public TcpHandler
/**
* Destructor
*/
virtual ~LibUvHandler() = default;
~LibUvHandler()
{
// stopping the timer
uv_timer_stop(_timer);

// closing the timer handle
uv_close(reinterpret_cast<uv_handle_t*>(_timer), [](uv_handle_t* handle){

// freeing the memory after callback
delete reinterpret_cast<uv_timer_t*>(handle);
});
};
};

/**
Expand Down