0%

muduo

muduo 类图

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
127
128
129
130
131
132
133
134
135
136
137
138
@startuml


class Channel {
int fd_
EventLoop* loop
ReadEventCallback readCallback_;
EventCallback writeCallback_;
EventCallback closeCallback_;
EventCallback errorCallback_;

void handleEvent(Timestamp receiveTime)
void tie(const std::shared_ptr<void>&)
}
note left of Channel: channel负责管理fd感兴趣的事件

class EventLoop {
std::unique_ptr<Poller> poller_;
std::unique_ptr<TimerQueue> timerQueue_;
ChannelList activeChannels_;
Channel* currentActiveChannel_;

void loop();
void runInLoop(Functor cb);
void queueInLoop(Functor cb);
TimerId runAt(Timestamp time, TimerCallback cb);
TimerId runAfter(double delay, TimerCallback cb);
TimerId runEvery(double interval, TimerCallback cb);
void cancel(TimerId timerId);
}
note left of EventLoop: EventLoop提供单线程事件驱动机制,reactor

class EventLoopThread{
EventLoop* loop_
Thread thread_

EventLoop* startLoop()
}
note right of EventLoopThread: EventLoop的工作线程。

class EventLoopThreadPool {
EventLoop* baseLoop_;
std::vector<std::unique_ptr<EventLoopThread>> threads_;
std::vector<EventLoop*> loops_;

void start(const ThreadInitCallback& cb)
EventLoop* getNextLoop()
}
note left of EventLoopThreadPool: 线程池,每个线程有独立的eventLoop

interface Poller {
std::map<int, Channel*> channels_;
EventLoop* ownerLoop_;

Timestamp poll(int timeoutMs, std::vector<Channel*>* activeChannels) = 0;
}

class EpollPoller {
std::vector<struct epoll_event> events_
int epollfd_
}

class PollPoller {
vector<struct pollfd> pollfds_
}

class Acceptor {
EventLoop* loop_
Socket acceptSocket_
Channel acceptChannel_
NewConnectionCallback newConnectionCallback_

void listen()
}
note left of Acceptor: Acceptor负责监听端口 & 建立链接

class TcpConnection {
EventLoop* loop_;
std::unique_ptr<Socket> socket_
std::unique_ptr<Channel> channel_
size_t highWaterMark_;
Buffer inputBuffer_
Buffer outputBuffer_

void send(Buffer* message)
void shutdown()
void forceClose()
void forceCloseWithDelay(double seconds)
void startRead()
void stopRead()
void connectEstablished()
void connectDestroyed()
}

class TcpServer {
EventLoop* loop_
std::unique_ptr<Acceptor> acceptor_
std::shared_ptr<EventLoopThreadPool> threadPool_
std::map<string, TcpConnectionPtr> connections_

void start()
}

class TcpClient {
EventLoop* loop_
ConnectorPtr connector_
TcpConnectionPtr connection_

void connect()
void disconnect()
void stop()
}

class Connector {
EventLoop* loop_;
std::unique_ptr<Channel> channel_;

void start()
void restart()
void stop()
}

Poller <|.. EpollPoller
Poller <|.. PollPoller

EventLoop "1" o--> "n" Channel
EventLoop "1" *--> "1" Poller
EventLoopThread "1" *--> "1" EventLoop
EventLoopThreadPool "1" *--> "n" EventLoopThread
TcpConnection "1" *--> "1" Socket
TcpConnection "1" *--> "1" Channel
TcpServer "1" *--> "1" Acceptor
TcpServer "1" *--> "1" EventLoopThreadPool
TcpServer "1" *--> "n" TcpConnection
Connector "1" *--> "1" Channel
TcpClient "1" *--> "1" Connector
TcpClient "1" *--> "1" TcpConnection
@enduml