Skip to content

Commit 7dec2cf

Browse files
committed
Add use of shared pointer to for async write
1 parent 18daaee commit 7dec2cf

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

test/TestWebsocketServer.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <ProxySettings.h>
66
#include <exception>
77
#include <iostream>
8+
#include <memory>
89
#include <boost/uuid/uuid.hpp>
910
#include <boost/uuid/uuid_io.hpp>
1011
#include <boost/uuid/uuid_generators.hpp>
@@ -158,27 +159,31 @@ void TestWebsocketServer::send_message(web_socket_stream &ws, message const &mes
158159
//calculate total frame size
159160
std::size_t const frame_size = static_cast<std::size_t>(message.ByteSizeLong()) +
160161
adapter_settings.get<size_t>(KEY_DATA_LENGTH_SIZE);
161-
boost::beast::flat_buffer outgoing_message_buffer{ frame_size };
162+
// Use shared_ptr to keep buffer alive until async_write completes
163+
auto outgoing_message_buffer = std::make_shared<boost::beast::flat_buffer>(frame_size);
162164
//get pointers to where data length and protobuf msg will be written to
163-
void *frame_data = outgoing_message_buffer.prepare(frame_size).data();
165+
void *frame_data = outgoing_message_buffer->prepare(frame_size).data();
164166
void *frame_data_msg_offset = reinterpret_cast<void *>(reinterpret_cast<std::uint8_t *>(frame_data)
165167
+ adapter_settings.get<size_t>(KEY_DATA_LENGTH_SIZE));
166-
//get the protobuf data length and wirte it to start the frame
168+
//get the protobuf data length and write it to start the frame
167169
std::uint16_t data_length = static_cast<std::uint16_t>(message.ByteSizeLong());
168170
*reinterpret_cast<std::uint16_t *>(frame_data) = boost::endian::native_to_big(data_length);
169171
//write the protobuf msg into the buffer next
170172
message.SerializeToArray(frame_data_msg_offset, static_cast<int>(adapter_settings.get<size_t>(KEY_MESSAGE_MAX_SIZE)));
171173
//commit the entire frame to the outgoing message buffer
172-
outgoing_message_buffer.commit(frame_size);
174+
outgoing_message_buffer->commit(frame_size);
173175
//no controls in test mode over async writes, test flow dictates this
174-
ws.async_write(outgoing_message_buffer.data(),
175-
std::bind(&TestWebsocketServer::on_write_complete, this, std::ref(ws),
176-
std::placeholders::_1, std::placeholders::_2));
176+
// Capture shared_ptr in lambda to extend buffer lifetime until write completes
177+
ws.async_write(outgoing_message_buffer->data(),
178+
[this, &ws, outgoing_message_buffer](boost::system::error_code const &ec, size_t bytes_written)
179+
{
180+
on_write_complete(ws, ec, bytes_written);
181+
});
177182
}
178183

179184
bool TestWebsocketServer::parse_protobuf_and_consume_input(boost::beast::multi_buffer &message_buffer, size_t data_length, message &msg)
180185
{
181-
//copy into a continguous buffer for simplified protobuf parsing
186+
//copy into a contiguous buffer for simplified protobuf parsing
182187
message_parse_buffer.consume(message_parse_buffer.size());
183188
msg.Clear();
184189
boost::asio::buffer_copy(message_parse_buffer.prepare(data_length), message_buffer.data(), data_length);

0 commit comments

Comments
 (0)