Base module

PacketTransaction

Request/response transaction with timeout and retry logic.

PacketTransaction

#include <icy/packettransaction.h>

Inherits: Sendable, Stateful< TransactionState >, RefCounted< PacketTransaction< PacketT > > Subclassed by: Transaction< Message >, Transaction< PacketT >

Request/response transaction with timeout and retry logic.

Lifetime is managed by IntrusivePtr via the RefCounted base class. The transaction is kept alive as long as at least one IntrusivePtr references it. The owning client holds a Ptr in its transaction list; callbacks should capture a Ptr copy to prevent premature deletion.

When a terminal state (Success or Failed) is reached, the transaction cleans up its timer but does NOT delete itself. The IntrusivePtr destructor handles deletion when the last reference is released.

Public Methods

ReturnNameDescription
PacketTransaction inline#### Parameters
PacketTransaction inline#### Parameters
boolsend virtual inlineStarts the transaction timer and sends the request. Overriding classes should implement send logic here.
voidcancel virtual inlineCancellation means that the agent will not retransmit the request, will not treat the lack of response to be a failure, but will wait the duration of the transaction timeout for a response. Transitions the transaction to the Cancelled state.
boolcancelled const inline#### Returns
voiddispose virtual inlineStops the timer and unregisters callbacks. Does NOT delete the object; the IntrusivePtr destructor handles deletion when the last reference is released. Safe to call multiple times.
booldisposed const inline#### Returns
boolcanResend virtual inline#### Returns
intattempts const inline#### Returns
intretries const inline#### Returns
PacketT &request inline#### Returns
PacketTrequest const inline#### Returns
PacketT &response inline#### Returns
PacketTresponse const inline#### Returns

PacketTransaction

inline

inline PacketTransaction(long timeout, int retries, uv::Loop * loop)

Parameters

  • timeout Transaction timeout in milliseconds before failure or retry.

  • retries Maximum number of retransmissions (0 means one attempt only).

  • loop Event loop for the timeout timer.


PacketTransaction

inline

inline PacketTransaction(const PacketT & request, long timeout, int retries, uv::Loop * loop)

Parameters

  • request Initial request packet to store and send.

  • timeout Transaction timeout in milliseconds before failure or retry.

  • retries Maximum number of retransmissions (0 means one attempt only).

  • loop Event loop for the timeout timer.


send

virtual inline

virtual inline bool send()

Starts the transaction timer and sends the request. Overriding classes should implement send logic here.


cancel

virtual inline

virtual inline void cancel()

Cancellation means that the agent will not retransmit the request, will not treat the lack of response to be a failure, but will wait the duration of the transaction timeout for a response. Transitions the transaction to the Cancelled state.


cancelled

const inline

inline bool cancelled() const

Returns

True if the transaction is in the Cancelled state.


dispose

virtual inline

virtual inline void dispose()

Stops the timer and unregisters callbacks. Does NOT delete the object; the IntrusivePtr destructor handles deletion when the last reference is released. Safe to call multiple times.


disposed

const inline

inline bool disposed() const

Returns

True if dispose() has been called.


canResend

virtual inline

virtual inline bool canResend()

Returns

True if the transaction is not cancelled and has attempts remaining.


attempts

const inline

inline int attempts() const

Returns

The number of times [send()](#send) has been called for this transaction.


retries

const inline

inline int retries() const

Returns

The configured maximum number of retransmissions.


request

inline

inline PacketT & request()

Returns

Mutable reference to the outgoing request packet.


request

const inline

inline PacketT request() const

Returns

Copy of the outgoing request packet.


response

inline

inline PacketT & response()

Returns

Mutable reference to the received response packet.


response

const inline

inline PacketT response() const

Returns

Copy of the received response packet.

Protected Attributes

ReturnNameDescription
PacketT_request
PacketT_response
Timer_timerThe request timeout callback.
int_retriesThe maximum number of attempts before the transaction is considered failed.
int_attemptsThe number of times the transaction has been sent.
bool_disposed

_request

PacketT _request

_response

PacketT _response

_timer

Timer _timer

The request timeout callback.


_retries

int _retries

The maximum number of attempts before the transaction is considered failed.


_attempts

int _attempts

The number of times the transaction has been sent.


_disposed

bool _disposed = false

Protected Methods

ReturnNameDescription
voidonStateChange virtual inlinePost state change hook. Calls dispose() on terminal states to stop the timer, but does not delete the object; IntrusivePtr handles that.
boolhandlePotentialResponse virtual inlineProcesses a potential response candidate and updates the state accordingly.
boolcheckResponseChecks a potential response candidate and returns true on successful match.
voidonResponse virtual inlineCalled when a successful response is received.
voidonTimeout virtual inlineCalled by the timer when the transaction timeout elapses. Retransmits if retries remain, otherwise transitions to Failed.

onStateChange

virtual inline

virtual inline void onStateChange(TransactionState & state, const TransactionState &)

Post state change hook. Calls dispose() on terminal states to stop the timer, but does not delete the object; IntrusivePtr handles that.


handlePotentialResponse

virtual inline

virtual inline bool handlePotentialResponse(const PacketT & packet)

Processes a potential response candidate and updates the state accordingly.


checkResponse

bool checkResponse(const PacketT & packet)

Checks a potential response candidate and returns true on successful match.


onResponse

virtual inline

virtual inline void onResponse()

Called when a successful response is received.


onTimeout

virtual inline

virtual inline void onTimeout()

Called by the timer when the transaction timeout elapses. Retransmits if retries remain, otherwise transitions to Failed.

Public Types

NameDescription
Ptr

Ptr

IntrusivePtr< PacketTransaction< PacketT > > Ptr()