forked from youcaiguai/NetServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventLoop.cpp
More file actions
78 lines (71 loc) · 1.66 KB
/
Copy pathEventLoop.cpp
File metadata and controls
78 lines (71 loc) · 1.66 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
77
78
// Copyright 2019, Chen Shuaihao.
//
//Author: Chen Shuaihao
//
//
#include "EventLoop.h"
#include <iostream>
#include <sys/eventfd.h>
#include <unistd.h>
#include <stdlib.h>
//参照muduo,实现跨线程唤醒
int CreateEventFd()
{
int evtfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
if (evtfd < 0)
{
std::cout << "Failed in eventfd" << std::endl;
exit(1);
}
return evtfd;
}
EventLoop::EventLoop(/* args */)
: functorlist_(),
channellist_(),
activechannellist_(),
poller_(),
quit_(true),
tid_(std::this_thread::get_id()),
mutex_(),
wakeupfd_(CreateEventFd()),
wakeupchannel_()
{
wakeupchannel_.SetFd(wakeupfd_);
wakeupchannel_.SetEvents(EPOLLIN | EPOLLET);
wakeupchannel_.SetReadHandle(std::bind(&EventLoop::HandleRead, this));
wakeupchannel_.SetErrorHandle(std::bind(&EventLoop::HandleError, this));
AddChannelToPoller(&wakeupchannel_);
}
EventLoop::~EventLoop()
{
close(wakeupfd_);
}
void EventLoop::WakeUp()
{
uint64_t one = 1;
ssize_t n = write(wakeupfd_, (char*)(&one), sizeof one);
}
void EventLoop::HandleRead()
{
uint64_t one = 1;
ssize_t n = read(wakeupfd_, &one, sizeof one);
}
void EventLoop::HandleError()
{
;
}
void EventLoop::loop()
{
quit_ = false;
while(!quit_)
{
poller_.poll(activechannellist_);
//std::cout << "server HandleEvent" << std::endl;
for(Channel *pchannel : activechannellist_)
{
pchannel->HandleEvent();//处理事件
}
activechannellist_.clear();
ExecuteTask();
}
}