-
Notifications
You must be signed in to change notification settings - Fork 10
/
ServerHttp.hpp
50 lines (40 loc) · 1.66 KB
/
ServerHttp.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
40
41
42
43
44
45
46
47
48
49
#pragma once
#include "ServerBase.hpp"
#include <functional>
namespace WebServer {
// boost::asio::ip::tcp::socket对象,实际上就是一个 HTTP 的 Socket 连接
typedef boost::asio::ip::tcp::socket HTTP;
template<typename socket_type = HTTP>
class ServerHttp : public ServerBase<socket_type>
{
public:
ServerHttp(unsigned short port, size_t num_threads);
virtual ~ServerHttp();
private:
virtual void accept();
};
template<typename socket_type>
WebServer::ServerHttp<socket_type>::ServerHttp(unsigned short port, size_t num_threads) :
ServerBase<socket_type>::ServerBase(port, num_threads)
{
}
template<typename socket_type>
WebServer::ServerHttp<socket_type>::~ServerHttp()
{
}
template<typename socket_type>
void WebServer::ServerHttp<socket_type>::accept() {
// 为当前连接创建一个新的 socket
// Shared_ptr 用于传递临时对象给匿名函数
// socket 会被推导为 std::shared_ptr<HTTP> 类型
auto socket = std::make_shared<HTTP>(ServerBase<socket_type>::m_io_service);
ServerBase<socket_type>::acceptor.async_accept(*socket, [this, socket](const boost::system::error_code& ec) {
// 立即启动并接受一个连接;
accept();
Logger::LogNotification("HTTP Connection established with remote address " + socketToIP(socket));
//std::cout << "HTTP Connection established with remote address " + socketToIP(socket) << std::endl;
// 如果出现错误
if(!ec) ServerBase<socket_type>::process_request_and_response(socket);
});
}
}