viayie / talkroom Goto Github PK
View Code? Open in Web Editor NEWLinux下基于epoll反应堆模型和线程池的即时聊天软件
Linux下基于epoll反应堆模型和线程池的即时聊天软件
in the epoll.h
`void initlistensocket(int efd, short port)
{
/创建监听套接字/
int lfd = Socket(AF_INET, SOCK_STREAM, 0);
/*修改lfd为非阻塞*/
int flag = 0;
if ((flag = fcntl(lfd, F_SETFL, O_NONBLOCK)) < 0) {
printf("%s: fcntl nonblocking failed, %s\n", __func__, strerror(errno));
return;
}
/*端口复用*/
int opt = 1;
setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
/*初始化lfd*/
//void eventset(struct myevent_s *ev, int fd, void (*call_back)(int, int, void *), void *arg);
//将lfd放置在将自定义结构体数组的最后
//acceptconn()是lfd的回调函数
//arg是回调函数acceptconn()的参数,指向lfd带的结构体中ptr指向的自定义结构体
eventset(&g_events[MAX_EVENTS], lfd, acceptconn, &g_events[MAX_EVENTS]);
/*将lfd挂在红黑树上,设定监听事件为读事件*/
eventadd(efd, EPOLLIN, &g_events[MAX_EVENTS]);
/*绑定*/
struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(port);
Bind(lfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
/*监听*/
Listen(lfd, 128);
return;
}`
eventset(&g_events[MAX_EVENTS], lfd, acceptconn, &g_events[MAX_EVENTS]);
eventadd(efd, EPOLLIN, &g_events[MAX_EVENTS]);
如果要初始化g_events[max_events + 1] 不应该有for循环吗?
另外acceptconn()函数怎么不像一个回调函数?你在eventset() 把他当作了回调函数, 但是你在acceptconn函数中还调用了eventset()函数,这时候eventset(),有是另一个所谓的回调? 你不觉得把 eventset()函数放在acceptconn()中更好吗?,然后eventset在调用dataprocess().
就像这样?
for(i= 0 ; i < max_events + 1; i++){ acceptcon(); }
void acceptcon(int sd, ...){ eventset(...)//初始化g_events[i] }
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.