Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "server.h"
00012
00013 #include <boost/thread.hpp>
00014 #include <boost/bind.hpp>
00015 #include <boost/shared_ptr.hpp>
00016 #include <boost/lexical_cast.hpp>
00017
00018 #include <vector>
00019
00020 namespace ros_http_video_streamer
00021 {
00022
00023 server::server(const ServerConfiguration& server_conf, std::size_t thread_pool_size)
00024 : server_conf_(server_conf),
00025 thread_pool_size_(thread_pool_size),
00026 acceptor_(io_service_),
00027 encoder_manager_(),
00028 new_connection_(new connection(io_service_, encoder_manager_, server_conf))
00029 {
00030
00031 boost::asio::ip::tcp::resolver resolver(io_service_);
00032 boost::asio::ip::tcp::resolver::query query("0.0.0.0",
00033 boost::lexical_cast<std::string>(server_conf.port_));
00034 boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
00035 acceptor_.open(endpoint.protocol());
00036 acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
00037 acceptor_.bind(endpoint);
00038 acceptor_.listen();
00039 acceptor_.async_accept(new_connection_->socket(),
00040 boost::bind(&server::handle_accept, this,
00041 boost::asio::placeholders::error));
00042 }
00043
00044 void server::run()
00045 {
00046
00047 std::vector<boost::shared_ptr<boost::thread> > threads;
00048 for (std::size_t i = 0; i < thread_pool_size_; ++i)
00049 {
00050 boost::shared_ptr<boost::thread> thread(new boost::thread(
00051 boost::bind(&boost::asio::io_service::run, &io_service_)));
00052 threads.push_back(thread);
00053 }
00054
00055
00056 for (std::size_t i = 0; i < threads.size(); ++i)
00057 threads[i]->join();
00058 }
00059
00060 void server::stop()
00061 {
00062 io_service_.stop();
00063 }
00064
00065 void server::handle_accept(const boost::system::error_code& e)
00066 {
00067 if (!e)
00068 {
00069
00070
00071
00072 new_connection_->start();
00073 new_connection_.reset(new connection(io_service_, encoder_manager_, server_conf_));
00074 acceptor_.async_accept(new_connection_->socket(),
00075 boost::bind(&server::handle_accept, this,
00076 boost::asio::placeholders::error));
00077 }
00078 }
00079
00080 }
00081