- 论坛徽章:
- 0
|
各位大家看一下我这个服务器线程有什么问题没有,谢谢了!
void *thread_user_server(GMutex *mutex)
{
int ret;
struct user_conn_context_t *context=&auth_datas->user_conn_context;
context->user_socket=socket(AF_INET,SOCK_STREAM,0);
if(context->user_socket==-1)
{
/*skip*/
}
/* set socket reuse and keep alive option */
int option_value = 1;
setsockopt(context->user_socket,
SOL_SOCKET,
SO_REUSEADDR, &option_value, sizeof(option_value));
setsockopt(context->user_socket,
SOL_SOCKET,
SO_KEEPALIVE, &option_value, sizeof(option_value));
struct sockaddr_in usr_addr;
usr_addr.sin_family=AF_INET;
usr_addr.sin_port=htons(USER_SERVER_PORT);
usr_addr.sin_addr.s_addr=inet_addr(AUTHSRV_IP);
bzero(&(usr_addr.sin_zero),8);
ret=bind(context->user_socket,(struct sockaddr*)&usr_addr,sizeof(struct sockaddr));
if (ret) {
/*skip*/
}
printf("[+]user server bind on port %d ...\n",USER_SERVER_PORT);
/* Listen ! */
int socket_fd;
socket_fd = listen(context->user_socket,5);
if (socket_fd == -1) {
/*skip*/
}
int client_socket[context->max_user_num];
FD_ZERO(&context->read_set);
FD_SET(context->user_socket,&context->read_set);
context->mx=context->user_socket+1;
struct timeval tv;
fd_set wk_set;
int i,z,n,index=0;
g_message("[+]wait for client connections!");
while(g_mutex_trylock(mutex))
{
g_mutex_unlock(mutex);
/*copy read set to working set*/
FD_ZERO(&wk_set);
g_mutex_lock(context->wr_mutex);
for(z=0;z<context->mx;z++)
if(FD_ISSET(z,&context->read_set))
FD_SET(z,&wk_set);
g_mutex_unlock(context->wr_mutex);
/*wait read events during 1 second
* read events:
* 1.context->user_socket can read
* 2.client_socket can read*/
tv.tv_sec=0;
tv.tv_usec=250000;
n=select(context->mx,&wk_set,NULL,NULL,&tv);
if(n==-1)
{
/*signal SIGNIPE was captured,ingore it*/
if(errno==EINTR)
{
g_message("broken pipe!\n");
continue;
}
if(errno==EBADF)
{
g_message("bad file!\n");
int i;
/*a clinet disconnect between FD_SET and select
* try to find it*/
for(i=0;i<context->mx;i++)
{
if(FD_ISSET(i,&context->read_set))
{
struct stat s;
if(fstat(i,&s)<0)
{
g_message("%d is a bad socket",i);
g_mutex_lock(context->wr_mutex);
FD_CLR(i,&context->read_set);
g_mutex_unlock(context->wr_mutex);
}
}
}
continue;
}
}else if(!n)
{
/*time up*/
continue;
}
/*check if a socket can accept
* that means new user connection*/
if(FD_ISSET(context->user_socket,&wk_set))
{
if(context->curr_user_num>context->max_user_num)
{
g_message("max client connection reached,deny new connections!");
continue;
}
struct sockaddr user_sock_addr;
int len=sizeof(struct sockaddr);
/*add new client fd to client_socket array*/
client_socket[context->curr_user_num]=accept(context->user_socket,&user_sock_addr,&len);
/*ckeck fd sanity*/
if(client_socket[context->curr_user_num]<0)
{
g_message("bad connection accepted");
continue;
}
g_message("%d client online",context->curr_user_num+1);
/*set keepalive option*/
setsockopt(context->user_socket,
SOL_SOCKET,
SO_KEEPALIVE,
&option_value,
sizeof(option_value)
);
/*update mx if needed*/
if(client_socket[context->curr_user_num]>context->mx)
context->mx=client_socket[context->curr_user_num]+1;
/*add new client fd to read set watch range*/
g_mutex_lock(context->wr_mutex);
FD_SET(client_socket[index],&context->read_set);
g_mutex_unlock(context->wr_mutex);
g_thread_pool_push(auth_datas->user_data_accept_pool,
&client_socket[index],
NULL);
context->curr_user_num++;
continue;
}
/*check if a socket can recv*/
for(i=0;i<context->mx;i++)
{
/*skip user_socket*/
if(i==context->user_socket)
continue;
if(FD_ISSET(i,&wk_set))
g_thread_pool_push(auth_datas->user_data_accept_pool,
&client_socket[i],
NULL);
}
}
close(context->user_socket);
g_message("uesr server thread exit ok\n");
return NULL;
}
|
[ 本帖最后由 gordenisgk 于 2009-5-9 20:25 编辑 ] |
|