diff options
author | Claudius 'keldu' Holeksa <mail@keldu.de> | 2024-08-07 17:17:43 +0200 |
---|---|---|
committer | Claudius 'keldu' Holeksa <mail@keldu.de> | 2024-08-07 17:17:43 +0200 |
commit | a51d012bb1e7d34ff5e27bebccd8026bc731515c (patch) | |
tree | dae2e55bb97d88377191bd1ff4b835b9c311aae5 /modules/io_codec | |
parent | 5f6c71df18a65d5c4023caa08e08f493090c3721 (diff) |
Weird behaviour in peer client. Connection being established even though
no server exists
Diffstat (limited to 'modules/io_codec')
-rw-r--r-- | modules/io_codec/c++/io_peer.tmpl.hpp | 10 | ||||
-rw-r--r-- | modules/io_codec/examples/SConscript | 2 | ||||
-rw-r--r-- | modules/io_codec/examples/peer_echo_client.cpp | 12 | ||||
-rw-r--r-- | modules/io_codec/examples/peer_echo_server.cpp | 70 |
4 files changed, 86 insertions, 8 deletions
diff --git a/modules/io_codec/c++/io_peer.tmpl.hpp b/modules/io_codec/c++/io_peer.tmpl.hpp index f4f965f..4deabd3 100644 --- a/modules/io_codec/c++/io_peer.tmpl.hpp +++ b/modules/io_codec/c++/io_peer.tmpl.hpp @@ -91,13 +91,15 @@ error_or<void> streaming_io_peer<Incoming, Outgoing, TransportEncoding, ContentE msg) { bool restart_write = (out_buffer_.read_segment_length() == 0u); - /* + auto eov = out_buffer_.write_from(msg.get_buffer()); if (eov.is_error()) { - return eov; + auto& err = eov.get_error(); + return std::move(err); } - */ + auto& len_val = eov.get_value(); + out_buffer_.write_advance(len_val); - if (false && restart_write) { + if (restart_write && out_buffer_.read_segment_length() > 0u) { io_stream_->write(&out_buffer_.read(), out_buffer_.read_segment_length()); } diff --git a/modules/io_codec/examples/SConscript b/modules/io_codec/examples/SConscript index fb06ec0..7240b2c 100644 --- a/modules/io_codec/examples/SConscript +++ b/modules/io_codec/examples/SConscript @@ -25,7 +25,7 @@ examples_env.echo_server = examples_env.Program('#bin/peer_echo_server', ['peer_ # Set Alias env.examples = [ examples_env.echo_client -#, examples_env.echo_server +, examples_env.echo_server ]; env.Alias('examples', env.examples); diff --git a/modules/io_codec/examples/peer_echo_client.cpp b/modules/io_codec/examples/peer_echo_client.cpp index 40f0510..d2c4351 100644 --- a/modules/io_codec/examples/peer_echo_client.cpp +++ b/modules/io_codec/examples/peer_echo_client.cpp @@ -2,10 +2,10 @@ #include "../c++/io_peer.hpp" -#include <iostream> - #include <forstio/codec/transport.hpp> +#include <iostream> + int main(){ using namespace saw; @@ -46,15 +46,21 @@ int main(){ } network.connect(*addr).then([&](saw::own<saw::io_stream> client){ + if(!client){ + return; + } + auto echo_stream = saw::heap<saw::async_io_stream>(std::move(client)); auto echo_peer_stream_p = saw::new_streaming_io_peer<sch::Echo, sch::Echo, trans::FixedLength<8u>, encode::KelSimple, ring_buffer>(std::move(echo_stream)); + std::cout<<"Connected"<<std::endl; { auto eo_send = echo_peer_stream_p.first->send(std::move(simple_echo)); if(eo_send.is_error()){ return; } } + std::cout<<"Sent data"<<std::endl; { simple_echo = {}; auto eov = simple_codec.encode(nat_echo, simple_echo); @@ -72,6 +78,8 @@ int main(){ } std::cout<<std::endl; return eov; + + keep_running = false; }).detach(); echo_peer_stream_p.first->on_read_disconnected().attach(std::move(echo_peer_stream_p.first)).then([]() -> error_or<void>{ diff --git a/modules/io_codec/examples/peer_echo_server.cpp b/modules/io_codec/examples/peer_echo_server.cpp index ec8cf8d..a949ff5 100644 --- a/modules/io_codec/examples/peer_echo_server.cpp +++ b/modules/io_codec/examples/peer_echo_server.cpp @@ -2,9 +2,13 @@ #include "../c++/io_peer.hpp" +#include <forstio/codec/transport.hpp> + #include <iostream> int main(){ + using namespace saw; + auto eo_aio = saw::setup_async_io(); if(eo_aio.is_error()){ auto& err = eo_aio.get_error(); @@ -15,7 +19,7 @@ int main(){ /** * Make the event loop the current event loop on this thread */ - saw::wait_scope wait_scope{aio.event_loop}; + saw::wait_scope wait{aio.event_loop}; bool keep_running = true; aio.event_port.on_signal(saw::Signal::Terminate).then([&keep_running](){ @@ -24,5 +28,69 @@ int main(){ auto& network = aio.io->get_network(); + auto eo_addr = network.resolve_address(saw::echo_address, saw::echo_port).take(); + if(eo_addr.is_error()){ + return -1; + } + auto& addr = eo_addr.get_value(); + + data<sch::Echo> nat_echo{"hello"}; + data<sch::Echo, encode::KelSimple> simple_echo; + codec<sch::Echo, encode::KelSimple> simple_codec; + + { + auto eov = simple_codec.encode(nat_echo, simple_echo); + if(eov.is_error()){ + return -1; + } + } + + auto echo_srv = network.listen(*addr); + if(!echo_srv){ + return -2; + } + + echo_srv->accept().then([&](saw::own<saw::io_stream> client){ + if(!client){ + keep_running = false; + return; + } + auto echo_stream = saw::heap<saw::async_io_stream>(std::move(client)); + auto echo_peer_stream_p = saw::new_streaming_io_peer<sch::Echo, sch::Echo, trans::FixedLength<8u>, encode::KelSimple, ring_buffer>(std::move(echo_stream)); + + + echo_peer_stream_p.second.then([&](auto simp_resp) -> error_or<void>{ + data<sch::Echo> nat_resp; + { + auto eov = simple_codec.decode(simp_resp, nat_resp); + if(eov.is_error()){ + return eov; + } + } + std::cout<<"Request:\n"; + for(uint64_t i = 0u; i < nat_resp.size(); ++i){ + std::cout<<nat_resp.at(i); + } + std::cout<<std::endl; + + { + auto eo_send = echo_peer_stream_p.first->send(std::move(simp_resp)); + if(eo_send.is_error()){ + auto& err = eo_send.get_error(); + return std::move(err); + } + } + return make_void(); + }).detach(); + + echo_peer_stream_p.first->on_read_disconnected().attach(std::move(echo_peer_stream_p.first)).then([]() -> error_or<void>{ + return make_error<err::critical>("Destroy pipeline on purpose :>"); + }).detach(); + }).detach(); + + while(keep_running){ + wait.wait(std::chrono::seconds{1u}); + } + return 0; } |