diff options
author | Claudius "keldu" Holeksa <mail@keldu.de> | 2024-03-08 19:57:18 +0100 |
---|---|---|
committer | Claudius "keldu" Holeksa <mail@keldu.de> | 2024-03-08 19:57:18 +0100 |
commit | 21f52f91ddbaf409c45663a71d84033fbde22198 (patch) | |
tree | ebe7d664b89e506240ec255f878feb7b6bfaf5d5 /modules | |
parent | 7a097bd3bdb288342cc7314f6942347274811030 (diff) |
io: Preparing example env
Diffstat (limited to 'modules')
-rw-r--r-- | modules/io/SConstruct | 13 | ||||
-rw-r--r-- | modules/io/examples/SConscript | 28 | ||||
-rw-r--r-- | modules/io/examples/echo.hpp | 8 | ||||
-rw-r--r-- | modules/io/examples/echo_client.cpp | 35 | ||||
-rw-r--r-- | modules/io/examples/echo_server.cpp | 84 |
5 files changed, 166 insertions, 2 deletions
diff --git a/modules/io/SConstruct b/modules/io/SConstruct index 5b03a9f..035bdda 100644 --- a/modules/io/SConstruct +++ b/modules/io/SConstruct @@ -45,8 +45,17 @@ env_vars.Add('prefix', env=Environment(ENV=os.environ, variables=env_vars, CPPPATH=[], CPPDEFINES=['SAW_UNIX'], - CXXFLAGS=['-std=c++20','-g','-Wall','-Wextra'], - LIBS=['forstio-async']) + CXXFLAGS=[ + '-std=c++20', + '-g', + '-Wall', + '-Wextra' + ], + LIBS=[ + 'forstio-core', + 'forstio-async' + ] +); env.__class__.add_source_files = add_kel_source_files env.Tool('compilation_db'); env.cdb = env.CompilationDatabase('compile_commands.json'); diff --git a/modules/io/examples/SConscript b/modules/io/examples/SConscript new file mode 100644 index 0000000..850edcd --- /dev/null +++ b/modules/io/examples/SConscript @@ -0,0 +1,28 @@ +#!/bin/false + +import os +import os.path +import glob + + +Import('env') + +dir_path = Dir('.').abspath + +# Environment for base library +examples_env = env.Clone(); + +examples_env.sources = sorted(glob.glob(dir_path + "/*.cpp")) +examples_env.headers = sorted(glob.glob(dir_path + "/*.hpp")) + +env.sources += examples_env.sources; +env.headers += examples_env.headers; + +objects_static = [] +examples_env.echo_client = examples_env.Program('#bin/echo_client', ['echo_client.cpp', env.sources]); +examples_env.echo_server = examples_env.Program('#bin/echo_server', ['echo_server.cpp', env.sources]); + +# Set Alias +env.Alias('examples', [examples_env.echo_client, examples_env.echo_server]); + +env.targets += ['examples']; diff --git a/modules/io/examples/echo.hpp b/modules/io/examples/echo.hpp new file mode 100644 index 0000000..2201457 --- /dev/null +++ b/modules/io/examples/echo.hpp @@ -0,0 +1,8 @@ +#pragma once + +struct message { + std::array<uint8_t, 256> data; + uint64_t already_read = 0; +}; + +constexpr std::string_view message_content = "Hello! This is an echo msg."; diff --git a/modules/io/examples/echo_client.cpp b/modules/io/examples/echo_client.cpp new file mode 100644 index 0000000..9e56f6b --- /dev/null +++ b/modules/io/examples/echo_client.cpp @@ -0,0 +1,35 @@ +#include "../c++/io.hpp" + +#include <iostream> + +#include "echo.hpp" + +int main(){ + /** + * Create EventLoop + * Setup EventPort to the outside world + * And setup the io comms to the outside. + */ + auto eo_aio = saw::setup_async_io(); + if(eo_aio.is_error()){ + auto& err = eo_aio.get_error(); + std::cerr<<err.get_message()<<std::endl; + return err.get_id(); + } + auto& aio = eo_aio.get_value(); + /** + * Make the event loop the current event loop on this thread + */ + saw::wait_scope wait_scope{aio.event_loop}; + + bool keep_running = true; + aio.event_port.on_signal(saw::Signal::Terminate).then([&keep_running](){ + keep_running = false; + }).detach(); + + while(keep_running){ + wait_scope.wait(); + } + + return 0; +} diff --git a/modules/io/examples/echo_server.cpp b/modules/io/examples/echo_server.cpp new file mode 100644 index 0000000..82513ff --- /dev/null +++ b/modules/io/examples/echo_server.cpp @@ -0,0 +1,84 @@ +#include "../c++/io.hpp" + +#include <iostream> + +#include "echo.hpp" + +void handle_echo_message(saw::io_stream& rmt_clt, bool& keep_running, message& state){ + rmt_clt.read_ready().then([&](){ + for(;;){ + uint64_t tbr = state.data.size() < state.already_read ? 0: state.data.size() - state.already_read; + if(tbr == 0){ + exit(-1); + } + auto eov = rmt_clt.read(&state.data[0], tbr); + + if(eov.is_error()){ + auto& err = eov.get_error(); + if(err.is_critical()){ + exit(err.get_id()); + } + } + } + }).detach(); + + rmt_clt.on_read_disconnected().then([&](){ + keep_running = false; + }).detach(); +} + +int main(){ + auto eo_aio = saw::setup_async_io(); + if(eo_aio.is_error()){ + auto& err = eo_aio.get_error(); + std::cerr<<err.get_message()<<std::endl; + return err.get_id(); + } + auto& aio = eo_aio.get_value(); + /** + * Make the event loop the current event loop on this thread + */ + saw::wait_scope wait_scope{aio.event_loop}; + + bool keep_running = true; + aio.event_port.on_signal(saw::Signal::Terminate).then([&keep_running](){ + keep_running = false; + }).detach(); + + auto& network = aio.io->get_network(); + saw::own<saw::network_address> addr = nullptr; + saw::own<saw::server> srv = nullptr; + saw::own<saw::io_stream> remote_client = nullptr; + + message msg_state; + + /** + * Try to resolve address. If resolved + */ + network.resolve_address("127.0.0.1", 4321).then([&](auto net_addr){ + addr = std::move(net_addr); + srv = network.listen(*addr); + if(srv){ + srv->accept().then([&](auto client) -> saw::error_or<void>{ + if(!remote_client){ + remote_client = std::move(client); + + if(remote_client){ + handle_echo_message(*remote_client, keep_running, msg_state); + }else{ + keep_running = false; + } + } + return saw::make_error<saw::err::critical>(); + }).detach(); + }else{ + keep_running = false; + } + }).detach(); + + while(keep_running){ + wait_scope.wait(); + } + + return 0; +} |