Files

229 lines
7.0 KiB
C++

/**
* @file include/ActivMiddleware/ActivFramework/UdpHelper.h
*
* @brief Header file for the udp helper class.
*/
#if (!defined ACTIV_FRAMEWORK_UDP_HELPER_H)
#define ACTIV_FRAMEWORK_UDP_HELPER_H
#include "ActivMiddleware/ActivFramework/External.h"
#include "ActivMiddleware/ActivBase/FieldTypes/SInt.h"
#include "ActivMiddleware/ActivBase/FieldTypes/UInt.h"
#include "ActivMiddleware/ActivBase/Ip/NetworkAffinityFilter.h"
#include "ActivMiddleware/ActivBase/Ip/UdpDatagramInfo.h"
#include "ActivMiddleware/ActivFramework/IHeap.h"
//! Activ namespace.
namespace Activ
{
class ManagedEndPoint;
class UdpHelperImpl;
class IpAddress;
// ---------------------------------------------------------------------------------------------------------------------------------
//! Udp helper class
class UdpHelper : private Boost::noncopyable
{
public:
//! Receive callback function type definition.
typedef UdpDatagramCallbackFunction ReceiveCallbackFunction;
//! Settings class.
class Settings
{
public:
/**
* @brief Constructor.
*
* @param pHeap heap to use if heap settings are not defined, or 0 for system heap.
*/
ACTIV_FRAMEWORK_API Settings(IHeap * const pHeap = 0);
/**
* @brief Read settings from an ini file section.
*
* @param sectionIterator iterator to an ini file section.
*
* @throw Exception
*/
ACTIV_FRAMEWORK_API void Read(const IniFileParser::ConstSectionIterator &sectionIterator);
IHeap * m_pHeap; ///< Default: 0 (system heap).
IHeap::Settings m_heapSettings; ///< Default: empty. Otherwise, create a heap of this size to use.
bool m_shouldStartEnabled; ///< Default: true. Should this helper startup enabled.
bool m_shouldPostReadOnStartup; ///< Default: false. Should this helper post read on startup.
String m_interfaceAddress; ///< The interface address.
String m_remoteAddress; ///< The remote address (source/destination for unicast).
String m_multicastGroup; ///< The multicast group.
uint16_t m_port; ///< Default: 0. The port.
uint16_t m_localUnicastPort; ///< Default: 0. The local port used for the local unicast interface.
// Receive settings.
UInt m_socketRxBufferSize; ///< Default: [NotInitialized]. Leave as system assigned.
uint16_t m_maximumTransmissionUnitSize; ///< Default: 1500. The maximum transmission unit size.
// Posix only receive settings.
UInt m_maxLocalReadBytes; ///< Default: 65536. Posix only. Read this number of bytes max from socket before checking others. 0 for no limit.
uint16_t m_idealTransmissionUnitSize; ///< Default: 1500. Posix only. The expected normal maximum datagram size.
// Transmit settings.
UInt m_socketTxBufferSize; ///< Default: [NotInitialized]. leave as system assigned.
uint32_t m_ttl; ///< Default 8. Ttl for socket.
UInt m_typeOfService; ///< IP_TOS value (Linux only). Supported string values "low-delay", "throughput", "reliable", "min-cost" in ini-file key TypeOfService.
NetworkAffinityFilter::Settings m_networkAffinityFilterSettings;
};
//! Statistics class.
class Statistics
{
public:
//! Default constructor.
ACTIV_FRAMEWORK_API Statistics();
//! Reset.
ACTIV_FRAMEWORK_API void Reset();
size_t m_numberOfTransmittedDatagrams; ///< Number of datagrams successfully written to underlying socket.
uint64_t m_numberOfTransmittedBytes; ///< Number of bytes successfully written to underlying socket.
size_t m_numberOfTransmitFailures; ///< Number of datagrams failed to write to underlying socket.
size_t m_numberOfBlockingTransmits; ///< Number of datagram transmissions that blocked.
size_t m_numberOfReceivedDatagrams; ///< Number of datagrams received from the underlying socket.
uint64_t m_numberOfReceivedBytes; ///< Number of bytes received from the underlying socket.
};
/**
* @brief Constructor.
*
* @param managedEndPoint reference to a ManagedEndPoint object.
* @param name the name of this helper.
* @param settings setting to use for this object.
* @param receiveCallBackFunction receive callback function to send data to, if null then do not configure rx.
*
* @retval STATUS_CODE_SUCCESS
* @retval ...
*/
ACTIV_FRAMEWORK_API UdpHelper(ManagedEndPoint &managedEndPoint, const String &name, const Settings &settings,
ReceiveCallbackFunction receiveCallBackFunction = 0);
//! Destructor
ACTIV_FRAMEWORK_API ~UdpHelper();
//! Get the settings.
ACTIV_FRAMEWORK_API const Settings &GetSettings() const;
//! Get the settings.
ACTIV_FRAMEWORK_API const Statistics &GetStatistics() const;
//! Get name.
ACTIV_FRAMEWORK_API const String &GetName() const;
//! Is constructed (is the underlying socket valid).
ACTIV_FRAMEWORK_API bool IsConstructed() const;
//! Is enabled.
ACTIV_FRAMEWORK_API bool IsEnabled() const;
//! Reset statistics.
ACTIV_FRAMEWORK_API void ResetStatistics();
/**
* @brief Set enabled state.
*
* @param isEnabled
*
* @retval STATUS_CODE_SUCCESS
* @retval ...
*/
ACTIV_FRAMEWORK_API StatusCode SetEnabledState(const bool isEnabled);
//! Is read posted.
ACTIV_FRAMEWORK_API bool IsReadPosted() const;
/**
* @brief Initiate read.
*
* @retval STATUS_CODE_SUCCESS
* @retval ...
*/
ACTIV_FRAMEWORK_API StatusCode InitiateRead();
/**
* @brief Join multicast group.
*
* @retval STATUS_CODE_SUCCESS
* @retval ...
*/
ACTIV_FRAMEWORK_API StatusCode JoinMulticastGroup();
/**
* @brief Leave multicast group.
*
* @retval STATUS_CODE_SUCCESS
* @retval ...
*/
ACTIV_FRAMEWORK_API StatusCode LeaveMulticastGroup();
/**
* @brief Post data.
*
* @param messageBuilder message builder containing the data to send.
*
* @retval STATUS_CODE_SUCCESS
* @retval ...
*/
ACTIV_FRAMEWORK_API StatusCode PostData(const MessageBuilder &messageBuilder) const;
/**
* @brief Post data.
*
* @param pData pointer to data to send.
* @param dataLength length of data.
*
* @retval STATUS_CODE_SUCCESS
* @retval ...
*/
ACTIV_FRAMEWORK_API StatusCode PostData(const void * const pData, const size_t dataLength) const;
/**
* @brief Post data.
*
* @param messageBuilder message builder containing the data to send.
* @param destinationIpAddress ip address to send the data to.
*
* @retval STATUS_CODE_SUCCESS
* @retval ...
*/
ACTIV_FRAMEWORK_API StatusCode PostData(const MessageBuilder &messageBuilder, const IpAddress &destinationIpAddress) const;
/**
* @brief Post data.
*
* @param pData pointer to data to send.
* @param dataLength length of data.
* @param destinationIpAddress ip address to send the data to.
*
* @retval STATUS_CODE_SUCCESS
* @retval ...
*/
ACTIV_FRAMEWORK_API StatusCode PostData(const void * const pData, const size_t dataLength,
const IpAddress &destinationIpAddress) const;
//! Get local IpAddress.
ACTIV_FRAMEWORK_API const IpAddress &GetLocalIpAddress() const;
private:
Boost::scoped_ptr<UdpHelperImpl> m_pImpl; ///< Udp helper implementation.
};
} // namespace Activ
#endif // (!defined ACTIV_FRAMEWORK_UDP_HELPER_H)