-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for beast websockets
- Loading branch information
Showing
14 changed files
with
505 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// | ||
// Copyright (c) 2023 Kasper Laudrup (laudrup at stacktrace dot dk) | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
// | ||
|
||
#ifndef BOOST_WINTLS_BEAST_HPP | ||
#define BOOST_WINTLS_BEAST_HPP | ||
|
||
#if __has_include("boost/beast.hpp") | ||
|
||
#include <boost/version.hpp> | ||
#include <boost/beast/websocket.hpp> | ||
#include <wintls/context.hpp> | ||
#include <wintls/stream.hpp> | ||
|
||
namespace boost { | ||
namespace beast { | ||
|
||
namespace detail { | ||
|
||
template<class AsyncStream> | ||
struct wintls_shutdown_op : boost::asio::coroutine { | ||
wintls_shutdown_op(wintls::stream<AsyncStream>& s, role_type role) | ||
: s_(s) | ||
, role_(role) { | ||
} | ||
|
||
template<class Self> | ||
void operator()(Self& self, error_code ec = {}, std::size_t = 0) { | ||
BOOST_ASIO_CORO_REENTER(*this) { | ||
#if (BOOST_VERSION / 100 % 1000) >= 77 | ||
self.reset_cancellation_state(net::enable_total_cancellation()); | ||
#endif | ||
|
||
BOOST_ASIO_CORO_YIELD | ||
s_.async_shutdown(std::move(self)); | ||
ec_ = ec; | ||
|
||
using boost::beast::websocket::async_teardown; | ||
BOOST_ASIO_CORO_YIELD | ||
async_teardown(role_, s_.next_layer(), std::move(self)); | ||
if (!ec_) { | ||
ec_ = ec; | ||
} | ||
|
||
self.complete(ec_); | ||
} | ||
} | ||
|
||
private: | ||
wintls::stream<AsyncStream>& s_; | ||
role_type role_; | ||
error_code ec_; | ||
}; | ||
|
||
} // namespace detail | ||
|
||
template<class AsyncStream, class TeardownHandler> | ||
void async_teardown(role_type role, wintls::stream<AsyncStream>& stream, TeardownHandler&& handler) { | ||
return boost::asio::async_compose<TeardownHandler, void(error_code)>( | ||
detail::wintls_shutdown_op<AsyncStream>(stream, role), handler, stream); | ||
} | ||
|
||
template<class AsyncStream> | ||
void teardown(boost::beast::role_type role, wintls::stream<AsyncStream>& stream, boost::system::error_code& ec) { | ||
stream.shutdown(ec); | ||
using boost::beast::websocket::teardown; | ||
boost::system::error_code ec2; | ||
teardown(role, stream.next_layer(), ec ? ec2 : ec); | ||
} | ||
|
||
} // namespace beast | ||
} // namespace boost | ||
|
||
#endif | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// | ||
// Copyright (c) 2023 Kasper Laudrup (laudrup at stacktrace dot dk) | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
// | ||
|
||
#ifndef BOOST_WINTLS_TEST_ASYNC_ECHO_CLIENT_HPP | ||
#define BOOST_WINTLS_TEST_ASYNC_ECHO_CLIENT_HPP | ||
|
||
#include "unittest.hpp" | ||
|
||
template<typename Stream> | ||
struct async_ws_echo_client : public Stream { | ||
public: | ||
using Stream::stream; | ||
|
||
async_ws_echo_client(net::io_context& context, const std::string& message) | ||
: Stream(context) | ||
, message_(message) | ||
, ws_(stream) { | ||
} | ||
|
||
void run() { | ||
do_tls_handshake(); | ||
} | ||
|
||
std::string received_message() const { | ||
return std::string(net::buffers_begin(recv_buffer_.data()), | ||
net::buffers_begin(recv_buffer_.data()) + static_cast<std::ptrdiff_t>(recv_buffer_.size())); | ||
} | ||
|
||
private: | ||
void do_tls_handshake() { | ||
ws_.next_layer().async_handshake(Stream::handshake_type::client, [this](const boost::system::error_code& ec) { | ||
REQUIRE_FALSE(ec); | ||
do_ws_handshake(); | ||
}); | ||
} | ||
|
||
void do_ws_handshake() { | ||
ws_.async_handshake("localhost", "/", [this](const boost::system::error_code& ec) { | ||
REQUIRE_FALSE(ec); | ||
do_write(); | ||
}); | ||
} | ||
|
||
void do_write() { | ||
ws_.async_write(net::buffer(message_), [this](const boost::system::error_code& ec, std::size_t) { | ||
REQUIRE_FALSE(ec); | ||
do_read(); | ||
}); | ||
} | ||
|
||
void do_read() { | ||
ws_.async_read(recv_buffer_, [this](const boost::system::error_code& ec, std::size_t) { | ||
REQUIRE_FALSE(ec); | ||
do_shutdown(); | ||
}); | ||
} | ||
|
||
void do_shutdown() { | ||
ws_.async_close(websocket::close_code::normal, [](const boost::system::error_code& ec) { | ||
REQUIRE_FALSE(ec); | ||
}); | ||
} | ||
|
||
std::string message_; | ||
beast::flat_buffer recv_buffer_; | ||
websocket::stream<decltype(stream)&> ws_; | ||
}; | ||
|
||
#endif // BOOST_WINTLS_TEST_ASYNC_ECHO_CLIENT_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// | ||
// Copyright (c) 2023 Kasper Laudrup (laudrup at stacktrace dot dk) | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
// | ||
|
||
#ifndef BOOST_WINTLS_TEST_ASYNC_ECHO_SERVER_HPP | ||
#define BOOST_WINTLS_TEST_ASYNC_ECHO_SERVER_HPP | ||
|
||
#include "unittest.hpp" | ||
|
||
template<typename Stream> | ||
class async_ws_echo_server : public Stream { | ||
public: | ||
using Stream::stream; | ||
|
||
async_ws_echo_server(net::io_context& context) | ||
: Stream(context) | ||
, ws_(stream) { | ||
} | ||
|
||
void run() { | ||
do_tls_handshake(); | ||
} | ||
|
||
private: | ||
void do_tls_handshake() { | ||
ws_.next_layer().async_handshake(Stream::handshake_type::server, [this](const boost::system::error_code& ec) { | ||
REQUIRE_FALSE(ec); | ||
do_ws_handshake(); | ||
}); | ||
} | ||
|
||
void do_ws_handshake() { | ||
ws_.async_accept([this](const boost::system::error_code& ec) { | ||
REQUIRE_FALSE(ec); | ||
do_read(); | ||
}); | ||
} | ||
|
||
void do_read() { | ||
ws_.async_read(recv_buffer_, [this](const boost::system::error_code& ec, std::size_t) { | ||
REQUIRE_FALSE(ec); | ||
ws_.text(ws_.got_text()); | ||
do_write(); | ||
}); | ||
} | ||
|
||
void do_write() { | ||
ws_.async_write(recv_buffer_.data(), [this](const boost::system::error_code& ec, std::size_t) { | ||
REQUIRE_FALSE(ec); | ||
do_shutdown(); | ||
}); | ||
} | ||
|
||
void do_shutdown() { | ||
ws_.async_close(websocket::close_code::normal, [](const boost::system::error_code& ec) { | ||
REQUIRE_FALSE(ec); | ||
}); | ||
} | ||
|
||
beast::flat_buffer recv_buffer_; | ||
websocket::stream<decltype(stream)&> ws_; | ||
}; | ||
|
||
#endif // BOOST_WINTLS_TEST_ASYNC_ECHO_SERVER_HPP |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.