Boost C++ Libraries Home Libraries People FAQ More

Home | Reference | Tutorial | Examples | Design

s11n_example::server Class Reference

Collaboration diagram for s11n_example::server:

Collaboration graph
List of all members.

Detailed Description

Serves stock quote information to any client that connects to it.

Definition at line 13 of file server.cpp.

Public Member Functions

 server (boost::asio::io_service &io_service, unsigned short port)
 Constructor opens the acceptor and starts waiting for the first incoming connection.
void handle_accept (const boost::asio::error &e, connection_ptr conn)
 Handle completion of a accept operation.
void handle_write (const boost::asio::error &e, connection_ptr conn)
 Handle completion of a write operation.

Private Attributes

boost::asio::ip::tcp::acceptor acceptor_
 The acceptor object used to accept incoming socket connections.
std::vector< stockstocks_
 The data to be sent to each client.


Constructor & Destructor Documentation

s11n_example::server::server ( boost::asio::io_service io_service,
unsigned short  port 
)

Constructor opens the acceptor and starts waiting for the first incoming connection.

Definition at line 17 of file server.cpp.

00019     : acceptor_(io_service,
00020         boost::asio::ip::tcp::endpoint(boost::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           boost::asio::placeholders::error, new_conn));


Member Function Documentation

void s11n_example::server::handle_accept ( const boost::asio::error e,
connection_ptr  conn 
)

Handle completion of a accept operation.

Definition at line 54 of file server.cpp.

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             boost::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             boost::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     }

void s11n_example::server::handle_write ( const boost::asio::error e,
connection_ptr  conn 
)

Handle completion of a write operation.

Definition at line 81 of file server.cpp.

00083   {
00084     // Nothing to do. The socket will be closed automatically when the last
00085     // reference to the connection object goes away.


Member Data Documentation

boost::asio::ip::tcp::acceptor s11n_example::server::acceptor_ [private]

The acceptor object used to accept incoming socket connections.

Definition at line 89 of file server.cpp.

std::vector<stock> s11n_example::server::stocks_ [private]

The data to be sent to each client.

Definition at line 92 of file server.cpp.


The documentation for this class was generated from the following file:
Copyright © 2003 - 2006 Christopher M. Kohlhoff

Home | Reference | Tutorial | Examples | Design