From 9f78196611d8065f63ae4a81297723663d222ebe Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Mon, 27 Dec 2010 02:47:56 +0100 Subject: Added a simple HTTP service which doesn't yet do anything useful. --- httpd.cpp | 35 +++++++++++++++++++++++++++++++++++ httpd.h | 35 +++++++++++++++++++++++++++++++++++ main.cpp | 5 +++++ 3 files changed, 75 insertions(+) create mode 100644 httpd.cpp create mode 100644 httpd.h diff --git a/httpd.cpp b/httpd.cpp new file mode 100644 index 0000000..5de9948 --- /dev/null +++ b/httpd.cpp @@ -0,0 +1,35 @@ +#include "httpd.h" + +#include + +#include + +HTTPConnection::HTTPConnection(boost::asio::io_service& io_service) : socket(io_service) { +} + +void HTTPConnection::handle_write(const boost::system::error_code& error, size_t bytes_transferred) { +} + +HTTPConnection::pointer HTTPConnection::create(boost::asio::io_service& io_service) { + return pointer(new HTTPConnection(io_service)); +} + +void HTTPConnection::start() { + std::cout << "kake" << std::endl; +} + +HTTPServer::HTTPServer(boost::asio::io_service& io_service) : acceptor_(io_service, tcp::endpoint(tcp::v4(), 8000)) { + start_accept(); +} + +void HTTPServer::start_accept() { + HTTPConnection::pointer new_connection = HTTPConnection::create(acceptor_.io_service()); + acceptor_.async_accept(new_connection->socket, boost::bind(&HTTPServer::handle_accept, this, new_connection, boost::asio::placeholders::error)); +} + +void HTTPServer::handle_accept(HTTPConnection::pointer new_connection, const boost::system::error_code& error) { + if(!error) { + new_connection->start(); + start_accept(); + } +} diff --git a/httpd.h b/httpd.h new file mode 100644 index 0000000..f5a3414 --- /dev/null +++ b/httpd.h @@ -0,0 +1,35 @@ +#ifndef HTTPD_H +#define HTTPD_H + +#include +#include + +using boost::asio::ip::tcp; + +class HTTPConnection : public boost::enable_shared_from_this { + friend class HTTPServer; + + private: + HTTPConnection(boost::asio::io_service& io_service); + void handle_write(const boost::system::error_code& error, size_t bytes_transferred); + tcp::socket socket; + + public: + typedef boost::shared_ptr pointer; + + static pointer create(boost::asio::io_service& io_service); + void start(); +}; + +class HTTPServer { + private: + void start_accept(); + void handle_accept(HTTPConnection::pointer new_connection, const boost::system::error_code& error); + + tcp::acceptor acceptor_; + + public: + HTTPServer(boost::asio::io_service& io_service); +}; + +#endif diff --git a/main.cpp b/main.cpp index 55c1de0..bb165a1 100644 --- a/main.cpp +++ b/main.cpp @@ -1,9 +1,14 @@ #include #include "music.h" +#include "httpd.h" int main(int argc, char **argv) { try { music::init(argv[1]); + + boost::asio::io_service io_service; + HTTPServer httpd(io_service); + io_service.run(); } catch(std::exception& e) { std::cerr << e.what() << std::endl; } -- cgit v1.2.3