Ejemplo n.º 1
0
void WebSocket::send(const char* message, int length, wsFrameType type)
{
	uint8_t frameHeader[16] = {0};
	size_t headSize = sizeof(frameHeader);
	wsMakeFrame(nullptr, length, frameHeader, &headSize, type);
	connection->write((char*)frameHeader, headSize, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE);
	connection->writeString(message, TCP_WRITE_FLAG_COPY);
	connection->flush();
}
Ejemplo 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;
			}
		}
	}
}