/**
 * Process request; worker (thread)
 *
 * @param queue
 */
void process_request(work_queue& queue) {
  while (running) {
    request_data::pointer request(queue.get());
    if (request) {

      // some heavy work!
      std::this_thread::sleep_for(std::chrono::seconds(10));

      request->conn->set_status(server::connection::ok);
      request->conn->write("Hello, world!");
    }

    std::this_thread::sleep_for(std::chrono::microseconds(1000));
  }
}
示例#2
0
/**
 * Process request; worker (thread)
 *
 * @param queue
 */
void process_request(work_queue& queue)
{
  Worker worker;
  while(!boost::this_thread::interruption_requested()) {
    request_data::pointer p_req(queue.get());
    if (p_req) {

      // some heavy work!
      long long result = worker.work();
      std::ostringstream body;
      body << "Current length: " << result << std::endl;

      p_req->conn->set_status(server::connection::ok);
      server::response_header headers[] = { {"Content-Type", "text/plain"}, {"Content-Length", std::to_string(body.tellp())} };
      p_req->conn->set_headers(boost::make_iterator_range(headers, headers+2));
      p_req->conn->write(body.str());
    }

    boost::this_thread::sleep(boost::posix_time::microseconds(1000));
  }
}
void process_request(work_queue& queue)
{
    using namespace std;

    while(!boost::this_thread::interruption_requested()) {
        request_data::pointer p_req(queue.get());
        if (p_req) {
            cout << "Received client p_req from " << p_req->req.source << endl; // source 已经包含port
            cout << "destination = " << p_req->req.destination << endl;  // 去除了URL port
            cout << "method = " << p_req->req.method << endl;
            cout << "http version = " << (uint32_t)(p_req->req.http_version_major) 
                 << "." << (uint32_t)(p_req->req.http_version_minor) << endl;
            cout << "headers:" << endl;
            for (const auto &header : p_req->req.headers)
                cout << header.name << " = " << header.value << endl;

            size_t nRead;
            for (const auto &v : p_req->req.headers) {
                if (boost::iequals(v.name, "Content-Length")) {
                    nRead = boost::lexical_cast<size_t>(v.value);
                } // if
            } // for

            WorkItemPtr pWork( new WorkItem(p_req->req.source, p_req->req.destination, nRead) );
            pWork->readBody(p_req->conn);

            // some heavy work!
            boost::this_thread::sleep(boost::posix_time::seconds(3));

            p_req->conn->set_status(server::connection::ok);
            p_req->conn->write("Hello, world!");
        }

        boost::this_thread::sleep(boost::posix_time::microseconds(1000));
    }
}