/* * StockServer.cpp * * Created on: 18.11.2011 * Author: heber */ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include "connection.hpp" // Must come before boost/serialization headers. #include #include "FragmentJob.hpp" #include "StockServer.hpp" StockServer::StockServer(boost::asio::io_service& io_service, unsigned short port) : acceptor_(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port) ) { FragmentJob s(std::string("test"), 1); jobs_.push_back(s); // Start an accept operation for a new connection. connection_ptr new_conn(new connection(acceptor_.get_io_service())); acceptor_.async_accept(new_conn->socket(), boost::bind(&StockServer::handle_accept, this, boost::asio::placeholders::error, new_conn)); } void StockServer::handle_accept(const boost::system::error_code& e, connection_ptr conn) { std::cout << "handle_accept called." << std::endl; if (!e) { // Successfully accepted a new connection. Send the list of stocks to the // client. The connection::async_write() function will automatically // serialize the data structure for us. conn->async_write(jobs_, boost::bind(&StockServer::handle_write, this, boost::asio::placeholders::error, conn)); } connection_ptr new_conn(new connection(acceptor_.get_io_service())); acceptor_.async_accept(new_conn->socket(), boost::bind(&StockServer::handle_accept, this, boost::asio::placeholders::error, new_conn)); } void StockServer::handle_write(const boost::system::error_code& e, connection_ptr conn) { // Nothing to do. The socket will be closed automatically when the last // reference to the connection object goes away. }