2015-11-01 01:26:04 +08:00
|
|
|
/**
|
|
|
|
|
* TcpConnection.cpp
|
|
|
|
|
*
|
|
|
|
|
* Implementation file for the TCP connection
|
|
|
|
|
*
|
|
|
|
|
* @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
|
2018-03-10 21:55:57 +08:00
|
|
|
* @copyright 2015 - 2018 Copernica BV
|
2015-11-01 01:26:04 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Dependencies
|
|
|
|
|
*/
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
#include "tcpresolver.h"
|
2018-03-08 17:02:42 +08:00
|
|
|
#include "tcpstate.h"
|
2015-11-01 01:26:04 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set up namespace
|
|
|
|
|
*/
|
|
|
|
|
namespace AMQP {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor
|
|
|
|
|
* @param handler User implemented handler object
|
2015-11-01 04:13:41 +08:00
|
|
|
* @param hostname The address to connect to
|
2015-11-01 01:26:04 +08:00
|
|
|
*/
|
2015-11-01 04:13:41 +08:00
|
|
|
TcpConnection::TcpConnection(TcpHandler *handler, const Address &address) :
|
2018-11-05 06:34:31 +08:00
|
|
|
_handler(handler),
|
|
|
|
|
_state(new TcpResolver(this, address.hostname(), address.port(), address.secure())),
|
2015-11-01 04:13:41 +08:00
|
|
|
_connection(this, address.login(), address.vhost()) {}
|
|
|
|
|
|
2018-03-10 21:55:57 +08:00
|
|
|
/**
|
|
|
|
|
* Destructor
|
|
|
|
|
*/
|
|
|
|
|
TcpConnection::~TcpConnection() noexcept = default;
|
|
|
|
|
|
2017-12-13 00:10:51 +08:00
|
|
|
/**
|
|
|
|
|
* The filedescriptor that is used for this connection
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
int TcpConnection::fileno() const
|
|
|
|
|
{
|
|
|
|
|
// pass on to the state object
|
|
|
|
|
return _state->fileno();
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-01 17:54:05 +08:00
|
|
|
/**
|
|
|
|
|
* The number of outgoing bytes queued on this connection.
|
2018-04-02 04:34:15 +08:00
|
|
|
* @return std::size_t
|
2018-04-01 17:54:05 +08:00
|
|
|
*/
|
2018-04-02 04:34:15 +08:00
|
|
|
std::size_t TcpConnection::queued() const
|
2018-04-01 17:54:05 +08:00
|
|
|
{
|
2018-04-02 04:34:15 +08:00
|
|
|
return _state->queued();
|
2018-04-01 17:54:05 +08:00
|
|
|
}
|
|
|
|
|
|
2015-11-01 01:26:04 +08:00
|
|
|
/**
|
|
|
|
|
* Process the TCP connection
|
|
|
|
|
* This method should be called when the filedescriptor that is registered
|
|
|
|
|
* in the event loop becomes active. You should pass in a flag holding the
|
|
|
|
|
* flags AMQP::readable or AMQP::writable to indicate whether the descriptor
|
|
|
|
|
* was readable or writable, or bitwise-or if it was both
|
|
|
|
|
* @param fd The filedescriptor that became readable or writable
|
|
|
|
|
* @param events What sort of events occured?
|
|
|
|
|
*/
|
|
|
|
|
void TcpConnection::process(int fd, int flags)
|
|
|
|
|
{
|
2018-03-08 17:02:42 +08:00
|
|
|
// monitor the object for destruction, because you never know what the user
|
|
|
|
|
Monitor monitor(this);
|
2018-11-05 06:34:31 +08:00
|
|
|
|
2018-06-11 02:46:11 +08:00
|
|
|
// store the old state
|
|
|
|
|
auto *oldstate = _state.get();
|
|
|
|
|
|
2015-11-01 01:26:04 +08:00
|
|
|
// pass on the the state, that returns a new impl
|
2018-06-11 02:46:11 +08:00
|
|
|
auto *newstate = _state->process(monitor, fd, flags);
|
|
|
|
|
|
|
|
|
|
// if the state did not change, we do not have to update a member,
|
|
|
|
|
// when the newstate is nullptr, the object is (being) destructed
|
|
|
|
|
// and we do not have to do anything else either
|
|
|
|
|
if (oldstate == newstate || newstate == nullptr) return;
|
2016-01-15 21:19:09 +08:00
|
|
|
|
2018-06-11 02:46:11 +08:00
|
|
|
// in a bizarre set of circumstances, the user may have implemented the
|
|
|
|
|
// handler in such a way that the connection object was destructed
|
|
|
|
|
if (!monitor.valid())
|
2018-06-01 23:30:30 +08:00
|
|
|
{
|
2018-06-11 02:46:11 +08:00
|
|
|
// ok, user code is weird, connection object no longer exist, get rid of the state too
|
|
|
|
|
delete newstate;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// replace it with the new implementation
|
|
|
|
|
_state.reset(newstate);
|
2018-06-01 23:30:30 +08:00
|
|
|
}
|
2015-11-01 01:26:04 +08:00
|
|
|
}
|
|
|
|
|
|
2016-09-19 16:19:06 +08:00
|
|
|
/**
|
|
|
|
|
* Flush the tcp connection
|
|
|
|
|
*/
|
|
|
|
|
void TcpConnection::flush()
|
|
|
|
|
{
|
|
|
|
|
// monitor the object for destruction
|
|
|
|
|
Monitor monitor(this);
|
|
|
|
|
|
|
|
|
|
// keep looping
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
// flush the object
|
2018-03-08 17:02:42 +08:00
|
|
|
auto *newstate = _state->flush(monitor);
|
2016-09-19 16:19:06 +08:00
|
|
|
|
|
|
|
|
// done if object no longer exists
|
|
|
|
|
if (!monitor.valid()) return;
|
|
|
|
|
|
|
|
|
|
// also done if the object is still in the same state
|
|
|
|
|
if (newstate == _state.get()) return;
|
|
|
|
|
|
|
|
|
|
// replace the new state
|
|
|
|
|
_state.reset(newstate);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-29 01:13:13 +08:00
|
|
|
/**
|
|
|
|
|
* Close the connection
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
bool TcpConnection::close(bool immediate)
|
|
|
|
|
{
|
2018-11-05 22:49:22 +08:00
|
|
|
// @todo what if not yet connected / still doing a lookup / already disconnected?
|
2018-11-05 06:34:31 +08:00
|
|
|
|
2018-10-29 01:13:13 +08:00
|
|
|
// if no immediate disconnect is needed, we can simply start the closing handshake
|
|
|
|
|
if (!immediate) return _connection.close();
|
|
|
|
|
|
2018-11-05 06:34:31 +08:00
|
|
|
// fail the connection / report the error to user-space
|
|
|
|
|
_connection.fail("connection prematurely closed by client");
|
2018-10-29 01:13:13 +08:00
|
|
|
|
2018-11-05 22:49:22 +08:00
|
|
|
// change the state
|
|
|
|
|
_state.reset(new TcpClosed(_state.get()));
|
|
|
|
|
|
2018-10-29 01:13:13 +08:00
|
|
|
// done, we return true because the connection is closed
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-02 17:46:55 +08:00
|
|
|
/**
|
|
|
|
|
* Method that is called when the heartbeat frequency is negotiated.
|
|
|
|
|
* @param connection The connection that suggested a heartbeat interval
|
|
|
|
|
* @param interval The suggested interval from the server
|
|
|
|
|
* @return uint16_t The interval to use
|
|
|
|
|
*/
|
|
|
|
|
uint16_t TcpConnection::onNegotiate(Connection *connection, uint16_t interval)
|
|
|
|
|
{
|
2018-11-05 06:34:31 +08:00
|
|
|
// tell the max-frame size
|
|
|
|
|
_state->maxframe(connection->maxFrame());
|
|
|
|
|
|
|
|
|
|
// tell the handler
|
|
|
|
|
return _handler->onNegotiate(this, interval);
|
2015-12-02 17:46:55 +08:00
|
|
|
}
|
|
|
|
|
|
2015-11-01 01:26:04 +08:00
|
|
|
/**
|
|
|
|
|
* Method that is called by the connection when data needs to be sent over the network
|
|
|
|
|
* @param connection The connection that created this output
|
|
|
|
|
* @param buffer Data to send
|
|
|
|
|
* @param size Size of the buffer
|
|
|
|
|
*/
|
|
|
|
|
void TcpConnection::onData(Connection *connection, const char *buffer, size_t size)
|
|
|
|
|
{
|
2016-03-05 00:34:28 +08:00
|
|
|
// send the data over the connection
|
2015-11-01 01:26:04 +08:00
|
|
|
_state->send(buffer, size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method called when the connection ends up in an error state
|
|
|
|
|
* @param connection The connection that entered the error state
|
|
|
|
|
* @param message Error message
|
|
|
|
|
*/
|
|
|
|
|
void TcpConnection::onError(Connection *connection, const char *message)
|
|
|
|
|
{
|
2018-11-05 06:34:31 +08:00
|
|
|
// monitor to check if "this" is destructed
|
|
|
|
|
Monitor monitor(this);
|
|
|
|
|
|
|
|
|
|
// remember the old state (this is necessary because _state may be modified by user-code)
|
|
|
|
|
auto *oldstate = _state.get();
|
|
|
|
|
|
|
|
|
|
// tell the state that an error occured at the amqp level
|
|
|
|
|
auto *newstate = _state->onAmqpError(monitor, message);
|
|
|
|
|
|
|
|
|
|
// leap out if nothing changes
|
|
|
|
|
if (newstate == nullptr || newstate == oldstate) return;
|
|
|
|
|
|
|
|
|
|
// assign the new state
|
|
|
|
|
_state.reset(newstate);
|
2015-11-01 01:26:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method that is called when the connection was closed.
|
|
|
|
|
* @param connection The connection that was closed and that is now unusable
|
|
|
|
|
*/
|
|
|
|
|
void TcpConnection::onClosed(Connection *connection)
|
|
|
|
|
{
|
2018-11-05 06:34:31 +08:00
|
|
|
// monitor to check if "this" is destructed
|
|
|
|
|
Monitor monitor(this);
|
|
|
|
|
|
|
|
|
|
// remember the old state (this is necessary because _state may be modified by user-code)
|
|
|
|
|
auto *oldstate = _state.get();
|
|
|
|
|
|
|
|
|
|
// tell the state that the connection was closed at the amqp level
|
|
|
|
|
auto *newstate = _state->onAmqpClosed(monitor);
|
|
|
|
|
|
|
|
|
|
// leap out if nothing changes
|
|
|
|
|
if (newstate == nullptr || newstate == oldstate) return;
|
|
|
|
|
|
|
|
|
|
// assign the new state
|
|
|
|
|
_state.reset(newstate);
|
2015-11-01 01:26:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* End of namespace
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
|