Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
fc::message_buffer< buffer_len > Class Template Reference

abstraction for a message buffer that spans a chain of physical buffers More...

#include <message_buffer.hpp>

Public Types

typedef std::pair< uint32_t, uint32_tindex_t
 

Public Member Functions

 message_buffer ()
 
 ~message_buffer ()
 
index_t read_index () const
 
index_t write_index () const
 
char * read_ptr ()
 
char * write_ptr ()
 
void add_buffer_to_chain ()
 
void add_space (uint32_t bytes)
 
void reset ()
 
uint32_t bytes_to_read () const
 
uint32_t bytes_to_read_from_index (const index_t &ind) const
 
uint32_t bytes_to_write () const
 
uint32_t total_bytes () const
 
void advance_read_ptr (uint32_t bytes)
 
void advance_write_ptr (uint32_t bytes)
 
std::vector< boost::asio::mutable_buffer > get_buffer_sequence_for_boost_async_read ()
 
bool read (void *s, uint32_t size)
 
bool peek (void *s, uint32_t size, index_t &index) const
 
mb_datastream< buffer_len > create_datastream ()
 
mb_peek_datastream< buffer_len > create_peek_datastream ()
 

Static Public Member Functions

static void advance_index (index_t &index, uint32_t bytes)
 

Detailed Description

template<uint32_t buffer_len>
class fc::message_buffer< buffer_len >

This message buffer abstraction will allocate individual character arrays of size buffer_len from a boost::singleton_pool. It supports creation of a vector of boost::mutable_buffer for use with async_read() and async_read_some(). It also supports use with the fc unpack() functionality via a datastream helper class.

Definition at line 25 of file message_buffer.hpp.

Member Typedef Documentation

◆ index_t

template<uint32_t buffer_len>
std::pair<uint32_t, uint32_t> fc::message_buffer< buffer_len >::index_t

Definition at line 32 of file message_buffer.hpp.

Constructor & Destructor Documentation

◆ message_buffer()

template<uint32_t buffer_len>
fc::message_buffer< buffer_len >::message_buffer ( )
inline

Definition at line 34 of file message_buffer.hpp.

34: buffers{malloc()}, read_ind{0,0}, write_ind{0,0}, sanity_check (1) { }

◆ ~message_buffer()

template<uint32_t buffer_len>
fc::message_buffer< buffer_len >::~message_buffer ( )
inline

Definition at line 36 of file message_buffer.hpp.

36 {
37 while (buffers.size() > 0) {
38 free(buffers.back());
39 buffers.pop_back();
40 }
41 }

Member Function Documentation

◆ add_buffer_to_chain()

template<uint32_t buffer_len>
void fc::message_buffer< buffer_len >::add_buffer_to_chain ( )
inline

Definition at line 75 of file message_buffer.hpp.

75 {
76 sanity_check++;
77 buffers.push_back(malloc());
78 }
Here is the caller graph for this function:

◆ add_space()

template<uint32_t buffer_len>
void fc::message_buffer< buffer_len >::add_space ( uint32_t bytes)
inline

Definition at line 85 of file message_buffer.hpp.

85 {
86 int buffers_to_add = bytes / buffer_len + 1;
87 for (int i = 0; i < buffers_to_add; i++) {
88 sanity_check++;
89 buffers.push_back(malloc());
90 }
91 }
std::vector< char > bytes
Definition alt_bn128.hpp:10
Here is the caller graph for this function:

◆ advance_index()

template<uint32_t buffer_len>
static void fc::message_buffer< buffer_len >::advance_index ( index_t & index,
uint32_t bytes )
inlinestatic

Definition at line 249 of file message_buffer.hpp.

249 {
250 index.first += (bytes + index.second) / buffer_len;
251 index.second = (bytes + index.second) % buffer_len;
252 }
Here is the caller graph for this function:

◆ advance_read_ptr()

template<uint32_t buffer_len>
void fc::message_buffer< buffer_len >::advance_read_ptr ( uint32_t bytes)
inline

Definition at line 159 of file message_buffer.hpp.

