140 lines
5.1 KiB
C++
140 lines
5.1 KiB
C++
/**
|
|
* @file include/ActivMiddleware/ActivFramework/SocketEventManagee.h
|
|
*
|
|
* @brief Header file for SocketEventManagee class.
|
|
*/
|
|
|
|
#if (!defined ACTIV_FRAMEWORK_SOCKET_EVENT_MANAGEE_H)
|
|
#define ACTIV_FRAMEWORK_SOCKET_EVENT_MANAGEE_H
|
|
|
|
#include "ActivMiddleware/ActivFramework/External.h"
|
|
|
|
#include "ActivMiddleware/ActivBase/EventManagee.h"
|
|
|
|
#if (defined ACTIV_PLATFORM_WINDOWS)
|
|
#include "ActivMiddleware/ActivBase/Windows/Ip/WsaBufHelper.h"
|
|
#include "ActivMiddleware/Misc/ScopedHelper.h"
|
|
#else
|
|
#include "ActivMiddleware/ActivBase/Posix/Ip/IoVecHelper.h"
|
|
#endif
|
|
|
|
#include "ActivMiddleware/ActivFramework/Component.h"
|
|
#include "ActivMiddleware/ActivFramework/HeapMessage.h"
|
|
|
|
#include ACTIV_BOOST_INCLUDE(scoped_array.hpp)
|
|
#include ACTIV_BOOST_INCLUDE(smart_ptr/intrusive_ptr.hpp)
|
|
#include ACTIV_BOOST_INCLUDE(smart_ptr/intrusive_ref_counter.hpp)
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
/**
|
|
* @brief Class to hook a local socket in to an endpoint's EventManager.
|
|
*
|
|
* If there is intention to destroy a SocketEventManagee inside the context of the OnEvents() callback, then it should be
|
|
* referenced counted with SocketEventManageePtr.
|
|
*/
|
|
class SocketEventManagee : public EventManagee,
|
|
public Boost::intrusive_ref_counter<SocketEventManagee, Boost::thread_unsafe_counter>
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Constructor, used for RouterService based sockets.
|
|
*
|
|
* No read is initiated on the socket; you must call InitiateRead(). Datagrams will be passed to component.OnData().
|
|
*
|
|
* @param component the Component to invoke OnData() on with a received datagram.
|
|
* @param socketAddress the Address of a socket created by the RouterServiceApi.
|
|
* @param maxReadBytes the maximum number of bytes to read from the socket in one event. 0 for no limit.
|
|
* @param maximumTransmissionUnitSize the maximum datagram size that can be received.
|
|
* @param idealTransmissionUnitSize the expected normal datagram size.
|
|
*
|
|
* @throw Exception
|
|
*/
|
|
ACTIV_FRAMEWORK_API SocketEventManagee(Component &component, const Address &socketAddress, const size_t maxReadBytes,
|
|
const size_t maximumTransmissionUnitSize, const size_t idealTransmissionUnitSize);
|
|
|
|
/**
|
|
* @brief Constructor, used for sockets created in this thread. The life time of the socket handle is managed externally.
|
|
*
|
|
* No read is initiated on the socket; you must call InitiateRead(). Datagrams will be passed to component.OnData().
|
|
*
|
|
* @param component the Component to invoke OnData() on with a received datagram.
|
|
* @param socketHandle the socket handle.
|
|
* @param maxReadBytes the maximum number of bytes to read from the socket in one event. 0 for no limit.
|
|
* @param maximumTransmissionUnitSize the maximum datagram size that can be received.
|
|
* @param idealTransmissionUnitSize the expected normal datagram size.
|
|
*
|
|
* @throw Exception
|
|
*/
|
|
ACTIV_FRAMEWORK_API SocketEventManagee(Component &component, const HANDLE socketHandle, const size_t maxReadBytes,
|
|
const size_t maximumTransmissionUnitSize, const size_t idealTransmissionUnitSize);
|
|
|
|
//! Destructor.
|
|
ACTIV_FRAMEWORK_API ~SocketEventManagee();
|
|
|
|
/**
|
|
* @brief Initiate a read on the socket.
|
|
*
|
|
* The socket receive buffer is emptied first (without invoking OnData()). This is to avoid issues with receive buffer
|
|
* overflows between socket creation and calling this function (which might cause retransmissions, for example).
|
|
*
|
|
* @return STATUS_CODE_SUCCESS or an error value.
|
|
*/
|
|
ACTIV_FRAMEWORK_API const StatusCode InitiateRead();
|
|
|
|
/**
|
|
* @brief Do a blocking send of some data to a specified ip address.
|
|
*
|
|
* @param ipAddress destination ip address.
|
|
* @param pData pointer to data to send.
|
|
* @param dataLength length of data to send.
|
|
*
|
|
* @return STATUS_CODE_SUCCESS or an error value.
|
|
*/
|
|
ACTIV_FRAMEWORK_API const StatusCode PostData(const IpAddress &ipAddress, const void * const pData, const size_t dataLength);
|
|
|
|
//! Get the socket address.
|
|
const Address &GetSocketAddress() const;
|
|
|
|
protected:
|
|
ACTIV_FRAMEWORK_API virtual void OnEvents(const Events events) ACTIV_OVERRIDE;
|
|
|
|
private:
|
|
void ProcessReadEvent();
|
|
void InvokeCallback(HeapMessage &heapMessage) const;
|
|
void EmptySocketReceiveBuffer();
|
|
SOCKET GetSocket() const;
|
|
|
|
Component & m_component;
|
|
Boost::scoped_ptr<Component> m_pDummyComponent;
|
|
const Address m_socketAddress;
|
|
const size_t m_maxReadBytes;
|
|
const size_t m_maximumTransmissionUnitSize;
|
|
const size_t m_idealTransmissionUnitSize;
|
|
|
|
Boost::scoped_array<char> m_pMaximumTransmissionUnitBuffer;
|
|
IpBufferHelper m_ipBufferHelper;
|
|
HeapMessage m_rxHeapMessage;
|
|
|
|
#if (defined ACTIV_PLATFORM_WINDOWS)
|
|
ScopedSocket m_socket;
|
|
#endif
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
typedef Boost::intrusive_ptr<SocketEventManagee> SocketEventManageePtr;
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline const Address &SocketEventManagee::GetSocketAddress() const
|
|
{
|
|
return m_socketAddress;
|
|
}
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_FRAMEWORK_SOCKET_EVENT_MANAGEE_H)
|