irccd  3.0.3
transport_client.hpp
1 /*
2  * transport_client.hpp -- server side transport clients
3  *
4  * Copyright (c) 2013-2019 David Demelier <markand@malikania.fr>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef IRCCD_DAEMON_TRANSPORT_CLIENT_HPP
20 #define IRCCD_DAEMON_TRANSPORT_CLIENT_HPP
21 
27 #include <irccd/sysconfig.hpp>
28 
29 #include <deque>
30 #include <memory>
31 #include <string_view>
32 
33 #include <irccd/stream.hpp>
34 
35 namespace irccd::daemon {
36 
37 class transport_server;
38 
45 class transport_client : public std::enable_shared_from_this<transport_client> {
46 public:
50  using handshake_handler = std::function<void (std::error_code)>;
51 
55  enum class state {
57  ready,
58  closing
59  };
60 
61 private:
63  std::weak_ptr<transport_server> parent_;
64  std::shared_ptr<stream> stream_;
65  std::deque<std::pair<nlohmann::json, stream::send_handler>> queue_;
66 
67  void auth(handshake_handler);
68  void flush();
69  void erase();
70 
71 public:
79  transport_client(std::weak_ptr<transport_server> server,
80  std::shared_ptr<stream> stream) noexcept;
81 
87  auto get_state() const noexcept -> state;
88 
94  void set_state(state state) noexcept;
95 
103 
117  void read(stream::recv_handler handler);
118 
131  void write(nlohmann::json json, stream::send_handler handler = nullptr);
132 
140  void success(const std::string& command, stream::send_handler handler = nullptr);
141 
150  void error(std::error_code code, stream::send_handler handler = nullptr);
151 
161  void error(std::error_code code, std::string_view command, stream::send_handler handler = nullptr);
162 };
163 
164 } // !irccd::daemon
165 
166 #endif // !IRCCD_DAEMON_TRANSPORT_CLIENT_HPP
The class that connect to a IRC server.
Definition: server.hpp:256
Abstract transport client class.
Definition: transport_client.hpp:45
void write(nlohmann::json json, stream::send_handler handler=nullptr)
void success(const std::string &command, stream::send_handler handler=nullptr)
state
Definition: transport_client.hpp:55
@ authenticating
client is authenticating
void set_state(state state) noexcept
transport_client(std::weak_ptr< transport_server > server, std::shared_ptr< stream > stream) noexcept
std::function< void(std::error_code)> handshake_handler
Definition: transport_client.hpp:50
void error(std::error_code code, stream::send_handler handler=nullptr)
auto get_state() const noexcept -> state
void read(stream::recv_handler handler)
void handshake(handshake_handler)
Abstract stream interface.
Definition: stream.hpp:58
Main irccd namespace.
Definition: bot.hpp:41
Definition: bot.hpp:253