1#ifndef BLUETOE_WRITE_QUEUE_HPP
2#define BLUETOE_WRITE_QUEUE_HPP
8#include <bluetoe/meta_types.hpp>
13 struct write_queue_meta_type {};
47 template < std::u
int16_t S >
51 details::write_queue_meta_type,
52 details::valid_server_option_meta_type {};
54 static constexpr std::uint16_t queue_size = S;
63 template <
typename QueueParameter >
66 template < std::u
int16_t S >
76 template <
typename ConData >
77 std::uint8_t* allocate_from_write_queue( std::size_t n, ConData& client );
82 template <
typename ConData >
83 void free_write_queue( ConData& client );
88 template <
typename ConData >
89 std::pair< std::uint8_t*, std::size_t > first_write_queue_element( ConData& client );
95 template <
typename ConData >
96 std::pair< std::uint8_t*, std::size_t > next_write_queue_element( std::uint8_t* current, ConData& );
99 std::size_t read_size( std::uint8_t* )
const;
101 void* current_client_;
102 std::uint8_t buffer_[ S ];
103 std::uint16_t buffer_end_;
109 class write_queue< no_such_type >
112 template <
typename ConData >
113 void free_write_queue( ConData& ) {}
119 template <
typename ConData,
typename WriteQueue >
120 class write_queue_guard
123 write_queue_guard( ConData& client, WriteQueue& queue );
124 ~write_queue_guard();
127 write_queue_guard(
const write_queue_guard& ) =
delete;
128 write_queue_guard& operator=(
const write_queue_guard& ) =
delete;
135 template < std::u
int16_t S >
136 write_queue< shared_write_queue< S > >::write_queue()
137 : current_client_( nullptr )
142 template < std::u
int16_t S >
143 template <
typename ConData >
144 std::uint8_t* write_queue< shared_write_queue< S > >::allocate_from_write_queue( std::size_t size, ConData& client )
148 if ( size + 2 >
static_cast< std::size_t
>( S - buffer_end_ ) || ( current_client_ !=
nullptr && current_client_ != &client ) )
151 buffer_[ buffer_end_ ] = size & 0xff;
152 buffer_[ buffer_end_ + 1 ] = size >> 8;
154 current_client_ = &client;
155 buffer_end_ += size + 2;
157 return &buffer_[ buffer_end_ - size ];
160 template < std::u
int16_t S >
161 template <
typename ConData >
162 void write_queue< shared_write_queue< S > >::free_write_queue( ConData& client )
164 if( current_client_ == &client )
167 current_client_ =
nullptr;
171 template < std::u
int16_t S >
172 template <
typename ConData >
173 std::pair< std::uint8_t*, std::size_t > write_queue< shared_write_queue< S > >::first_write_queue_element( ConData& client )
175 if( current_client_ != &client || buffer_end_ == 0 )
177 return std::make_pair(
nullptr, 0 );
180 return std::make_pair( &buffer_[ 2 ], read_size( &buffer_[ 2 ] ) );
183 template < std::u
int16_t S >
184 template <
typename ConData >
185 std::pair< std::uint8_t*, std::size_t > write_queue< shared_write_queue< S > >::next_write_queue_element( std::uint8_t* last, ConData& client )
188 assert( last >= &buffer_[ 2 ] );
189 assert( last <= &buffer_[ S ] );
190 assert( &client == current_client_ );
191 static_cast< void >( client );
193 const std::size_t size = read_size( last );
198 return last == &buffer_[ buffer_end_ ]
199 ? std::make_pair(
nullptr, 0 )
200 : std::make_pair( last + 2, read_size( last + 2 ) );
203 template < std::u
int16_t S >
204 std::size_t write_queue< shared_write_queue< S > >::read_size( std::uint8_t* last )
const
206 return *( last - 2 ) + *( last - 1 ) * 256;
209 template <
typename ConData,
typename WriteQueue >
210 write_queue_guard< ConData, WriteQueue >::write_queue_guard( ConData& client, WriteQueue& queue )
216 template <
typename ConData,
typename WriteQueue >
217 write_queue_guard< ConData, WriteQueue >::~write_queue_guard()
219 queue_.free_write_queue( client_ );
defines a write queue size that is shared among all connected clients
Definition: write_queue.hpp:48