379 lines
10 KiB
C++
379 lines
10 KiB
C++
/**
|
|
* @file include/ActivMiddleware/ActivBase/Posix/Ip/TcpSocket.h
|
|
*
|
|
* @brief Header file for tcp socket class.
|
|
*/
|
|
|
|
#if (!defined ACTIV_BASE_POSIX_IP_TCP_SOCKET_H)
|
|
#define ACTIV_BASE_POSIX_IP_TCP_SOCKET_H
|
|
|
|
#include "ActivMiddleware/ActivBase/External.h"
|
|
#include "ActivMiddleware/ActivBase/Ip/IpSocket.h"
|
|
|
|
#include "ActivMiddleware/ActivBase/FieldTypes/Boolean.h"
|
|
#include "ActivMiddleware/ActivBase/FieldTypes/UInt.h"
|
|
|
|
/**
|
|
* @brief List of tcp socket states.
|
|
*/
|
|
#define ACTIV_TCP_SOCKET_STATE_LIST(d) \
|
|
d(STATE_INITIALIZED) \
|
|
d(STATE_W4_CONNECT) \
|
|
d(STATE_W4_ACCEPT) \
|
|
d(STATE_W4_ACCEPT_COMPLETION) \
|
|
d(STATE_CONNECTED) \
|
|
d(STATE_W4_DISCONNECT)
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
class TcpListener;
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief TCP socket class.
|
|
*/
|
|
class TcpSocket : public IpSocket
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Connection states.
|
|
*/
|
|
enum State
|
|
{
|
|
ACTIV_TCP_SOCKET_STATE_LIST(ACTIV_DECLARE_VALUE_WITH_COMMA)
|
|
NUM_STATES
|
|
};
|
|
|
|
/**
|
|
* @brief Settings.
|
|
*/
|
|
class Settings : public IpSocket::Settings
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param shouldCork set TCP_CORK, if available, whilst in tx loop.
|
|
*/
|
|
ACTIV_BASE_API Settings(const bool shouldCork = false);
|
|
};
|
|
|
|
/**
|
|
* @brief Stats.
|
|
*/
|
|
class Stats : public IpSocket::Stats
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* Note if constructing a Stats object directly, the socket's mutex must be locked (this isn't
|
|
* done by the Stats constructor). Alternatively, use the helper function IpSocket::Stats::Get() which will
|
|
* lock the mutex for you before gathering the stats.
|
|
*
|
|
* @param socket to get stats from.
|
|
*/
|
|
ACTIV_BASE_API Stats(const TcpSocket &socket);
|
|
|
|
State m_state; ///< Connection state.
|
|
bool m_connectPosted; ///< Connect posted.
|
|
bool m_acceptPosted; ///< Accept posted.
|
|
UInt m_txCompressionPercent; ///< If compressing, compression ratio as % (compressed-bytes / source-bytes).
|
|
|
|
// OS-level stats; may not be defined.
|
|
UInt m_txRtt;
|
|
UInt m_txCongestionWindowSize;
|
|
UInt m_totalRetransmissions;
|
|
UInt m_reorders;
|
|
Boolean m_isNagleEnabled;
|
|
};
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param ipManager the IpManager that owns the socket.
|
|
* @param settings settings for this socket.
|
|
*
|
|
* @throw Exception.
|
|
*/
|
|
ACTIV_BASE_API TcpSocket(IpManager &ipManager, const Settings &settings = Settings());
|
|
|
|
/**
|
|
* @brief Destructor.
|
|
*/
|
|
ACTIV_BASE_API ~TcpSocket();
|
|
|
|
/**
|
|
* @brief Initiate connection.
|
|
*
|
|
* @param remoteAddress the remote address to connect to.
|
|
* @param remotePort the remote port to connect to (local endian).
|
|
* @param localAddress the optional local address to bind to.
|
|
* @param localPort the optional local port number to bind to (local endian).
|
|
*
|
|
* @throw Exception.
|
|
*/
|
|
ACTIV_BASE_API void Connect(const char *remoteAddress, const uint16_t remotePort, const char *localAddress = 0,
|
|
const uint16_t localPort = 0);
|
|
|
|
/**
|
|
* @brief Initiate connection.
|
|
*
|
|
* @param remoteAddress the remote address to connect to.
|
|
* @param localAddress the local address to bind to.
|
|
*
|
|
* @throw Exception.
|
|
*/
|
|
ACTIV_BASE_API void Connect(const IpAddress &remoteAddress, const IpAddress &localAddress);
|
|
|
|
/**
|
|
* @brief Initiate connection.
|
|
*
|
|
* As Connect(), but do not obtain the socket mutex first (assumes the mutex is already locked).
|
|
*
|
|
* @param remoteAddress the remote address to connect to.
|
|
* @param localAddress the local address to bind to.
|
|
*
|
|
* @throw Exception.
|
|
*/
|
|
ACTIV_BASE_API void PreLockedConnect(const IpAddress &remoteAddress, const IpAddress &localAddress);
|
|
|
|
/**
|
|
* @brief Set up the local and remote addresses but do not initiate a connection.
|
|
*
|
|
* NB mutex must already be locked.
|
|
*
|
|
* @param remoteAddress the remote address to connect to.
|
|
* @param localAddress the local address to bind to.
|
|
*
|
|
* @throw Exception if the socket wasn't in STATE_INITIALIZED.
|
|
*/
|
|
ACTIV_BASE_API void PreLockedSetAddresses(const IpAddress &remoteAddress, const IpAddress &localAddress);
|
|
|
|
/**
|
|
* @brief Post a connect to an already set up address.
|
|
*
|
|
* NB mutex must already be locked.
|
|
*
|
|
* @throw Exception.
|
|
*/
|
|
ACTIV_BASE_API void PreLockedPostConnect();
|
|
|
|
/**
|
|
* @brief Post a connect to an already set up address.
|
|
*
|
|
* @throw Exception.
|
|
*/
|
|
ACTIV_BASE_API void PostConnect();
|
|
|
|
/**
|
|
* @brief Post an accept.
|
|
*
|
|
* @param listener the server to accept from.
|
|
*
|
|
* @throw Exception.
|
|
*/
|
|
ACTIV_BASE_API void Accept(TcpListener &listener);
|
|
|
|
/**
|
|
* @brief Close a connection.
|
|
*
|
|
* @param gracefully true to only close the socket once any outstanding tx has completed,
|
|
* false to force the socket closed immediately.
|
|
*
|
|
* @retval true the socket is fully closed.
|
|
* @retval false a close event will be fired when the socket is fully closed (OnDisconnect()).
|
|
*
|
|
* @throw Exception.
|
|
*/
|
|
ACTIV_BASE_API bool Disconnect(const bool gracefully = false);
|
|
|
|
/**
|
|
* @brief Forcably initiate a connection break. If the socket was connected, OnBreak() will be fired.
|
|
*
|
|
* Note this function will obtain the socket mutex for the duration of the call.
|
|
*/
|
|
ACTIV_BASE_API void LockedBreak();
|
|
|
|
/**
|
|
* @brief Forcably initiate a connection break. If the socket was connected, OnBreak() will be fired.
|
|
*
|
|
* As LockedBreak(), but do not obtain the socket mutex first (assumes the mutex is already locked).
|
|
*/
|
|
ACTIV_BASE_API void PreLockedBreak();
|
|
|
|
/**
|
|
* @brief Get the socket's state.
|
|
*
|
|
* @return Socket state.
|
|
*/
|
|
State GetState() const;
|
|
|
|
/**
|
|
* @brief Return state as string.
|
|
*
|
|
* @return Socket state in string form.
|
|
*/
|
|
ACTIV_BASE_API static String StateToString(const State state);
|
|
|
|
/**
|
|
* @brief Enable or disable clean shutdown of a connected socket.
|
|
*
|
|
* After a connection is established (OnConnect(), or OnAccept()), a socket will normally attempt to
|
|
* cleanly shutdown when Disconnect() is called (rather than close abortively), sending a FIN packet.
|
|
*
|
|
* However, this can cause very long delays in closing sockets with some configurations (e.g. connections to a proxy
|
|
* which is receiving an infinite HTTP response).
|
|
*
|
|
* Disabling shutdown will cause an abortive close (TCP RST) to be done instead.
|
|
*
|
|
* Note the clean shutdown is re-enabled after a reconnect.
|
|
*
|
|
* This function should only be called inside a callback or when the socket mutex is locked.
|
|
*
|
|
* @param isEnabled whether to enable or disable clean shutdown.
|
|
*/
|
|
void PreLockedEnableShutdown(const bool isEnabled);
|
|
|
|
protected:
|
|
// IpSocket overrides.
|
|
ACTIV_BASE_API virtual bool OnInitializeIo() ACTIV_OVERRIDE;
|
|
ACTIV_BASE_API virtual CompletionResult OnRxBuffers() ACTIV_OVERRIDE;
|
|
ACTIV_BASE_API virtual CompletionResult OnDummyRxCompletion(const size_t numberOfBytes) const ACTIV_OVERRIDE;
|
|
ACTIV_BASE_API virtual CompletionResult OnTxBuffers() ACTIV_OVERRIDE;
|
|
|
|
/**
|
|
* @brief Connection has closed at the user's request.
|
|
*/
|
|
ACTIV_BASE_API virtual void OnDisconnect();
|
|
|
|
/**
|
|
* @brief Connection has broken (the user didn't initiate).
|
|
*/
|
|
ACTIV_BASE_API virtual void OnBreak();
|
|
|
|
/**
|
|
* @brief Connect has been initiated asynchronously.
|
|
*
|
|
* @throw If an exception propagates outside of an overriden OnConnectPosted(), the connection attempt is aborted and will
|
|
* result in an OnConnectFailed() callback.
|
|
*/
|
|
ACTIV_BASE_API virtual void OnConnectPosted();
|
|
|
|
/**
|
|
* @brief Connect has succeeded.
|
|
*/
|
|
ACTIV_BASE_API virtual void OnConnect();
|
|
|
|
/**
|
|
* @brief Connect has been aborted (with a Disconnect()) before it completed either successfully or unsuccessfully.
|
|
*/
|
|
ACTIV_BASE_API virtual void OnConnectAborted();
|
|
|
|
/**
|
|
* @brief Connect has failed.
|
|
*
|
|
* @param errorCode os error code.
|
|
*/
|
|
ACTIV_BASE_API virtual void OnConnectFailed(const uint32_t errorCode);
|
|
|
|
/**
|
|
* @brief Accept has succeeded.
|
|
*/
|
|
ACTIV_BASE_API virtual void OnAccept();
|
|
|
|
/**
|
|
* @brief Accept has been aborted. Calling Disconnect() before an accept has completed will cause this event.
|
|
*/
|
|
ACTIV_BASE_API virtual void OnAcceptAborted();
|
|
|
|
/**
|
|
* @brief Accept has failed. Stopping the listener will cause this event on an accepting connection.
|
|
*
|
|
* @param errorCode os error code.
|
|
*/
|
|
ACTIV_BASE_API virtual void OnAcceptFailed(const uint32_t errorCode);
|
|
|
|
State m_state; ///< The state of the connection.
|
|
|
|
private:
|
|
/**
|
|
* @brief Initiate an accept on this socket.
|
|
*
|
|
* @param listener the TcpListener.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_WRONG_STATE
|
|
*/
|
|
StatusCode InitiateAccept(TcpListener &listener);
|
|
|
|
/**
|
|
* @brief Initiate disconnect on socket with i/o outstanding.
|
|
*
|
|
* @param gracefully
|
|
*/
|
|
void InitiateDisconnect(const bool gracefully);
|
|
|
|
// IpSocket overrides.
|
|
ACTIV_BASE_API virtual void HandleIoFailure(const int errorCode) ACTIV_OVERRIDE;
|
|
|
|
// EventManagee overrides.
|
|
ACTIV_BASE_API virtual void OnEvents(const Events events) ACTIV_OVERRIDE;
|
|
|
|
/**
|
|
* @brief Process connect completion.
|
|
*/
|
|
void ProcessConnectCompletion();
|
|
|
|
/**
|
|
* @brief Process successful connect completion.
|
|
*/
|
|
void ProcessSuccessfulConnectCompletion();
|
|
|
|
/**
|
|
* @brief Process disconnect completion.
|
|
*/
|
|
void ProcessDisconnectCompletion();
|
|
|
|
/**
|
|
* @brief Process a server shutdown.
|
|
*/
|
|
void ProcessServerShutdown();
|
|
|
|
/**
|
|
* @brief Process an accept.
|
|
*
|
|
* This function does not close the provided socket on error conditions.
|
|
*
|
|
* @param socket the accepted socket.
|
|
* @param remoteAddress
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS state now STATE_W4_ACCEPT_COMPLETION
|
|
* @retval STATUS_CODE_WRONG_STATE a Disconnect() has been performed on this object, it can no longer accept connections
|
|
* @retval STATUS_CODE_FAILURE failed to add socket to EventManager
|
|
*/
|
|
StatusCode ProcessAccept(const SOCKET socket, const IpAddress &remoteAddress);
|
|
|
|
/**
|
|
* @brief Process accept completion.
|
|
*/
|
|
void ProcessAcceptCompletion();
|
|
|
|
bool m_isShutdownEnabled;
|
|
|
|
// Client side members.
|
|
IpAddress m_providedLocalAddress;
|
|
|
|
// Server side members.
|
|
TcpListener * m_pTcpListener; ///< The server that will accept.
|
|
|
|
friend class TcpListener;
|
|
};
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_BASE_POSIX_IP_TCP_SOCKET_H)
|