-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDispatcher.h
127 lines (100 loc) · 2.7 KB
/
Dispatcher.h
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* File: Dispatcher.h
* Author: try
*
* Created on 2011年5月27日, 上午9:14
*/
#ifndef _DISPATCHER_H
#define _DISPATCHER_H
#include "libev.h"
#include <vector>
#include <queue>
#include "Process.h"
#include "HttpProcess.h"
#include "resources.h"
#include "Config.h"
#include "ICleaner.h"
/**
* 用于管理子进程和分发HTTP请求
*/
class HttpServer;
class Dispatcher : public ICleaner{
friend class HttpProcess;
public:
Dispatcher();
virtual ~Dispatcher();
/**
* 启动子进程
*/
bool start();
/**
* 客户端连接成功,调用此方法向子进程派发连接FD
*
* @param fd 客户端连接FD
*
* @return true, 操作成功
*/
bool dispatch(FD fd);
/**
* 向子所有进程发送消息
*/
void sendMessage(MSG_TYPE type, void* msg, size_t len);
/**
* 通知所有子进程退出
*/
void notifyCloseProcesses();
/**
* 强行关闭所有子进程
*/
void killProcesses();
/**
* 在httpProcesses中返回所有子进程
*/
std::vector<HttpProcess*>& getHttpProcesses(std::vector<HttpProcess*>& httpProcesses);
/**
* 移除所有进程,主要用于在子进程移除从父进程中带过来的进程其它进程
*/
void removeProcesses();
/**
* 返回活动进程数量
*/
int processesCount();
/**
* @see Cleaner#cleanup()
*/
void cleanup();
public:
/** 指向HttpServer */
HttpServer* httpServer;
/** 指向监听Socket的ev::io */
ev::io** evioSocket;
/** 指向Config */
Config* config;
private:
//定义进程容器类型
typedef std::vector<HttpProcess*> HttpProcessVector;
typedef std::unordered_map<HttpProcess*, PointerHash, PointerCompare> HttpProcessSet;
//进程总数
int processCount;
//所有工作进程
// HttpProcessSet allProcesses;
HttpProcessVector allProcesses;
// //用于空闲和忙进程切换的容器
// HttpProcessVector firstProcesses;
// HttpProcessVector secondProcesses;
//
//将要被销毁的进程
HttpProcessVector destroyedProcesses;
//
// //指向比较空闲的工作进程
// HttpProcessVector* idleProcesses;
// //指向比较忙的工作进程
// HttpProcessVector* busyProcesses;
private:
//返回一个比较空闲的进程, 并同时从空闲进程池中移除,并添加到比较忙的进程池中
HttpProcess* getIdleProcess();
//创建新进程, 返回实际成功创建数量
int createProcess(int n);
void addToDestroyedProcesses(HttpProcess* p);
};
#endif /* _DISPATCHER_H */