Line data Source code
1 : #include <iostream>
2 : #include <iow/io/descriptor/holder.hpp>
3 : #include <iow/io/socket/stream/aspect.hpp>
4 : #include <iow/io/socket/stream/options.hpp>
5 : #include <iow/io/reader/asio/aspect.hpp>
6 : #include <iow/io/writer/asio/aspect.hpp>
7 : #include <iow/io/rw/aspect.hpp>
8 : #include <iow/io/basic/aspect.hpp>
9 :
10 : #include <fas/testing.hpp>
11 :
12 : typedef std::vector<char> data_type;
13 : typedef iow::asio::posix::stream_descriptor descriptor_type;
14 : typedef iow::io::socket::stream::options options_type;
15 :
16 : /*
17 : struct ad_initialize
18 : {
19 : template<typename T, typename O>
20 : void operator()(T& t, O&& opt) const
21 : {
22 : t.get_aspect().template get< ::iow::io::rw::_initialize_ >()(t, opt);
23 : }
24 : };
25 : */
26 :
27 : struct aspect_stream : fas::aspect<
28 : fas::type< ::iow::io::descriptor::_descriptor_type_, descriptor_type >,
29 : fas::type< ::iow::io::_options_type_, options_type >,
30 : fas::alias< ::iow::io::socket::stream::_initialize_, ::iow::io::rw::_initialize_>,
31 : ::iow::io::socket::stream::aspect,
32 : // Заглужка descriptor_type не поддерживает set_options
33 :
34 : ::iow::io::reader::asio::aspect,
35 : ::iow::io::writer::asio::aspect,
36 : ::iow::io::rw::aspect,
37 : ::iow::io::basic::aspect< std::recursive_mutex >::advice_list
38 : >{};
39 :
40 : typedef ::iow::io::descriptor::holder<aspect_stream> stream_holder_t;
41 :
42 :
43 3 : UNIT(stream_holder_unit, "")
44 : {
45 : using namespace fas::testing;
46 1 : iow::asio::io_service service;
47 1 : int f1[2]={-1, -1};
48 1 : int f2[2]={-1, -1};
49 1 : int res1 = ::pipe(f1);
50 1 : int res2 = ::pipe(f2);
51 1 : t << message("pipe1: ") << res1 << " " << f1[0] << " " << f1[1] << std::endl;
52 1 : t << message("pipe2: ") << res2 << " " << f2[0] << " " << f2[1] << std::endl;
53 2 : descriptor_type d1(service, f1[0]);
54 2 : descriptor_type d2(service, f2[1]);
55 :
56 2 : auto h1 = std::make_shared<stream_holder_t>(std::move(d1));
57 2 : auto h2 = std::make_shared<stream_holder_t>(std::move(d2));
58 1 : const char* instr = "Hello world!\r\nBye!";
59 1 : t << message("write...");
60 1 : res1 = int(write(f1[1], instr, std::strlen(instr) ));
61 1 : t << message("...write:") << res1;
62 :
63 2 : options_type opt;
64 2 : opt.input_handler = [&]( iow::io::data_ptr d, size_t, options_type::output_handler_type /*callback*/){
65 1 : t << message("data: ") << d ;
66 1 : h2->get_aspect().get< ::iow::io::writer::_output_>()( *h2, std::move(d) );
67 1 : };
68 1 : opt.reader.sep = "\r\n";
69 1 : opt.reader.trimsep = true;
70 1 : opt.writer.sep = "";
71 1 : t << message("start1");
72 1 : h1->start(opt);
73 1 : t << message("start2");
74 1 : h2->reconfigure(opt);
75 1 : t << message("service.run()...");
76 1 : service.run_one();
77 1 : t << message("...service.run()");
78 :
79 :
80 : char outstr[128];
81 1 : ssize_t size = read(f2[0], outstr, 128);
82 1 : outstr[size]='\0';
83 1 : t << message("outstr: ") << outstr << std::endl;
84 1 : t << nothing;
85 2 : t << equal<expect, std::string>(outstr, "Hello world!") << FAS_FL;
86 1 : }
87 :
88 1 : BEGIN_SUITE(stream_holder,"")
89 : ADD_UNIT(stream_holder_unit)
90 7 : END_SUITE(stream_holder)
91 :
|