Line data Source code
1 : //
2 : // Author: Vladimir Migashko <migashko@gmail.com>, (C) 2013-2018
3 : //
4 : // Copyright: See COPYING file that comes with this distribution
5 : //
6 :
7 : #pragma once
8 :
9 : #include <iow/ip/tcp/connection/connection.hpp>
10 : #include <memory>
11 : #include <mutex>
12 : #include <wfc/iinterface.hpp>
13 : #include <wfc/logger.hpp>
14 :
15 : namespace wfc{ namespace io{
16 :
17 0 : class tcp_connection
18 : : public ::iow::ip::tcp::connection::connection_base<>
19 : , public std::enable_shared_from_this<tcp_connection>
20 : , public iinterface
21 : {
22 : public:
23 : typedef ::iow::ip::tcp::connection::connection_base<> super;
24 : typedef super::descriptor_type descriptor_type;
25 : typedef super::io_id_type io_id_type;
26 : typedef super::output_handler_type output_handler_type;
27 : typedef super::mutex_type mutex_type;
28 : typedef super::data_ptr data_ptr;
29 :
30 0 : explicit tcp_connection(descriptor_type&& desc)
31 0 : : super( std::move(desc) )
32 0 : {}
33 :
34 : void reset()
35 : {
36 : std::lock_guard< mutex_type > lk( super::mutex() );
37 : super::reset_(*this);
38 : }
39 :
40 0 : void start()
41 : {
42 0 : std::lock_guard< mutex_type > lk( super::mutex() );
43 0 : this->start_(*this);
44 0 : }
45 :
46 : template<typename O>
47 0 : void start(O&& opt)
48 : {
49 0 : std::lock_guard< mutex_type > lk( super::mutex() );
50 0 : super::start_(*this, std::forward<O>(opt));
51 0 : }
52 :
53 : template<typename O>
54 : void reconfigure(O&& opt)
55 : {
56 : std::lock_guard< mutex_type > lk( super::mutex() );
57 : super::reconfigure_(*this, std::forward<O>(opt));
58 : }
59 :
60 : template<typename O>
61 0 : void initialize(O opt)
62 : {
63 0 : std::lock_guard< mutex_type > lk( super::mutex() );
64 0 : auto wtarget = opt.target;
65 0 : auto startup_handler = opt.startup_handler;
66 0 : auto shutdown_handler = opt.shutdown_handler;
67 0 : std::weak_ptr<tcp_connection> wthis = this->shared_from_this();
68 :
69 0 : if ( !opt.direct_mode)
70 : {
71 0 : opt.startup_handler = [wtarget, wthis, startup_handler](io_id_type id, output_handler_type outgoing)
72 : {
73 0 : if ( auto ptarget = wtarget.lock() )
74 : {
75 0 : ptarget->reg_io(id, wthis);
76 : }
77 0 : if ( startup_handler!=nullptr )
78 : {
79 0 : startup_handler(id, outgoing);
80 : }
81 0 : };
82 :
83 0 : opt.shutdown_handler = [wtarget, shutdown_handler](io_id_type id)
84 : {
85 0 : if ( auto ptarget = wtarget.lock() )
86 : {
87 0 : ptarget->unreg_io(id);
88 : }
89 :
90 0 : if ( shutdown_handler!=nullptr )
91 : {
92 0 : shutdown_handler(id);
93 : }
94 0 : };
95 : }
96 :
97 0 : super::initialize_(*this, std::forward<O>(opt));
98 0 : }
99 :
100 :
101 : void close()
102 : {
103 : std::lock_guard< mutex_type > lk( super::mutex() );
104 : this->close_(*this);
105 : }
106 :
107 0 : void stop()
108 : {
109 0 : std::lock_guard< mutex_type > lk( super::mutex() );
110 0 : super::stop_(*this);
111 0 : }
112 :
113 : template<typename Handler>
114 : void shutdown(Handler&& handler)
115 : {
116 : std::lock_guard< mutex_type > lk( super::mutex() );
117 : super::shutdown_(*this, std::forward<Handler>(handler));
118 : }
119 :
120 0 : virtual void reg_io(io_id_t /*io_id*/, std::weak_ptr<iinterface> /*itf*/) override
121 : {
122 0 : }
123 :
124 0 : virtual void unreg_io(io_id_t /*io_id*/) override
125 : {
126 0 : }
127 :
128 0 : virtual void perform_io(data_ptr d, io_id_t /*io_id*/, output_handler_t /*handler*/) override
129 : {
130 0 : std::lock_guard< mutex_type > lk( super::mutex() );
131 0 : if ( auto outgoing = super::get_aspect().get< iow::io::descriptor::_context_>().output_handler )
132 : {
133 0 : outgoing( std::move(d) );
134 0 : }
135 0 : }
136 :
137 : };
138 :
139 : }}
|