asio 0.3.8rc3 Home | Reference | Tutorial | Examples | Design
Examples

serialization/server.cpp

Go to the documentation of this file.
00001 //
00002 // server.cpp
00003 // ~~~~~~~~~~
00004 //
00005 // Copyright (c) 2003-2007 Christopher M. Kohlhoff (chris at kohlhoff dot com)
00006 //
00007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
00008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
00009 //
00010 
00011 #include <asio.hpp>
00012 #include <boost/bind.hpp>
00013 #include <boost/lexical_cast.hpp>
00014 #include <iostream>
00015 #include <vector>
00016 #include "connection.hpp" // Must come before boost/serialization headers.
00017 #include <boost/serialization/vector.hpp>
00018 #include "stock.hpp"
00019 
00020 namespace s11n_example {
00021 
00023 class server
00024 {
00025 public:
00028   server(asio::io_service& io_service, unsigned short port)
00029     : acceptor_(io_service,
00030         asio::ip::tcp::endpoint(asio::ip::tcp::v4(), port))
00031   {
00032     // Create the data to be sent to each client.
00033     stock s;
00034     s.code = "ABC";
00035     s.name = "A Big Company";
00036     s.open_price = 4.56;
00037     s.high_price = 5.12;
00038     s.low_price = 4.33;
00039     s.last_price = 4.98;
00040     s.buy_price = 4.96;
00041     s.buy_quantity = 1000;
00042     s.sell_price = 4.99;
00043     s.sell_quantity = 2000;
00044     stocks_.push_back(s);
00045     s.code = "DEF";
00046     s.name = "Developer Entertainment Firm";
00047     s.open_price = 20.24;
00048     s.high_price = 22.88;
00049     s.low_price = 19.50;
00050     s.last_price = 19.76;
00051     s.buy_price = 19.72;
00052     s.buy_quantity = 34000;
00053     s.sell_price = 19.85;
00054     s.sell_quantity = 45000;
00055     stocks_.push_back(s);
00056 
00057     // Start an accept operation for a new connection.
00058     connection_ptr new_conn(new connection(acceptor_.io_service()));
00059     acceptor_.async_accept(new_conn->socket(),
00060         boost::bind(&server::handle_accept, this,
00061           asio::placeholders::error, new_conn));
00062   }
00063 
00065   void handle_accept(const asio::error_code& e, connection_ptr conn)
00066   {
00067     if (!e)
00068     {
00069       // Successfully accepted a new connection. Send the list of stocks to the
00070       // client. The connection::async_write() function will automatically
00071       // serialize the data structure for us.
00072       conn->async_write(stocks_,
00073           boost::bind(&server::handle_write, this,
00074             asio::placeholders::error, conn));
00075 
00076       // Start an accept operation for a new connection.
00077       connection_ptr new_conn(new connection(acceptor_.io_service()));
00078       acceptor_.async_accept(new_conn->socket(),
00079           boost::bind(&server::handle_accept, this,
00080             asio::placeholders::error, new_conn));
00081     }
00082     else
00083     {
00084       // An error occurred. Log it and return. Since we are not starting a new
00085       // accept operation the io_service will run out of work to do and the
00086       // server will exit.
00087       std::cerr << e.message() << std::endl;
00088     }
00089   }
00090 
00092   void handle_write(const asio::error_code& e, connection_ptr conn)
00093   {
00094     // Nothing to do. The socket will be closed automatically when the last
00095     // reference to the connection object goes away.
00096   }
00097 
00098 private:
00100   asio::ip::tcp::acceptor acceptor_;
00101 
00103   std::vector<stock> stocks_;
00104 };
00105 
00106 } // namespace s11n_example
00107 
00108 int main(int argc, char* argv[])
00109 {
00110   try
00111   {
00112     // Check command line arguments.
00113     if (argc != 2)
00114     {
00115       std::cerr << "Usage: server <port>" << std::endl;
00116       return 1;
00117     }
00118     unsigned short port = boost::lexical_cast<unsigned short>(argv[1]);
00119 
00120     asio::io_service io_service;
00121     s11n_example::server server(io_service, port);
00122     io_service.run();
00123   }
00124   catch (std::exception& e)
00125   {
00126     std::cerr << e.what() << std::endl;
00127   }
00128 
00129   return 0;
00130 }
asio 0.3.8rc3 Home | Reference | Tutorial | Examples | Design