bool ServerConnectDaemon::connectStatePlayerStateSync()
{
    ConnectProcessStateMessage state_mesg;
    int send_ret_val;
    int percent_complete;

    if ( PlayerInterface::syncPlayerState( &send_ret_val, &percent_complete ) == true ) {
        if( send_ret_val != _network_ok ) {
            connect_player_state->setStatus( _player_state_free );
            resetConnectFsm();
            return( true );
        }

        state_mesg.message_enum = _connect_state_message_sync_player_info_percent;
        state_mesg.percent_complete = percent_complete;
        send_ret_val = SERVER->sendMessage( connect_player_id, &state_mesg, sizeof(ConnectProcessStateMessage), 0);

        if( send_ret_val != _network_ok ) {
            connect_player_state->setStatus( _player_state_free );
            resetConnectFsm();
            return( true );
        }

        delete connect_unit_sync;
        connect_unit_sync = new UnitSync();

        state_mesg.message_enum = _connect_state_message_sync_units;
        send_ret_val = SERVER->sendMessage( connect_player_id, &state_mesg, sizeof(ConnectProcessStateMessage), 0 );

        if( send_ret_val != _network_ok ) {
            connect_player_state->setStatus( _player_state_free );
            resetConnectFsm();
            return( true );
        }

        SERVER->addClientToSendList( connect_player_id );

        PlayerStateSync player_state_update
            (connect_player_state->getNetworkPlayerState());

        SERVER->sendMessage( &player_state_update, sizeof(PlayerStateSync), 0 );


        connection_state = _connect_state_unit_sync;
        return( true );
    } else
        if ( percent_complete > 0 ) {
            state_mesg.message_enum = _connect_state_message_sync_player_info_percent;
            state_mesg.percent_complete = percent_complete;
            send_ret_val = SERVER->sendMessage( connect_player_id, &state_mesg, sizeof(ConnectProcessStateMessage), 0 );

            if( send_ret_val != _network_ok ) {
                connect_player_state->setStatus( _player_state_free );
                resetConnectFsm();
                return( true );
            }
        }

    return( true );
}
    virtual T process()
    {
        ObjectiveInterface::syncObjectives( connect_client );

        PowerUpInterface::syncPowerUps( connect_client );

        
        PlayerState * player = PlayerInterface::getPlayer(connect_client->getPlayerIndex());

        player->setStateSelectingFlag();

        PlayerFlagSync pfs;
        pfs.player_id = connect_client->player_id;
        ResourceManager::getFlag(connect_client->player_id)->frameToBuffer(pfs.player_flag, sizeof(pfs.player_flag));
        SERVER->broadcastMessage(&pfs, sizeof(pfs));

        PlayerStateSync player_state_update
            (player->getNetworkPlayerState());

        SERVER->broadcastMessage( &player_state_update, sizeof(PlayerStateSync));

        ConnectProcessStateMessage state_mesg;
        state_mesg.setMessageEnum(_connect_state_sync_complete);
        connect_client->sendMessage( &state_mesg,
                                     sizeof(ConnectProcessStateMessage));


        sendConnectionAlert( connect_client );
        if (GameControlRulesDaemon::getGameState() == _game_state_prepare_team)
        {
            GameControlCyclePrepareTeam prepare_team_mesg;
            SERVER->sendMessage(connect_client->player_id, &prepare_team_mesg, sizeof(GameControlCyclePrepareTeam));
        }
        return connect_state_idle;
    }
    virtual void enter()
    {
        player = PlayerInterface::getPlayer(connect_client->getPlayerIndex());

        sync_count = 0;
        sync_done = 0;
        sync_end = 0;
        sync_total = UnitInterface::getTotalUnitCount();

        if ( sync_total )
        {
            sync_end = UnitInterface::getUnits().rbegin()->first;
        }

        sync_end += 1;

        ConnectProcessStateMessage state_mesg;
        state_mesg.setMessageEnum(_connect_state_message_sync_units);
        connect_client->sendMessage( &state_mesg,
                                     sizeof(ConnectProcessStateMessage));

        SERVER->addClientToSendList( connect_client );

        PlayerState *p = PlayerInterface::getPlayer(connect_client->getPlayerIndex());
        PlayerStateSync player_state_update( p->getNetworkPlayerState() );
        SERVER->broadcastMessage(&player_state_update, sizeof(PlayerStateSync));

        sendunitpercent_timer.reset();
        time_out_timer.reset();
    }
