Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
DatagramHandlerDefault.hxx
Go to the documentation of this file.
1
35#ifndef _OPENLCB_DATAGRAMHANDLERDEFAULT_HXX_
36#define _OPENLCB_DATAGRAMHANDLERDEFAULT_HXX_
37
38#include "openlcb/Datagram.hxx"
39
40namespace openlcb
41{
42
48{
49protected:
51 : DatagramHandlerFlow(if_datagram)
52 {
53 }
54
55public:
56 DatagramService *dg_service()
57 {
58 return static_cast<DatagramService *>(service());
59 }
60
61protected:
67 Action respond_ok(uint8_t flags)
68 {
69 prepare_respond_ok(flags);
70 return allocate_and_call(
71 dg_service()->iface()->addressed_message_write_flow(),
72 STATE(send_ok_response));
73 }
74
75
80 void inline_respond_ok(uint8_t flags)
81 {
82 prepare_respond_ok(flags);
83 auto *b =
84 dg_service()->iface()->addressed_message_write_flow()->alloc();
86 }
87
88private:
91 void prepare_respond_ok(uint8_t flags)
92 {
93 responseMti_ = Defs::MTI_DATAGRAM_OK;
94 responseErrorCode_ = flags;
95 }
96
99 {
100 b->data()->reset(responseMti_, message()->data()->dst->node_id(),
101 message()->data()->src,
102 Payload(1, (char)(responseErrorCode_ & 0xff)));
103 dg_service()->iface()->addressed_message_write_flow()->send(b);
104 }
105
106 Action send_ok_response()
107 {
108 auto *b = get_allocation_result(
109 dg_service()->iface()->addressed_message_write_flow());
112 }
113
114protected:
121 Action respond_reject(uint16_t error_code)
122 {
123 responseMti_ = Defs::MTI_DATAGRAM_REJECTED;
124 responseErrorCode_ = error_code;
125 return allocate_and_call(
126 dg_service()->iface()->addressed_message_write_flow(),
127 STATE(send_reject_response));
128 }
129
130private:
131 Action send_reject_response()
132 {
133 auto *b = get_allocation_result(
134 dg_service()->iface()->addressed_message_write_flow());
135 b->data()->reset(responseMti_, message()->data()->dst->node_id(),
136 message()->data()->src,
137 error_to_buffer(responseErrorCode_));
138 dg_service()->iface()->addressed_message_write_flow()->send(b);
139 return release_and_exit();
140 }
141
142protected:
146 {
147 return release_and_exit();
148 }
149
151 size_t size()
152 {
153 return message()->data()->payload.size();
154 }
155
158 const uint8_t *payload()
159 {
160 return reinterpret_cast<const uint8_t *>(
161 message()->data()->payload.data());
162 }
163
164private:
165 uint16_t responseErrorCode_;
166 openlcb::Defs::MTI responseMti_;
167};
168
169} // namespace
170
171#endif // _OPENLCB_DATAGRAMHANDLERDEFAULT_HXX_
#define STATE(_fn)
Turns a function name into an argument to be supplied to functions expecting a state.
Definition StateFlow.hxx:61
Base class for all QMember types that hold data in an expandable format.
Definition Buffer.hxx:195
T * data()
get a pointer to the start of the data.
Definition Buffer.hxx:215
virtual void send(MessageType *message, unsigned priority=UINT_MAX)=0
Entry point to the flow.
MessageType * alloc()
Synchronously allocates a message buffer from the pool of this flow.
Return type for a state flow callback.
Service * service()
Return a pointer to the service I am bound to.
Action allocate_and_call(FlowInterface< Buffer< T > > *target_flow, Callback c, Pool *pool=nullptr)
Allocates a buffer from a pool and proceed to the next state when allocation is successful.
Buffer< T > * get_allocation_result(FlowInterface< Buffer< T > > *target_flow)
Takes the result of the asynchronous allocation.
Action release_and_exit()
Terminates the processing of the current message.
State flow with a given typed input queue.
Action call_immediately(Callback c)
Imediately call the next state upon return.
Transport-agnostic dispatcher of datagrams.
Definition Datagram.hxx:161
Base class with utility functionality that implements some common functionality.
Action respond_reject(uint16_t error_code)
Sends a DATAGRAM_REJECT response to the datagram originator node.
void respond_ok_helper(Buffer< GenMessage > *b)
Helper function for respond_ok.
Action respond_ok(uint8_t flags)
Sends a DATAGRAM_OK response to the datagram originator node.
void prepare_respond_ok(uint8_t flags)
Helper function for respond_ok.
virtual Action ok_response_sent()
This state is where the handling will end up after a respond_ok call.
void inline_respond_ok(uint8_t flags)
Sends a DATAGRAM_OK response to the datagram originator node.
MessageHandler * addressed_message_write_flow()
Definition If.hxx:210
string Payload
Container that carries the data bytes in an NMRAnet message.
StateFlow< Buffer< IncomingDatagram >, QList< 1 > > DatagramHandlerFlow
Derive datagram handlers that are stateflows from this base class.
Definition Datagram.hxx:75
string error_to_buffer(uint16_t error_code, uint16_t mti)
Formats a payload for response of error response messages such as OPtioanl Interaction Rejected or Te...
Definition If.cxx:90
MTI
Known Message type indicators.
@ MTI_DATAGRAM_REJECTED
datagram rejected by receiver
@ MTI_DATAGRAM_OK
datagram received okay