1 #ifndef VTESTBED_SERVER_REMOTE_CLIENT_HH 2 #define VTESTBED_SERVER_REMOTE_CLIENT_HH 4 #include <unistdx/base/log_message> 5 #include <unistdx/base/websocketbuf> 6 #include <unistdx/io/fildesbuf> 7 #include <unistdx/net/pstream> 8 #include <unistdx/net/socket> 10 #include <vtestbed/core/testbed.hh> 11 #include <vtestbed/server/remote_client_state.hh> 18 public sys::basic_websocketbuf<char,std::char_traits<char>>,
19 public sys::basic_fildesbuf<char,std::char_traits<char>,sys::socket>
26 sys::basic_websocketbuf<char,std::char_traits<char>>(),
37 typedef sys::pstream stream_type;
38 typedef stream_type::ipacket_guard ipacket_guard;
39 typedef sys::opacket_guard<stream_type> opacket_guard;
43 sys::socket_address _address;
44 Remote_client_state _state = Remote_client_state::Initial;
45 bool _verbose =
false;
51 const sys::socket_address& address,
54 _buffer(std::move(socket)),
58 ? websocketbuf::role_type::server
59 : websocketbuf::role_type::client
64 _buffer(std::move(rhs._buffer.fd())),
65 _address(rhs._address),
67 _verbose(rhs._verbose) {}
70 _buffer.fd().shutdown(sys::shutdown_flag::read_write);
76 if (_state != Remote_client_state::Initial) {
77 return handshake_succeded();
79 if (_buffer.handshake()) {
80 state(Remote_client_state::Handshake_succeeded);
82 return handshake_succeded();
86 process(
const sys::epoll_event& ev,
testbed_type& testbed) {
88 if (handshake_succeded()) {
89 if (_buffer.read_packet()) {
97 if (!handshake_succeded()) {
101 stream_type s(&_buffer);
102 ipacket_guard guard(s.rdbuf());
105 this->error(
"Unable to receive packet: _", err.what());
111 if (!handshake_succeded()) {
115 stream_type s(&_buffer);
116 opacket_guard guard(s);
121 this->error(
"Unable to send packet: _", err.what());
127 auto n1 = _buffer.available();
129 auto n2 = _buffer.available();
130 this->info(
"_ received _ bytes", _address, n2-n1);
135 if (!_buffer.dirty()) {
139 auto n1 = _buffer.remaining();
141 auto n2 = _buffer.remaining();
142 this->info(
"_ sent _ bytes", _address, n1-n2);
143 }
catch (
const sys::bad_call& err) {
144 if (err.errc() != std::errc::broken_pipe) {
155 inline const sys::socket&
156 socket()
const noexcept {
160 inline const sys::socket_address&
161 address()
const noexcept {
166 is_server()
const noexcept {
167 return _buffer.role() == websocketbuf::role_type::server;
171 is_client()
const noexcept {
172 return _buffer.role() == websocketbuf::role_type::client;
176 handshake_succeded()
const noexcept {
177 return _state == Remote_client_state::Handshake_succeeded;
181 handshake_failed()
const noexcept {
182 return _state == Remote_client_state::Handshake_failed;
186 verbose(
bool rhs) noexcept {
193 state(Remote_client_state rhs) {
195 this->info(
"_ _", _address, _state);
198 template <
class ... Args>
200 info(
const Args& ... args) {
202 sys::log_message(
"client", args ...);
206 template <
class ... Args>
208 error(
const Args& ... args) {
209 sys::log_message(
"client", args ...);
218 #endif // vim:filetype=cpp