-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpServer.h
More file actions
75 lines (57 loc) · 1.53 KB
/
Copy pathHttpServer.h
File metadata and controls
75 lines (57 loc) · 1.53 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "ThreadPool.h"
#include "HttpRequest.h"
#include <string>
#include <map>
#include <sys/epoll.h>
#include <memory>
//连接信息
struct Connection
{
int fd;
HttpRequest request;
};
class HttpServer
{
public:
HttpServer(unsigned short port, const std::string& baseDir);
~HttpServer();
void run();
void stop();
//设置线程池大小
void setThreadPoolSize(int minThreads, int maxThreads);
//添加状态查询接口
ThreadPool<Connection>::PoolStatus getThreadPoolStatus()
{
return threadPool_.getPoolStatus();
}
//添加监控输出函数
void printThreadPoolStatus();
private:
//初始化监听socket
bool initListenSocket();
//处理epoll事件
void handEpollEvents();
//接收新连接
void acceptNewConnection();
//处理HTTP请求(在线程池中执行)
void processRequest(Connection* conn);
//发送响应
void sendResponse(int cfd, int status, const std::string& content);
//文件操作
std::string getFileType(const std::string& fileName);
void sendDir(const std::string& difName, const std::string& urlPath, int cfd);
void sendFile(const std::string& fileName, int cfd);
void sendHeadMsg(int cfd, int status, const std::string& descr, const std::string& type, int len);
void sendErrorResponse(int cfd, int status, const std::string& description);
//线程池任务完成回调
void onTaskComplete(std::shared_ptr<Connection> conn);
int listenFd_;
int epollFd_;
unsigned short port_;
std::string baseDir_;
bool running_;
//线程池
ThreadPool<Connection> threadPool_;//T=Connection
//连接管理
std::map<int, std::shared_ptr<Connection>> connections_;
};