Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
async_stream_test_helper.hxx
3
4namespace openlcb
5{
6
7static constexpr uint8_t LOCAL_STREAM_ID = 0x3a;
8static constexpr uint8_t SRC_STREAM_ID = 0xa7;
9
11string get_payload_data(size_t length)
12{
13 string r(length, 0);
14 for (size_t i = 0; i < length; ++i)
15 {
16 r[i] = i & 0xff;
17 }
18 return r;
19}
20
25struct CollectData : public ByteSink
26{
28 string data;
32 bool keepBuffers_ {false};
33
34 void send(ByteBuffer *msg, unsigned prio) override
35 {
36 auto rb = get_buffer_deleter(msg);
37 data.append((char *)msg->data()->data_, msg->data()->size());
38 if (keepBuffers_)
39 {
40 q.insert(msg->ref());
41 }
42 }
43
45 string qtake()
46 {
47 ByteBuffer *b = (ByteBuffer *)q.next(0);
48 HASSERT(b != nullptr);
49 string ret((char *)b->data()->data_, b->data()->size());
50 b->unref();
51 return ret;
52 }
53};
54
56{
57protected:
59 {
60 mainBufferPool->alloc(&recvRequest_);
61 }
62
64 CollectData sink_;
65};
66
67} // namespace openlcb
DynamicPool * mainBufferPool
main buffer pool instance
Definition Buffer.cxx:37
BufferPtr< T > get_buffer_deleter(Buffer< T > *b)
Helper function to create a BufferPtr of an appropriate type without having to explicitly specify the...
Definition Buffer.hxx:272
AutoReleaseBuffer< T > BufferPtr
Smart pointer for buffers.
Definition Buffer.hxx:259
Base class for all QMember types that hold data in an expandable format.
Definition Buffer.hxx:195
Buffer< T > * ref()
Add another reference to the buffer.
Definition Buffer.hxx:203
void unref()
Decrement count.
Definition Buffer.hxx:675
T * data()
get a pointer to the start of the data.
Definition Buffer.hxx:215
void alloc(Buffer< BufferType > **result, Executable *flow=NULL)
Get a free item out of the pool.
Definition Buffer.hxx:292
This class implements a linked list "queue" of buffers.
Definition Queue.hxx:98
void insert(QMember *item, unsigned index=0)
Add an item to the back of the queue.
Definition Queue.hxx:124
QMember * next(unsigned index)
Get an item from the front of the queue.
Definition Queue.hxx:167
Test base class for OpenLCB unittests that simulate two physical or virtual nodes talking to each oth...
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138
string get_payload_data(size_t length)
Generates some deterministic data to send via streams.
Helper class that acts as a data sink for a stream receiver.
string qtake()
Takes a single element from the queue, and releases it.
string data
Bytes that arrived so far.
void send(ByteBuffer *msg, unsigned prio) override
Entry point to the flow.
bool keepBuffers_
if true, the buffers are added to the queue instead of unref'ed.