예제 #1
0
bool TCPSocket::SendPacket( const Message& packet )
{
	bool result = false;
	unsigned int dataSize = sizeof( unsigned int ) + packet.GetSerializationSize();
	Byte* serializedPacket = static_cast<Byte*>( tMalloc( dataSize ) );
	Byte* walker = serializedPacket;
	SerializationUtility::CopyAndIncrementDestination( walker, &dataSize, sizeof( unsigned int ) );
	packet.Serialize( walker );

#if NETWORK_DEBUG
	if ( walker != serializedPacket + dataSize )
	{
		int differentiatingSize = static_cast<int>(walker - serializedPacket - dataSize);
		assert(false);
	}
	MemoryAllocator::VerifyPointer( serializedPacket );
#endif

	if ( serializedPacket != nullptr )
	{
		result = SendRawData( serializedPacket, dataSize );
		tFree( serializedPacket );
	}

	return result;
}
예제 #2
0
Message* TCPSocket::Receive()
{
	if ( m_Socket == INVALID_SOCKET )
	{
		Logger::Log( "Attempted to receive from invalid socket", "TCPSocket", LogSeverity::WARNING_MSG );
		return nullptr;
	}

	int nrOfBytesReceived;
	if ( m_ExpectedHeaderBytes > 0 ) // If we are waiting for header data
	{
		nrOfBytesReceived = recv( m_Socket, m_Pos, m_ExpectedHeaderBytes, 0 ); // Try to receive header

		if ( nrOfBytesReceived == 0 ) // Check if the socket was gracefully disconnected
		{
			m_Connected = false;
			Logger::Log( "Connection to " + m_Destination.GetPrintableAddressAndPort() + " terminated gracefully", "TCPSocket", LogSeverity::INFO_MSG );
		} 
		else if ( nrOfBytesReceived == -1 )
		{
			int error = GET_NETWORK_ERROR;
			if ( error != ROBOEWOULDBLOCK )
			{
				if ( error == ROBOECONNECTIONABORTED || error == ROBOECONNRESET )
				{
					m_Connected = false;
					Logger::Log( "Connection to " + m_Destination.GetPrintableAddressAndPort( ) + " was aborted", "TCPSocket", LogSeverity::INFO_MSG );
				}
				else
					LogErrorMessage( "An unhandled error occured while receiving header data", "TCPSocket" );
			}
			return nullptr; // No data was ready to be received or there was an error
		}

		if ( nrOfBytesReceived == m_ExpectedHeaderBytes )
		{
			m_ExpectedHeaderBytes = 0;	// We received the full header

			// Get the size of the packet (Embedded as first part) and create a buffer of that size
			m_PayloadData = static_cast<Byte*>( tMalloc( m_ExpectedPayloadBytes ) );

			m_ExpectedPayloadBytes -= sizeof( unsigned int ); // We have already received the size value
			m_Pos = m_PayloadData;	// Pos now points to the new buffer since that is where we will want to write on the next recv
		}
		else // Only a part of the header was received. Account for this and handle it on next function call
		{
			m_ExpectedHeaderBytes -= nrOfBytesReceived;
			m_Pos += nrOfBytesReceived;
			return nullptr;
		}
	}

	nrOfBytesReceived = recv( m_Socket, m_Pos, m_ExpectedPayloadBytes, 0 ); // Try to receive payload

	if ( nrOfBytesReceived == -1 )
	{
		int error = GET_NETWORK_ERROR;
		if ( error != ROBOEWOULDBLOCK )
		{
			if ( error == ROBOECONNECTIONABORTED )
			{
				m_Connected = false;
				Logger::Log( "Connection to " + m_Destination.GetPrintableAddressAndPort( ) + " was aborted", "TCPSocket", LogSeverity::INFO_MSG );
			}
			else
				LogErrorMessage( "An unhandled error occured while receiving payload data", "TCPSocket" );

		}
		return nullptr; // No data was ready to be received or there was an error
	}

	// If all data was received. Clean up, prepare for next call and return the buffer as a packet (Will need to be cast to the correct type on the outside using the Type field)
	if ( nrOfBytesReceived == m_ExpectedPayloadBytes )
	{
		MessageTypes::MessageType messageType;
		Byte* walker = m_PayloadData;

		memcpy( &messageType, walker, sizeof( MESSAGE_TYPE_ENUM_UNDELYING_TYPE ) );
		Message* packet;
		if ( messageType == 0 )
		{
			NetworkMessageTypes::NetworkMessageType networkedMessageType;
			memcpy( &networkedMessageType, walker + sizeof( MESSAGE_TYPE_ENUM_UNDELYING_TYPE ) + sizeof( bool ), sizeof( NetworkMessageTypes::NetworkMessageType ) );
			packet = NetworkMessages::GetDefaultMessage( networkedMessageType );
		}
		else
		{
			packet = Messages::GetDefaultMessage( messageType );
		}
		packet->Deserialize( ( const char*& )walker );

#if NETWORK_DEBUG
		if ( walker != m_PayloadData + packet->GetSerializationSize( ) )
		{
			int differentiatingSize = walker - m_PayloadData - packet->GetSerializationSize( );
			assert( false );
		}
		MemoryAllocator::VerifyPointer( packet );
#endif

		tFree( m_PayloadData );
		m_PayloadData = nullptr;
		m_Pos = reinterpret_cast<Byte*>( &m_ExpectedPayloadBytes );
		m_ExpectedPayloadBytes = -1;
		m_ExpectedHeaderBytes = sizeof( unsigned int );

		return packet;
	}
	else // Only part of the payload was received. Account for this and try to receive the rest in an upcoming function call
	{
		m_ExpectedPayloadBytes -= nrOfBytesReceived;
		m_Pos += nrOfBytesReceived;
		return nullptr;
	}
}
int get_judgment_list (char *fname, judgment_list *list)
{
    int fd;
    int size = 0;
    char *trec_qrels_buf;
    char *ptr;
    char *current_qid;
    char *qid_ptr, *docno_ptr, *rel_ptr;
    long i;
    long rel;
    TREC_QRELS *current_qrels = NULL;

    /* Read entire file into memory */
    if (-1 == (fd = open (fname, 0)) ||
        -1 == (size = lseek (fd, 0L, 2)) ||
        NULL == (trec_qrels_buf = malloc ((unsigned) size+2)) ||
        -1 == lseek (fd, 0L, 0) ||
        size != read (fd, trec_qrels_buf, size) ||
	-1 == close (fd)) {

        set_error (SM_ILLPA_ERR, "Cannot read qrels file", "trec_eval");
        return (UNDEF);
    }

    current_qid = "";

    /* Initialize all_trec_qrels */
    list->num_q_qrels = 0;
    list->max_num_q_qrels = INIT_NUM_QUERIES;
    if (NULL == (list->trec_qrels = tMalloc (INIT_NUM_QUERIES,
						     TREC_QRELS)))
	return (UNDEF);
    
    if (size == 0)
	return (0);

    /* Append ending newline if not present, Append NULL terminator */
    if (trec_qrels_buf[size-1] != '\n') {
	trec_qrels_buf[size] = '\n';
	size++;
    }
    trec_qrels_buf[size] = '\0';

    ptr = trec_qrels_buf;

    while (*ptr) {
	/* Get current line */
	/* Get qid */
	while (*ptr != '\n' && isspace (*ptr)) ptr++;
	qid_ptr = ptr;
	while (! isspace (*ptr)) ptr++;
	if (*ptr == '\n') {
	    set_error (SM_ILLPA_ERR, "Malformed qrels line", "trec_eval");
	    return (UNDEF);
	}
	*ptr++ = '\0';
	/* Get docno */
	while (*ptr != '\n' && isspace (*ptr)) ptr++;
	docno_ptr = ptr;
	while (! isspace (*ptr)) ptr++;
	if (*ptr == '\n') {
	    set_error (SM_ILLPA_ERR, "Malformed qrels line", "trec_eval");
	    return (UNDEF);
	}
	*ptr++ = '\0';
	/* move on to end of line */
	while (*ptr != '\n') ptr++;
	*ptr++ = '\0';

	if (0 != strcmp (qid_ptr, current_qid)) {
	    /* Query has changed. Must check if new query or this is more
	       judgements for an old query */
	    for (i = 0; i < list->num_q_qrels; i++) {
		if (0 == strcmp (qid_ptr, list->trec_qrels[i].qid))
		    break;
	    }
	    if (i >= list->num_q_qrels) {
		/* New unseen query, add and initialize it */
		if (list->num_q_qrels >=
		    list->max_num_q_qrels) {
		    list->max_num_q_qrels *= 10;
		    if (NULL == (list->trec_qrels = 
				 tRealloc (list->trec_qrels,
					  list->max_num_q_qrels,
					  TREC_QRELS)))
			return (UNDEF);
		}
		current_qrels = &list->trec_qrels[i];
		current_qrels->qid = qid_ptr;
		current_qrels->num_text_qrels = 0;
		current_qrels->max_num_text_qrels = INIT_NUM_RELS;
		if (NULL == (current_qrels->text_qrels =
			     tMalloc (INIT_NUM_RELS, TEXT_QRELS)))
		    return (UNDEF);
		list->num_q_qrels++;
	    }
	    else {
		/* Old query, just switch current_q_index */
		current_qrels = &list->trec_qrels[i];
	    }
	    current_qid = current_qrels->qid;
	}
	
	/* Add judgement to current query's list */
	if (current_qrels->num_text_qrels >= 
	    current_qrels->max_num_text_qrels) {
	    /* Need more space */
	    current_qrels->max_num_text_qrels *= 10;
	    if (NULL == (current_qrels->text_qrels = 
			 tRealloc (current_qrels->text_qrels,
				  current_qrels->max_num_text_qrels,
				  TEXT_QRELS)))
		return (UNDEF);
	}
	current_qrels->text_qrels[current_qrels->num_text_qrels].docno =
		docno_ptr;
	current_qrels->text_qrels[current_qrels->num_text_qrels++].rel = 0;
    }

	list->ptr = trec_qrels_buf;

    return (1);
}