/** * @file include/ActivMiddleware/ActivBase/Ip/SocketHelper.h * * @brief Header file for socket helper functions. */ #if (!defined ACTIV_BASE_IP_SOCKET_HELPER_H) #define ACTIV_BASE_IP_SOCKET_HELPER_H #include "ActivMiddleware/ActivBase/External.h" #include "ActivMiddleware/ActivBase/OsException.h" #include "ActivMiddleware/ActivBase/Ip/IpAddress.h" #include "ActivMiddleware/ActivBase/Ip/IpException.h" #include "ActivMiddleware/ActivBase/Ip/IpManager.h" #include //! Activ namespace. namespace Activ { //! SocketHelper namespace. namespace SocketHelper { /** * @brief Create a bound non-block, no clone socket. * * @param localAddress address to use for socket create (will be updated with used port if binding to 0). * @param shouldSetReUseAddrSocketOption whether the SO_REUSEADDR socket option be set. * * @return the socket handle. * * @throw OsException */ ACTIV_BASE_API SOCKET CreateBoundSocket(IpAddress &localAddress, const bool shouldSetReUseAddrSocketOption = true); // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Set socket option. * * @param socket * @param optionName * @param pOptionValue * @param optionLength * @param level * * @throw OsException */ ACTIV_BASE_API void SetSocketOption(const SOCKET socket, const int optionName, const char *pOptionValue, const socklen_t optionLength, const int level); // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Get socket option. * * @param socket * @param optionName * @param pOptionValue * @param pOptionLength * @param level * * @throw OsException */ ACTIV_BASE_API void GetSocketOption(const SOCKET socket, const int optionName, char *pOptionValue, socklen_t *pOptionLength, const int level); // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Get rx buffer size. * * @param socket socket handle to get the rx buffer size of. * @param size rx buffer size. * * @throw OsException */ ACTIV_BASE_API void GetRxBufferSize(const SOCKET socket, int &size); // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Set rx buffer size. * * @param socket socket handle to modify the rx buffer size of. * @param size new rx buffer size. * * @throw OsException * @throw LogMessageException */ ACTIV_BASE_API void SetRxBufferSize(const SOCKET socket, const int size); // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Get tx buffer size. * * @param socket socket handle to get the tx buffer size of. * @param size tx buffer size. * * @throw OsException */ ACTIV_BASE_API void GetTxBufferSize(const SOCKET socket, int &size); // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Set tx buffer size. * * @param socket socket handle to modify the tx buffer size of. * @param size new tx buffer size. * * @throw OsException * @throw LogMessageException */ ACTIV_BASE_API void SetTxBufferSize(const SOCKET socket, const int size); // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Get type of service. * * @param socket socket handle to get the type of service for.modify the multicast ttl of. * @param typeOfService the sockets current type of service. * * @throw OsException */ ACTIV_BASE_API void GetTypeOfService(const SOCKET socket, byte_t &typeOfService); // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Set type of service. * * @param socket socket handle to set the type of service for. * @param typeOfService the type of service to set. * * @throw OsException */ ACTIV_BASE_API void SetTypeOfService(const SOCKET socket, const byte_t typeOfService); // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Set non-blocking. * * @param socket socket handle to set to non-blocking for. * * @throw OsException */ ACTIV_BASE_API void SetNonBlocking(const SOCKET socket); // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Disable clone on exec. * * @param socket socket handle to disable cloning for. * * @throw OsException */ ACTIV_BASE_API void DisableCloneOnExec(const SOCKET socket); // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Duplicate a socket. * * @param socket * * @return The duplicated socket or INVALID_SOCKET. */ ACTIV_BASE_API SOCKET DuplicateSocket(const SOCKET socket); // --------------------------------------------------------------------------------------------------------------------------------- //! Udp namespace. namespace Udp { /** * @brief Create socket. * * @param localAddress address to use for socket create (will be updated with used port if binding to 0). * @param shouldSetReUseAddrSocketOption whether the SO_REUSEADDR socket option be set. * * @return the socket handle. * * @throw OsException */ ACTIV_BASE_API SOCKET CreateSocket(IpAddress &localAddress, const bool shouldSetReUseAddrSocketOption = true); // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Connect. * * @param socket socket handle to connect. * @param remoteAddress the remote address to connect to. * * @throw OsException */ ACTIV_BASE_API void Connect(const SOCKET socket, const IpAddress &remoteAddress); // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Disconnect. * * @param socket socket handle to disconnect. * * @throw OsException */ ACTIV_BASE_API void Disconnect(const SOCKET socket); // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Join multicast group. * * @param socket socket handle to add the multicast group membership to. * @param multicastAddress multicast group address. * @param interfaceAddress interface address. * * @throw OsException */ ACTIV_BASE_API void JoinMulticastGroup(const SOCKET socket, const IpAddress &multicastAddress, const IpAddress &interfaceAddress); // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Join multicast group with source specific multicast filtering. * * @param socket socket handle to add the multicast group membership to. * @param multicastAddress multicast group address. * @param interfaceAddress interface address. * @param sourceAddress source address. If !sourceAddress.IsUnspecified(), a source specific multicast join is attempted. * * @throw OsException */ ACTIV_BASE_API void JoinMulticastGroup(const SOCKET socket, const IpAddress &multicastAddress, const IpAddress &interfaceAddress, const IpAddress &sourceAddress); // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Leave multicast group. * * @param socket socket handle to add the multicast group membership to. * @param multicastAddress multicast group address. * @param interfaceAddress interface address. * * @throw OsException */ ACTIV_BASE_API void LeaveMulticastGroup(const SOCKET socket, const IpAddress &multicastAddress, const IpAddress &interfaceAddress); // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Leave multicast group with source specific multicast filtering. * * @param socket socket handle to add the multicast group membership to. * @param multicastAddress multicast group address. * @param interfaceAddress interface address. * @param sourceAddress source address. If !sourceAddress.IsUnspecified(), a source specific multicast leave is attempted. * * @throw OsException */ ACTIV_BASE_API void LeaveMulticastGroup(const SOCKET socket, const IpAddress &multicastAddress, const IpAddress &interfaceAddress, const IpAddress &sourceAddress); // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Set multicast TX interface. * * @param socket socket handle to modify the multicast tx interface of. * @param interfaceAddressString string representation of the interface address to use. * * @throw OsException */ ACTIV_BASE_API void SetMulticastTxInterface(const SOCKET socket, const String &interfaceAddressString); // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Set multicast ttl. * * @param socket socket handle to modify the multicast ttl of. * @param localAddress to determine family. * @param ttl new multicast ttl. * * @throw OsException */ ACTIV_BASE_API void SetMulticastTtl(const SOCKET socket, IpAddress localAddress, const int ttl); // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Set unicast ttl. * * @param socket socket handle to modify the multicast ttl of. * @param localAddress to determine family. * @param ttl new unicast ttl. * * @throw OsException */ ACTIV_BASE_API void SetUnicastTtl(const SOCKET socket, IpAddress localAddress, const int ttl); } // namespace Udp // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- //! Tcp namespace. namespace Tcp { /** * @brief Connect. * * @param socket socket handle to connect. * @param remoteAddress the remote address to connect to. * * @throw OsException */ ACTIV_BASE_API void Connect(const SOCKET socket, const IpAddress &remoteAddress); // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Async connect. * * @param socket socket handle to connect. * @param remoteAddress the remote address to connect to. * * @throw OsException */ ACTIV_BASE_API void AsyncConnect(const SOCKET socket, const IpAddress &remoteAddress); // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Enable Nagle. * * @param socket socket handle to disable Nagle for. * * @throw OsException */ ACTIV_BASE_API void EnableNagle(const SOCKET socket); // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Disable Nagle. * * @param socket socket handle to disable Nagle for. * * @throw OsException */ ACTIV_BASE_API void DisableNagle(const SOCKET socket); } // namespace Tcp } // namespace SocketHelper // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Scoped addrinfo helper class. * * This takes a pointer to freeaddrinfo() to cope with dynamic loading of the library providing it. */ class ScopedAddrInfo : private Boost::noncopyable { public: #if (defined ACTIV_PLATFORM_WINDOWS) typedef void (WINAPI *FreeAddrInfoPtr)(addrinfo FAR *); #else typedef void (*FreeAddrInfoPtr)(addrinfo *); #endif /** * @brief Constructor. * * @param pFreeAddrInfo pointer to freeaddrinfo() function. */ ScopedAddrInfo(FreeAddrInfoPtr pFreeAddrInfo) : m_pAddrInfo(0), m_pFreeAddrInfo(pFreeAddrInfo) { } /** * @brief Constructor. * * @param pAddrInfo addrinfo pointer to manage. * @param pFreeAddrInfo pointer to freeaddrinfo() function. */ ScopedAddrInfo(addrinfo *pAddrInfo, FreeAddrInfoPtr pFreeAddrInfo) : m_pAddrInfo(pAddrInfo), m_pFreeAddrInfo(pFreeAddrInfo) { } //! Destructor. Calls freeaddrinfo() on a non-NULL managed pointer. ~ScopedAddrInfo() { if ((0 != m_pAddrInfo) && (0 != m_pFreeAddrInfo)) m_pFreeAddrInfo(m_pAddrInfo); } //! Accessor for managed pointer. operator addrinfo*() { return m_pAddrInfo; } //! Const accessor for managed pointer. operator const addrinfo*() const { return m_pAddrInfo; } //! Accessor for managed pointer. addrinfo *operator->() { return m_pAddrInfo; } //! Address of operator. addrinfo **operator&() { return &m_pAddrInfo; } private: addrinfo * m_pAddrInfo; FreeAddrInfoPtr m_pFreeAddrInfo; }; } // namespace Activ #endif // (!defined ACTIV_BASE_IP_SOCKET_HELPER_H)