leechanx / easy-reactor Goto Github PK
View Code? Open in Web Editor NEWEasy-Reactor是一个Linux C++高性能TCP服务框架,基于Reactor模式,支持单线程、多线程Reactor,也支持UDP服务
License: MIT License
Easy-Reactor是一个Linux C++高性能TCP服务框架,基于Reactor模式,支持单线程、多线程Reactor,也支持UDP服务
License: MIT License
边缘模式的写事件(神坑!)
tcp_server:
侦听的socket有新连接到达时,从已经运行的连接线程中里获取了一个线程,把该连接事件添加到线程的处理消息队列, 不知道我理解错了吗
//multi-thread reactor model: round-robin a event loop and give message to it
if (_thd_pool)
{
thread_queue<queue_msg>* cq = _thd_pool->get_next_thread();
queue_msg msg;
msg.cmd_type = queue_msg::NEW_CONN;
msg.connfd = connfd;
cq->send_msg(msg);
}
这个地方感觉不用队列吧,直接调用epoll_ctrl 把connfd 添加到线程的epoll监听列表中不是更好吗? 本身epoll_wait 和epoll_ctrl是线程安全的,https://www.zhihu.com/question/49741301
1.do_accept 里 connfd =-1 就应该退出循环了吧。 看代码中EINTR和EMFILE 这2个错误为什么不用退出啊?
`
void tcp_server::do_accept()
{
int connfd;
bool conn_full = false;
while (true)
{
connfd = ::accept(_sockfd, (struct sockaddr*)&_connaddr, &_addrlen);
if (connfd == -1)
{
if (errno == EINTR)
{
continue;
}
else if (errno == EMFILE)
{
conn_full = true;
::close(_reservfd);
}
else if (errno == EAGAIN)
{
break;
}
else
{
exit_log("accept()");
}
}
}`
event_loop 添加io event,fd找不到的时候 _io_evs[fd]这么调用不会报错?
`
void event_loop::add_ioev(int fd, io_callback* proc, int mask, void* args)
{
int f_mask = 0;//finial mask
int op;
ioev_it it = _io_evs.find(fd);
if (it == _io_evs.end())
{
f_mask = mask;
op = EPOLL_CTL_ADD;
}
else
{
f_mask = it->second.mask | mask;
op = EPOLL_CTL_MOD;
}
if (mask & EPOLLIN)
{
_io_evs[fd].read_cb = proc;
_io_evs[fd].rcb_args = args;
}
else if (mask & EPOLLOUT)
{
_io_evs[fd].write_cb = proc;
_io_evs[fd].wcb_args = args;
}
_io_evs[fd].mask = f_mask;
struct epoll_event event;
event.events = f_mask;
event.data.fd = fd;
int ret = ::epoll_ctl(_epfd, op, fd, &event);
error_if(ret == -1, "epoll_ctl");
listening.insert(fd);//加入到监听集合中
}
`
当有定时器超时时,难道不需要read(timerfd)吗?代码中是直接调用了定时器绑定的callback,这样不会导致定时器可读事件一直发生吗
epoll 使用都是用边缘触发吧,
因为水平触发模式,可写的时候,会一直返回EAGAIN吧
当socket可写时,会不停的触发socket可写的事件,LT模式下不需要读写的文件描述符仍会不停地返回就绪,这样就会影响我们监测需要关心的文件描述符的效率。
如果用了水平模式,写完应该把文件描述符移除epoll 监听吧
看你说 ET模式有坑, 链接打不开,不知道是啥原因?
你好我想学习这个项目,但是阅读上有一定困难,请问有更详细的项目讲解吗
无法make成功,请问这是什么原因,请分析。
这是make的错误提示:
$ make
g++ -g -O2 -Wall -fPIC -Wno-deprecated -c -o src/tcp_client.o src/tcp_client.cc -Iinclude
g++ -g -O2 -Wall -fPIC -Wno-deprecated -c -o src/udp_server.o src/udp_server.cc -Iinclude
g++ -g -O2 -Wall -fPIC -Wno-deprecated -c -o src/io_buffer.o src/io_buffer.cc -Iinclude
g++ -g -O2 -Wall -fPIC -Wno-deprecated -c -o src/event_loop.o src/event_loop.cc -Iinclude
g++ -g -O2 -Wall -fPIC -Wno-deprecated -c -o src/udp_client.o src/udp_client.cc -Iinclude
g++ -g -O2 -Wall -fPIC -Wno-deprecated -c -o src/config_reader.o src/config_reader.cc -Iinclude
g++ -g -O2 -Wall -fPIC -Wno-deprecated -c -o src/tcp_conn.o src/tcp_conn.cc -Iinclude
In file included from include/thread_pool.h:5:0,
from include/tcp_server.h:5,
from src/tcp_conn.cc:3:
include/thread_queue.h: In destructor ‘thread_queue<T>::~thread_queue()’:
include/thread_queue.h:30:9: error: ‘::close’ has not been declared
::close(_evfd);
^
include/thread_queue.h: In member function ‘void thread_queue<T>::send_msg(const T&)’:
include/thread_queue.h:38:19: error: ‘::write’ has not been declared
int ret = ::write(_evfd, &number, sizeof(unsigned long long));
^
include/thread_queue.h: In member function ‘void thread_queue<T>::recv_msg(std::queue<T>&)’:
include/thread_queue.h:47:19: error: ‘::read’ has not been declared
int ret = ::read(_evfd, &number, sizeof(unsigned long long));
^
makefile:17: recipe for target 'src/tcp_conn.o' failed
make: *** [src/tcp_conn.o] Error 1
gcc的版本
Thread model: posix
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.10)
多线程启动定时器 线程不安全吧
没有左右子节点?感觉像是个冒泡排序啊, 为啥没有比较左右子节点,但是你取父节点又使用了 完全二叉树的写法 int prt_pos = (curr_pos - 1) / 2; 很奇怪
`void timer_queue::heap_add(timer_event& te)
{
_event_lst.push_back(te);
//update position
_position[te.timer_id] = _count;
int curr_pos = _count;
_count++;
int prt_pos = (curr_pos - 1) / 2;
while (prt_pos >= 0 && _event_lst[curr_pos].ts < _event_lst[prt_pos].ts)
{
timer_event tmp = _event_lst[curr_pos];
_event_lst[curr_pos] = _event_lst[prt_pos];
_event_lst[prt_pos] = tmp;
//update position
_position[_event_lst[curr_pos].timer_id] = curr_pos;
_position[tmp.timer_id] = prt_pos;
curr_pos = prt_pos;
prt_pos = (curr_pos - 1) / 2;
}
}
`
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.