Esempio n. 1
0
//recv from pipe
void onMasterPipeReadable( aeEventLoop *el, int fd, void *privdata, int mask )
{ 
    int readlen =0;
    aePipeData data;

    //是否要加锁,多个线程同时对一个管道读数据
    readlen = read( fd, &data , PIPE_DATA_HEADER_LENG ); // 
	
    if( readlen == 0 )
    {
        close( fd );
    }
    else if( readlen == PIPE_DATA_HEADER_LENG )
    {
        //message,close,这样做是防止粘包,或半包
        if( data.type == PIPE_EVENT_MESSAGE )
        {
            if( servG->sendToClient )
            {
		readBodyFromPipe( el, fd , data );
            }
        }
        else if( data.type == PIPE_EVENT_CLOSE )
        {
            //close client socket
			printf( "onMasterPipeReadable PIPE_EVENT_CLOSE fd=%d \n" , data.connfd  );
            if( servG->closeClient )
            {
           	if( sdslen( servG->connlist[data.connfd].send_buffer ) == 0 )
		{
	           servG->closeClient( &servG->connlist[data.connfd] );
		}
		else
		{
		   servG->connlist[data.connfd].disable = 2;
		}
            }
        }
        else
        {
            printf( "recvFromPipe recv unkown data.type=%d" , data.type );
        }
    }
    else
    {
        if( errno == EAGAIN )
        {
            return;
        }
        else
        {
            //printf( "Reactor Recv errno=%d,errstr=%s \n" , errno , strerror( errno ));
        }
    }	
}
Esempio n. 2
0
void onMasterPipeReadable( aeEventLoop *el , int fd , void *privdata , int mask )
{
	int readlen = 0;
	appnetPipeData data;
	
	while (( readlen = read( fd , &data , PIPE_DATA_HEADER_LENG ) ) > 0)
	{
		if (readlen == 0)
		{
			close( fd );
			
		}
		else if (readlen == PIPE_DATA_HEADER_LENG)
		{
			if (data.type == PIPE_EVENT_MESSAGE)
			{
				
				if (servG->sendToClient)
				{
					readBodyFromPipe( el , fd , data );
				}
				
			}
			else if (data.type == PIPE_EVENT_TASK)
			{
				
				readBodyFromPipe( el , fd , data );
			}
			else if (data.type == PIPE_EVENT_CLOSE)
			{
				
				char proto_type = servG->connlist[data.connfd].proto_type;
				if (proto_type == WEBSOCKET)
				{
					int outlen = 0;
					char close_buff[1024];
					char *reason = "normal_close";
					
					wsMakeFrame( reason , strlen( reason ) , &close_buff , &outlen ,
							WS_CLOSING_FRAME );
					
					if (sdslen( servG->connlist[data.connfd].send_buffer ) == 0)
					{
						
						int ret = aeCreateFileEvent( el , data.connfd , AE_WRITABLE ,
								onClientWritable , NULL );
						
						if (ret == AE_ERR)
						{
							printf( "setPipeWritable_error %s:%d \n" , __FILE__ , __LINE__ );
						}
					}
					
					servG->connlist[data.connfd].send_buffer = sdscatlen(
							servG->connlist[data.connfd].send_buffer , close_buff , outlen );
				}
				
				if (servG->closeClient)
				{
					
					if (sdslen( servG->connlist[data.connfd].send_buffer ) == 0)
					{
						servG->closeClient( &servG->connlist[data.connfd] );
					}
					else
					{
						servG->connlist[data.connfd].disable = 2;
					}
				}
			}
		}
		else
		{
			if (errno == EAGAIN)
			{
				return;
			}
		}
	}
}