159 {
160 advance_index(read_ind, bytes);
161 if (read_ind == write_ind) {
162 reset();
163 } else if (read_ind.first > 0) {
164 while (read_ind.first > 0) {
165 free(buffers.front());
166 buffers.pop_front();
167 sanity_check--;
168 read_ind.first--;
169 write_ind.first--;
170 }
171 }
172 }
static void advance_index(index_t &index, uint32_t bytes)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ advance_write_ptr()

template<uint32_t buffer_len>
void fc::message_buffer< buffer_len >::advance_write_ptr ( uint32_t bytes)
inline

Definition at line 178 of file message_buffer.hpp.

178 {
179 advance_index(write_ind, bytes);
180 while (write_ind.first >= buffers.size()) {
181 sanity_check++;
182 buffers.push_back(malloc());
183 }
184 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ bytes_to_read()

template<uint32_t buffer_len>
uint32_t fc::message_buffer< buffer_len >::bytes_to_read ( ) const
inline

Definition at line 122 of file message_buffer.hpp.

122 {
123 return bytes_to_read_from_index(read_ind);
124 }
uint32_t bytes_to_read_from_index(const index_t &ind) const
Here is the call graph for this function:
Here is the caller graph for this function:

◆ bytes_to_read_from_index()

template<uint32_t buffer_len>
uint32_t fc::message_buffer< buffer_len >::bytes_to_read_from_index ( const index_t & ind) const
inline

Definition at line 130 of file message_buffer.hpp.

130 {
131 return (write_ind.first - ind.first) * buffer_len + write_ind.second - ind.second;
132 }
Here is the caller graph for this function:

◆ bytes_to_write()

template<uint32_t buffer_len>
uint32_t fc::message_buffer< buffer_len >::bytes_to_write ( ) const
inline

Definition at line 140 of file message_buffer.hpp.

140 {
141 return total_bytes() - write_ind.first * buffer_len - write_ind.second;
142 }
uint32_t total_bytes() const
Here is the call graph for this function:
Here is the caller graph for this function:

◆ create_datastream()

template<uint32_t buffer_len>
mb_datastream< buffer_len > fc::message_buffer< buffer_len >::create_datastream ( )
inline

Definition at line 313 of file message_buffer.hpp.

313 {
314 return mb_datastream<buffer_len>(*this);
315 }
Here is the caller graph for this function:

◆ create_peek_datastream()

template<uint32_t buffer_len>
mb_peek_datastream< buffer_len > fc::message_buffer< buffer_len >::create_peek_datastream ( )
inline

Definition at line 348 of file message_buffer.hpp.

348 {
349 return mb_peek_datastream<buffer_len>( *this );
350 }
Here is the caller graph for this function:

◆ get_buffer_sequence_for_boost_async_read()

template<uint32_t buffer_len>
std::vector< boost::asio::mutable_buffer > fc::message_buffer< buffer_len >::get_buffer_sequence_for_boost_async_read ( )
inline

Definition at line 192 of file message_buffer.hpp.

192 {
193 std::vector<boost::asio::mutable_buffer> seq;
194 FC_ASSERT(write_ind.first < buffers.size());
195 seq.push_back(boost::asio::buffer(&buffers[write_ind.first]->at(write_ind.second),
196 buffer_len - write_ind.second));
197 for (std::size_t i = write_ind.first + 1; i < buffers.size(); i++) {
198 seq.push_back(boost::asio::buffer(&buffers[i]->at(0), buffer_len));
199 }
200 return seq;
201 }
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
int seq
Here is the caller graph for this function:

◆ peek()

template<uint32_t buffer_len>
bool fc::message_buffer< buffer_len >::peek ( void * s,
uint32_t size,
index_t & index ) const
inline

Definition at line 228 of file message_buffer.hpp.

228 {
229 if (bytes_to_read_from_index(index) < size) {
230 FC_THROW_EXCEPTION( out_of_range_exception, "tried to peek ${r} but only ${s} left",
231 ("r", size)( "s", bytes_to_read_from_index( index ) ) );
232 }
233 if (index.second + size <= buffer_len) {
234 memcpy(s, get_ptr(index), size);
235 advance_index(index, size);
236 } else {
237 uint32_t num_in_buffer = buffer_len - index.second;
238 memcpy(s, get_ptr(index), num_in_buffer);
239 advance_index(index, num_in_buffer);
240 peek((char*)s + num_in_buffer, size - num_in_buffer, index);
241 }
242 return true;
243 }
bool peek(void *s, uint32_t size, index_t &index) const
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
unsigned int uint32_t
Definition stdint.h:126
char * s
memcpy((char *) pInfo->slotDescription, s, l)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ read()

template<uint32_t buffer_len>
bool fc::message_buffer< buffer_len >::read ( void * s,
uint32_t size )
inline

Definition at line 207 of file message_buffer.hpp.

207 {
208 if (bytes_to_read() < size) {
209 FC_THROW_EXCEPTION( out_of_range_exception, "tried to read ${r} but only ${s} left",
210 ("r", size)( "s", bytes_to_read() ) );
211 }
212 if (read_ind.second + size <= buffer_len) {
213 memcpy(s, read_ptr(), size);
214 advance_read_ptr(size);
215 } else {
216 uint32_t num_in_buffer = buffer_len - read_ind.second;
217 memcpy(s, read_ptr(), num_in_buffer);
218 advance_read_ptr(num_in_buffer);
219 read((char*)s + num_in_buffer, size - num_in_buffer);
220 }
221 return true;
222 }
uint32_t bytes_to_read() const
bool read(void *s, uint32_t size)
void advance_read_ptr(uint32_t bytes)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_index()

template<uint32_t buffer_len>
index_t fc::message_buffer< buffer_len >::read_index ( ) const
inline

Definition at line 47 of file message_buffer.hpp.

47{ return read_ind; }
Here is the caller graph for this function:

◆ read_ptr()

template<uint32_t buffer_len>
char * fc::message_buffer< buffer_len >::read_ptr ( )
inline

Definition at line 59 of file message_buffer.hpp.

59 {
60 return &buffers[read_ind.first]->at(read_ind.second);
61 }
Here is the caller graph for this function:

◆ reset()

template<uint32_t buffer_len>
void fc::message_buffer< buffer_len >::reset ( )
inline

Definition at line 97 of file message_buffer.hpp.

97 {
98 // some condition exists that can send *both* buffers.size() and sanity_check to well over 10^6.
99 // this seems to be related to some sort of memory overrun possibly. By forcing an exit here, an
100 // external watchdog can be used to restart the process and avoid hanging.
101 if( buffers.size() != sanity_check || buffers.size() > 1000000) {
102 elog( "read_ind = ${r1}, ${r2} write_ind = ${w1}, ${w2}, buff.size = ${bs}, sanity = ${s}",
103 ( "r1", read_ind.first )( "r2", read_ind.second )( "w1", write_ind.first )( "w2", write_ind.second )
104 ( "bs", buffers.size() )( "s", sanity_check ) );
105 elog("Buffer manager overwrite detected. Terminating to allow external restart");
106 exit(1);
107 }
108 while (buffers.size() > 1) {
109 sanity_check--;
110 free(buffers.back());
111 buffers.pop_back();
112 }
113
114 read_ind = { 0, 0 };
115 write_ind = { 0, 0 };
116 }
#define elog(FORMAT,...)
Definition logger.hpp:130
Here is the caller graph for this function:

◆ total_bytes()

template<uint32_t buffer_len>
uint32_t fc::message_buffer< buffer_len >::total_bytes ( ) const
inline

Definition at line 147 of file message_buffer.hpp.

147 {
148 return buffer_len * buffers.size();
149 }
Here is the caller graph for this function:

◆ write_index()

template<uint32_t buffer_len>
index_t fc::message_buffer< buffer_len >::write_index ( ) const
inline

Definition at line 53 of file message_buffer.hpp.

53{ return write_ind; }
Here is the caller graph for this function:

◆ write_ptr()

template<uint32_t buffer_len>
char * fc::message_buffer< buffer_len >::write_ptr ( )
inline

Definition at line 67 of file message_buffer.hpp.

67 {
68 return &buffers[write_ind.first]->at(write_ind.second);
69 }
Here is the caller graph for this function:

The documentation for this class was generated from the following file: