-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebserver_muduo.h
More file actions
76 lines (60 loc) · 1.8 KB
/
webserver_muduo.h
File metadata and controls
76 lines (60 loc) · 1.8 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
76
#ifndef WEBSERVER_MUDUO_H
#define WEBSERVER_MUDUO_H
#include "./CGImysql/sql_connection_pool.h"
#include "./http/HttpServer.h"
#include "./muduo/include/EventLoop.h"
#include "./muduo/include/InetAddress.h"
#include "muduo/include/noncopyable.h"
#include <atomic>
#include <condition_variable>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
// Muduo 风格的 WebServer
class WebServerMuduo:noncopyable {
public:
WebServerMuduo(EventLoop *loop, const InetAddress &listenAddr,
const std::string &name);
~WebServerMuduo();
// 初始化配置
void init(std::string user, std::string passWord, std::string databaseName,
int log_write, int close_log, int sql_num, int thread_num);
// 设置线程数
void setThreadNum(int numThreads);
// 启动服务器
void start();
private:
// HTTP 请求处理回调
void onRequest(const HttpContext &req, HttpResponse *resp);
// 业务逻辑处理
void handleStaticFile(const HttpContext &req, HttpResponse *resp);
void handleCGI(const HttpContext &req, HttpResponse *resp);
// 初始化数据库连接池
void initSqlPool();
// 加载用户数据
void loadUsers();
EventLoop *loop_;
std::unique_ptr<HttpServer> server_;
// 数据库相关
connection_pool *connPool_;
std::string sqlUser_;
std::string sqlPasswd_;
std::string sqlName_;
int sqlNum_;
// 日志配置
int logWrite_;
int closeLog_;
// 线程数
int threadNum_;
// 文档根目录
std::string docRoot_;
// 用户数据缓存
std::map<std::string, std::string> users_;
std::mutex usersMutex_;
std::atomic<bool> usersLoaded_{false};
std::condition_variable usersLoadedCV_;
std::thread loadUsersThread_;
};
#endif