From 21f52f91ddbaf409c45663a71d84033fbde22198 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Fri, 8 Mar 2024 19:57:18 +0100 Subject: io: Preparing example env --- modules/io/SConstruct | 13 +++++- modules/io/examples/SConscript | 28 +++++++++++++ modules/io/examples/echo.hpp | 8 ++++ modules/io/examples/echo_client.cpp | 35 ++++++++++++++++ modules/io/examples/echo_server.cpp | 84 +++++++++++++++++++++++++++++++++++++ 5 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 modules/io/examples/SConscript create mode 100644 modules/io/examples/echo.hpp create mode 100644 modules/io/examples/echo_client.cpp create mode 100644 modules/io/examples/echo_server.cpp (limited to 'modules') 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 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 + +#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< + +#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<get_network(); + saw::own addr = nullptr; + saw::own srv = nullptr; + saw::own 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{ + 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(); + }).detach(); + }else{ + keep_running = false; + } + }).detach(); + + while(keep_running){ + wait_scope.wait(); + } + + return 0; +} -- cgit v1.2.3