Skip to content

Commit 874aaeb

Browse files
author
me
committed
Use boost::asio::const_buffer in async_ws_write()
1 parent b565b12 commit 874aaeb

File tree

7 files changed

+28
-39
lines changed

7 files changed

+28
-39
lines changed

examples/client_ws_awaitable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ awaitable_strand read_write_one(auto& sock, std::string_view host, std::vector<c
3838
{
3939
constexpr bool is_text{false};
4040
co_await http::async_ws_handshake(sock, host, "/ws");
41-
co_await http::async_ws_write(sock, msg, is_text);
41+
co_await http::async_ws_write(sock, boost::asio::buffer(msg), is_text);
4242
co_await http::async_ws_read(sock, msg);
4343
co_await http::async_ws_close(sock, http::ws_going_away);
4444
}

examples/client_ws_coro.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void read_write_one(Sock& sock, std::string_view host, std::vector<char>& msg, y
4141
{
4242
constexpr bool is_text{false};
4343
http::async_ws_handshake(sock, host, "/ws", yield);
44-
http::async_ws_write(sock, msg, is_text, yield);
44+
http::async_ws_write(sock, boost::asio::buffer(msg), is_text, yield);
4545
http::async_ws_read(sock, msg, yield);
4646
http::async_ws_close(sock, http::ws_going_away, yield);
4747
}

examples/server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ awaitable_strand websocket_write_loop(std::shared_ptr<websocket_impl<Socket>> ws
275275
{
276276
auto buf = std::move(ws->buf_write_queue.front());
277277
ws->buf_write_queue.erase(begin(ws->buf_write_queue));
278-
co_await http::async_ws_write(ws->sock, buf.data, buf.is_text);
278+
co_await http::async_ws_write(ws->sock, boost::asio::buffer(buf.data), buf.is_text);
279279
}
280280
}
281281
catch(const std::exception& e)

examples/unit_tests/async.cpp

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,9 @@ TEST_SUITE("[ASYNC]")
224224
REQUIRE(!bool(ec));
225225
http::async_ws_handshake(client, "localhost", "/ws", [&](boost::system::error_code ec) {
226226
REQUIRE(!bool(ec));
227-
data_client.assign(begin(data), end(data));
228-
http::async_ws_write(client, data_client, false, [&](boost::system::error_code ec, std::size_t) {
227+
http::async_ws_write(client, boost::asio::buffer(data), false, [&](boost::system::error_code ec, std::size_t) {
229228
REQUIRE(!bool(ec));
230-
data_client.assign(begin(text), end(text));
231-
http::async_ws_write(client, data_client, true, [&](boost::system::error_code ec, std::size_t) {
229+
http::async_ws_write(client, boost::asio::buffer(text), true, [&](boost::system::error_code ec, std::size_t) {
232230
REQUIRE(!bool(ec));
233231
http::async_ws_close(client, http::ws_going_away, [&](boost::system::error_code ec) {
234232
REQUIRE(!bool(ec));
@@ -274,10 +272,8 @@ TEST_SUITE("[ASYNC]")
274272
{
275273
co_await boost::asio::async_connect(client.lowest_layer(), co_await resolver.async_resolve("localhost", "6667"));
276274
co_await http::async_ws_handshake(client, "localhost", "/ws");
277-
data_client.assign(begin(data), end(data));
278-
co_await http::async_ws_write(client, data_client, false);
279-
data_client.assign(begin(text), end(text));
280-
co_await http::async_ws_write(client, data_client, true);
275+
co_await http::async_ws_write(client, boost::asio::buffer(data), false);
276+
co_await http::async_ws_write(client, boost::asio::buffer(text), true);
281277
co_await http::async_ws_close(client, http::ws_going_away);
282278
};
283279

@@ -320,10 +316,8 @@ TEST_SUITE("[ASYNC]")
320316
constexpr bool is_server{false};
321317
boost::asio::async_connect(client.lowest_layer(), resolver.async_resolve("localhost", "6667", yield), yield);
322318
http::async_ws_handshake(client, "localhost", "/ws", yield);
323-
data_client.assign(begin(data), end(data));
324-
http::async_ws_write(client, data_client, false, yield);
325-
data_client.assign(begin(text), end(text));
326-
http::async_ws_write(client, data_client, true, yield);
319+
http::async_ws_write(client, boost::asio::buffer(data), false, yield);
320+
http::async_ws_write(client, boost::asio::buffer(text), true, yield);
327321
http::async_ws_close(client, http::ws_going_away, yield);
328322
};
329323

@@ -348,11 +342,9 @@ TEST_SUITE("[ASYNC]")
348342
REQUIRE(req.is_websocket_req());
349343
http::async_ws_accept(peer, req, [&](boost::system::error_code ec, std::size_t) {
350344
REQUIRE(!bool(ec));
351-
data_peer.assign(begin(data), end(data));
352-
http::async_ws_write(peer, data_peer, false, [&](boost::system::error_code ec, std::size_t nwritten) {
345+
http::async_ws_write(peer, boost::asio::buffer(data), false, [&](boost::system::error_code ec, std::size_t nwritten) {
353346
REQUIRE(!bool(ec));
354-
data_peer.assign(begin(text), end(text));
355-
http::async_ws_write(peer, data_peer, true, [&](boost::system::error_code ec, std::size_t) {
347+
http::async_ws_write(peer, boost::asio::buffer(text), true, [&](boost::system::error_code ec, std::size_t) {
356348
REQUIRE(!bool(ec));
357349
http::async_ws_close(peer, http::ws_going_away, [&](boost::system::error_code ec) {
358350
REQUIRE(!bool(ec));
@@ -406,10 +398,8 @@ TEST_SUITE("[ASYNC]")
406398
co_await http::async_http_read(peer, req);
407399
REQUIRE(req.is_websocket_req());
408400
co_await http::async_ws_accept(peer, req);
409-
data_peer.assign(begin(data), end(data));
410-
co_await http::async_ws_write(peer, data_peer, false);
411-
data_peer.assign(begin(text), end(text));
412-
co_await http::async_ws_write(peer, data_peer, true);
401+
co_await http::async_ws_write(peer, boost::asio::buffer(data), false);
402+
co_await http::async_ws_write(peer, boost::asio::buffer(text), true);
413403
co_await http::async_ws_close(peer, http::ws_going_away);
414404
};
415405

@@ -450,10 +440,8 @@ TEST_SUITE("[ASYNC]")
450440
http::async_http_read(peer, req, yield);
451441
REQUIRE(req.is_websocket_req());
452442
http::async_ws_accept(peer, req, yield);
453-
data_peer.assign(begin(data), end(data));
454-
http::async_ws_write(peer, data_peer, false, yield);
455-
data_peer.assign(begin(text), end(text));
456-
http::async_ws_write(peer, data_peer, true, yield);
443+
http::async_ws_write(peer, boost::asio::buffer(data), false, yield);
444+
http::async_ws_write(peer, boost::asio::buffer(text), true, yield);
457445
http::async_ws_close(peer, http::ws_going_away, yield);
458446
};
459447

src/http.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include <cassert>
22
#include <cstring>
3-
#include <cstdarg>
43
#include <cctype>
54
#include <algorithm>
65
#include <filesystem>
@@ -1565,7 +1564,7 @@ namespace http
15651564

15661565
//----------------------------------------------------------------------------------------------------------------
15671566

1568-
void serialize_websocket_message(const std::vector<char>& msg, websocket_opcode opcode, bool do_mask, std::string& buf)
1567+
void serialize_websocket_message(boost::asio::const_buffer msg, websocket_opcode opcode, bool do_mask, std::string& buf)
15691568
{
15701569
// Header
15711570
websocket_frame hdr{};
@@ -1638,13 +1637,13 @@ namespace http
16381637
// Add data
16391638
if (do_mask)
16401639
{
1640+
auto data = static_cast<const uint8_t*>(msg.data());
16411641
for (size_t i = 0 ; i < msg.size() ; ++i)
1642-
buf[hdr_len+i] = msg[i] ^ mask_key[i%4];
1642+
buf[hdr_len+i] = data[i] ^ mask_key[i%4];
16431643
}
16441644
else
16451645
{
1646-
for (size_t i = 0 ; i < msg.size() ; ++i)
1647-
buf[hdr_len+i] = msg[i];
1646+
memcpy(&buf[hdr_len], msg.data(), msg.size());
16481647
}
16491648
}
16501649

src/http.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <array>
77
#include <memory>
88
#include <system_error>
9+
#include <boost/asio/buffer.hpp>
910

1011
namespace http
1112
{
@@ -626,7 +627,7 @@ namespace http
626627

627628
//----------------------------------------------------------------------------------------------------------------
628629

629-
void serialize_websocket_message(const std::vector<char>& msg, websocket_opcode opcode, bool do_mask, std::string& buf);
630+
void serialize_websocket_message(boost::asio::const_buffer msg, websocket_opcode opcode, bool do_mask, std::string& buf);
630631

631632
//----------------------------------------------------------------------------------------------------------------
632633

src/http_async.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#pragma once
22

33
#include <boost/asio/version.hpp>
4-
#include <boost/asio/compose.hpp>
4+
#include <boost/asio/buffer.hpp>
55
#include <boost/asio/write.hpp>
6+
#include <boost/asio/compose.hpp>
67
#include "http.h"
78

89
namespace http
@@ -139,7 +140,7 @@ namespace http
139140
>
140141
auto async_ws_write (
141142
stream<Sock>& sock,
142-
const std::vector<char>& msg,
143+
boost::asio::const_buffer msg,
143144
bool is_text,
144145
CompletionToken&& token = boost::asio::default_completion_token_t<typename Sock::executor_type>()
145146
);
@@ -636,7 +637,7 @@ namespace http
636637
>
637638
inline auto async_ws_write (
638639
stream<Sock>& sock,
639-
const std::vector<char>& buf,
640+
boost::asio::const_buffer buf,
640641
websocket_opcode opcode,
641642
CompletionToken&& token
642643
)
@@ -658,7 +659,7 @@ namespace http
658659
>
659660
inline auto async_ws_write (
660661
stream<Sock>& sock,
661-
const std::vector<char>& buf,
662+
boost::asio::const_buffer buf,
662663
bool is_text,
663664
CompletionToken&& token
664665
)
@@ -788,7 +789,7 @@ namespace http
788789
{
789790
state = read_response ? reading : done;
790791
serialize_ws_code(*msg, reason);
791-
async_ws_write(sock, *msg, WS_OPCODE_CLOSE, std::move(self));
792+
async_ws_write(sock, boost::asio::buffer(*msg), WS_OPCODE_CLOSE, std::move(self));
792793
}
793794

794795
// Read echoed CLOSE frame
@@ -931,7 +932,7 @@ namespace http
931932
break;
932933
case WS_OPCODE_PING:
933934
state = reading;
934-
async_ws_write(sock, msg, WS_OPCODE_PONG, std::move(self));
935+
async_ws_write(sock, boost::asio::buffer(msg), WS_OPCODE_PONG, std::move(self));
935936
break;
936937
case WS_OPCODE_PONG:
937938
async_ws_read_one(sock, msg, std::move(self));

0 commit comments

Comments
 (0)