481 lines
12 KiB
C++
481 lines
12 KiB
C++
/**
|
|
* @file include/ActivMiddleware/ActivBase/Windows/Ip/TcpSocket.h
|
|
*
|
|
* @brief Header file for tcp socket class.
|
|
*/
|
|
|
|
#if (!defined ACTIV_BASE_IP_WINDOWS_TCP_SOCKET_H)
|
|
#define ACTIV_BASE_IP_WINDOWS_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"
|
|
|
|
#include <mswsock.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_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 overridden 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 Win32 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 Win32 error code.
|
|
*/
|
|
ACTIV_BASE_API virtual void OnAcceptFailed(const uint32_t errorCode);
|
|
|
|
State m_state; ///< The state of the connection.
|
|
|
|
private:
|
|
/**
|
|
* @brief Set up a client side socket.
|
|
*
|
|
* @throw Exception.
|
|
*/
|
|
void CreateClientSocket();
|
|
|
|
/**
|
|
* @brief Set up an accepting socket.
|
|
*
|
|
* @throw Exception.
|
|
*/
|
|
void CreateAcceptSocket();
|
|
|
|
/**
|
|
* @brief Complete set up of an accepting socket.
|
|
*
|
|
* @throw Exception.
|
|
*/
|
|
void CompleteCreateAcceptSocket();
|
|
|
|
/**
|
|
* @brief Do final cleanup on the connection, including firing disconnection events.
|
|
*/
|
|
void Cleanup();
|
|
|
|
// IpSocket overrides.
|
|
ACTIV_BASE_API virtual void HandleIoFailure(const bool gracefully) ACTIV_OVERRIDE;
|
|
|
|
/**
|
|
* @brief Process connect completion.
|
|
*/
|
|
void ProcessConnectCompletion();
|
|
|
|
/**
|
|
* @brief Process successful connect completion.
|
|
*/
|
|
void ProcessSuccessfulConnectCompletion();
|
|
|
|
/**
|
|
* @brief Process accept completion.
|
|
*/
|
|
void ProcessAcceptCompletion();
|
|
|
|
/**
|
|
* @brief Process successful accept completion.
|
|
*/
|
|
void ProcessSuccessfulAcceptCompletion();
|
|
|
|
/**
|
|
* @brief Is an overlapped read successful?
|
|
*
|
|
* @param overlapped
|
|
*
|
|
* @return false if the socket should be closed.
|
|
*/
|
|
bool IsSuccessfulRxCompletion(const IoCompletionManager::Overlapped &overlapped) const;
|
|
|
|
/**
|
|
* @brief Sync bsd thread info.
|
|
*/
|
|
class BsdThreadInfo
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param tcpSocket TcpSocket object initiating the operation.
|
|
* @param socket socket to operate on.
|
|
*/
|
|
BsdThreadInfo(TcpSocket &tcpSocket, const SOCKET socket);
|
|
|
|
TcpSocket & m_tcpSocket;
|
|
SOCKET m_socket;
|
|
};
|
|
|
|
/**
|
|
* @brief Sync bsd thread connect info.
|
|
*/
|
|
class ConnectThreadInfo : public BsdThreadInfo
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param tcpSocket TcpSocket object initiating the connect.
|
|
* @param socket socket to connect on.
|
|
* @param remoteAddress address to connect to.
|
|
*/
|
|
ConnectThreadInfo(TcpSocket &tcpSocket, const SOCKET socket, const IpAddress &remoteAddress);
|
|
|
|
IpAddress m_remoteAddress;
|
|
};
|
|
|
|
/**
|
|
* @brief Initiate a bsd sync connect.
|
|
*
|
|
* @param pConnectThreadInfo info for the connect.
|
|
*/
|
|
void BsdConnect(ConnectThreadInfo * const pConnectThreadInfo);
|
|
|
|
/**
|
|
* @brief Thread function to invoke bsd connect.
|
|
*/
|
|
static unsigned WINAPI BsdConnectThread(void *p);
|
|
|
|
/**
|
|
* @brief Initiate a bsd sync accept.
|
|
*
|
|
* @param pBsdThreadInfo info for the accept.
|
|
*/
|
|
void BsdAccept(BsdThreadInfo * const pBsdThreadInfo);
|
|
|
|
/**
|
|
* @brief Thread function to invoke bsd accept.
|
|
*/
|
|
static unsigned WINAPI BsdAcceptThread(void *p);
|
|
|
|
/**
|
|
* @brief Overlapped class for connects.
|
|
*/
|
|
class ConnectOverlapped : public IoCompletionManager::Overlapped
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param tcpSocket TcpSocket containing this object.
|
|
*/
|
|
ConnectOverlapped(TcpSocket &tcpSocket);
|
|
|
|
private:
|
|
virtual void OnIoCompletion() ACTIV_OVERRIDE;
|
|
|
|
TcpSocket &m_tcpSocket;
|
|
};
|
|
|
|
/**
|
|
* @brief Overlapped class for accepts.
|
|
*/
|
|
class AcceptOverlapped : public IoCompletionManager::Overlapped
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param tcpSocket TcpSocket containing this object.
|
|
*/
|
|
AcceptOverlapped(TcpSocket &tcpSocket);
|
|
|
|
private:
|
|
virtual void OnIoCompletion() ACTIV_OVERRIDE;
|
|
|
|
TcpSocket &m_tcpSocket;
|
|
};
|
|
|
|
bool m_isShutdownEnabled;
|
|
|
|
// Client side members.
|
|
ConnectOverlapped m_connectOverlapped; ///< Overlapped info for async connect.
|
|
LPFN_CONNECTEX m_pConnectEx; ///< Pointer to ConnectEx() function.
|
|
IpAddress m_providedLocalAddress; ///< Provided local address.
|
|
|
|
// Server side members.
|
|
static const size_t PADDED_ADDRESS_SIZE = sizeof(SOCKADDR_IN6) + 16;
|
|
static const size_t ACCEPT_BUFFER_SIZE = 2 * PADDED_ADDRESS_SIZE;
|
|
|
|
AcceptOverlapped m_acceptOverlapped; ///< Overlapped info for async accept.
|
|
SOCKET m_listeningSocket; ///< The server socket that will accept.
|
|
byte_t m_acceptBuffer[ACCEPT_BUFFER_SIZE]; ///< Buffer for addresses.
|
|
LPFN_ACCEPTEX m_pAcceptEx; ///< Pointer to AcceptEx() function.
|
|
LPFN_GETACCEPTEXSOCKADDRS m_pGetAcceptExSockAddrs; ///< Pointer to GetAcceptExSockAddrs() function.
|
|
};
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_BASE_IP_WINDOWS_TCP_SOCKET_H)
|