A future and stream library for modern C++, inspired by futures-rs.
futures_cpp provides a full stack future and stream framework for
asynchronize event-driven programming:
- Future & Stream monand
- Core utils, ported from folly
- Proactors:
- CPU Pool Exectutor
- IO Executor, driven by
libev
- Promise, Timer, Timeout, Channel
- IO Futures:
- Async File
- Async DNS Resolver
- Pipe
- TCP Server/Client Socket with SSL support
- Pipeline RPC Server/Client
- HTTP/HTTPS
- Websocket
- Custom protocol
- Modules
- readline
- redis
- mysql
- zookeeper
futures_cpp use C++11 move semantic extensively, inspired by the Rust
programming.
futures_cpp support linux and macOSX.
- g++ 4.9 or later
- clang 3.6 or later
Install g++4.9 or later.
apt-get install libboost-dev libssl-dev
mkdir build
cd build && CXX=g++-4.9 CC=gcc-4.9 cmake -DENABLE_EXAMPLES=1 .. && make -j5brew install boost openssl cmake
mkdir build
cd build && cmake -DENABLE_EXAMPLES=1 .. && make -j5A minimun example:
#include <iostream>
#include <futures/EventExecutor.h>
#include <futures/Timeout.h>
#include <futures/Stream.h>
using namespace futures;
int main(int argc, char *argv[]) {
EventExecutor loop(true);
auto print = nTimes(10).andThen([&loop] (int i) {
std::cerr << "Timer: " << i << std::endl;
return delay(&loop, 1.0);
}).drop();
loop.spawn(std::move(print));
loop.run();
return 0;
}This example will run for 10 seconds, and 10 timers is fired sequentially.
All asynchronized events is represented as the Future<T> monand:
nTimes(10)create aStream<int>, which iterator over 0 to 9.andThen()iterate the stream, with lambda.delay()returns a Future, which will be fullfiled after one second, in the meantime, the iterator will be scheduled out, and wait for timer event.- After the timer event fired,
andThen()goes to the next iteration.
In futures_cpp, the statement is compiled to a single large state machine, which
is the print future.
Now we can run the future:
loop.spawn()will moveprintinto the executor, and enqueue the task corresponding toprintfuture.loop.run()will run all tasks in the executor until no more tasks.
Modules are optional IO drivers that implements Future<T> or Stream<T>
concepts, e.g. event-driven database drivers, network protocols, etc.
Event-driven MySQL DB driver based on mariadb-connector, with modern C++ interfaces and connection pool.
Event-driven redis driver based on hiredis.
Event-driven redis driver based on zookeeper C client.
Console module with line editing, based on readline or editline.
Fork & PR on Github.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.