Exemplo n.º 1
0
//recv from client,write to worker pipe buffer,
void onClientReadable(aeEventLoop *el, int fd, void *privdata, int mask)
{
    aeServer* serv = servG; 
    aePipeData  data = {0};
    data.type = PIPE_EVENT_MESSAGE;
    data.connfd = fd;

    ssize_t nread;
    unsigned int readlen, rcvbuflen ,datalen;
    readlen = sizeof(data.data);
    int worker_id = fd % serv->workerNum;
	
	//
	//worker send_buffer
	//sndbuf ->|----c1----|---c2------|--c3-----|-----c2---|---c3-------|
	//c1  	 ->|---header---|--------body------------|
	//client recv buffer

    while(1) {
        rcvbuflen = sdslen( servG->workers[worker_id].send_buffer );
        nread = read(fd, &data.data ,readlen);
		
        if (nread == -1 && errno == EAGAIN) return; /* No more data ready. */
        if (nread == 0 )
		{
			onCloseByClient(  el , privdata , serv,  &serv->connlist[fd] );
            return;
        } 
		else
		{
            		/* Read data and recast the pointer to the new buffer. */
			// |--header--|-------data-------|
			data.len = nread;
			datalen = PIPE_DATA_HEADER_LENG + data.len;
	
			setPipeWritable( el , privdata , worker_id  );
			pthread_mutex_lock( &servG->workers[worker_id].w_mutex );
			//append
			servG->workers[worker_id].send_buffer = sdscatlen( servG->workers[worker_id].send_buffer , &data, datalen );
			pthread_mutex_unlock( &servG->workers[worker_id].w_mutex );
			
			rcvbuflen += datalen;
			return;
        }
    }
}
Exemplo n.º 2
0
void onClientReadable( aeEventLoop *el , int connfd , void *privdata , int mask )
{
	
	appnetServer *serv = servG;
	ssize_t nread;
	unsigned int readlen,rcvbuflen,datalen;
	int worker_id = servG->connlist[connfd].worker_id;
	int thid = servG->connlist[connfd].reactor_id;
	char buffer[TMP_BUFFER_LENGTH];
	
	while (1)
	{
		
		nread = 0;
		memset( &buffer , 0 , sizeof( buffer ) );
		nread = read( connfd , &buffer , sizeof( buffer ) );
		
		if (nread == -1 && errno == EAGAIN)
		{
			return; /* No more data ready. */
		}
		
		if (nread == 0)
		{
			onCloseByClient( el , privdata , serv , &serv->connlist[connfd] );
			return; /* Close event */
		}
		else if (nread > 0)
		{
			
			/* Append client recv_buffer */
			servG->connlist[connfd].recv_buffer =
					sdscatlen( servG->connlist[connfd].recv_buffer , &buffer , nread );
			
			int ret =
					parseRequestMessage( connfd , servG->connlist[connfd].recv_buffer ,
							sdslen( servG->connlist[connfd].recv_buffer ) );
			
			if (ret == BREAK_RECV)
			{
				int complete_length = servG->reactor_threads[thid].hh->complete_length;
				
				if (complete_length > 0)
				{
					sdsrange( servG->connlist[connfd].recv_buffer , complete_length , -1 );
				}
				else
				{
					sdsclear( servG->connlist[connfd].recv_buffer );
				}
				break;
				
			}
			else if (ret == CONTINUE_RECV)
			{
				continue;
				
			}
			else if (ret == CLOSE_CONNECT)
			{
				freeClient( &servG->connlist[connfd] );
				return;
			}
			return;
		}
		else
		{
			printf( "Recv Errno=%d,Err=%s \n" , errno , strerror( errno ) );
		}
	}
}