void PlayerInterface::disconnectPlayerCleanup( PlayerID &player_id )
{
    PlayerAllianceUpdate allie_update;

    unsigned short player_index;
    unsigned short disconnect_player_index;

    disconnect_player_index = player_id.getIndex();

    for ( player_index = 0; player_index < max_players; player_index++ ) {
        if ( isAllied( disconnect_player_index, player_index ) == true ) {
            allie_update.set( disconnect_player_index, player_index, _player_break_alliance );
            SERVER->sendMessage( &allie_update, sizeof( PlayerAllianceUpdate ), 0 );
        }

        if ( isAllied( player_index, disconnect_player_index ) == true ) {
            allie_update.set( player_index, disconnect_player_index, _player_break_alliance );
            SERVER->sendMessage( &allie_update, sizeof( PlayerAllianceUpdate ), 0 );
        }

    } // ** for

    player_lists[ disconnect_player_index ].setStatus( _player_state_free );

    PlayerState *player_state = getPlayerState( player_id );

    PlayerStateSync player_state_update(player_state->getNetworkPlayerState());

    SERVER->sendMessage( &player_state_update, sizeof( PlayerStateSync ), 0 );
}
bool ServerConnectDaemon::connectStateUnitSync()
{
    ConnectProcessStateMessage state_mesg;
    int percent_complete = connect_unit_sync->getPercentComplete();

    // send a unit
    if(connect_unit_sync->sendNextUnit(connect_player_id)) {
        if ( sendunitpercent_timer.count() ) {
            state_mesg.setMessageEnum(_connect_state_message_sync_units_percent);
            state_mesg.setPercentComplete(percent_complete);
            SERVER->sendMessage(connect_player_id, &state_mesg,
                    sizeof(ConnectProcessStateMessage));
            sendunitpercent_timer.reset();
        }
        return true;
    }

    // Sending finished
    state_mesg.setMessageEnum(_connect_state_message_sync_units_percent);
    state_mesg.setPercentComplete(100);
    SERVER->sendMessage( connect_player_id, &state_mesg,
            sizeof(ConnectProcessStateMessage));

    UnitSyncIntegrityCheck unit_integrity_check_mesg;
    SERVER->sendMessage( connect_player_id, &unit_integrity_check_mesg,
            sizeof(UnitSyncIntegrityCheck));

    ObjectiveInterface::syncObjectives( connect_player_id );

    PowerUpInterface::syncPowerUps( connect_player_id );

    GameManager::spawnPlayer( connect_player_id );

    connect_player_state->setStatus( _player_state_active );

    PlayerStateSync player_state_update
        (connect_player_state->getNetworkPlayerState());

    SERVER->sendMessage( &player_state_update, sizeof(PlayerStateSync));

    state_mesg.setMessageEnum(_connect_state_sync_complete);
    SERVER->sendMessage( connect_player_id, &state_mesg,
            sizeof(ConnectProcessStateMessage));
    sendConnectionAlert( connect_player_id, _connect_alert_mesg_connect );

    connection_state = connect_state_idle;
    return true;
}
void PlayerInterface::disconnectPlayerCleanup( PlayerID player_id )
{
    PlayerState *player_state = getPlayer( player_id );
    if ( player_state )
    {
        SDL_mutexP(mutex);

        // XXX ALLY
        disconnectedPlayerAllianceCleanup(player_id);

        player_state->setStateFree();
        player_state->setAdmin(false);

        PlayerStateSync player_state_update(player_state->getNetworkPlayerState());

        SDL_mutexV(mutex);

        SERVER->broadcastMessage(&player_state_update, sizeof(PlayerStateSync));
    }
}
void PlayerInterface::disconnectPlayerCleanup( Uint16 index )
{
    PlayerState *player_state = getPlayer( index );
    if ( player_state )
    {
        PlayerAllianceUpdate allie_update;

        unsigned short disconnect_player_index;

        disconnect_player_index = index;

        SDL_mutexP(mutex);

        disconnectedPlayerAllianceCleanup(disconnect_player_index);

        player_state->setStatus( _player_state_free );

        PlayerStateSync player_state_update(player_state->getNetworkPlayerState());

        SDL_mutexV(mutex);

        SERVER->broadcastMessage(&player_state_update, sizeof(PlayerStateSync));
    }
}
bool ServerConnectDaemon::connectStatePlayerStateSync()
{
    ConnectProcessStateMessage state_mesg;
    int percent_complete;

    if (PlayerInterface::syncPlayerState( &percent_complete ) == true ) {
        state_mesg.setMessageEnum(_connect_state_message_sync_player_info_percent);
        state_mesg.setPercentComplete(percent_complete);
        SERVER->sendMessage(connect_player_id, &state_mesg,
                sizeof(ConnectProcessStateMessage));
        delete connect_unit_sync;
        connect_unit_sync = new UnitSync();
        sendunitpercent_timer.reset();

        state_mesg.setMessageEnum(_connect_state_message_sync_units);
        SERVER->sendMessage( connect_player_id, &state_mesg,
                sizeof(ConnectProcessStateMessage));
        SERVER->addClientToSendList( connect_player_id );

        PlayerStateSync player_state_update
            (connect_player_state->getNetworkPlayerState());

        SERVER->sendMessage(&player_state_update, sizeof(PlayerStateSync));
		if(connection_state != connect_state_idle) {
			connection_state = connect_state_unit_sync;
		}
        return true;
    } else if ( percent_complete > 0 ) {
        state_mesg.setMessageEnum(_connect_state_message_sync_player_info_percent);
        state_mesg.setPercentComplete(percent_complete);
        SERVER->sendMessage(connect_player_id, &state_mesg,
                sizeof(ConnectProcessStateMessage));
    }

    return true;
}
bool ServerConnectDaemon::connectStateUnitSync()
{
    ConnectProcessStateMessage state_mesg;
    int percent_complete = connect_unit_sync->getPercentComplete();

    // send a unit
    if(connect_unit_sync->sendNextUnit(connect_player_id)) {
        state_mesg.message_enum = _connect_state_message_sync_units_percent;
        state_mesg.percent_complete = percent_complete;
        int ret = SERVER->sendMessage(connect_player_id, &state_mesg,
                sizeof(ConnectProcessStateMessage), 0);
        
        if(ret != _network_ok ) {
            resetConnectFsm();
        }

        return true;
    }

    // Sending finished
    state_mesg.message_enum = _connect_state_message_sync_units_percent;
    state_mesg.percent_complete = percent_complete;
    int ret = SERVER->sendMessage( connect_player_id, &state_mesg,
            sizeof(ConnectProcessStateMessage), 0 );

    if(ret != _network_ok) {
        resetConnectFsm();
        return true;
    }

    UnitSyncIntegrityCheck unit_integrity_check_mesg;
    ret = SERVER->sendMessage( connect_player_id, &unit_integrity_check_mesg, sizeof(UnitSyncIntegrityCheck), 0 );
    
    if(ret != _network_ok ) {
        resetConnectFsm();
        return true;
    }

    ObjectiveInterface::syncObjectives( connect_player_id );

    PowerUpInterface::syncPowerUps( connect_player_id );

    GameManager::spawnPlayer( connect_player_id );

    connect_player_state->setStatus( _player_state_active );

    PlayerStateSync player_state_update
        (connect_player_state->getNetworkPlayerState());

    SERVER->sendMessage( &player_state_update, sizeof(PlayerStateSync), 0 );

    state_mesg.message_enum = _connect_state_sync_complete;
    ret = SERVER->sendMessage( connect_player_id, &state_mesg, sizeof(ConnectProcessStateMessage), 0 );

    if(ret != _network_ok ) {
        resetConnectFsm();
        return true;
    }

    sendConnectionAlert( connect_player_id, _connect_alert_mesg_connect );

    connection_state = _connect_state_idle;
    return true;
}