Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Acceptors

Accepting Requests
Example 1
Example 2

TODO: Document using Acceptors

A basic_request_acceptor<> is responsible for accepting requests. The following typedefs are provided for typical use:

boost::cgi::acceptor // CGI
boost::fcgi::acceptor // FastCGI

Accepting requests is considered distinct to the loading stage for several reasons, the most significant of which is to allow for multiplexing protocols like FastCGI work with the other protocols.

A basic_request_acceptor<> takes an instance of a ProtocolService in its constructor. The following member functions are available, where Request is any class that models the Request concept:

Function signature

Purpose

void accept(Request& empty_request)

Takes an empty request and blocks until a request is waiting to be handled. This will throw if the accept fails.

boost::system::error_code& accept(Request& empty_request, boost::system::error_code& ec)

Takes an empty request and blocks until a request is waiting to be handled. ec will be set such that !ec == false if the accept fails with an error code ec.

void accept(RequestHandler handler)

Accepts a new request and calls handler with the new request. A RequestHandler takes a request by reference and should match the signature function<int (Request& req)>.

void async_accept(Request& empty_request, Handler handler)

Takes an empty request and asynchronously accepts a request. handler must be a model of _Handler_ and will be invoked when a request is accepted or an error occurs. The function always returns immediately.

void cancel()

Cancel all outstanding asynchronous accepts that are running

A simple example that runs a handler function for each request that is accepted. The requests are handled one at a time.

#include <boost/cgi/fcgi.hpp>

namespace fcgi = boost::fcgi;

int handler(fcgi::request& req)
{
  // use the request.
}

int main()
{
  // create a ProtocolService
  fcgi::service service;

  // create an acceptor
  fcgi::acceptor acceptor(service);

  int status(0)
  while(!status) {
    // accept a request
    status = acceptor.accept(&handler);
  }

  return status;
}

Another example that allows you to construct your own request object and then accept a new request into the object. Useful if you need to store a list of requests in your application.

#include <boost/cgi/fcgi.hpp>

int main()
{
  using namespace cgi::fcgi; // for example

  // create a ProtocolService
  service service;

  // create an acceptor
  acceptor acceptor(service);

  // make an empty request
  request req(service);

  boost::system::error_code ec;
  // accept a request
  acceptor.accept(req, ec);

  if (!ec)
  {
    // a request was accepted ok
  }

  return 0;
}


PrevUpHomeNext