Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

websocket: Reduce number of re-allocation of string parameters #535

Merged
merged 6 commits into from
Sep 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 56 additions & 44 deletions include/crow/websocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ namespace crow
/// A base class for websocket connection.
struct connection
{
virtual void send_binary(const std::string& msg) = 0;
virtual void send_text(const std::string& msg) = 0;
virtual void send_ping(const std::string& msg) = 0;
virtual void send_pong(const std::string& msg) = 0;
virtual void close(const std::string& msg = "quit") = 0;
virtual void send_binary(std::string msg) = 0;
virtual void send_text(std::string msg) = 0;
virtual void send_ping(std::string msg) = 0;
virtual void send_pong(std::string msg) = 0;
virtual void close(std::string const& msg = "quit") = 0;
virtual std::string get_remote_ip() = 0;
virtual ~connection() {}
virtual ~connection() = default;

void userdata(void* u) { userdata_ = u; }
void* userdata() { return userdata_; }
Expand Down Expand Up @@ -120,75 +120,55 @@ namespace crow

/// Send data through the socket.
template<typename CompletionHandler>
void dispatch(CompletionHandler handler)
void dispatch(CompletionHandler&& handler)
{
adaptor_.get_io_service().dispatch(handler);
asio::dispatch(adaptor_.get_io_service(), std::forward<CompletionHandler>(handler));
}

/// Send data through the socket and return immediately.
template<typename CompletionHandler>
void post(CompletionHandler handler)
void post(CompletionHandler&& handler)
{
adaptor_.get_io_service().post(handler);
asio::post(adaptor_.get_io_service(), std::forward<CompletionHandler>(handler));
}

/// Send a "Ping" message.

///
/// Usually invoked to check if the other point is still online.
void send_ping(const std::string& msg) override
void send_ping(std::string msg) override
{
dispatch([this, msg] {
auto header = build_header(0x9, msg.size());
write_buffers_.emplace_back(std::move(header));
write_buffers_.emplace_back(msg);
do_write();
});
send_data(0x9, std::move(msg));
}

/// Send a "Pong" message.

///
/// Usually automatically invoked as a response to a "Ping" message.
void send_pong(const std::string& msg) override
void send_pong(std::string msg) override
{
dispatch([this, msg] {
auto header = build_header(0xA, msg.size());
write_buffers_.emplace_back(std::move(header));
write_buffers_.emplace_back(msg);
do_write();
});
send_data(0xA, std::move(msg));
}

/// Send a binary encoded message.
void send_binary(const std::string& msg) override
void send_binary(std::string msg) override
{
dispatch([this, msg] {
auto header = build_header(2, msg.size());
write_buffers_.emplace_back(std::move(header));
write_buffers_.emplace_back(msg);
do_write();
});
send_data(0x2, std::move(msg));
}

/// Send a plaintext message.
void send_text(const std::string& msg) override
void send_text(std::string msg) override
{
dispatch([this, msg] {
auto header = build_header(1, msg.size());
write_buffers_.emplace_back(std::move(header));
write_buffers_.emplace_back(msg);
do_write();
});
send_data(0x1, std::move(msg));
}

/// Send a close signal.

///
/// Sets a flag to destroy the object once the message is sent.
void close(const std::string& msg) override
void close(std::string const& msg) override
{
dispatch([this, msg] {
dispatch([this, msg]() mutable {
has_sent_close_ = true;
if (has_recv_close_ && !is_close_handler_called_)
{
Expand Down Expand Up @@ -244,10 +224,11 @@ namespace crow
/// Finishes the handshake process, then starts reading messages from the socket.
void start(std::string&& hello)
{
static std::string header = "HTTP/1.1 101 Switching Protocols\r\n"
"Upgrade: websocket\r\n"
"Connection: Upgrade\r\n"
"Sec-WebSocket-Accept: ";
static const std::string header =
"HTTP/1.1 101 Switching Protocols\r\n"
"Upgrade: websocket\r\n"
"Connection: Upgrade\r\n"
"Sec-WebSocket-Accept: ";
write_buffers_.emplace_back(header);
write_buffers_.emplace_back(std::move(hello));
write_buffers_.emplace_back(crlf);
Expand Down Expand Up @@ -656,6 +637,37 @@ namespace crow
delete this;
}


struct SendMessageType
{
std::string payload;
Connection* self;
int opcode;

void operator()()
{
self->send_data_impl(this);
}
};

void send_data_impl(SendMessageType* s)
{
auto header = build_header(s->opcode, s->payload.size());
write_buffers_.emplace_back(std::move(header));
write_buffers_.emplace_back(std::move(s->payload));
do_write();
}

void send_data(int opcode, std::string&& msg)
{
SendMessageType event_arg{
std::move(msg),
this,
opcode};

post(std::move(event_arg));
}

private:
Adaptor adaptor_;
Handler* handler_;
Expand Down