From 0e7f2cef26bde782a5758b5e9a3dfe20f745df8f Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Fri, 31 Dec 2010 01:25:38 +0100 Subject: Added an echoing telnet server as a base for a telnet command interface. --- config.cpp | 1 + main.cpp | 2 ++ telnet_connection.cpp | 15 +++++++++++++++ telnet_connection.h | 26 ++++++++++++++++++++++++++ telnetd.cpp | 19 +++++++++++++++++++ telnetd.h | 19 +++++++++++++++++++ 6 files changed, 82 insertions(+) create mode 100644 telnet_connection.cpp create mode 100644 telnet_connection.h create mode 100644 telnetd.cpp create mode 100644 telnetd.h diff --git a/config.cpp b/config.cpp index e59f2c2..68e8a2f 100644 --- a/config.cpp +++ b/config.cpp @@ -11,6 +11,7 @@ void config::init() { desc.add_options() ("audist.music_root", po::value(), "music root") ("audist.httpd_port", po::value()->default_value(8000), "httpd port") + ("audist.telnetd_port", po::value()->default_value(7681), "telnetd port") ("audist.threads", po::value()->default_value(10), "threads") ("audist.database", po::value(), "database string") ; diff --git a/main.cpp b/main.cpp index 28b846b..ccca2ad 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,7 @@ #include "decoder.h" #include "encoder.h" #include "httpd.h" +#include "telnetd.h" #include #include @@ -19,6 +20,7 @@ int main(int argc, char **argv) { boost::asio::io_service io_service; HTTP::Server httpd(io_service, tcp::endpoint(tcp::v6(), config::vm["audist.httpd_port"].as())); + telnet::Server telnetd(io_service, tcp::endpoint(tcp::v6(), config::vm["audist.telnetd_port"].as())); std::size_t num_threads = config::vm["audist.threads"].as(); std::vector > threads; diff --git a/telnet_connection.cpp b/telnet_connection.cpp new file mode 100644 index 0000000..e2ea96f --- /dev/null +++ b/telnet_connection.cpp @@ -0,0 +1,15 @@ +#include "telnet_connection.h" + +#include + +telnet::Connection::Connection(boost::asio::io_service& io_service) : socket(io_service) { +} + +void telnet::Connection::handle_read(const boost::system::error_code& error, size_t bytes_transferred) { + boost::asio::write(socket, buf); +} + +void telnet::Connection::start() { + boost::asio::async_read_until(socket, buf, "\n", boost::bind(&Connection::handle_read, shared_from_this(), + boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); +} diff --git a/telnet_connection.h b/telnet_connection.h new file mode 100644 index 0000000..8ea3cad --- /dev/null +++ b/telnet_connection.h @@ -0,0 +1,26 @@ +#ifndef TELNET_CONNECTION_H +#define TELNET_CONNECTION_H + +#include +#include + +using boost::asio::ip::tcp; + +namespace telnet { + class Connection : public boost::enable_shared_from_this { + friend class Server; + + private: + Connection(boost::asio::io_service& io_service); + void handle_read(const boost::system::error_code& error, size_t bytes_transferred); + tcp::socket socket; + boost::asio::streambuf buf; + + public: + typedef boost::shared_ptr p; + + void start(); + }; +}; + +#endif diff --git a/telnetd.cpp b/telnetd.cpp new file mode 100644 index 0000000..9999d50 --- /dev/null +++ b/telnetd.cpp @@ -0,0 +1,19 @@ +#include "telnetd.h" + +#include + +telnet::Server::Server(boost::asio::io_service& io_service, const tcp::endpoint& endpoint) : acceptor_(io_service, endpoint) { + start_accept(); +} + +void telnet::Server::start_accept() { + Connection::p new_connection = Connection::p(new Connection(acceptor_.io_service())); + acceptor_.async_accept(new_connection->socket, boost::bind(&Server::handle_accept, this, new_connection, boost::asio::placeholders::error)); +} + +void telnet::Server::handle_accept(Connection::p new_connection, const boost::system::error_code& error) { + if(!error) { + new_connection->start(); + start_accept(); + } +} diff --git a/telnetd.h b/telnetd.h new file mode 100644 index 0000000..e6126f1 --- /dev/null +++ b/telnetd.h @@ -0,0 +1,19 @@ +#ifndef TELNETD_H +#define TELNETD_H + +#include "telnet_connection.h" + +namespace telnet { + class Server { + public: + Server(boost::asio::io_service& io_service, const tcp::endpoint& endpoint); + + private: + void start_accept(); + void handle_accept(Connection::p new_connection, const boost::system::error_code& error); + + tcp::acceptor acceptor_; + }; +}; + +#endif -- cgit v1.2.3