- 论坛徽章:
- 0
|
回复 2# hellioncu
void * CheckAndProcessOnSockets(void *arg)
{
INIT_CONFIG * pInitConfig = (INIT_CONFIG*)arg;
struct sockaddr_in stSockAddr;
int clientSocket;
socklen_t len = sizeof(stSockAddr);
while (1)
{
printf("Accept ready...\n");
clientSocket = accept(pInitConfig->nListenSocket,(struct sockaddr *)&stSockAddr,&len);
printf("Accept client:%d\n",clientSocket);
if (clientSocket > 0)
{
if (clientSocket > MAX_SOCKET_NUM)
{
nDebugLog(LOG_PRIORITY_ERROR, "Too Many Sockets, should start another process!");
close(clientSocket);
continue;
}
if (SetNonblock(clientSocket) < 0)
{
close(clientSocket);
continue;
}
int nFlag = 1;
setsockopt(clientSocket, SOL_SOCKET, SO_KEEPALIVE, (void *)&nFlag, sizeof(nFlag));
struct linger l = {1,2};
setsockopt(clientSocket, SOL_SOCKET, SO_LINGER, (void *) &l, sizeof(l));
setsockopt(clientSocket, IPPROTO_TCP, TCP_NODELAY, &nFlag, sizeof(nFlag));
m_sSocketContext[clientSocket].nSocketType = CONN_SOCKET;
m_sSocketContext[clientSocket].dwTimeLast = time(0);
memset(m_sSocketContext[clientSocket].sIP,0,sizeof(m_sSocketContext[clientSocket].sIP));
char* sTemp = inet_ntoa(stSockAddr.sin_addr);
memcpy(m_sSocketContext[clientSocket].sIP,sTemp,strlen(sTemp));
m_sSocketContext[clientSocket].nPort = ntohs(stSockAddr.sin_port);
m_sSocketContext[clientSocket].ev.data.fd = clientSocket;
m_sSocketContext[clientSocket].ev.events = EPOLLIN | EPOLLET;
epoll_ctl(m_pIOWorkList[clientSocket%pInitConfig->nIOThreadNum].epoll_fd, EPOLL_CTL_ADD, clientSocket, &(m_sSocketContext[clientSocket].ev));
}
}
}
void AcceptThreadInit()
{
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tid, &attr, CheckAndProcessOnSockets, &m_cInitConfig);
} |
|