⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sessioninfo.cpp

📁 Visual C++ 游戏开发与设计实例 源代码(所有)
💻 CPP
📖 第 1 页 / 共 5 页
字号:
            
            if( m_eType == SERVER )
            {
                // Clients receive all their information from the server,
                // so bundle up the new player's name and flags, and broadcast
                // to the session.
                SendPlayerInfoToAll( pMsg->dpnidPlayer );
                SynchronizeWithPlayer( pMsg->dpnidPlayer );
            }   

            // Invalidate the dialog
            m_bDlgValid = FALSE;

            // Log the message
            _sntprintf( strMessage, 
                        200, 
                        TEXT("Create Player: player 0x%x"), 
                        pMsg->dpnidPlayer );

            strMessage[ 200 ] = TEXT('\0');
            break;
        }

        case DPN_MSGID_DESTROY_GROUP:
        {
            DPNMSG_DESTROY_GROUP* pMsg = (DPNMSG_DESTROY_GROUP*) pMsgBuffer;
        
            Lock();
            hr = DestroyGroup( pMsg->dpnidGroup );
            Unlock();
             
            // Invalidate the dialog
            m_bDlgValid = FALSE;

            // Log the message
            _sntprintf( strMessage, 
                        200, 
                        TEXT("Destroy Group: group 0x%x"), 
                        pMsg->dpnidGroup );

            strMessage[ 200 ] = TEXT('\0');
            break;
        }

        case DPN_MSGID_DESTROY_PLAYER:
        {
            DPNMSG_DESTROY_PLAYER* pMsg = (DPNMSG_DESTROY_PLAYER*) pMsgBuffer;
        
            Lock();
            hr = DestroyPlayer( pMsg->dpnidPlayer );
            Unlock();   
            
            // Invalidate the dialog
            m_bDlgValid = FALSE;

            // Log the message
            _sntprintf( strMessage, 
                        200, 
                        TEXT("Destroy Player: player 0x%x"), 
                        pMsg->dpnidPlayer );

            strMessage[ 200 ] = TEXT('\0');
            break;
        }

        case DPN_MSGID_ENUM_HOSTS_QUERY:
        {
            // Log the message
            _sntprintf( strMessage, 
                        200, 
                        TEXT("Enum Hosts Query") );

            strMessage[ 200 ] = TEXT('\0');
            break;
        }

        case DPN_MSGID_ENUM_HOSTS_RESPONSE:
        {
            DPNMSG_ENUM_HOSTS_RESPONSE* pMsg = (DPNMSG_ENUM_HOSTS_RESPONSE*) pMsgBuffer;
        
            // Log the message
            TCHAR szSessionName[200];
            DXUtil_ConvertWideStringToGenericCch( szSessionName, pMsg->pApplicationDescription->pwszSessionName, 200 ); 

            _sntprintf( strMessage, 
                        200, 
                        TEXT("Enum Hosts Response: latency %d ms, session \"%s\""), 
                        pMsg->dwRoundTripLatencyMS, 
                        szSessionName );

            strMessage[ 200 ] = TEXT('\0');
            break;
        }

        case DPN_MSGID_GROUP_INFO:
        {
            DPNMSG_GROUP_INFO* pMsg = (DPNMSG_GROUP_INFO*) pMsgBuffer;
            
            RefreshGroupInfo( pMsg->dpnidGroup );
             
            if( m_eType == SERVER )
                SendGroupInfoToAll( pMsg->dpnidGroup );

            // Invalidate the dialog
            m_bDlgValid = FALSE;
            
            // Log the message
            _sntprintf( strMessage, 
                        200, 
                        TEXT("Group Info: group 0x%x"), 
                        pMsg->dpnidGroup );

            strMessage[ 200 ] = TEXT('\0');
            break;
        }

        case DPN_MSGID_HOST_MIGRATE:
        {
            DPNMSG_HOST_MIGRATE* pMsg = (DPNMSG_HOST_MIGRATE*) pMsgBuffer;

            m_dpnidHost = pMsg->dpnidNewHost;
            RefreshPlayerInfo( pMsg->dpnidNewHost );

            // Invalidate the dialog
            m_bDlgValid = FALSE;

            // Log the message
            _sntprintf( strMessage, 
                        200, 
                        TEXT("Host Migrate: new host 0x%x"), 
                        pMsg->dpnidNewHost );

            strMessage[ 200 ] = TEXT('\0');
            break;
        }

        case DPN_MSGID_INDICATE_CONNECT:
        {
            DPNMSG_INDICATE_CONNECT* pMsg = (DPNMSG_INDICATE_CONNECT*) pMsgBuffer;
        
            // Log the message
            _sntprintf( strMessage, 
                        200, 
                    #ifdef _WIN64
                        TEXT("Indicate Connect: player context 0x%I64x"), 
                        (ULONGLONG) pMsg->pvPlayerContext );
                    #else
                        TEXT("Indicate Connect: player context 0x%x"), 
                        (DWORD) pMsg->pvPlayerContext );
                    #endif // _WIN64
                        

            strMessage[ 200 ] = TEXT('\0');
            break;
        }

        case DPN_MSGID_INDICATED_CONNECT_ABORTED:
        {
            DPNMSG_INDICATED_CONNECT_ABORTED* pMsg = (DPNMSG_INDICATED_CONNECT_ABORTED*) pMsgBuffer;
        
            // Log the message
            _sntprintf( strMessage, 
                        200, 
                    #ifdef _WIN64
                        TEXT("Indicated Connect Aborted: player context 0x%I64x"), 
                        (ULONGLONG) pMsg->pvPlayerContext );
                    #else
                        TEXT("Indicated Connect Aborted: player context 0x%x"), 
                        (DWORD) pMsg->pvPlayerContext );
                    #endif // _WIN64

            strMessage[ 200 ] = TEXT('\0');
            break;
        }

        case DPN_MSGID_PEER_INFO:
        {
            DPNMSG_PEER_INFO* pMsg = (DPNMSG_PEER_INFO*) pMsgBuffer;
            OnDpInfoChange( pMsg->dpnidPeer );

            // Log the message
            _sntprintf( strMessage, 
                        200, 
                        TEXT("Peer Info: peer 0x%x"), 
                        pMsg->dpnidPeer );

            strMessage[ 200 ] = TEXT('\0');
            break;
        }

        case DPN_MSGID_RECEIVE:
        {
            DPNMSG_RECEIVE* pMsg = (DPNMSG_RECEIVE*) pMsgBuffer;
            
            if( pMsg->dwSize < sizeof(SI_MSG) )
                break;
            
            SI_MSG* pSIMsg = (SI_MSG*) pMsg->pReceiveData;

            switch( pSIMsg->dwMsgID )
            {
                case SI_MSGID_PLAYERINFO:
                {
                    if( pMsg->dwReceiveDataSize < sizeof(SI_MSG_PLAYERINFO) )
                        break;

                    SI_MSG_PLAYERINFO* pPlayerInfo = (SI_MSG_PLAYERINFO*) pMsg->pReceiveData;

                    // Verify the message is properly sized
                    if( pMsg->dwReceiveDataSize != sizeof(SI_MSG_PLAYERINFO) +
                        ( sizeof(WCHAR) * ( pPlayerInfo->dwNameLength + 1 ) ) )
                        break;
                    
                    // Pass the data off to the message handler function
                    OnPlayerInfoReceive( pPlayerInfo );

                    // Attempt to get additional information about the player
                    RefreshPlayerInfo( pPlayerInfo->dpnID );
                    
                    // Invalidate the dialog
                    m_bDlgValid = FALSE;

                    return TRUE;
                }

                case SI_MSGID_GROUPINFO:
                {
                    if( pMsg->dwReceiveDataSize < sizeof(SI_MSG_GROUPINFO) )
                        break;

                    SI_MSG_GROUPINFO* pGroupInfo = (SI_MSG_GROUPINFO*) pMsg->pReceiveData;

                    if( pMsg->dwReceiveDataSize != sizeof(SI_MSG_GROUPINFO) +
                        ( sizeof(WCHAR) * ( pGroupInfo->dwNameLength + 1 ) ) )
                    {
                        break;
                    }

                    // Pass the data off to the message handler function
                    OnGroupInfoReceive( pGroupInfo );

                    // Invalidate the dialog
                    m_bDlgValid = FALSE;

                    return TRUE;
                }
            }

            // Log the message
            _sntprintf( strMessage, 
                        200, 
                        TEXT("Receive: sender 0x%x, data size %d bytes"), 
                        pMsg->dpnidSender, 
                        pMsg->dwReceiveDataSize );

            strMessage[ 200 ] = TEXT('\0');
            break;
        }

        case DPN_MSGID_REMOVE_PLAYER_FROM_GROUP:
        {
            DPNMSG_REMOVE_PLAYER_FROM_GROUP* pMsg = (DPNMSG_REMOVE_PLAYER_FROM_GROUP*) pMsgBuffer;
        
            Lock();
            hr = RemovePlayerFromGroup( pMsg->dpnidPlayer, pMsg->dpnidGroup );
            Unlock();
            
            // Invalidate the dialog
            m_bDlgValid = FALSE;

            // Log the message
            _sntprintf( strMessage, 
                        200, 
                        TEXT("Remove Player From Group: player 0x%x, group 0x%x"), 
                        pMsg->dpnidPlayer, 
                        pMsg->dpnidGroup );

            strMessage[ 200 ] = TEXT('\0');
            break;
        }

        case DPN_MSGID_RETURN_BUFFER:
        {
            DPNMSG_RETURN_BUFFER* pMsg = (DPNMSG_RETURN_BUFFER*) pMsgBuffer;
            
            // Log the message
            _sntprintf( strMessage, 
                        200, 
                    #ifdef _WIN64
                        TEXT("Return Buffer: user context 0x%I64x, result 0x%x"), 
                        (ULONGLONG) pMsg->pvUserContext,
                    #else
                        TEXT("Return Buffer: user context 0x%x, result 0x%x"), 
                        (DWORD) pMsg->pvUserContext,
                    #endif // _WIN64
                        pMsg->hResultCode );

            strMessage[ 200 ] = TEXT('\0');
            break;
        }

        case DPN_MSGID_SEND_COMPLETE:
        {
            DPNMSG_SEND_COMPLETE* pMsg = (DPNMSG_SEND_COMPLETE*) pMsgBuffer;
            
            // Log the message
            _sntprintf( strMessage, 
                        200, 
                        TEXT("Send Complete: handle 0x%x, result 0x%x, send time %d ms"), 
                        pMsg->hAsyncOp, 
                        pMsg->hResultCode, 
                        pMsg->dwSendTime );

            strMessage[ 200 ] = TEXT('\0');
            break;
        }
        
        case DPN_MSGID_SERVER_INFO:
        {
            DPNMSG_SERVER_INFO* pMsg = (DPNMSG_SERVER_INFO*) pMsgBuffer;
            OnDpInfoChange( pMsg->dpnidServer );

            // Log the message
            _sntprintf( strMessage, 
                        200, 
                        TEXT("Server Info: server 0x%x"), 
                        pMsg->dpnidServer );

            strMessage[ 200 ] = TEXT('\0');
            break;
        }

        case DPN_MSGID_TERMINATE_SESSION:
        {
            DPNMSG_TERMINATE_SESSION* pMsg = (DPNMSG_TERMINATE_SESSION*) pMsgBuffer;
            
            // Log the message
            _sntprintf( strMessage, 
                        200, 
                        TEXT("Terminate Session: result 0x%x"), 
                        pMsg->hResultCode );

            strMessage[ 200 ] = TEXT('\0');
            break;
        }

        
    }

    CMessageList* pMsgList = NULL;
    int nDlgID = 0;

    // Add the message string to the stored list
    switch( dwMessageId )
    {
        case DPN_MSGID_RECEIVE:
        case DPN_MSGID_SEND_COMPLETE:
        {
            pMsgList = &m_AppMessages;
            nDlgID = IDC_SI_APPMSG;
            break; 
        }

        default:
        {
            pMsgList = &m_DPlayMessages;
            nDlgID = IDC_SI_DPLAYMSG;
            break;   
        }
    }
    
    // Lock the message list
    pMsgList->Lock();

    // If the message queue is already full, remove the bottom item from the dialog box 
    if( pMsgList->IsFull() )
        SendMessage( GetDlgItem( m_hDlgMessages, nDlgID ), LB_DELETESTRING, SI_MAX_MESSAGES-1, 0 );

    // Add the message to the stored list
    TCHAR* strTimeStamped = pMsgList->AddMessage( strMessage );

    // Unlock the message list
    pMsgList->Unlock();

    // Post the new string to the top of the list box
    if( m_hDlgMessages )
        SendMessage( GetDlgItem( m_hDlgMessages, nDlgID ), LB_INSERTSTRING, 0, (LPARAM) strTimeStamped );

    // Return false to indicate that either the message was not handled, or the
    // handled message should also be sent to the application message handler.
    return FALSE;
}




