diff options
Diffstat (limited to 'modules/io_codec/examples/peer_echo_client.cpp')
-rw-r--r-- | modules/io_codec/examples/peer_echo_client.cpp | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/modules/io_codec/examples/peer_echo_client.cpp b/modules/io_codec/examples/peer_echo_client.cpp new file mode 100644 index 0000000..54f82a9 --- /dev/null +++ b/modules/io_codec/examples/peer_echo_client.cpp @@ -0,0 +1,68 @@ +#include "../c++/io_peer.hpp" + +#include <array> +#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(); + + saw::own<saw::network_address> net_addr = nullptr; + saw::own<saw::async_io_stream> async_rmt = nullptr; + + std::array<uint8_t, 32> read_data; + uint64_t read_bytes = 0; + auto& network = aio.io->get_network(); + network.resolve_address("127.0.0.1", 4322).then([&](auto addr){ + net_addr = std::move(addr); + network.connect(*net_addr).then([&](auto rmt_srv){ + async_rmt = saw::heap<saw::async_io_stream>(std::move(rmt_srv)); + async_rmt->write("foo", 3); + + async_rmt->read(&read_data[0], 3, read_data.size()-1); + + async_rmt->read_done().then([&](size_t b){ + for(uint64_t i = 0; i < b; ++i){ + std::cout<<static_cast<char>(read_data[i]); + } + std::cout<<std::endl; + + keep_running = false; + }).detach(); + + async_rmt->on_read_disconnected().then([&](){ + keep_running = false; + }).detach(); + }).detach(); + }).detach(); + + wait_scope.poll(); + while(keep_running){ + wait_scope.wait(); + } + + std::cout<<"Shutting down echo client"<<std::endl; + + return 0; +} |