Telnet++  3.1.0.2
A C++ library for interacting with Telnet streams
session.hpp
1 #pragma once
2 
3 #include "telnetpp/core.hpp"
4 #include "telnetpp/element.hpp"
5 
6 #include <functional>
7 #include <memory>
8 
9 namespace telnetpp {
10 
11 class client_option;
12 class server_option;
13 
14 //* =========================================================================
139 //* =========================================================================
140 class TELNETPP_EXPORT session final // NOLINT
141 {
142 public:
143  //* =====================================================================
145  //* =====================================================================
146  template <typename Channel>
147  explicit session(Channel &channel) : session{}
148  {
149  channel_ = std::unique_ptr<channel_concept>(
150  std::make_unique<channel_model<Channel>>(channel));
151  }
152 
153  //* =====================================================================
155  //* =====================================================================
157 
158  //* =====================================================================
160  //* =====================================================================
161  [[nodiscard]] bool is_alive() const;
162 
163  //* =====================================================================
165  //* =====================================================================
166  void close();
167 
168  //* =====================================================================
178  //* =====================================================================
179  void async_read(std::function<void(telnetpp::bytes)> const &callback);
180 
181  //* =====================================================================
185  //* =====================================================================
186  void write(telnetpp::element const &elem);
187 
188  //* =====================================================================
190  //* =====================================================================
191  void install(
192  telnetpp::command_type cmd,
193  std::function<void(telnetpp::command)> const &handler);
194 
195  //* =====================================================================
197  //* =====================================================================
198  void install(telnetpp::client_option &option);
199 
200  //* =====================================================================
202  //* =====================================================================
203  void install(telnetpp::server_option &option);
204 
205 private:
206  //* =====================================================================
208  //* =====================================================================
209  session();
210 
211  //* =====================================================================
213  //* =====================================================================
214  struct channel_concept
215  {
216  //* =================================================================
218  //* =================================================================
219  virtual ~channel_concept() = default;
220 
221  //* =================================================================
224  //* =================================================================
225  virtual void async_read(std::function<void(bytes)> const &) = 0;
226 
227  //* =================================================================
229  //* =================================================================
230  virtual void write(bytes data) = 0;
231 
232  //* =================================================================
234  //* =================================================================
235  [[nodiscard]] virtual bool is_alive() const = 0;
236 
237  //* =================================================================
239  //* =================================================================
240  virtual void close() = 0;
241  };
242 
243  template <typename Channel>
244  struct channel_model final : channel_concept
245  {
246  //* =================================================================
248  //* =================================================================
249  explicit channel_model(Channel &channel) : channel_{channel}
250  {
251  }
252 
253  //* =================================================================
256  //* =================================================================
257  void async_read(std::function<void(bytes)> const &callback) override
258  {
259  channel_.async_read(callback);
260  }
261 
262  //* =================================================================
264  //* =================================================================
265  void write(bytes data) override
266  {
267  channel_.write(data);
268  }
269 
270  //* =================================================================
272  //* =================================================================
273  [[nodiscard]] bool is_alive() const override
274  {
275  return channel_.is_alive();
276  }
277 
278  //* =================================================================
280  //* =================================================================
281  void close() override
282  {
283  channel_.close();
284  }
285 
286  private:
287  Channel &channel_;
288  };
289 
290  struct impl;
291  std::unique_ptr<channel_concept> channel_;
292  std::unique_ptr<impl> pimpl_;
293 };
294 
295 } // namespace telnetpp
A class that represents a Telnet option's client side. That is, the side that receives WILL and WONT ...
Definition: client_option.hpp:35
A class that encapsulates the value of a Telnet command.
Definition: command.hpp:13
A common type that can contain any Telnet operation, such as a command, negotiation,...
An class that encapsulates one side of a Telnet option.
Definition: option.hpp:46
A class that represents a Telnet option's server side. That is, the side that receives DO and DONT ne...
Definition: server_option.hpp:35
An abstraction for a Telnet session.
Definition: session.hpp:141
session(Channel &channel)
Constructor.
Definition: session.hpp:147
~session()
Destructor.