blob: fa9781d3358ff06a4e37adff2c7b9d69143887c3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
#include "../c++/io.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();
}).detach();
}).detach();
wait_scope.poll();
while(keep_running){
wait_scope.wait();
}
std::cout<<"Shutting down echo client"<<std::endl;
return 0;
}
|