Comments (6)
If you're using CO2, there's co2::task<T>
. If you're using Coroutine TS, you can try cppcoro, which provides cppcoro::task<T>
, but it's more like CO2's lazy_task
(i.e. only starts the task when you await on it). I haven't tried cppcoro myself though.
from act.
If you just want to try the example, below is enough:
#include <iostream>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/write.hpp>
#include <act/socket.hpp>
#include <act/write.hpp>
#include <act/acceptor.hpp>
#include <experimental/coroutine>
namespace asio = boost::asio;
namespace std::experimental
{
template<class... T>
struct coroutine_traits<void, T...>
{
struct promise_type
{
void get_return_object() {}
suspend_never initial_suspend() noexcept
{
return {};
}
suspend_never final_suspend() noexcept
{
return {};
}
void return_void() noexcept {}
};
};
}
void session(asio::ip::tcp::socket sock)
{
try
{
char buf[1024];
std::cout << "connected: " << sock.remote_endpoint() << std::endl;
for (; ; )
{
act::error_code ec;
auto len = co_await act::read_some(sock, asio::buffer(buf), ec);
if (ec == asio::error::eof)
co_return;
co_await act::write(sock, asio::buffer(buf, len));
}
}
catch (std::exception& e)
{
std::cout << "error: " << sock.remote_endpoint() << ": " << e.what() << std::endl;
}
}
void server(asio::io_context& io, unsigned short port)
{
asio::ip::tcp::endpoint endpoint{asio::ip::tcp::v4(), port};
asio::ip::tcp::acceptor acceptor{io, endpoint};
asio::ip::tcp::socket sock{io};
std::cout << "server running at: " << endpoint << std::endl;
for (; ; )
{
co_await act::accept(acceptor, sock);
session(std::move(sock));
}
}
int main(int argc, char *argv[])
{
asio::io_context io;
server(io, std::atoi(argv[1]));
io.run();
return EXIT_SUCCESS;
}
from act.
Yes, I'm aware of cppcoro task, but as you said it has lazy evaluation. I'm struggling to find a preferably cross-platform task implementation with eager evaluation, nested coroutines and exception support.
I've tried to implement one, but it seems that eager evaluation and nested coroutines are mutually exclusive.
from act.
But on a second thought it's doable https://github.com/cos-public/coro-task/blob/master/include/task.h
from act.
Beware of data race. While one thread awaits on the task (i.e. await_suspend is called), in the meantime another thread may have the task completed (i.e final_suspend is called). Lazy task avoids that problem by not starting eagerly.
from act.
Just a note, I've ported some stuff from CO2 to CoroutineTS, you can check it out here. It's trickier than I thought because of some deficiency in CoroutineTS.
from act.
Related Issues (3)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from act.