?/TD>
Microsoft DirectX 9.0

Implementing a DirectPlay Networking Callback Using Critical Section Objects


Microsoft?DirectPlay?networking and voice callback are multithreaded. Therefore, in order to correctly and reliably access data in DirectPlay callbacks, you are required to implement a method of multithreading synchronization.

Currently, there are three methods of synchronizing mulithreaded callback data.

The DirectPlay voice samples that ship with the Microsoft DirectX?SDK demonstrate synchronization using Critical Section Objects, and the following topics will also demonstrate how Critical Section Objects are used. If you want to implement a Mutex or Semaphore Object, these topics are discussed in the Microsoft Platform Software Development Kit (SDK) as well as in many reference books. Implementing any of these synchronization methods requires an expert knowledge level in these areas due of the level of complexity and difficulty in debugging should any issues arise.

CRITICAL_SECTION g_csPlayerContext;
InitializeCriticalSectionAndSpinCount(&g_csPlayerContext, 0);

Next, implement the DirectPlay message callback handler.

HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext, 
                                         DWORD dwMessageId, 
                                         PVOID pMsgBuffer )
{
    switch( dwMessageId )
    {
        case DPN_MSGID_CREATE_PLAYER:
        {
             EnterCriticalSection( &g_csPlayerContext );
             //callback is now locked
             //perform operation on player data
             LeaveCriticalSection( &g_csPlayerContext );
        }
    }
}

Finally, during application exit, ensure that you call the DeleteCriticalSection function to free the memory associated with your critical section object.

DeleteCriticalSection( &g_csPlayerContext );


© 2002 Microsoft Corporation. All rights reserved.