|
@@ -0,0 +1,90 @@
|
|
|
+#include "CEpollServer.h"
|
|
|
+
|
|
|
+CEpollServer::CEpollServer(unsigned short port)
|
|
|
+{
|
|
|
+ //初始化 TcpServer类
|
|
|
+ this->tcp = new CTcpServer(port);
|
|
|
+ this->tcp->Start(true);
|
|
|
+
|
|
|
+ //新创客户对像
|
|
|
+ this->conn=new CConnection();
|
|
|
+
|
|
|
+ //初始化数据成员
|
|
|
+ this->epollfd = 0;
|
|
|
+ this->epollwaitefd = 0;
|
|
|
+ this->acceptFd = 0;
|
|
|
+ bzero(this->buf, sizeof(this, buf));
|
|
|
+
|
|
|
+ //事件结构体初始化
|
|
|
+ bzero(&(this->epollEvent), sizeof(this->epollEvent));
|
|
|
+ //绑定当前准备好的sockedfd(可用网络对象)
|
|
|
+ this->epollEvent.data.fd = this->tcp->getSocketFd();
|
|
|
+ //绑定事件为客户端接入事件
|
|
|
+ this->epollEvent.events = EPOLLIN;
|
|
|
+ //创建epoll
|
|
|
+ this->epollfd = epoll_create(EPOLL_SIZE);
|
|
|
+ //将已经准备好的网络描述符添加到epoll事件队列中
|
|
|
+ epoll_ctl(this->epollfd, EPOLL_CTL_ADD, this->tcp->getSocketFd(), &(this->epollEvent));
|
|
|
+
|
|
|
+ this->conn->setEpollFd(this->epollfd);
|
|
|
+}
|
|
|
+
|
|
|
+CEpollServer::~CEpollServer()
|
|
|
+{
|
|
|
+}
|
|
|
+
|
|
|
+void CEpollServer::Start()
|
|
|
+{
|
|
|
+ CONN_DEF *connParam=NULL;
|
|
|
+ wlog_info("epoll server start run");
|
|
|
+ while (1)
|
|
|
+ {
|
|
|
+ this->epollwaitefd = epoll_wait(this->epollfd, epollEventArray, EPOLL_SIZE, -1);
|
|
|
+ if (this->epollwaitefd < 0)
|
|
|
+ {
|
|
|
+ if(errno==EINTR) continue;
|
|
|
+ wlog_error("epoll wait error:%s, exit system", strerror(errno));
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ for (int i = 0; i < this->epollwaitefd; i++)
|
|
|
+ {
|
|
|
+ //判断是否有客户端上线
|
|
|
+ if (epollEventArray[i].data.fd == this->tcp->getSocketFd())
|
|
|
+ {
|
|
|
+ this->acceptFd = accept(this->tcp->getSocketFd(), NULL, NULL);
|
|
|
+ wlog_warn("client<%d>connected",this->acceptFd);
|
|
|
+
|
|
|
+ //上线的客户端描述符是acceptfd 绑定事件添加到epoll
|
|
|
+ epollEvent.data.fd = this->acceptFd;
|
|
|
+ epollEvent.events = EPOLLIN; //EPOLLIN表示对应的文件描述符可以读
|
|
|
+ epoll_ctl(this->epollfd, EPOLL_CTL_ADD, this->acceptFd, &epollEvent);
|
|
|
+
|
|
|
+ this->conn->push_item(this->acceptFd);
|
|
|
+ }
|
|
|
+ else if (epollEventArray[i].events & EPOLLIN)
|
|
|
+ {
|
|
|
+ connParam=this->conn->findConnBySock(epollEventArray[i].data.fd);
|
|
|
+ bzero(this->buf, sizeof(this->buf));
|
|
|
+ int res = read(epollEventArray[i].data.fd, this->buf, sizeof(this->buf));
|
|
|
+ if(NULL==connParam){//正常来说不会出现这个
|
|
|
+ wlog_error("sock %d not found in db, close it!!!", epollEventArray[i].data.fd);
|
|
|
+ CConnection::disconnect(connParam,this->epollfd);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (res > 0) CConnection::TSendData(connParam->tcp_fd, this->buf,res);
|
|
|
+ else if (res <= 0){
|
|
|
+ wlog_warn("client<%d>disconnect", epollEventArray[i].data.fd);
|
|
|
+ CConnection::disconnect(connParam,this->epollfd);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ wlog_warn("CEpollServer exist!!!");
|
|
|
+}
|
|
|
+
|
|
|
+void CEpollServer::shut_sock(int fd){
|
|
|
+ struct epoll_event epollEvent;
|
|
|
+
|
|
|
+ epollEvent.events=EPOLLRDHUP;
|
|
|
+ epoll_ctl(this->epollfd, EPOLL_CTL_ADD, fd, &epollEvent);
|
|
|
+}
|