//-----------------------------------------------------------------------------
// Name: OnPlayerInfoReceive()
// Desc: Handles the extraction and storage of incoming player data
//-----------------------------------------------------------------------------
HRESULT CSessionInfo::OnPlayerInfoReceive( SI_MSG_PLAYERINFO* pPlayerInfo )
{
    HRESULT hr = S_OK;

    // Extract the player name
    LPWSTR pStrName = (LPWSTR) (pPlayerInfo + 1);
    pStrName[ pPlayerInfo->dwNameLength ] = 0;

    Lock();
      
    // Search for the player with the given ID
    CSIPlayer* pPlayer = FindPlayer( pPlayerInfo->dpnID ); 

    // If not found, create a new player
    if( NULL == pPlayer )
    {
        hr = CreatePlayer( pPlayerInfo->dpnID );
        if( FAILED(hr) )
            goto LCleanReturn;
            
        pPlayer = FindPlayer( pPlayerInfo->dpnID );
    }

    // Set updated information
    pPlayer->bIsHost = pPlayerInfo->dwFlags & DPNPLAYER_HOST;
    
    DXUtil_ConvertWideStringToGenericCch( pPlayer->strName, pStrName, 256 ); 
    
    if( pPlayerInfo->dwFlags & DPNPLAYER_LOCAL )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -