msxml3/tests: Correct variant type.
[wine/multimedia.git] / dlls / dplayx / dplayx_messages.c
blob8b9320d034591c201ee6c7d1ff457bf585afc243
1 /* DirectPlay & DirectPlayLobby messaging implementation
3 * Copyright 2000,2001 - Peter Hunnisett
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 * NOTES
20 * o Messaging interface required for both DirectPlay and DirectPlayLobby.
23 #include <stdarg.h>
24 #include <string.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29 #include "winerror.h"
31 #include "dplayx_messages.h"
32 #include "dplay_global.h"
33 #include "dplayx_global.h"
34 #include "name_server.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(dplay);
39 typedef struct tagMSGTHREADINFO
41 HANDLE hStart;
42 HANDLE hDeath;
43 HANDLE hSettingRead;
44 HANDLE hNotifyEvent;
45 } MSGTHREADINFO, *LPMSGTHREADINFO;
47 static DWORD CALLBACK DPL_MSG_ThreadMain( LPVOID lpContext );
48 static LPVOID DP_MSG_ExpectReply( IDirectPlay2AImpl* This, LPDPSP_SENDDATA data,
49 DWORD dwWaitTime, WORD wReplyCommandId,
50 LPVOID* lplpReplyMsg, LPDWORD lpdwMsgBodySize );
53 /* Create the message reception thread to allow the application to receive
54 * asynchronous message reception
56 DWORD CreateLobbyMessageReceptionThread( HANDLE hNotifyEvent, HANDLE hStart,
57 HANDLE hDeath, HANDLE hConnRead )
59 DWORD dwMsgThreadId;
60 LPMSGTHREADINFO lpThreadInfo;
61 HANDLE hThread;
63 lpThreadInfo = HeapAlloc( GetProcessHeap(), 0, sizeof( *lpThreadInfo ) );
64 if( lpThreadInfo == NULL )
66 return 0;
69 /* The notify event may or may not exist. Depends if async comm or not */
70 if( hNotifyEvent &&
71 !DuplicateHandle( GetCurrentProcess(), hNotifyEvent,
72 GetCurrentProcess(), &lpThreadInfo->hNotifyEvent,
73 0, FALSE, DUPLICATE_SAME_ACCESS ) )
75 ERR( "Unable to duplicate event handle\n" );
76 goto error;
79 /* These 3 handles don't need to be duplicated because we don't keep a
80 * reference to them where they're created. They're created specifically
81 * for the message thread
83 lpThreadInfo->hStart = hStart;
84 lpThreadInfo->hDeath = hDeath;
85 lpThreadInfo->hSettingRead = hConnRead;
87 hThread = CreateThread( NULL, /* Security attribs */
88 0, /* Stack */
89 DPL_MSG_ThreadMain, /* Msg reception function */
90 lpThreadInfo, /* Msg reception func parameter */
91 0, /* Flags */
92 &dwMsgThreadId /* Updated with thread id */
94 if ( hThread == NULL )
96 ERR( "Unable to create msg thread\n" );
97 goto error;
100 CloseHandle(hThread);
102 return dwMsgThreadId;
104 error:
106 HeapFree( GetProcessHeap(), 0, lpThreadInfo );
108 return 0;
111 static DWORD CALLBACK DPL_MSG_ThreadMain( LPVOID lpContext )
113 LPMSGTHREADINFO lpThreadInfo = lpContext;
114 DWORD dwWaitResult;
116 TRACE( "Msg thread created. Waiting on app startup\n" );
118 /* Wait to ensure that the lobby application is started w/ 1 min timeout */
119 dwWaitResult = WaitForSingleObject( lpThreadInfo->hStart, 10000 /* 10 sec */ );
120 if( dwWaitResult == WAIT_TIMEOUT )
122 FIXME( "Should signal app/wait creation failure (0x%08x)\n", dwWaitResult );
123 goto end_of_thread;
126 /* Close this handle as it's not needed anymore */
127 CloseHandle( lpThreadInfo->hStart );
128 lpThreadInfo->hStart = 0;
130 /* Wait until the lobby knows what it is */
131 dwWaitResult = WaitForSingleObject( lpThreadInfo->hSettingRead, INFINITE );
132 if( dwWaitResult == WAIT_TIMEOUT )
134 ERR( "App Read connection setting timeout fail (0x%08x)\n", dwWaitResult );
137 /* Close this handle as it's not needed anymore */
138 CloseHandle( lpThreadInfo->hSettingRead );
139 lpThreadInfo->hSettingRead = 0;
141 TRACE( "App created && initialized starting main message reception loop\n" );
143 for ( ;; )
145 MSG lobbyMsg;
146 GetMessageW( &lobbyMsg, 0, 0, 0 );
149 end_of_thread:
150 TRACE( "Msg thread exiting!\n" );
151 HeapFree( GetProcessHeap(), 0, lpThreadInfo );
153 return 0;
156 /* DP messaging stuff */
157 static HANDLE DP_MSG_BuildAndLinkReplyStruct( IDirectPlay2Impl* This,
158 LPDP_MSG_REPLY_STRUCT_LIST lpReplyStructList,
159 WORD wReplyCommandId );
160 static LPVOID DP_MSG_CleanReplyStruct( LPDP_MSG_REPLY_STRUCT_LIST lpReplyStructList,
161 LPVOID* lplpReplyMsg, LPDWORD lpdwMsgBodySize );
164 static
165 HANDLE DP_MSG_BuildAndLinkReplyStruct( IDirectPlay2Impl* This,
166 LPDP_MSG_REPLY_STRUCT_LIST lpReplyStructList, WORD wReplyCommandId )
168 lpReplyStructList->replyExpected.hReceipt = CreateEventW( NULL, FALSE, FALSE, NULL );
169 lpReplyStructList->replyExpected.wExpectedReply = wReplyCommandId;
170 lpReplyStructList->replyExpected.lpReplyMsg = NULL;
171 lpReplyStructList->replyExpected.dwMsgBodySize = 0;
173 /* Insert into the message queue while locked */
174 EnterCriticalSection( &This->unk->DP_lock );
175 DPQ_INSERT( This->dp2->replysExpected, lpReplyStructList, replysExpected );
176 LeaveCriticalSection( &This->unk->DP_lock );
178 return lpReplyStructList->replyExpected.hReceipt;
181 static
182 LPVOID DP_MSG_CleanReplyStruct( LPDP_MSG_REPLY_STRUCT_LIST lpReplyStructList,
183 LPVOID* lplpReplyMsg, LPDWORD lpdwMsgBodySize )
185 CloseHandle( lpReplyStructList->replyExpected.hReceipt );
187 *lplpReplyMsg = lpReplyStructList->replyExpected.lpReplyMsg;
188 *lpdwMsgBodySize = lpReplyStructList->replyExpected.dwMsgBodySize;
190 return lpReplyStructList->replyExpected.lpReplyMsg;
193 HRESULT DP_MSG_SendRequestPlayerId( IDirectPlay2AImpl* This, DWORD dwFlags,
194 LPDPID lpdpidAllocatedId )
196 LPVOID lpMsg;
197 LPDPMSG_REQUESTNEWPLAYERID lpMsgBody;
198 DWORD dwMsgSize;
199 HRESULT hr = DP_OK;
201 dwMsgSize = This->dp2->spData.dwSPHeaderSize + sizeof( *lpMsgBody );
203 lpMsg = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, dwMsgSize );
205 lpMsgBody = (LPDPMSG_REQUESTNEWPLAYERID)( (BYTE*)lpMsg +
206 This->dp2->spData.dwSPHeaderSize );
208 /* Compose dplay message envelope */
209 lpMsgBody->envelope.dwMagic = DPMSGMAGIC_DPLAYMSG;
210 lpMsgBody->envelope.wCommandId = DPMSGCMD_REQUESTNEWPLAYERID;
211 lpMsgBody->envelope.wVersion = DPMSGVER_DP6;
213 /* Compose the body of the message */
214 lpMsgBody->dwFlags = dwFlags;
216 /* Send the message */
218 DPSP_SENDDATA data;
220 data.dwFlags = DPSEND_GUARANTEED;
221 data.idPlayerTo = 0; /* Name server */
222 data.idPlayerFrom = 0; /* Sending from DP */
223 data.lpMessage = lpMsg;
224 data.dwMessageSize = dwMsgSize;
225 data.bSystemMessage = TRUE; /* Allow reply to be sent */
226 data.lpISP = This->dp2->spData.lpISP;
228 TRACE( "Asking for player id w/ dwFlags 0x%08x\n",
229 lpMsgBody->dwFlags );
231 DP_MSG_ExpectReply( This, &data, DPMSG_DEFAULT_WAIT_TIME, DPMSGCMD_NEWPLAYERIDREPLY,
232 &lpMsg, &dwMsgSize );
235 /* Need to examine the data and extract the new player id */
236 if( SUCCEEDED(hr) )
238 LPCDPMSG_NEWPLAYERIDREPLY lpcReply;
240 lpcReply = lpMsg;
242 *lpdpidAllocatedId = lpcReply->dpidNewPlayerId;
244 TRACE( "Received reply for id = 0x%08x\n", lpcReply->dpidNewPlayerId );
246 /* FIXME: I think that the rest of the message has something to do
247 * with remote data for the player that perhaps I need to setup.
248 * However, with the information that is passed, all that it could
249 * be used for is a standardized initialization value, which I'm
250 * guessing we can do without. Unless the message content is the same
251 * for several different messages?
254 HeapFree( GetProcessHeap(), 0, lpMsg );
257 return hr;
260 HRESULT DP_MSG_ForwardPlayerCreation( IDirectPlay2AImpl* This, DPID dpidServer )
262 LPVOID lpMsg;
263 LPDPMSG_FORWARDADDPLAYER lpMsgBody;
264 DWORD dwMsgSize;
265 HRESULT hr = DP_OK;
267 dwMsgSize = This->dp2->spData.dwSPHeaderSize + sizeof( *lpMsgBody );
269 lpMsg = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, dwMsgSize );
271 lpMsgBody = (LPDPMSG_FORWARDADDPLAYER)( (BYTE*)lpMsg +
272 This->dp2->spData.dwSPHeaderSize );
274 /* Compose dplay message envelope */
275 lpMsgBody->envelope.dwMagic = DPMSGMAGIC_DPLAYMSG;
276 lpMsgBody->envelope.wCommandId = DPMSGCMD_FORWARDADDPLAYER;
277 lpMsgBody->envelope.wVersion = DPMSGVER_DP6;
279 #if 0
281 LPBYTE lpPData;
282 DWORD dwDataSize;
284 /* SP Player remote data needs to be propagated at some point - is this the point? */
285 IDirectPlaySP_GetSPPlayerData( This->dp2->spData.lpISP, 0, &lpPData, &dwDataSize, DPSET_REMOTE );
287 ERR( "Player Data size is 0x%08lx\n"
288 "[%02x%02x %02x%02x %02x%02x %02x%02x %02x%02x %02x%02x %02x%02x %02x%02x]\n"
289 "[%02x%02x %02x%02x %02x%02x %02x%02x %02x%02x %02x%02x %02x%02x %02x%02x]\n",
291 dwDataSize,
292 lpPData[0], lpPData[1], lpPData[2], lpPData[3], lpPData[4],
293 lpPData[5], lpPData[6], lpPData[7], lpPData[8], lpPData[9],
294 lpPData[10], lpPData[11], lpPData[12], lpPData[13], lpPData[14],
295 lpPData[15], lpPData[16], lpPData[17], lpPData[18], lpPData[19],
296 lpPData[20], lpPData[21], lpPData[22], lpPData[23], lpPData[24],
297 lpPData[25], lpPData[26], lpPData[27], lpPData[28], lpPData[29],
298 lpPData[30], lpPData[31]
300 DebugBreak();
302 #endif
304 /* Compose body of message */
305 lpMsgBody->dpidAppServer = dpidServer;
306 lpMsgBody->unknown2[0] = 0x0;
307 lpMsgBody->unknown2[1] = 0x1c;
308 lpMsgBody->unknown2[2] = 0x6c;
309 lpMsgBody->unknown2[3] = 0x50;
310 lpMsgBody->unknown2[4] = 0x9;
312 lpMsgBody->dpidAppServer2 = dpidServer;
313 lpMsgBody->unknown3[0] = 0x0;
314 lpMsgBody->unknown3[0] = 0x0;
315 lpMsgBody->unknown3[0] = 0x20;
316 lpMsgBody->unknown3[0] = 0x0;
317 lpMsgBody->unknown3[0] = 0x0;
319 lpMsgBody->dpidAppServer3 = dpidServer;
320 lpMsgBody->unknown4[0] = 0x30;
321 lpMsgBody->unknown4[1] = 0xb;
322 lpMsgBody->unknown4[2] = 0x0;
324 lpMsgBody->unknown4[3] = NS_GetNsMagic( This->dp2->lpNameServerData ) -
325 0x02000000;
326 TRACE( "Setting first magic to 0x%08x\n", lpMsgBody->unknown4[3] );
328 lpMsgBody->unknown4[4] = 0x0;
329 lpMsgBody->unknown4[5] = 0x0;
330 lpMsgBody->unknown4[6] = 0x0;
332 lpMsgBody->unknown4[7] = NS_GetNsMagic( This->dp2->lpNameServerData );
333 TRACE( "Setting second magic to 0x%08x\n", lpMsgBody->unknown4[7] );
335 lpMsgBody->unknown4[8] = 0x0;
336 lpMsgBody->unknown4[9] = 0x0;
337 lpMsgBody->unknown4[10] = 0x0;
338 lpMsgBody->unknown4[11] = 0x0;
340 lpMsgBody->unknown5[0] = 0x0;
341 lpMsgBody->unknown5[1] = 0x0;
343 /* Send the message */
345 DPSP_SENDDATA data;
347 data.dwFlags = DPSEND_GUARANTEED;
348 data.idPlayerTo = 0; /* Name server */
349 data.idPlayerFrom = dpidServer; /* Sending from session server */
350 data.lpMessage = lpMsg;
351 data.dwMessageSize = dwMsgSize;
352 data.bSystemMessage = TRUE; /* Allow reply to be sent */
353 data.lpISP = This->dp2->spData.lpISP;
355 TRACE( "Sending forward player request with 0x%08x\n", dpidServer );
357 lpMsg = DP_MSG_ExpectReply( This, &data,
358 DPMSG_WAIT_60_SECS,
359 DPMSGCMD_GETNAMETABLEREPLY,
360 &lpMsg, &dwMsgSize );
363 /* Need to examine the data and extract the new player id */
364 if( lpMsg != NULL )
366 FIXME( "Name Table reply received: stub\n" );
369 return hr;
372 /* Queue up a structure indicating that we want a reply of type wReplyCommandId. DPlay does
373 * not seem to offer any way of uniquely differentiating between replies of the same type
374 * relative to the request sent. There is an implicit assumption that there will be no
375 * ordering issues on sends and receives from the opposite machine. No wonder MS is not
376 * a networking company.
378 static
379 LPVOID DP_MSG_ExpectReply( IDirectPlay2AImpl* This, LPDPSP_SENDDATA lpData,
380 DWORD dwWaitTime, WORD wReplyCommandId,
381 LPVOID* lplpReplyMsg, LPDWORD lpdwMsgBodySize )
383 HRESULT hr;
384 HANDLE hMsgReceipt;
385 DP_MSG_REPLY_STRUCT_LIST replyStructList;
386 DWORD dwWaitReturn;
388 /* Setup for receipt */
389 hMsgReceipt = DP_MSG_BuildAndLinkReplyStruct( This, &replyStructList,
390 wReplyCommandId );
392 TRACE( "Sending msg and expecting cmd %u in reply within %u ticks\n",
393 wReplyCommandId, dwWaitTime );
394 hr = (*This->dp2->spData.lpCB->Send)( lpData );
396 if( FAILED(hr) )
398 ERR( "Send failed: %s\n", DPLAYX_HresultToString( hr ) );
399 return NULL;
402 /* The reply message will trigger the hMsgReceipt event effectively switching
403 * control back to this thread. See DP_MSG_ReplyReceived.
405 dwWaitReturn = WaitForSingleObject( hMsgReceipt, dwWaitTime );
406 if( dwWaitReturn != WAIT_OBJECT_0 )
408 ERR( "Wait failed 0x%08x\n", dwWaitReturn );
409 return NULL;
412 /* Clean Up */
413 return DP_MSG_CleanReplyStruct( &replyStructList, lplpReplyMsg, lpdwMsgBodySize );
416 /* Determine if there is a matching request for this incoming message and then copy
417 * all important data. It is quite silly to have to copy the message, but the documents
418 * indicate that a copy is taken. Silly really.
420 void DP_MSG_ReplyReceived( IDirectPlay2AImpl* This, WORD wCommandId,
421 LPCVOID lpcMsgBody, DWORD dwMsgBodySize )
423 LPDP_MSG_REPLY_STRUCT_LIST lpReplyList;
425 #if 0
426 if( wCommandId == DPMSGCMD_FORWARDADDPLAYER )
428 DebugBreak();
430 #endif
432 /* Find, and immediately remove (to avoid double triggering), the appropriate entry. Call locked to
433 * avoid problems.
435 EnterCriticalSection( &This->unk->DP_lock );
436 DPQ_REMOVE_ENTRY( This->dp2->replysExpected, replysExpected, replyExpected.wExpectedReply,
437 ==, wCommandId, lpReplyList );
438 LeaveCriticalSection( &This->unk->DP_lock );
440 if( lpReplyList != NULL )
442 lpReplyList->replyExpected.dwMsgBodySize = dwMsgBodySize;
443 lpReplyList->replyExpected.lpReplyMsg = HeapAlloc( GetProcessHeap(),
444 HEAP_ZERO_MEMORY,
445 dwMsgBodySize );
446 CopyMemory( lpReplyList->replyExpected.lpReplyMsg,
447 lpcMsgBody, dwMsgBodySize );
449 /* Signal the thread which sent the message that it has a reply */
450 SetEvent( lpReplyList->replyExpected.hReceipt );
452 else
454 ERR( "No receipt event set - only expecting in reply mode\n" );
455 DebugBreak();
459 void DP_MSG_ToSelf( IDirectPlay2AImpl* This, DPID dpidSelf )
461 LPVOID lpMsg;
462 LPDPMSG_SENDENVELOPE lpMsgBody;
463 DWORD dwMsgSize;
465 dwMsgSize = This->dp2->spData.dwSPHeaderSize + sizeof( *lpMsgBody );
467 lpMsg = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, dwMsgSize );
469 lpMsgBody = (LPDPMSG_SENDENVELOPE)( (BYTE*)lpMsg +
470 This->dp2->spData.dwSPHeaderSize );
472 /* Compose dplay message envelope */
473 lpMsgBody->dwMagic = DPMSGMAGIC_DPLAYMSG;
474 lpMsgBody->wCommandId = DPMSGCMD_JUSTENVELOPE;
475 lpMsgBody->wVersion = DPMSGVER_DP6;
477 /* Send the message to ourselves */
479 DPSP_SENDDATA data;
481 data.dwFlags = 0;
482 data.idPlayerTo = dpidSelf; /* Sending to session server */
483 data.idPlayerFrom = 0; /* Sending from session server */
484 data.lpMessage = lpMsg;
485 data.dwMessageSize = dwMsgSize;
486 data.bSystemMessage = TRUE; /* Allow reply to be sent */
487 data.lpISP = This->dp2->spData.lpISP;
489 lpMsg = DP_MSG_ExpectReply( This, &data,
490 DPMSG_WAIT_5_SECS,
491 DPMSGCMD_JUSTENVELOPE,
492 &lpMsg, &dwMsgSize );
496 void DP_MSG_ErrorReceived( IDirectPlay2AImpl* This, WORD wCommandId,
497 LPCVOID lpMsgBody, DWORD dwMsgBodySize )
499 LPCDPMSG_FORWARDADDPLAYERNACK lpcErrorMsg;
501 lpcErrorMsg = lpMsgBody;
503 ERR( "Received error message %u. Error is %s\n",
504 wCommandId, DPLAYX_HresultToString( lpcErrorMsg->errorCode) );
505 DebugBreak();