Telnet++
3.1.0.2
A C++ library for interacting with Telnet streams
|
An abstraction for a Telnet session. More...
#include <session.hpp>
Classes | |
struct | impl |
Public Member Functions | |
template<typename Channel > | |
session (Channel &channel) | |
Constructor. | |
~session () | |
Destructor. | |
bool | is_alive () const |
Returns whether the session is still alive. | |
void | close () |
Closes the session. | |
void | async_read (std::function< void(telnetpp::bytes)> const &callback) |
Requests data from the underlying channel, acting on any Telnet primitives that are received. More... | |
void | write (telnetpp::element const &elem) |
Sends a Telnet data element. Translates the element into a sequence of bytes, that is then sent to the continuation. More... | |
void | install (telnetpp::command_type cmd, std::function< void(telnetpp::command)> const &handler) |
Installs a handler for the given command. | |
void | install (telnetpp::client_option &option) |
Installs a client option. | |
void | install (telnetpp::server_option &option) |
Installs a server option. | |
An abstraction for a Telnet session.
The first part of using a telnetpp::session is understanding how to send and receive data. Even if no options are used during a session, this is still necessary because some data needs special handling.
Sending data uses the telnetpp::session::write function. This handles any such special sequences and then passes that data onto the continuation supplied. Example:
Receiving data works using a continuation; a function that is called once the data is received. This means that it is non-blocking. Example:
Now that we can send and receive data over a Telnet connection, the next step is to install some options. This allows the session to automatically route messages to and from these options as part of the normal data flow. Example:
With these options installed, the normal message as implemented above will automatically activate or deactivate the options if this is requested by the remote. It is, of course, just fine if you want to activate these options yourself. Example:
Likewise, any functions that cause option-specific negotiation (subnegotiations) to occur use a similar pattern:
There also exist a small number of Telnet commands, such as AYT (Are You There), BRK (Break) and NOP (No Operation). These can be sent using the telnetpp::session::send function in a similar way to plain text. Example:
They can also have handlers registered in order to respond to them:
void telnetpp::session::async_read | ( | std::function< void(telnetpp::bytes)> const & | callback | ) |
Requests data from the underlying channel, acting on any Telnet primitives that are received.
Note: the callback may be called several times for any async_read. For an example, it may contain two pieces of regular text surrounding a block of negotiation, which may change the way those two pieces of text are interpreted. For this reason, after an async_read is complete, the callback will always be called with an empty parameter to indicate that a new read request can be made.
void telnetpp::session::write | ( | telnetpp::element const & | elem | ) |
Sends a Telnet data element. Translates the element into a sequence of bytes, that is then sent to the continuation.
content | The data to send |