/**断开一个连接,移除epoll监听,通知lua*/
void remove_fd(int fd, lua_State *L)
{
int ret, idx;
lua_State *Lx = lua_newthread(L);
lua_getglobal(Lx, F_ONCLOSE); // 调用lua里的onclose函数
lua_pushinteger(Lx, fd);
ret = lua_pcall(Lx, 1, LUA_MULTRET, 0);
if(ret != 0)
fprintf(stderr, "%s\n", lua_tostring(Lx, -1));
lua_settop(L, 0);
//从fds和clients中删除,释放内存
idx = fds[fd];
fds[fd] = -1;
free(clients[idx]);
clients[idx] = NULL;
//移除epoll监听,关闭连接
struct epoll_event e;
epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, &e);
close(fd);
}
/**接受连接请求,多个*/
void accept_fd(int listenfd)
{
int fd;
struct sockaddr_in addr;
socklen_t len = sizeof(addr);
while((fd = accept(listenfd, (struct sockaddr *)&addr, &len)) > 0)
add_fd(fd, TRUE);
}