Thank you to everyone who tested Corosio, read the docs, filed issues, and wrote a review. The Boost formal review has closed and we're now waiting on the review manager's report. Corosio is still here to try, and we're still reading everything you send us.
Two files, four commands, no dependency wrangling. Still the fastest way to see what Corosio actually feels like.
Make a new folder anywhere. Inside it, create CMakeLists.txt and main.cpp with the contents below.
cmake_minimum_required(VERSION 3.14...4.2)
project(echo_server VERSION 1.0 LANGUAGES CXX)
include(FetchContent)
FetchContent_Declare(capy
GIT_REPOSITORY https://github.com/cppalliance/capy.git
GIT_TAG develop
GIT_SHALLOW TRUE)
FetchContent_Declare(corosio
GIT_REPOSITORY https://github.com/cppalliance/corosio.git
GIT_TAG develop
GIT_SHALLOW TRUE)
FetchContent_MakeAvailable(capy corosio)
add_executable(echo_server main.cpp)
target_link_libraries(echo_server Boost::corosio)
#include <boost/capy.hpp>
#include <boost/corosio.hpp>
#include <iostream>
namespace corosio = boost::corosio;
namespace capy = boost::capy;
capy::task<> echo_session(corosio::tcp_socket sock)
{
char buf[1024];
for (;;)
{
auto [ec, n] = co_await sock.read_some(
capy::mutable_buffer(buf, sizeof(buf)));
auto [wec, wn] = co_await capy::write(sock,
capy::const_buffer(buf, n));
if (ec || wec) break;
}
sock.close();
}
capy::task<> accept_loop(
corosio::tcp_acceptor& acc,
corosio::io_context& ioc)
{
auto ep = acc.local_endpoint();
std::cout << "Listening on port " << ep.port() << "\n";
for (;;)
{
corosio::tcp_socket peer(ioc);
auto [ec] = co_await acc.accept(peer);
if (ec) { std::cout << ec.message(); continue; }
capy::run_async(ioc.get_executor())(
echo_session(std::move(peer)));
}
}
int main(int argc, char* argv[])
{
unsigned short port = 8080;
if (argc > 1)
port = static_cast<unsigned short>(std::atoi(argv[1]));
corosio::io_context ioc;
corosio::tcp_acceptor acc(ioc, corosio::endpoint(port));
capy::run_async(ioc.get_executor())(accept_loop(acc, ioc));
ioc.run();
}
Run this from your project folder. CMake will fetch Corosio and all its dependencies automatically, with nothing to install by hand.
Compile, then start the server. You should see it listening on port 8080.
Open a second terminal and send it something. You should see your message echoed back.
The review has closed, but the work hasn't. Whether you got it running or hit a wall on step one, we still want to hear about it. Doesn't need to be polished. Rough notes are great. Anything you tell us here goes directly to the people building this.