diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2010-12-27 02:47:56 +0100 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2010-12-27 02:47:56 +0100 |
commit | 9f78196611d8065f63ae4a81297723663d222ebe (patch) | |
tree | b70e94f4f581db1298d86d3a44b3a2e2851c076d | |
parent | 6e376f69305dabe59362bb80051e4265c6409fe3 (diff) |
Added a simple HTTP service which doesn't yet do anything useful.
-rw-r--r-- | httpd.cpp | 35 | ||||
-rw-r--r-- | httpd.h | 35 | ||||
-rw-r--r-- | main.cpp | 5 |
3 files changed, 75 insertions, 0 deletions
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 <boost/bind.hpp> + +#include <iostream> + +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(); + } +} @@ -0,0 +1,35 @@ +#ifndef HTTPD_H +#define HTTPD_H + +#include <boost/asio.hpp> +#include <boost/enable_shared_from_this.hpp> + +using boost::asio::ip::tcp; + +class HTTPConnection : public boost::enable_shared_from_this<HTTPConnection> { + 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<HTTPConnection> 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 @@ -1,9 +1,14 @@ #include <iostream> #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; } |