forked from chriskohlhoff/asio
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add wrapper for CXX_MODULES The example module test needs oppenssl
- Loading branch information
1 parent
7fcf4a4
commit 96a0e72
Showing
11 changed files
with
282 additions
and
16 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
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
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
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 @@ | ||
module; | ||
|
||
#include <asio.hpp> | ||
#include <asio/ssl.hpp> | ||
|
||
export module asio; | ||
|
||
namespace asio { | ||
export using asio::io_context; | ||
export using asio::post; | ||
export using asio::any_completion_executor; | ||
export using asio::any_io_executor; | ||
export using asio::bad_executor; | ||
export using asio::cancellation_signal; | ||
export using asio::cancellation_slot; | ||
export using asio::cancellation_state; | ||
export using asio::cancellation_type; | ||
export using asio::coroutine; | ||
export using asio::detached_t; | ||
export using asio::execution_context; | ||
export using asio::executor; | ||
export using asio::executor_arg_t; | ||
export using asio::invalid_service_owner; | ||
export using asio::io_context; | ||
export using asio::multiple_exceptions; | ||
export using asio::service_already_exists; | ||
export using asio::static_thread_pool; | ||
export using asio::system_context; | ||
export using asio::system_executor; | ||
export using asio::thread_pool; | ||
export using asio::awaitable; | ||
export using asio::deferred; | ||
export using asio::detached; | ||
export using asio::connect; | ||
export using asio::async_connect; | ||
export using asio::buffer; | ||
export using asio::socket_base; | ||
export using asio::co_spawn; | ||
|
||
namespace error { | ||
export using asio::error::make_error_code; | ||
} | ||
|
||
namespace this_coro { | ||
export using asio::this_coro::executor; | ||
} | ||
|
||
namespace ip { | ||
export using asio::ip::tcp; | ||
export using asio::ip::address; | ||
export using asio::ip::address_v4; | ||
} | ||
|
||
namespace ssl { | ||
export using asio::ssl::context; | ||
export using asio::ssl::context_base; | ||
export using asio::ssl::host_name_verification; | ||
export using asio::ssl::stream_base; | ||
export using asio::ssl::verify_context; | ||
export using asio::ssl::stream; | ||
namespace error { | ||
export using asio::ssl::error::stream_errors; | ||
export using asio::ssl::error::make_error_code; | ||
} | ||
} | ||
|
||
} |
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,3 @@ | ||
#include "main.hpp" | ||
|
||
int main() { return bench::main_impl(); } |
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,119 @@ | ||
#ifdef USE_MODULES | ||
# ifdef HAS_STDLIB_MODULES | ||
import std; | ||
import std.compat; | ||
# else | ||
# include <coroutine> | ||
# include <cstdlib> | ||
# include <exception> | ||
# include <system_error> | ||
# endif | ||
import asio; | ||
#else | ||
# include <asio/awaitable.hpp> | ||
# include <asio/buffer.hpp> | ||
# include <asio/co_spawn.hpp> | ||
# include <asio/connect.hpp> | ||
# include <asio/deferred.hpp> | ||
# include <asio/detached.hpp> | ||
# include <asio/io_context.hpp> | ||
# include <asio/ip/address_v4.hpp> | ||
# include <asio/ip/tcp.hpp> | ||
# include <asio/ssl/error.hpp> | ||
# include <asio/ssl/stream.hpp> | ||
# include <asio/this_coro.hpp> | ||
#endif | ||
|
||
namespace bench { | ||
|
||
namespace net = asio; | ||
namespace ssl = asio::ssl; | ||
using tcp = asio::ip::tcp; | ||
using std::error_code; | ||
|
||
inline void fail(error_code ec, char const* what) | ||
{ | ||
if(ec == net::ssl::error::stream_truncated) | ||
return; | ||
exit(1); | ||
} | ||
|
||
inline net::awaitable<void> do_session(tcp::socket client_sock) | ||
{ | ||
char buff [4096] {}; | ||
auto ex = co_await net::this_coro::executor; | ||
|
||
ssl::context ctx {ssl::context::tls_client}; | ||
ssl::stream<tcp::socket> stream {std::move(client_sock), ctx}; | ||
|
||
co_await stream.async_handshake(ssl::stream_base::server, net::deferred); | ||
co_await stream.async_read_some(net::buffer(buff), net::deferred); | ||
co_await stream.async_write_some(net::buffer(buff), net::deferred); | ||
co_await stream.async_shutdown(net::deferred); | ||
stream.next_layer().close(); | ||
} | ||
|
||
inline net::awaitable<void> do_listen() | ||
{ | ||
auto ex = co_await net::this_coro::executor; | ||
error_code ec; | ||
tcp::endpoint endpoint {net::ip::address_v4::loopback(), 3000}; | ||
|
||
// Open the acceptor | ||
tcp::acceptor acceptor{ex}; | ||
acceptor.open(endpoint.protocol(), ec); | ||
if(ec) | ||
fail(ec, "open"); | ||
|
||
// Allow address reuse | ||
acceptor.set_option(net::socket_base::reuse_address(true), ec); | ||
if(ec) | ||
fail(ec, "set_option"); | ||
|
||
// Bind to the server address | ||
acceptor.bind(endpoint, ec); | ||
if(ec) | ||
fail(ec, "bind"); | ||
|
||
// Start listening for connections | ||
acceptor.listen(net::socket_base::max_listen_connections, ec); | ||
if(ec) | ||
fail(ec, "listen"); | ||
|
||
for(;;) | ||
{ | ||
tcp::socket socket{ex}; | ||
co_await acceptor.async_accept(socket, net::deferred); | ||
net::co_spawn(ex, [s = std::move(socket)] mutable { | ||
return do_session(std::move(s)); | ||
}, net::detached); | ||
} | ||
} | ||
|
||
inline int main_impl() | ||
{ | ||
// The io_context is required for all I/O | ||
net::io_context ioc; | ||
|
||
// Spawn a listening port | ||
net::co_spawn( | ||
ioc, | ||
do_listen, | ||
// on completion, spawn will call this function | ||
[](std::exception_ptr ex) | ||
{ | ||
// if an exception occurred in the coroutine, | ||
// it's something critical, e.g. out of memory | ||
// we capture normal errors in the ec | ||
// so we just rethrow the exception here, | ||
// which will cause `ioc.run()` to throw | ||
if (ex) | ||
std::rethrow_exception(ex); | ||
}); | ||
|
||
ioc.run(); | ||
|
||
return 0; | ||
} | ||
|
||
} |
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
Oops, something went wrong.