|
5 | 5 | #include <ProxySettings.h> |
6 | 6 | #include <exception> |
7 | 7 | #include <iostream> |
| 8 | +#include <memory> |
8 | 9 | #include <boost/uuid/uuid.hpp> |
9 | 10 | #include <boost/uuid/uuid_io.hpp> |
10 | 11 | #include <boost/uuid/uuid_generators.hpp> |
@@ -158,27 +159,31 @@ void TestWebsocketServer::send_message(web_socket_stream &ws, message const &mes |
158 | 159 | //calculate total frame size |
159 | 160 | std::size_t const frame_size = static_cast<std::size_t>(message.ByteSizeLong()) + |
160 | 161 | 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); |
162 | 164 | //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(); |
164 | 166 | void *frame_data_msg_offset = reinterpret_cast<void *>(reinterpret_cast<std::uint8_t *>(frame_data) |
165 | 167 | + 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 |
167 | 169 | std::uint16_t data_length = static_cast<std::uint16_t>(message.ByteSizeLong()); |
168 | 170 | *reinterpret_cast<std::uint16_t *>(frame_data) = boost::endian::native_to_big(data_length); |
169 | 171 | //write the protobuf msg into the buffer next |
170 | 172 | message.SerializeToArray(frame_data_msg_offset, static_cast<int>(adapter_settings.get<size_t>(KEY_MESSAGE_MAX_SIZE))); |
171 | 173 | //commit the entire frame to the outgoing message buffer |
172 | | - outgoing_message_buffer.commit(frame_size); |
| 174 | + outgoing_message_buffer->commit(frame_size); |
173 | 175 | //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 | + }); |
177 | 182 | } |
178 | 183 |
|
179 | 184 | bool TestWebsocketServer::parse_protobuf_and_consume_input(boost::beast::multi_buffer &message_buffer, size_t data_length, message &msg) |
180 | 185 | { |
181 | | - //copy into a continguous buffer for simplified protobuf parsing |
| 186 | + //copy into a contiguous buffer for simplified protobuf parsing |
182 | 187 | message_parse_buffer.consume(message_parse_buffer.size()); |
183 | 188 | msg.Clear(); |
184 | 189 | boost::asio::buffer_copy(message_parse_buffer.prepare(data_length), message_buffer.data(), data_length); |
|
0 commit comments