2014-01-04 19:45:04 +08:00
|
|
|
/**
|
|
|
|
|
* ConnectionHandler.h
|
|
|
|
|
*
|
|
|
|
|
* Interface that should be implemented by the caller of the library and
|
|
|
|
|
* that is passed to the AMQP connection. This interface contains all sorts
|
2014-04-14 20:10:57 +08:00
|
|
|
* of methods that are called when data needs to be sent, or when the
|
2014-01-04 19:45:04 +08:00
|
|
|
* AMQP connection ends up in a broken state.
|
|
|
|
|
*
|
2018-05-11 17:58:55 +08:00
|
|
|
* @copyright 2014 - 2018 Copernica BV
|
2014-01-04 19:45:04 +08:00
|
|
|
*/
|
|
|
|
|
|
2015-11-01 17:48:13 +08:00
|
|
|
/**
|
|
|
|
|
* Include guard
|
|
|
|
|
*/
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2016-06-23 20:42:50 +08:00
|
|
|
/**
|
|
|
|
|
* Dependencies
|
|
|
|
|
*/
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
2014-01-04 19:45:04 +08:00
|
|
|
/**
|
|
|
|
|
* Set up namespace
|
|
|
|
|
*/
|
|
|
|
|
namespace AMQP {
|
|
|
|
|
|
2016-06-23 20:42:50 +08:00
|
|
|
/**
|
|
|
|
|
* Forward declarations
|
|
|
|
|
*/
|
|
|
|
|
class Connection;
|
|
|
|
|
|
2014-01-04 19:45:04 +08:00
|
|
|
/**
|
|
|
|
|
* Class definition
|
|
|
|
|
*/
|
|
|
|
|
class ConnectionHandler
|
|
|
|
|
{
|
|
|
|
|
public:
|
2016-07-01 16:07:01 +08:00
|
|
|
/**
|
|
|
|
|
* Destructor
|
|
|
|
|
*/
|
|
|
|
|
virtual ~ConnectionHandler() = default;
|
2018-10-28 17:04:34 +08:00
|
|
|
|
2018-11-08 06:22:25 +08:00
|
|
|
/**
|
|
|
|
|
* When the connection is being set up, the client and server exchange
|
|
|
|
|
* some information. This includes for example their name and version,
|
|
|
|
|
* copyright statement and the operating system name. Nothing in this
|
|
|
|
|
* exchange of information is very relevant for the actual AMQP protocol,
|
|
|
|
|
* but by overriding this method you can read out the information that
|
|
|
|
|
* was sent by the server, and you can decide which information you
|
|
|
|
|
* want to send back that describe the client. In RabbitMQ's management
|
|
|
|
|
* console the client-properties are visible on the "connections" tab,
|
|
|
|
|
* which could be helpful in certain scenarios, like debugging.
|
|
|
|
|
*
|
|
|
|
|
* The read-only "server" parameter contains the information sent by
|
|
|
|
|
* the server, while the "client" table may be filled with information
|
|
|
|
|
* about your application. The AMQP protocol says that this table should
|
|
|
|
|
* at least be filled with data for the "product", "version", "platform",
|
|
|
|
|
* "copyright" and "information" keys. However, you do not have to
|
|
|
|
|
* override this method, and even when you do, you do not have to ensure
|
|
|
|
|
* that these properties are indeed set, because the AMQP-CPP library
|
|
|
|
|
* takes care of filling in properties that were not explicitly set.
|
|
|
|
|
*
|
|
|
|
|
* @param connection The connection about which information is exchanged
|
|
|
|
|
* @param server Properties sent by the server
|
|
|
|
|
* @param client Properties that are to be sent back
|
|
|
|
|
*/
|
|
|
|
|
virtual void onProperties(Connection *connection, const Table &server, Table &client)
|
|
|
|
|
{
|
|
|
|
|
// make sure compilers dont complaint about unused parameters
|
|
|
|
|
(void) connection;
|
|
|
|
|
(void) server;
|
|
|
|
|
(void) client;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-02 17:46:55 +08:00
|
|
|
/**
|
|
|
|
|
* Method that is called when the heartbeat frequency is negotiated
|
2018-10-28 17:04:34 +08:00
|
|
|
* between the server and the client durion connection setup. You
|
|
|
|
|
* normally do not have to override this method, because in the default
|
2018-11-08 06:22:25 +08:00
|
|
|
* implementation the suggested heartbeat is simply rejected by the client.
|
2015-12-02 17:46:55 +08:00
|
|
|
*
|
2018-11-08 06:22:25 +08:00
|
|
|
* However, if you want to enable heartbeats you can override this
|
|
|
|
|
* method. You should "return interval" if you want to accept the
|
|
|
|
|
* heartbeat interval that was suggested by the server, or you can
|
|
|
|
|
* return an alternative value if you want a shorter or longer interval.
|
|
|
|
|
* Return 0 if you want to disable heartbeats.
|
2017-06-16 17:14:42 +08:00
|
|
|
*
|
|
|
|
|
* If heartbeats are enabled, you yourself are responsible to send
|
2018-11-30 13:42:16 +08:00
|
|
|
* out a heartbeat every *interval / 2* number of seconds by calling
|
2017-06-16 17:14:42 +08:00
|
|
|
* the Connection::heartbeat() method.
|
2015-12-02 17:46:55 +08:00
|
|
|
*
|
|
|
|
|
* @param connection The connection that suggested a heartbeat interval
|
|
|
|
|
* @param interval The suggested interval from the server
|
|
|
|
|
* @return uint16_t The interval to use
|
|
|
|
|
*/
|
|
|
|
|
virtual uint16_t onNegotiate(Connection *connection, uint16_t interval)
|
|
|
|
|
{
|
2018-05-11 17:58:55 +08:00
|
|
|
// make sure compilers dont complain about unused parameters
|
2018-05-11 08:52:43 +08:00
|
|
|
(void) connection;
|
|
|
|
|
(void) interval;
|
2018-05-11 17:58:55 +08:00
|
|
|
|
|
|
|
|
// default implementation, disable heartbeats
|
2017-06-16 18:17:13 +08:00
|
|
|
return 0;
|
2015-12-02 17:46:55 +08:00
|
|
|
}
|
|
|
|
|
|
2014-01-04 19:45:04 +08:00
|
|
|
/**
|
2018-11-08 06:22:25 +08:00
|
|
|
* Method that is called by AMQP-CPP when data has to be sent over the
|
|
|
|
|
* network. You must implement this method and send the data over a
|
|
|
|
|
* socket that is connected with RabbitMQ.
|
2014-01-04 19:45:04 +08:00
|
|
|
*
|
2014-04-14 20:10:57 +08:00
|
|
|
* Note that the AMQP library does no buffering by itself. This means
|
2014-01-04 19:45:04 +08:00
|
|
|
* that this method should always send out all data or do the buffering
|
|
|
|
|
* itself.
|
|
|
|
|
*
|
|
|
|
|
* @param connection The connection that created this output
|
|
|
|
|
* @param buffer Data to send
|
|
|
|
|
* @param size Size of the buffer
|
|
|
|
|
*/
|
|
|
|
|
virtual void onData(Connection *connection, const char *buffer, size_t size) = 0;
|
2014-04-14 20:10:57 +08:00
|
|
|
|
2015-12-02 17:46:55 +08:00
|
|
|
/**
|
2018-11-08 06:22:25 +08:00
|
|
|
* Method that is called when the AMQP-CPP library received a heartbeat
|
|
|
|
|
* frame that was sent by the server to the client.
|
2015-12-02 17:46:55 +08:00
|
|
|
*
|
|
|
|
|
* You do not have to do anything here, the client sends back a heartbeat
|
|
|
|
|
* frame automatically, but if you like, you can implement/override this
|
|
|
|
|
* method if you want to be notified of such heartbeats
|
|
|
|
|
*
|
|
|
|
|
* @param connection The connection over which the heartbeat was received
|
|
|
|
|
*/
|
2018-05-11 17:58:55 +08:00
|
|
|
virtual void onHeartbeat(Connection *connection)
|
|
|
|
|
{
|
|
|
|
|
// make sure compilers dont complain about unused parameters
|
|
|
|
|
(void) connection;
|
|
|
|
|
}
|
2015-12-02 17:46:55 +08:00
|
|
|
|
2014-01-04 19:45:04 +08:00
|
|
|
/**
|
|
|
|
|
* When the connection ends up in an error state this method is called.
|
2018-11-08 06:22:25 +08:00
|
|
|
* This happens when data comes in that does not match the AMQP protocol,
|
|
|
|
|
* or when an error message was sent by the server to the client.
|
2014-04-14 20:10:57 +08:00
|
|
|
*
|
2014-01-04 19:45:04 +08:00
|
|
|
* After this method is called, the connection no longer is in a valid
|
2014-01-04 21:11:06 +08:00
|
|
|
* state and can no longer be used.
|
2014-04-14 20:10:57 +08:00
|
|
|
*
|
2014-01-07 00:15:21 +08:00
|
|
|
* This method has an empty default implementation, although you are very
|
2014-04-15 19:01:27 +08:00
|
|
|
* much advised to implement it. When an error occurs, the connection
|
2014-01-07 00:15:21 +08:00
|
|
|
* is no longer usable, so you probably want to know.
|
2014-04-14 20:10:57 +08:00
|
|
|
*
|
2014-01-04 19:45:04 +08:00
|
|
|
* @param connection The connection that entered the error state
|
|
|
|
|
* @param message Error message
|
|
|
|
|
*/
|
2018-05-11 08:52:43 +08:00
|
|
|
virtual void onError(Connection *connection, const char *message)
|
|
|
|
|
{
|
2018-05-11 17:58:55 +08:00
|
|
|
// make sure compilers dont complain about unused parameters
|
2018-05-11 08:52:43 +08:00
|
|
|
(void) connection;
|
|
|
|
|
(void) message;
|
|
|
|
|
}
|
2014-04-14 20:10:57 +08:00
|
|
|
|
2014-01-04 19:45:04 +08:00
|
|
|
/**
|
|
|
|
|
* Method that is called when the login attempt succeeded. After this method
|
2018-10-28 17:04:34 +08:00
|
|
|
* is called, the connection is ready to use, and the RabbitMQ server is
|
|
|
|
|
* ready to receive instructions.
|
|
|
|
|
*
|
2014-01-07 00:15:21 +08:00
|
|
|
* According to the AMQP protocol, you must wait for the connection to become
|
|
|
|
|
* ready (and this onConnected method to be called) before you can start
|
2018-11-08 06:22:25 +08:00
|
|
|
* sending instructions to RabbitMQ. However, if you prematurely do send
|
|
|
|
|
* instructions, this AMQP-CPP library caches all methods that you call
|
|
|
|
|
* before the connection is ready and flushes them the moment the connection
|
|
|
|
|
* has been set up, so technically there is no real reason to wait for this
|
|
|
|
|
* method to be called before you send the first instructions.
|
2014-01-04 19:45:04 +08:00
|
|
|
*
|
|
|
|
|
* @param connection The connection that can now be used
|
|
|
|
|
*/
|
2018-11-08 15:34:58 +08:00
|
|
|
virtual void onReady(Connection *connection)
|
2018-05-11 17:58:55 +08:00
|
|
|
{
|
|
|
|
|
// make sure compilers dont complain about unused parameters
|
|
|
|
|
(void) connection;
|
|
|
|
|
}
|
2014-04-14 20:10:57 +08:00
|
|
|
|
2014-01-07 00:15:21 +08:00
|
|
|
/**
|
2018-11-08 15:34:58 +08:00
|
|
|
* Method that is called when the AMQP connection was closed.
|
2014-04-14 20:10:57 +08:00
|
|
|
*
|
2014-01-07 00:15:21 +08:00
|
|
|
* This is the counter part of a call to Connection::close() and it confirms
|
2018-11-08 15:34:58 +08:00
|
|
|
* that the connection was _correctly_ closed. Note that this only applies
|
|
|
|
|
* to the AMQP connection, the underlying TCP connection is not managed by
|
|
|
|
|
* AMQP-CPP and is still active.
|
2014-04-14 20:10:57 +08:00
|
|
|
*
|
2014-01-07 00:15:21 +08:00
|
|
|
* @param connection The connection that was closed and that is now unusable
|
|
|
|
|
*/
|
2018-05-11 17:58:55 +08:00
|
|
|
virtual void onClosed(Connection *connection)
|
|
|
|
|
{
|
|
|
|
|
// make sure compilers dont complain about unused parameters
|
|
|
|
|
(void) connection;
|
|
|
|
|
}
|
2023-07-03 17:39:06 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method that is called when the AMQP connection was blocked.
|
|
|
|
|
*
|
|
|
|
|
* This method is called, when the server connection gets blocked for the first
|
|
|
|
|
* time due to the broker running low on a resource (memory or disk). For
|
|
|
|
|
* example, when a RabbitMQ node detects that it is low on RAM, it sends a
|
|
|
|
|
* notification to all connected publishing clients supporting this feature.
|
|
|
|
|
* If before the connections are unblocked the node also starts running low on
|
|
|
|
|
* disk space, another notification will not be sent.
|
|
|
|
|
*
|
|
|
|
|
* @param connection The connection that was blocked
|
2023-07-03 19:05:09 +08:00
|
|
|
* @param reason Why was the connection blocked
|
2023-07-03 17:39:06 +08:00
|
|
|
*/
|
2023-07-03 19:05:09 +08:00
|
|
|
virtual void onBlocked(Connection *connection, const char *reason)
|
|
|
|
|
{
|
2023-07-03 17:39:06 +08:00
|
|
|
// make sure compilers dont complain about unused parameters
|
|
|
|
|
(void) connection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method that is called when the AMQP connection is no longer blocked.
|
|
|
|
|
*
|
|
|
|
|
* This method is called when all resource alarms have cleared and the
|
|
|
|
|
* connection is fully unblocked.
|
|
|
|
|
*
|
|
|
|
|
* @param connection The connection that is no longer blocked
|
|
|
|
|
*/
|
|
|
|
|
virtual void onUnblocked(Connection *connection)
|
|
|
|
|
{
|
|
|
|
|
// make sure compilers dont complain about unused parameters
|
|
|
|
|
(void) connection;
|
|
|
|
|
}
|
2014-01-04 19:45:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* End of namespace
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
|