- 论坛徽章:
- 0
|
代码
signal(SIGCHLD, SIG_IGN);
int enums,j,Newfd,listen_index,epollfd,listenq,epollsize,n;
struct sockaddr_in TheirAddr;
socklen_t len;
epollsize = (LGWTCPConf.GetConf()).MaxPollSize;
listenq = (LGWTCPConf.GetConf()).MaxListenQ;
ServerInit();
struct epoll_event ev;
struct epoll_event events[1000];
epollfd = epoll_create(epollsize);
LOG4CPLUS_INFO(log.GetLogInstance(),"epoll create "<< epollfd);
len = sizeof(struct sockaddr_in);
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = LGWTCPSock;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, LGWTCPSock, &ev) < 0) {
LOG4CPLUS_INFO(log.GetLogInstance(),"epoll set insertion error\n");
return -1;
}
LOG4CPLUS_INFO(log.GetLogInstance(),"start main loop \n");
while(1)
{
enums = epoll_wait(epollfd, events, listenq, 20);
if (enums == -1)
{
printf("errno=%d,%s\r\n",errno,strerror(errno));
if(errno==EINTR)
{
continue;
}
perror("epoll_wait");
}
else if(enums==0)
{
continue;
}
for (n = 0; n < enums; ++n) {
listen_index=events[n].data.u32;
if (events[n].data.fd == LGWTCPSock) {
do {
Newfd = accept(LGWTCPSock, (struct sockaddr *) &TheirAddr,&len);
if (Newfd < 0) {
break;
}
printf("Connect from %s:%d Newfd %d\n", inet_ntoa(TheirAddr.sin_addr),htons(TheirAddr.sin_port),Newfd);
setnonblocking(Newfd);
int keepCount = 3;
int keepInterval = 5;
int keepIdle = 600;
int keepAlive = 1;
setsockopt(Newfd, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepAlive, sizeof(keepAlive));
setsockopt(Newfd, SOL_TCP, TCP_KEEPIDLE, (void*)&keepIdle, sizeof(keepIdle));
setsockopt(Newfd, SOL_TCP, TCP_KEEPINTVL, (void *)&keepInterval, sizeof(keepInterval));
setsockopt(Newfd, SOL_TCP, TCP_KEEPCNT, (void *)&keepCount, sizeof(keepCount));
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = Newfd;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, Newfd, &ev) < 0) {
fprintf(stderr, " socket '%d' add epoll error! %s\n",Newfd, strerror(errno));
}
}while(Newfd>0);
}
else if(events[n].events & EPOLLIN)
{
for(j=0;j<MAXTHREAD;j++)
{
if(0==s_thread_para[j][0]) break;
}
if(j>MAXTHREAD)
{
LOG4CPLUS_INFO(log.GetLogInstance(),"thread pool is full!\n");
close(Newfd);
continue;
}
pthread_mutex_lock(&mutex);
s_thread_para[j][0]=1;
s_thread_para[j][1]=events[n].data.fd;
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(s_mutex+j);
}
}
}
这段有什么问题么 |
|