summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2010-12-31 01:25:38 +0100
committerJon Bergli Heier <snakebite@jvnv.net>2010-12-31 01:25:38 +0100
commit0e7f2cef26bde782a5758b5e9a3dfe20f745df8f (patch)
tree736b53010218fba8134a1660c7a3ad5ef998f01c
parentb439fcd1916cef267bde32f3424d8311519d3d16 (diff)
Added an echoing telnet server as a base for a telnet command interface.
-rw-r--r--config.cpp1
-rw-r--r--main.cpp2
-rw-r--r--telnet_connection.cpp15
-rw-r--r--telnet_connection.h26
-rw-r--r--telnetd.cpp19
-rw-r--r--telnetd.h19
6 files changed, 82 insertions, 0 deletions
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<std::string>(), "music root")
("audist.httpd_port", po::value<int>()->default_value(8000), "httpd port")
+ ("audist.telnetd_port", po::value<int>()->default_value(7681), "telnetd port")
("audist.threads", po::value<std::size_t>()->default_value(10), "threads")
("audist.database", po::value<std::string>(), "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 <iostream>
#include <vector>
@@ -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<int>()));
+ telnet::Server telnetd(io_service, tcp::endpoint(tcp::v6(), config::vm["audist.telnetd_port"].as<int>()));
std::size_t num_threads = config::vm["audist.threads"].as<std::size_t>();
std::vector<boost::shared_ptr<boost::thread> > 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 <boost/bind.hpp>
+
+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 <boost/asio.hpp>
+#include <boost/enable_shared_from_this.hpp>
+
+using boost::asio::ip::tcp;
+
+namespace telnet {
+ class Connection : public boost::enable_shared_from_this<Connection> {
+ 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<Connection> 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 <boost/bind.hpp>
+
+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