📄 sessioninfo.cpp
字号:
CSIGroup* pNewGroup = new CSIGroup( id );
if( NULL == pNewGroup )
return E_OUTOFMEMORY;
// Add the new object to the list
hr = m_pGroups->Add( pNewGroup );
if( FAILED(hr) )
{
// Release the allocated memory and return the error code
SAFE_DELETE( pNewGroup);
return hr;
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DestroyGroup()
// Desc: Removes the group with the given ID from the group list
//-----------------------------------------------------------------------------
HRESULT CSessionInfo::DestroyGroup( DPNID id )
{
// Find the group
for( UINT i=0; i < m_pGroups->Count(); i++ )
{
CSIGroup* pGroup = (CSIGroup*) m_pGroups->GetPtr( i );
// Group found
if( id == pGroup->id )
{
// Zero out the spot and return
m_pGroups->Remove( i );
SAFE_DELETE( pGroup );
return S_OK;
}
}
// Not found
return E_FAIL;
}
//-----------------------------------------------------------------------------
// Name: AddPlayerToGroup()
// Desc: Adds the given player ID to the list of member players in the group
// with the given group ID
//-----------------------------------------------------------------------------
HRESULT CSessionInfo::AddPlayerToGroup( DPNID idPlayer, DPNID idGroup )
{
// Find the group
for( UINT i=0; i < m_pGroups->Count(); i++ )
{
CSIGroup* pGroup = (CSIGroup*) m_pGroups->GetPtr( i );
// Group found
if( idGroup == pGroup->id )
{
// Add the dpnid and return
return pGroup->AddMember( idPlayer );
}
}
// Not found
return E_FAIL;
}
//-----------------------------------------------------------------------------
// Name: RemovePlayerFromGroup()
// Desc: Removes the given player ID from the list of member players in the
// group with the given group ID
//-----------------------------------------------------------------------------
HRESULT CSessionInfo::RemovePlayerFromGroup( DPNID idPlayer, DPNID idGroup )
{
// Find the group
for( UINT i=0; i < m_pGroups->Count(); i++ )
{
CSIGroup* pGroup = (CSIGroup*) m_pGroups->GetPtr( i );
// Group found
if( idGroup == pGroup->id )
{
// Remove the dpnid and return
return pGroup->RemoveMember( idPlayer );
}
}
// Not found
return E_FAIL;
}
//-----------------------------------------------------------------------------
// Name: RefreshPlayerInfo()
// Desc: Use DirectPlay to refresh all info for the player with the given ID
//-----------------------------------------------------------------------------
HRESULT CSessionInfo::RefreshPlayerInfo( DPNID id )
{
HRESULT hr = S_OK;
LPWSTR strURL = NULL;
DWORD dwNumChars = 0;
DPN_PLAYER_INFO* pDpPlayerInfo = NULL;
IDirectPlay8Address* rpAddress[16] = {0};
DWORD dwNumAddresses = 16;
#ifdef _DEBUG
// Parameter validation
if( NULL == id )
return E_INVALIDARG;
#endif // _DEBUG
// Attempt to get the name and flags
hr = GetDpPlayerInfo( id, &pDpPlayerInfo );
if( FAILED(hr) )
goto LCleanReturn;
// If receiving information about the local player, determine
// whether this app is the session host
if( id == m_dpnidLocal )
{
// Attempt to get the local address
switch( m_eType )
{
case PEER:
hr = m_pPeer->GetLocalHostAddresses( rpAddress, &dwNumAddresses, 0 );
break;
case SERVER:
hr = m_pServer->GetLocalHostAddresses( rpAddress, &dwNumAddresses, 0 );
break;
default:
hr = E_FAIL;
break;
}
}
else
{
// Attempt to get the remote address
dwNumAddresses = 1;
switch( m_eType )
{
case PEER:
hr = m_pPeer->GetPeerAddress( id, rpAddress, 0 );
break;
case SERVER:
hr = m_pServer->GetClientAddress( id, rpAddress, 0 );
break;
case CLIENT:
if( id == m_dpnidHost )
hr = m_pClient->GetServerAddress( rpAddress, 0 );
else
hr = DPNERR_INVALIDPLAYER;
break;
default:
hr = E_FAIL;
break;
}
}
// If the address was retrieved, extract the URL
if( SUCCEEDED(hr) )
{
// Get needed allocation size for the URL string
hr = rpAddress[0]->GetURLW( NULL, &dwNumChars );
if( FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL )
goto LCleanReturn;
// Allocate the URL string
strURL = new WCHAR[ dwNumChars ];
if( NULL == strURL )
{
hr = E_OUTOFMEMORY;
goto LCleanReturn;
}
// Get the URL
hr = rpAddress[0]->GetURLW( strURL, &dwNumChars );
if( FAILED(hr) )
goto LCleanReturn;
}
// Locate the stored player data
Lock();
CSIPlayer* pPlayer;
pPlayer = FindPlayer( id );
if( pPlayer )
{
// Set the player data
pPlayer->bIsHost = ( pDpPlayerInfo->dwPlayerFlags & DPNPLAYER_HOST );
if( strURL )
{
DXUtil_ConvertWideStringToGenericCch( pPlayer->strURL, strURL, 256 );
}
if( pDpPlayerInfo->pwszName)
{
DXUtil_ConvertWideStringToGenericCch( pPlayer->strName,
pDpPlayerInfo->pwszName, 256 );
}
}
Unlock();
LCleanReturn:
// Release resources
SAFE_DELETE_ARRAY( strURL );
for( UINT i=0; i < dwNumAddresses; i++ )
SAFE_RELEASE( rpAddress[i] );
SAFE_DELETE_ARRAY( pDpPlayerInfo );
return hr;
}
//-----------------------------------------------------------------------------
// Name: RefreshGroupInfo()
// Desc: Use DirectPlay to refresh all info for the group with the given ID
//-----------------------------------------------------------------------------
HRESULT CSessionInfo::RefreshGroupInfo( DPNID id )
{
HRESULT hr = S_OK;
DPN_GROUP_INFO* pDpGroupInfo = NULL;
// Attempt to get the name
hr = GetDpGroupInfo( id, &pDpGroupInfo );
if( FAILED(hr) )
goto LCleanReturn;
// Locate the stored player data
Lock();
CSIGroup* pGroup;
pGroup = FindGroup( id );
if( pGroup )
{
// Set the group data
DXUtil_ConvertWideStringToGenericCch( pGroup->strName, pDpGroupInfo->pwszName, 256 );
}
Unlock();
LCleanReturn:
// Release resources
SAFE_DELETE_ARRAY( pDpGroupInfo );
return hr;
}
//-----------------------------------------------------------------------------
// Name: MessageHandler
// Desc: Sift the information headed for the application's DirectPlay message
// handler, remove any messages used exclusively by this utility class,
// and store any useful information before the message is passed off to
// the application.
//-----------------------------------------------------------------------------
BOOL CSessionInfo::MessageHandler( DWORD dwMessageId, PVOID pMsgBuffer )
{
HRESULT hr = S_OK;
TCHAR strMessage[ 256 ];
switch( dwMessageId )
{
case DPN_MSGID_ADD_PLAYER_TO_GROUP:
{
DPNMSG_ADD_PLAYER_TO_GROUP* pMsg = (DPNMSG_ADD_PLAYER_TO_GROUP*) pMsgBuffer;
Lock();
hr = AddPlayerToGroup( pMsg->dpnidPlayer, pMsg->dpnidGroup );
Unlock();
// Invalidate the dialog
m_bDlgValid = FALSE;
// Log the message
_sntprintf( strMessage,
200,
TEXT("Add Player To Group: player 0x%x, group 0x%x"),
pMsg->dpnidPlayer,
pMsg->dpnidGroup );
strMessage[ 200 ] = TEXT('\0');
break;
}
case DPN_MSGID_APPLICATION_DESC:
{
// Set the window title
DPN_APPLICATION_DESC* pAppDesc;
if( SUCCEEDED( GetDpAppDesc( &pAppDesc ) ) )
{
TCHAR strTitle[ 256 ];
DXUtil_ConvertWideStringToGenericCch( strTitle,
pAppDesc->pwszSessionName,
256-50 );
lstrcat( strTitle, TEXT(" - Session Info") );
SendMessage( m_hDlg, WM_SETTEXT, 0, (LPARAM) strTitle );
}
SAFE_DELETE_ARRAY( pAppDesc );
// Log the message
_sntprintf( strMessage, 200, TEXT("Application Desc") );
strMessage[ 200 ] = TEXT('\0');
break;
}
case DPN_MSGID_ASYNC_OP_COMPLETE:
{
DPNMSG_ASYNC_OP_COMPLETE* pMsg = (DPNMSG_ASYNC_OP_COMPLETE*) pMsgBuffer;
// The messages sent by this helper class always use the same context.
// If the completed operation was initiated by this class, we can
// safely hide the message from the application.
if( SI_ASYNC_CONTEXT == pMsg->pvUserContext )
return TRUE;
// Log the message
_sntprintf( strMessage,
200,
TEXT("Async Op Complete: handle 0x%x, result 0x%x"),
pMsg->hAsyncOp,
pMsg->hResultCode );
strMessage[ 200 ] = TEXT('\0');
break;
}
case DPN_MSGID_CLIENT_INFO:
{
DPNMSG_CLIENT_INFO* pMsg = (DPNMSG_CLIENT_INFO*) pMsgBuffer;
OnDpInfoChange( pMsg->dpnidClient );
// Log the message
_sntprintf( strMessage,
200,
TEXT("Client Info: client 0x%x"),
pMsg->dpnidClient );
strMessage[ 200 ] = TEXT('\0');
break;
}
case DPN_MSGID_CONNECT_COMPLETE:
{
DPNMSG_CONNECT_COMPLETE* pMsg = (DPNMSG_CONNECT_COMPLETE*) pMsgBuffer;
// Log the message
_sntprintf( strMessage,
200,
TEXT("Connect Complete: handle 0x%x, result 0x%x"),
pMsg->hAsyncOp,
pMsg->hResultCode );
strMessage[ 200 ] = TEXT('\0');
break;
}
case DPN_MSGID_CREATE_GROUP:
{
DPNMSG_CREATE_GROUP* pMsg = (DPNMSG_CREATE_GROUP*) pMsgBuffer;
Lock();
hr = CreateGroup( pMsg->dpnidGroup );
Unlock();
RefreshGroupInfo( pMsg->dpnidGroup );
if( m_eType == SERVER )
SendGroupInfoToAll( pMsg->dpnidGroup );
// Invalidate the dialog
m_bDlgValid = FALSE;
// Log the message
_sntprintf( strMessage,
200,
TEXT("Create Group: group 0x%x, owner 0x%x"),
pMsg->dpnidGroup,
pMsg->dpnidOwner );
strMessage[ 200 ] = TEXT('\0');
break;
}
case DPN_MSGID_CREATE_PLAYER:
{
DPNMSG_CREATE_PLAYER* pMsg = (DPNMSG_CREATE_PLAYER*) pMsgBuffer;
Lock();
hr = CreatePlayer( pMsg->dpnidPlayer );
Unlock();
// If we don't know our local dpnid yet, try to initialize
if( m_dpnidLocal == NULL )
InitializeLocalPlayer( pMsg->dpnidPlayer );
// Update information about the new player
RefreshPlayerInfo( pMsg->dpnidPlayer );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -