asio 0.3.7 Home | Reference | Tutorial | Examples | Design
Examples

serialization/server.cpp

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