forked from changkun/modern-cpp-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.http.hpp
39 lines (35 loc) · 1.27 KB
/
server.http.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//
// server_http.hpp
// web_server
// created by changkun at changkun.de
// https://github.com/changkun/modern-cpp-tutorial/
//
#ifndef SERVER_HTTP_HPP
#define SERVER_HTTP_HPP
#include "server.base.hpp"
namespace Web {
typedef boost::asio::ip::tcp::socket HTTP;
template<>
class Server<HTTP> : public ServerBase<HTTP> {
public:
// use port, thread number to construct web server
// http server is much simple than https since it doesn't need to initial config file
Server(unsigned short port, size_t num_threads=1) :
ServerBase<HTTP>::ServerBase(port, num_threads) {};
private:
// implement accept() method
void accept() {
// create a new socket for current connection
// shared_ptr is used for passing temporal object to anonymous function
// socket will be deduce as type of std::shared_ptr<HTTP>
auto socket = std::make_shared<HTTP>(m_io_service);
acceptor.async_accept(*socket, [this, socket](const boost::system::error_code& ec) {
// establish a connection
accept();
// if no error
if(!ec) process_request_and_respond(socket);
});
}
};
}
#endif /* SERVER_HTTP_HPP */