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

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 23 of file server.cpp.

Public Member Functions

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

Private Attributes

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 ( asio::io_service io_service,
unsigned short  port 
)

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

Definition at line 27 of file server.cpp.

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));


Member Function Documentation

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

Handle completion of a accept operation.

Definition at line 64 of file server.cpp.

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     }

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

Handle completion of a write operation.

Definition at line 91 of file server.cpp.

00093   {
00094     // Nothing to do. The socket will be closed automatically when the last
00095     // reference to the connection object goes away.


Member Data Documentation

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

The acceptor object used to accept incoming socket connections.

Definition at line 99 of file server.cpp.

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

The data to be sent to each client.

Definition at line 102 of file server.cpp.


The documentation for this class was generated from the following file:
asio 0.3.8rc3 Home | Reference | Tutorial | Examples | Design