/** * @file include/ActivMiddleware/ActivBase/Windows/Ip/UdpSocket.h * * @brief Header file for udp socket class. */ #if (!defined ACTIV_BASE_IP_WINDOWS_UDP_SOCKET_H) #define ACTIV_BASE_IP_WINDOWS_UDP_SOCKET_H #include "ActivMiddleware/ActivBase/External.h" #include "ActivMiddleware/ActivBase/Ip/IpSocket.h" /** * @brief States. */ #define ACTIV_UDP_SOCKET_STATE_LIST(d) \ d(STATE_INITIALIZED) \ d(STATE_CREATED) \ d(STATE_CONNECTED) \ d(STATE_W4_CLOSE) //! Activ namespace. namespace Activ { /** * @brief UDP socket class. */ class UdpSocket : public IpSocket { public: /** * @brief States. */ enum State { ACTIV_UDP_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, you 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 UdpSocket &socket); State m_state; ///< Udp socket state. }; /** * @brief Constructor. * * @param ipManager the IpManager that owns the socket. * @param settings settings for this socket. * * @throw Exception. */ ACTIV_BASE_API UdpSocket(IpManager &ipManager, const Settings &settings = Settings()); /** * @brief Destructor. * * Note a socket is only closed in the destructor. */ ACTIV_BASE_API ~UdpSocket(); /** * @brief Create and bind a socket. * * @param localAddressString the local address to bind to in string form. * @param localPort the local port number to bind to (local endian). * * @throw Exception. */ ACTIV_BASE_API void Initialize(const String &localAddressString, const uint16_t localPort); /** * @brief Create and bind a socket. * * @param localAddress the local address to bind to. * * @throw Exception. */ ACTIV_BASE_API void Initialize(const IpAddress &localAddress); /** * @brief Connect to a remote address (filter all traffic from other addresses). * * @param remoteAddressString the remote address to connect to in string form. * @param remotePort the remote port to connect to (local endian). * * @throw Exception. */ ACTIV_BASE_API void Connect(const String &remoteAddressString, const uint16_t remotePort); /** * @brief Connect to a remote address (filter all traffic from other addresses). * * @param remoteAddress the remote address to connect to. * * @throw Exception. */ ACTIV_BASE_API void Connect(const IpAddress &remoteAddress); /** * @brief Disconnect from a remote address (allow all traffic). * * @throw Exception. */ ACTIV_BASE_API void Disconnect(); /** * @brief Shutdown the socket. * * @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 (OnShutdown()). */ ACTIV_BASE_API bool Shutdown(const bool gracefully = false); /** * @brief Initiate an abortive close on the socket. * * This will result in an OnError() if the socket * was open and not shutting down "cleanly" as the result of a Shutdown() call. */ ACTIV_BASE_API void AbortiveClose(); /** * @brief Join a multicast group. * * @param multicastGroupString an ipv4 or ipv6 multicast address string. * @param interfaceAddressString an ipv4 or ipv6 interface address string. * @param sourceAddressString an ipv4 or ipv6 source address string. If non-empty, a source specific multicast join is * attempted. * * @throw Exception. */ ACTIV_BASE_API void JoinMulticastGroup(const String &multicastGroupString, const String &interfaceAddressString, const String &sourceAddressString = String("")); /** * @brief Leave a multicast group. * * @param multicastGroupString an ipv4 or ipv6 multicast address string. * @param interfaceAddressString an ipv4 or ipv6 interface address string. * @param sourceAddressString an ipv4 or ipv6 source address string. If non-empty, a source specific multicast leave is * attempted. * * @throw Exception. */ ACTIV_BASE_API void LeaveMulticastGroup(const String &multicastGroupString, const String &interfaceAddressString, const String &sourceAddressString = String("")); /** * @brief Set multicast interface for transmission. * * @param interfaceAddressString an ipv4 or ipv6 address string. * * @throw Exception. */ ACTIV_BASE_API void SetMulticastTxInterface(const String &interfaceAddressString); /** * @brief Set TTL to be used for multicasting. * * @param ttl the TTL. * * @throw Exception. */ ACTIV_BASE_API void SetMulticastTtl(const int ttl); /** * @brief Set TTL to be used for unicasting. * * @param ttl the TTL. * * @throw Exception. */ ACTIV_BASE_API void SetUnicastTtl(const int ttl); /** * @brief Set multicast loopback. * * @param isEnabled true to enable, false to disable. * * @throw Exception. */ ACTIV_BASE_API void SetMulticastLoopback(const bool isEnabled); /** * @brief Return state as string. * * @return Socket state in string form. */ ACTIV_BASE_API static String StateToString(const State state); protected: // IpSocket overrides ACTIV_BASE_API virtual CompletionResult OnRxBuffers() ACTIV_OVERRIDE; ACTIV_BASE_API virtual CompletionResult OnTxBuffers() ACTIV_OVERRIDE; /** * @brief Socket has closed due to an error. */ ACTIV_BASE_API virtual void OnError(); /** * @brief Socket has closed after a Shutdown() call. */ ACTIV_BASE_API virtual void OnShutdown(); State m_state; ///< The state of the socket. private: /** * @brief Set up a socket. * * @throw Exception. */ void CreateSocket(); /** * @brief Do final cleanup on the connection, including firing disconnection events. */ void Cleanup(); ACTIV_BASE_API virtual void HandleIoFailure(const bool gracefully) ACTIV_OVERRIDE; /** * @brief Join or leave an ipv4 or ipv6 multicast group. * * @param multicastGroupString an ipv4 or ipv6 multicast address string. * @param interfaceAddressString an ipv4 or ipv6 interface address string. * @param sourceAddressString an ipv4 or ipv6 source address string. If non-empty, a source specific multicast join/leave * is attempted. * @param isJoin true to join the group, false to leave. * * @throw Exception. */ void MulticastGroupAction(const String &multicastGroupString, const String &interfaceAddressString, const String &sourceAddressString, const bool isJoin); /** * @brief Set the multicast tx interface for an ipv4 address. * * @param interfaceAddress the local interface address. * * @throw Exception. */ void SetIpv4MulticastTxInterface(const IpAddress &interfaceAddress); /** * @brief Set the multicast tx interface for an ipv6 address. * * @param interfaceAddress the local interface address. * * @throw Exception. */ void SetIpv6MulticastTxInterface(const IpAddress &interfaceAddress); }; } // namespace Activ #endif // (!defined ACTIV_BASE_IP_WINDOWS_UDP_SOCKET_H)