2015-11-01 01:26:04 +08:00
|
|
|
/**
|
|
|
|
|
* TcpConnected.h
|
|
|
|
|
*
|
|
|
|
|
* The actual tcp connection - this is the "_impl" of a tcp-connection after
|
|
|
|
|
* the hostname was resolved into an IP address
|
|
|
|
|
*
|
|
|
|
|
* @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
|
2018-03-06 00:29:37 +08:00
|
|
|
* @copyright 2015 - 2018 Copernica BV
|
2015-11-01 01:26:04 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Include guard
|
|
|
|
|
*/
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Dependencies
|
|
|
|
|
*/
|
2016-06-16 01:41:14 +08:00
|
|
|
#include "tcpoutbuffer.h"
|
2016-06-16 01:32:30 +08:00
|
|
|
#include "tcpinbuffer.h"
|
2018-03-06 00:29:37 +08:00
|
|
|
#include "wait.h"
|
2015-11-01 01:26:04 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set up namespace
|
|
|
|
|
*/
|
|
|
|
|
namespace AMQP {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class definition
|
|
|
|
|
*/
|
2015-11-01 16:43:17 +08:00
|
|
|
class TcpConnected : public TcpState, private Watchable
|
2015-11-01 01:26:04 +08:00
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
/**
|
|
|
|
|
* The socket file descriptor
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
|
|
|
|
int _socket;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The outgoing buffer
|
2016-06-16 01:41:14 +08:00
|
|
|
* @var TcpOutBuffer
|
2015-11-01 01:26:04 +08:00
|
|
|
*/
|
2016-06-16 01:41:14 +08:00
|
|
|
TcpOutBuffer _out;
|
2015-11-01 01:26:04 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* An incoming buffer
|
2016-06-16 01:32:30 +08:00
|
|
|
* @var TcpInBuffer
|
2015-11-01 01:26:04 +08:00
|
|
|
*/
|
2016-06-16 01:32:30 +08:00
|
|
|
TcpInBuffer _in;
|
2015-11-01 01:26:04 +08:00
|
|
|
|
2016-06-22 19:49:49 +08:00
|
|
|
/**
|
|
|
|
|
* Cached reallocation instruction
|
|
|
|
|
* @var size_t
|
|
|
|
|
*/
|
|
|
|
|
size_t _reallocate = 0;
|
|
|
|
|
|
|
|
|
|
|
2015-11-23 16:40:54 +08:00
|
|
|
/**
|
|
|
|
|
* Helper method to report an error
|
|
|
|
|
* @return bool Was an error reported?
|
|
|
|
|
*/
|
|
|
|
|
bool reportError()
|
|
|
|
|
{
|
|
|
|
|
// some errors are ok and do not (necessarily) mean that we're disconnected
|
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) return false;
|
|
|
|
|
|
|
|
|
|
// we have an error - report this to the user
|
|
|
|
|
_handler->onError(_connection, strerror(errno));
|
|
|
|
|
|
|
|
|
|
// done
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Construct the next state
|
|
|
|
|
* @param monitor Object that monitors whether connection still exists
|
|
|
|
|
* @return TcpState*
|
|
|
|
|
*/
|
|
|
|
|
TcpState *nextState(const Monitor &monitor)
|
|
|
|
|
{
|
|
|
|
|
// if the object is still in a valid state, we can move to the close-state,
|
|
|
|
|
// otherwise there is no point in moving to a next state
|
|
|
|
|
return monitor.valid() ? new TcpClosed(this) : nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-01 01:26:04 +08:00
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* Constructor
|
|
|
|
|
* @param connection Parent TCP connection object
|
|
|
|
|
* @param socket The socket filedescriptor
|
|
|
|
|
* @param buffer The buffer that was already built
|
|
|
|
|
* @param handler User-supplied handler object
|
|
|
|
|
*/
|
2016-06-16 01:41:14 +08:00
|
|
|
TcpConnected(TcpConnection *connection, int socket, TcpOutBuffer &&buffer, TcpHandler *handler) :
|
2015-11-01 01:26:04 +08:00
|
|
|
TcpState(connection, handler),
|
|
|
|
|
_socket(socket),
|
2016-06-16 01:32:30 +08:00
|
|
|
_out(std::move(buffer)),
|
|
|
|
|
_in(4096)
|
2015-11-01 01:26:04 +08:00
|
|
|
{
|
|
|
|
|
// if there is already an output buffer, we have to send out that first
|
|
|
|
|
if (_out) _out.sendto(_socket);
|
|
|
|
|
|
|
|
|
|
// tell the handler to monitor the socket, if there is an out
|
|
|
|
|
_handler->monitor(_connection, _socket, _out ? readable | writable : readable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Destructor
|
|
|
|
|
*/
|
2016-07-01 16:07:01 +08:00
|
|
|
virtual ~TcpConnected() noexcept
|
2015-11-01 01:26:04 +08:00
|
|
|
{
|
2017-04-26 21:33:17 +08:00
|
|
|
// skip if handler is already forgotten
|
|
|
|
|
if (_handler == nullptr) return;
|
|
|
|
|
|
2015-11-01 01:26:04 +08:00
|
|
|
// we no longer have to monitor the socket
|
|
|
|
|
_handler->monitor(_connection, _socket, 0);
|
|
|
|
|
|
|
|
|
|
// close the socket
|
|
|
|
|
close(_socket);
|
|
|
|
|
}
|
2017-12-13 00:10:51 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The filedescriptor of this connection
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
virtual int fileno() const override { return _socket; }
|
2015-11-01 01:26:04 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Process the filedescriptor in the object
|
|
|
|
|
* @param fd Filedescriptor that is active
|
|
|
|
|
* @param flags AMQP::readable and/or AMQP::writable
|
|
|
|
|
* @return New state object
|
|
|
|
|
*/
|
|
|
|
|
virtual TcpState *process(int fd, int flags) override
|
|
|
|
|
{
|
|
|
|
|
// must be the socket
|
|
|
|
|
if (fd != _socket) return this;
|
2015-11-23 16:40:54 +08:00
|
|
|
|
|
|
|
|
// because the object might soon be destructed, we create a monitor to check this
|
|
|
|
|
Monitor monitor(this);
|
2015-11-01 01:26:04 +08:00
|
|
|
|
|
|
|
|
// can we write more data to the socket?
|
|
|
|
|
if (flags & writable)
|
|
|
|
|
{
|
|
|
|
|
// send out the buffered data
|
2015-11-23 16:40:54 +08:00
|
|
|
auto result = _out.sendto(_socket);
|
|
|
|
|
|
|
|
|
|
// are we in an error state?
|
|
|
|
|
if (result < 0 && reportError()) return nextState(monitor);
|
2015-11-01 01:26:04 +08:00
|
|
|
|
|
|
|
|
// if buffer is empty by now, we no longer have to check for
|
|
|
|
|
// writability, but only for readability
|
|
|
|
|
if (!_out) _handler->monitor(_connection, _socket, readable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// should we check for readability too?
|
|
|
|
|
if (flags & readable)
|
|
|
|
|
{
|
|
|
|
|
// read data from buffer
|
2016-06-16 01:32:30 +08:00
|
|
|
ssize_t result = _in.receivefrom(_socket, _connection->expected());
|
2015-11-01 01:26:04 +08:00
|
|
|
|
2015-11-23 16:40:54 +08:00
|
|
|
// are we in an error state?
|
|
|
|
|
if (result < 0 && reportError()) return nextState(monitor);
|
2016-06-16 02:50:33 +08:00
|
|
|
|
2015-11-23 16:40:54 +08:00
|
|
|
// we need a local copy of the buffer - because it is possible that "this"
|
|
|
|
|
// object gets destructed halfway through the call to the parse() method
|
2016-06-16 01:32:30 +08:00
|
|
|
TcpInBuffer buffer(std::move(_in));
|
2015-11-01 16:43:17 +08:00
|
|
|
|
2015-11-23 16:40:54 +08:00
|
|
|
// parse the buffer
|
|
|
|
|
auto processed = _connection->parse(buffer);
|
2015-11-01 16:43:17 +08:00
|
|
|
|
2015-11-23 16:40:54 +08:00
|
|
|
// "this" could be removed by now, check this
|
|
|
|
|
if (!monitor.valid()) return nullptr;
|
|
|
|
|
|
|
|
|
|
// shrink buffer
|
|
|
|
|
buffer.shrink(processed);
|
|
|
|
|
|
|
|
|
|
// restore the buffer as member
|
2016-06-16 02:50:33 +08:00
|
|
|
_in = std::move(buffer);
|
2016-06-22 19:49:49 +08:00
|
|
|
|
|
|
|
|
// do we have to reallocate?
|
2018-03-06 05:24:19 +08:00
|
|
|
if (_reallocate) _in.reallocate(_reallocate);
|
|
|
|
|
|
|
|
|
|
// we can remove the reallocate instruction
|
|
|
|
|
_reallocate = 0;
|
2015-11-01 01:26:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// keep same object
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send data over the connection
|
|
|
|
|
* @param buffer buffer to send
|
|
|
|
|
* @param size size of the buffer
|
|
|
|
|
*/
|
2015-11-28 18:38:15 +08:00
|
|
|
virtual void send(const char *buffer, size_t size) override
|
2015-11-01 01:26:04 +08:00
|
|
|
{
|
|
|
|
|
// is there already a buffer of data that can not be sent?
|
|
|
|
|
if (_out) return _out.add(buffer, size);
|
2016-07-06 19:03:51 +08:00
|
|
|
|
2015-11-01 01:26:04 +08:00
|
|
|
// there is no buffer, send the data right away
|
2016-07-11 02:44:14 +08:00
|
|
|
auto result = ::send(_socket, buffer, size, AMQP_CPP_MSG_NOSIGNAL);
|
2016-07-06 19:03:51 +08:00
|
|
|
|
2015-11-01 01:26:04 +08:00
|
|
|
// number of bytes sent
|
|
|
|
|
size_t bytes = result < 0 ? 0 : result;
|
|
|
|
|
|
|
|
|
|
// ok if all data was sent
|
|
|
|
|
if (bytes >= size) return;
|
|
|
|
|
|
|
|
|
|
// add the data to the buffer
|
|
|
|
|
_out.add(buffer + bytes, size - bytes);
|
|
|
|
|
|
|
|
|
|
// start monitoring the socket to find out when it is writable
|
|
|
|
|
_handler->monitor(_connection, _socket, readable | writable);
|
|
|
|
|
}
|
2016-09-19 16:19:06 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Flush the connection, sent all buffered data to the socket
|
|
|
|
|
* @return TcpState new tcp state
|
|
|
|
|
*/
|
|
|
|
|
virtual TcpState *flush() override
|
|
|
|
|
{
|
2018-03-06 15:40:44 +08:00
|
|
|
// create an object to wait for the filedescriptor to becomes active
|
|
|
|
|
Wait wait(_socket);
|
2018-03-06 00:29:37 +08:00
|
|
|
|
2018-03-07 05:03:53 +08:00
|
|
|
// keep running until the out buffer is not empty
|
2016-09-19 16:19:06 +08:00
|
|
|
while (_out)
|
|
|
|
|
{
|
|
|
|
|
// poll the socket, is it already writable?
|
2018-03-06 00:29:37 +08:00
|
|
|
if (!wait.writable()) return this;
|
2016-09-19 16:19:06 +08:00
|
|
|
|
|
|
|
|
// socket is writable, send as much data as possible
|
|
|
|
|
auto *newstate = process(_socket, writable);
|
|
|
|
|
|
|
|
|
|
// are we done
|
|
|
|
|
if (newstate != this) return newstate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// all has been sent
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2016-06-16 01:32:30 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Report that heartbeat negotiation is going on
|
|
|
|
|
* @param heartbeat suggested heartbeat
|
|
|
|
|
* @return uint16_t accepted heartbeat
|
|
|
|
|
*/
|
|
|
|
|
virtual uint16_t reportNegotiate(uint16_t heartbeat) override
|
|
|
|
|
{
|
2018-03-06 05:24:19 +08:00
|
|
|
// remember that we have to reallocate (_in member can not be accessed because it is moved away)
|
2016-06-22 19:49:49 +08:00
|
|
|
_reallocate = _connection->maxFrame();
|
2016-06-16 01:32:30 +08:00
|
|
|
|
|
|
|
|
// pass to base
|
|
|
|
|
return TcpState::reportNegotiate(heartbeat);
|
|
|
|
|
}
|
2017-04-26 21:33:17 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Report to the handler that the connection was nicely closed
|
|
|
|
|
*/
|
|
|
|
|
virtual void reportClosed() override
|
|
|
|
|
{
|
|
|
|
|
// we no longer have to monitor the socket
|
|
|
|
|
_handler->monitor(_connection, _socket, 0);
|
|
|
|
|
|
|
|
|
|
// close the socket
|
|
|
|
|
close(_socket);
|
|
|
|
|
|
|
|
|
|
// socket is closed now
|
|
|
|
|
_socket = -1;
|
|
|
|
|
|
|
|
|
|
// copy the handler (if might destruct this object)
|
|
|
|
|
auto *handler = _handler;
|
|
|
|
|
|
|
|
|
|
// reset member before the handler can make a mess of it
|
|
|
|
|
_handler = nullptr;
|
|
|
|
|
|
|
|
|
|
// notify to handler
|
|
|
|
|
handler->onClosed(_connection);
|
|
|
|
|
}
|
2015-11-01 01:26:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* End of namespace
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
|