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.
|
|
|
|
|
*
|
|
|
|
|
* @copyright 2014 Copernica BV
|
|
|
|
|
*/
|
|
|
|
|
|
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;
|
|
|
|
|
|
2015-12-02 17:46:55 +08:00
|
|
|
/**
|
|
|
|
|
* Method that is called when the heartbeat frequency is negotiated
|
|
|
|
|
* between the server and the client. You normally do not have to
|
|
|
|
|
* override this method, because in the default implementation the
|
|
|
|
|
* suggested heartbeat is simply accepted by the client.
|
|
|
|
|
*
|
|
|
|
|
* However, if you want to disable heartbeats, or when you want an
|
|
|
|
|
* alternative heartbeat interval, you can override this method
|
|
|
|
|
* to use an other interval. You should return 0 if you want to
|
|
|
|
|
* disable heartbeats.
|
|
|
|
|
*
|
|
|
|
|
* @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)
|
|
|
|
|
{
|
|
|
|
|
// default implementation, suggested heartbeat is ok
|
|
|
|
|
return interval;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-04 19:45:04 +08:00
|
|
|
/**
|
|
|
|
|
* Method that is called when data needs to be sent over the network
|
|
|
|
|
*
|
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
|
|
|
/**
|
|
|
|
|
* Method that is called when the server sends a heartbeat to the client
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
virtual void onHeartbeat(Connection *connection) {}
|
|
|
|
|
|
2014-01-04 19:45:04 +08:00
|
|
|
/**
|
|
|
|
|
* When the connection ends up in an error state this method is called.
|
|
|
|
|
* This happens when data comes in that does not match the AMQP protocol
|
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
|
|
|
|
|
*/
|
2014-04-15 20:22:30 +08:00
|
|
|
virtual void onError(Connection *connection, const char *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
|
2014-04-15 19:01:27 +08:00
|
|
|
* is called, the connection is ready to use. This is the first method
|
2014-01-07 00:15:21 +08:00
|
|
|
* that is normally called after you've constructed the connection object.
|
2014-04-14 20:10:57 +08:00
|
|
|
*
|
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
|
|
|
|
|
* using the Connection object. However, this AMQP library will cache all
|
|
|
|
|
* methods that you call before the connection is ready, so in reality there
|
2014-04-15 19:01:27 +08:00
|
|
|
* 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
|
|
|
|
|
*/
|
2014-01-07 00:15:21 +08:00
|
|
|
virtual void onConnected(Connection *connection) {}
|
2014-04-14 20:10:57 +08:00
|
|
|
|
2014-01-07 00:15:21 +08:00
|
|
|
/**
|
|
|
|
|
* Method that is called when the 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
|
|
|
|
|
* that the connection was correctly closed.
|
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
|
|
|
|
|
*/
|
|
|
|
|
virtual void onClosed(Connection *connection) {}
|
2014-04-14 20:10:57 +08:00
|
|
|
|
2014-01-04 19:45:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* End of namespace
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
|