dplayx: Modified reply functions to save also the header of the reply
[wine/gsoc_dplay.git] / dlls / dplayx / dplayx_messages.c
blob02b74b36814b3e054a4804d6f7e36e6bc321e173
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"
30 #include "winnls.h"
32 #include "dplayx_messages.h"
33 #include "dplay_global.h"
34 #include "dplayx_global.h"
35 #include "name_server.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(dplay);
40 typedef struct tagMSGTHREADINFO
42 HANDLE hStart;
43 HANDLE hDeath;
44 HANDLE hSettingRead;
45 HANDLE hNotifyEvent;
46 } MSGTHREADINFO, *LPMSGTHREADINFO;
48 static DWORD CALLBACK DPL_MSG_ThreadMain( LPVOID lpContext );
49 static LPVOID DP_MSG_ExpectReply( IDirectPlay2AImpl* This, LPDPSP_SENDDATA data,
50 DWORD dwWaitTime, WORD wReplyCommandId,
51 LPVOID* lplpReplyHdr, LPVOID* lplpReplyMsg,
52 LPDWORD lpdwMsgBodySize );
53 static DWORD DP_MSG_FillSuperPackedPlayer( IDirectPlay2Impl* lpDP,
54 LPDPLAYI_SUPERPACKEDPLAYER lpPackedPlayer,
55 LPVOID lpData,
56 BOOL bIsGroup,
57 BOOL bAnsi );
58 static DWORD DP_MSG_FillPackedPlayer( IDirectPlay2Impl* lpDP,
59 LPDPLAYI_PACKEDPLAYER lpPackedPlayer,
60 LPVOID lpData,
61 BOOL bIsGroup,
62 BOOL bAnsi );
63 DWORD DP_CopyString( LPVOID destination, LPVOID source, BOOL bAnsi );
66 /* Create the message reception thread to allow the application to receive
67 * asynchronous message reception
69 DWORD CreateLobbyMessageReceptionThread( HANDLE hNotifyEvent, HANDLE hStart,
70 HANDLE hDeath, HANDLE hConnRead )
72 DWORD dwMsgThreadId;
73 LPMSGTHREADINFO lpThreadInfo;
75 lpThreadInfo = HeapAlloc( GetProcessHeap(), 0, sizeof( *lpThreadInfo ) );
76 if( lpThreadInfo == NULL )
78 return 0;
81 /* The notify event may or may not exist. Depends if async comm or not */
82 if( hNotifyEvent &&
83 !DuplicateHandle( GetCurrentProcess(), hNotifyEvent,
84 GetCurrentProcess(), &lpThreadInfo->hNotifyEvent,
85 0, FALSE, DUPLICATE_SAME_ACCESS ) )
87 ERR( "Unable to duplicate event handle\n" );
88 goto error;
91 /* These 3 handles don't need to be duplicated because we don't keep a
92 * reference to them where they're created. They're created specifically
93 * for the message thread
95 lpThreadInfo->hStart = hStart;
96 lpThreadInfo->hDeath = hDeath;
97 lpThreadInfo->hSettingRead = hConnRead;
99 if( !CreateThread( NULL, /* Security attribs */
100 0, /* Stack */
101 DPL_MSG_ThreadMain, /* Msg reception function */
102 lpThreadInfo, /* Msg reception func parameter */
103 0, /* Flags */
104 &dwMsgThreadId /* Updated with thread id */
108 ERR( "Unable to create msg thread\n" );
109 goto error;
112 /* FIXME: Should I be closing the handle to the thread or does that
113 terminate the thread? */
115 return dwMsgThreadId;
117 error:
119 HeapFree( GetProcessHeap(), 0, lpThreadInfo );
121 return 0;
124 static DWORD CALLBACK DPL_MSG_ThreadMain( LPVOID lpContext )
126 LPMSGTHREADINFO lpThreadInfo = lpContext;
127 DWORD dwWaitResult;
129 TRACE( "Msg thread created. Waiting on app startup\n" );
131 /* Wait to ensure that the lobby application is started w/ 1 min timeout */
132 dwWaitResult = WaitForSingleObject( lpThreadInfo->hStart, 10000 /* 10 sec */ );
133 if( dwWaitResult == WAIT_TIMEOUT )
135 FIXME( "Should signal app/wait creation failure (0x%08x)\n", dwWaitResult );
136 goto end_of_thread;
139 /* Close this handle as it's not needed anymore */
140 CloseHandle( lpThreadInfo->hStart );
141 lpThreadInfo->hStart = 0;
143 /* Wait until the lobby knows what it is */
144 dwWaitResult = WaitForSingleObject( lpThreadInfo->hSettingRead, INFINITE );
145 if( dwWaitResult == WAIT_TIMEOUT )
147 ERR( "App Read connection setting timeout fail (0x%08x)\n", dwWaitResult );
150 /* Close this handle as it's not needed anymore */
151 CloseHandle( lpThreadInfo->hSettingRead );
152 lpThreadInfo->hSettingRead = 0;
154 TRACE( "App created && initialized starting main message reception loop\n" );
156 for ( ;; )
158 MSG lobbyMsg;
159 GetMessageW( &lobbyMsg, 0, 0, 0 );
162 end_of_thread:
163 TRACE( "Msg thread exiting!\n" );
164 HeapFree( GetProcessHeap(), 0, lpThreadInfo );
166 return 0;
169 /* DP messaging stuff */
170 static HANDLE DP_MSG_BuildAndLinkReplyStruct( IDirectPlay2Impl* This,
171 LPDP_MSG_REPLY_STRUCT_LIST lpReplyStructList,
172 WORD wReplyCommandId );
173 static LPVOID DP_MSG_CleanReplyStruct( LPDP_MSG_REPLY_STRUCT_LIST lpReplyStructList,
174 LPVOID* lplpReplyHdr, LPVOID* lplpReplyMsg,
175 LPDWORD lpdwMsgBodySize );
178 static
179 HANDLE DP_MSG_BuildAndLinkReplyStruct( IDirectPlay2Impl* This,
180 LPDP_MSG_REPLY_STRUCT_LIST lpReplyStructList, WORD wReplyCommandId )
182 lpReplyStructList->replyExpected.hReceipt = CreateEventW( NULL, FALSE, FALSE, NULL );
183 lpReplyStructList->replyExpected.wExpectedReply = wReplyCommandId;
184 lpReplyStructList->replyExpected.lpReplyHdr = NULL;
185 lpReplyStructList->replyExpected.lpReplyMsg = NULL;
186 lpReplyStructList->replyExpected.dwMsgBodySize = 0;
188 /* Insert into the message queue while locked */
189 EnterCriticalSection( &This->unk->DP_lock );
190 DPQ_INSERT( This->dp2->replysExpected, lpReplyStructList, replysExpected );
191 LeaveCriticalSection( &This->unk->DP_lock );
193 return lpReplyStructList->replyExpected.hReceipt;
196 static
197 LPVOID DP_MSG_CleanReplyStruct( LPDP_MSG_REPLY_STRUCT_LIST lpReplyStructList,
198 LPVOID* lplpReplyHdr, LPVOID* lplpReplyMsg,
199 LPDWORD lpdwMsgBodySize )
201 CloseHandle( lpReplyStructList->replyExpected.hReceipt );
203 if ( lplpReplyHdr )
205 *lplpReplyHdr = lpReplyStructList->replyExpected.lpReplyHdr;
208 *lplpReplyMsg = lpReplyStructList->replyExpected.lpReplyMsg;
209 *lpdwMsgBodySize = lpReplyStructList->replyExpected.dwMsgBodySize;
211 return lpReplyStructList->replyExpected.lpReplyMsg;
214 HRESULT DP_MSG_PackMessage( IDirectPlay2AImpl* This, LPVOID* lpMsg,
215 LPDWORD lpMessageSize )
217 LPVOID lpPacket;
218 LPDPSP_MSG_PACKET lpPacketBody;
220 TRACE( "Packing message with command 0x%x\n",
221 ( (LPDPSP_MSG_ENVELOPE)
222 (((LPBYTE) *lpMsg) + This->dp2->spData.dwSPHeaderSize))->wCommandId );
223 FIXME( "TODO: Segment the package if needed\n" );
225 lpPacket = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
226 *lpMessageSize + sizeof(DPSP_MSG_PACKET) );
227 lpPacketBody = (LPDPSP_MSG_PACKET)( (LPBYTE) lpPacket +
228 This->dp2->spData.dwSPHeaderSize );
230 /* TODO: When do we have to send a DPMSGCMD_PACKET2_DATA? */
231 lpPacketBody->envelope.dwMagic = DPMSG_SIGNATURE;
232 lpPacketBody->envelope.wCommandId = DPMSGCMD_PACKET;
233 lpPacketBody->envelope.wVersion = DX61AVERSION;
235 CoCreateGuid( &lpPacketBody->GuidMessage );
236 lpPacketBody->PacketIndex = 0;
237 lpPacketBody->DataSize = *lpMessageSize - This->dp2->spData.dwSPHeaderSize;
238 lpPacketBody->Offset = 0;
239 lpPacketBody->TotalPackets = 1;
240 lpPacketBody->MessageSize = *lpMessageSize - This->dp2->spData.dwSPHeaderSize;
241 lpPacketBody->PackedOffset = 0;
243 /* Copy the header of the original message */
244 CopyMemory( lpPacket, *lpMsg, This->dp2->spData.dwSPHeaderSize );
245 /* Copy the body of the original message */
246 CopyMemory( lpPacketBody + 1,
247 ((LPBYTE) *lpMsg) + This->dp2->spData.dwSPHeaderSize,
248 *lpMessageSize - This->dp2->spData.dwSPHeaderSize );
249 *lpMessageSize += sizeof(DPSP_MSG_PACKET);
251 HeapFree( GetProcessHeap(), 0, *lpMsg );
252 *lpMsg = lpPacket;
254 return DP_OK;
257 HRESULT DP_MSG_SendRequestPlayerId( IDirectPlay2AImpl* This, DWORD dwFlags,
258 LPDPID lpdpidAllocatedId )
260 LPVOID lpMsg;
261 LPDPSP_MSG_REQUESTPLAYERID lpMsgBody;
262 DWORD dwMsgSize;
263 HRESULT hr = DP_OK;
265 dwMsgSize = This->dp2->spData.dwSPHeaderSize + sizeof( *lpMsgBody );
267 lpMsg = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, dwMsgSize );
269 lpMsgBody = (LPDPSP_MSG_REQUESTPLAYERID)( (LPBYTE)lpMsg +
270 This->dp2->spData.dwSPHeaderSize );
272 /* Compose dplay message envelope */
273 lpMsgBody->envelope.dwMagic = DPMSG_SIGNATURE;
274 lpMsgBody->envelope.wCommandId = DPMSGCMD_REQUESTPLAYERID;
275 lpMsgBody->envelope.wVersion = DX61AVERSION;
277 /* Compose the body of the message */
278 lpMsgBody->Flags = dwFlags;
280 /* Send the message */
282 DPSP_SENDDATA data;
284 data.dwFlags = DPSEND_GUARANTEED;
285 data.idPlayerTo = 0; /* Name server */
286 data.idPlayerFrom = 0; /* Sending from DP */
287 data.lpMessage = lpMsg;
288 data.dwMessageSize = dwMsgSize;
289 data.bSystemMessage = TRUE; /* Allow reply to be sent */
290 data.lpISP = This->dp2->spData.lpISP;
292 TRACE( "Asking for player id w/ Flags 0x%08x\n", lpMsgBody->Flags );
294 lpMsg = DP_MSG_ExpectReply( This, &data,
295 DPMSG_RELIABLE_API_TIMER,
296 DPMSGCMD_REQUESTPLAYERREPLY,
297 NULL, &lpMsg, &dwMsgSize );
300 /* Examine reply */
301 if( lpMsg )
303 *lpdpidAllocatedId = ((LPCDPSP_MSG_REQUESTPLAYERREPLY) lpMsg)->ID;
304 TRACE( "Received new id 0x%08x\n", *lpdpidAllocatedId );
306 else
308 ERR( "Didn't receive reply\n" );
309 hr = DPERR_GENERIC;
312 HeapFree( GetProcessHeap(), 0, lpMsg );
313 return hr;
316 HRESULT DP_MSG_ForwardPlayerCreation( IDirectPlay2AImpl* This, DPID dpidServer )
318 LPVOID lpMsg;
319 LPDPSP_MSG_ADDFORWARDREQUEST lpMsgBody;
320 DWORD dwMsgSize;
321 HRESULT hr = DP_OK;
323 dwMsgSize = This->dp2->spData.dwSPHeaderSize + sizeof( *lpMsgBody );
325 lpMsg = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, dwMsgSize );
327 lpMsgBody = (LPDPSP_MSG_ADDFORWARDREQUEST)( (LPBYTE)lpMsg +
328 This->dp2->spData.dwSPHeaderSize );
330 /* Compose dplay message envelope */
331 lpMsgBody->envelope.dwMagic = DPMSG_SIGNATURE;
332 lpMsgBody->envelope.wCommandId = DPMSGCMD_ADDFORWARDREQUEST;
333 lpMsgBody->envelope.wVersion = DX61AVERSION;
335 /* Compose body of message */
336 FIXME( "TODO\n" );
338 /* Send the message */
340 DPSP_SENDDATA data;
342 data.dwFlags = DPSEND_GUARANTEED;
343 data.idPlayerTo = 0; /* Name server */
344 data.idPlayerFrom = dpidServer; /* Sending from session server */
345 data.lpMessage = lpMsg;
346 data.dwMessageSize = dwMsgSize;
347 data.bSystemMessage = TRUE; /* Allow reply to be sent */
348 data.lpISP = This->dp2->spData.lpISP;
350 TRACE( "Sending forward player request with 0x%08x\n", dpidServer );
352 lpMsg = DP_MSG_ExpectReply( This, &data,
353 DPMSG_RELIABLE_API_TIMER,
354 DPMSGCMD_ADDFORWARD,
355 NULL, &lpMsg, &dwMsgSize );
358 /* Need to examine the data and extract the new player id */
359 if( lpMsg != NULL )
361 FIXME( "Name Table reply received: stub\n" );
364 return hr;
367 /* Queue up a structure indicating that we want a reply of type wReplyCommandId. DPlay does
368 * not seem to offer any way of uniquely differentiating between replies of the same type
369 * relative to the request sent. There is an implicit assumption that there will be no
370 * ordering issues on sends and receives from the opposite machine. No wonder MS is not
371 * a networking company.
373 static
374 LPVOID DP_MSG_ExpectReply( IDirectPlay2AImpl* This, LPDPSP_SENDDATA lpData,
375 DWORD dwWaitTime, WORD wReplyCommandId,
376 LPVOID* lplpReplyHdr, LPVOID* lplpReplyMsg,
377 LPDWORD lpdwMsgBodySize )
379 HRESULT hr;
380 HANDLE hMsgReceipt;
381 DP_MSG_REPLY_STRUCT_LIST replyStructList;
382 DWORD dwWaitReturn;
383 WORD wCommandId;
385 /* Setup for receipt */
386 hMsgReceipt = DP_MSG_BuildAndLinkReplyStruct( This, &replyStructList,
387 wReplyCommandId );
389 wCommandId = ((LPCDPSP_MSG_ENVELOPE)
390 ( ((LPBYTE) lpData->lpMessage) +
391 This->dp2->spData.dwSPHeaderSize ))->wCommandId;
392 TRACE( "Sending cmd 0x%x and expecting cmd 0x%x in reply within %u ticks\n",
393 wCommandId, 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, lplpReplyHdr,
414 lplpReplyMsg, lpdwMsgBodySize );
417 /* Determine if there is a matching request for this incoming message and then copy
418 * all important data. It is quite silly to have to copy the message, but the documents
419 * indicate that a copy is taken. Silly really.
421 BOOL DP_MSG_ReplyReceived( IDirectPlay2AImpl* This, WORD wCommandId,
422 LPCVOID lpcMsgHdr, LPCVOID lpcMsgBody,
423 DWORD dwMsgBodySize )
425 LPDP_MSG_REPLY_STRUCT_LIST lpReplyList;
427 /* Find, and immediately remove (to avoid double triggering), the appropriate entry. Call locked to
428 * avoid problems.
430 EnterCriticalSection( &This->unk->DP_lock );
431 DPQ_REMOVE_ENTRY( This->dp2->replysExpected, replysExpected, replyExpected.wExpectedReply,
432 ==, wCommandId, lpReplyList );
433 LeaveCriticalSection( &This->unk->DP_lock );
435 if( lpReplyList == NULL )
437 TRACE( "No receipt event set for cmd 0x%x\n", wCommandId );
438 return FALSE;
441 lpReplyList->replyExpected.dwMsgBodySize = dwMsgBodySize;
442 /* TODO: Can we avoid theese allocations? */
443 lpReplyList->replyExpected.lpReplyHdr = HeapAlloc( GetProcessHeap(),
444 HEAP_ZERO_MEMORY,
445 This->dp2->spData.dwSPHeaderSize );
446 lpReplyList->replyExpected.lpReplyMsg = HeapAlloc( GetProcessHeap(),
447 HEAP_ZERO_MEMORY,
448 dwMsgBodySize );
449 CopyMemory( lpReplyList->replyExpected.lpReplyHdr,
450 lpcMsgHdr, This->dp2->spData.dwSPHeaderSize );
451 CopyMemory( lpReplyList->replyExpected.lpReplyMsg,
452 lpcMsgBody, dwMsgBodySize );
454 /* Signal the thread which sent the message that it has a reply */
455 SetEvent( lpReplyList->replyExpected.hReceipt );
457 return TRUE;
460 DWORD DP_CopyString( LPVOID destination, LPVOID source, BOOL bAnsi )
462 /* Copies an ASCII string (bAnsi=TRUE) or a wide string (bAnsi=FALSE)
463 * from source to the previously allocated destination buffer.
464 * The destination string will be always wide.
465 * If destination is NULL, doesn't perform the copy but the
466 * returned size is still correct.
468 * Returns: The size in bytes of the written string. */
470 DWORD dwLength;
472 if ( source == NULL )
473 return 0;
475 if ( bAnsi )
477 dwLength = MultiByteToWideChar( CP_ACP, 0, (LPSTR) source, -1, NULL, 0 );
478 if ( destination )
479 MultiByteToWideChar( CP_ACP, 0, (LPSTR) source, -1,
480 (LPWSTR) destination, dwLength );
482 else
484 dwLength = lstrlenW( (LPWSTR) source ) + 1;
485 if ( destination )
486 CopyMemory( destination, source, dwLength );
489 return dwLength * sizeof(WCHAR);
492 static DWORD DP_MSG_FillSuperPackedPlayer( IDirectPlay2Impl* lpDP,
493 LPDPLAYI_SUPERPACKEDPLAYER lpPackedPlayer,
494 LPVOID lpData,
495 BOOL bIsGroup,
496 BOOL bAnsi )
498 /* Fills lpPackedPlayer with the information in lpData.
499 * lpData can be a player or a group.
500 * If lpPackedPlayer is NULL just returns the size
501 * needed to pack the player.
503 * Returns: the size of the written data in bytes */
505 #define PLAYER_OR_GROUP( bIsGroup, lpData, field ) \
506 ( bIsGroup \
507 ? ((lpGroupData) lpData)->field \
508 : ((lpPlayerData) lpData)->field )
510 DWORD offset, length, size;
511 LPVOID playerSPData;
513 if ( lpPackedPlayer == NULL )
515 size = sizeof(DPLAYI_SUPERPACKEDPLAYER); /* Fixed data */
516 size += DP_CopyString( NULL, /* Short name */
517 PLAYER_OR_GROUP( bIsGroup, lpData, name.lpszShortName ),
518 bAnsi );
519 size += DP_CopyString( NULL, /* Long name */
520 PLAYER_OR_GROUP( bIsGroup, lpData, name.lpszShortName ),
521 bAnsi );
522 /* Player data length */
523 /* Player data */
524 /* SP data length */
525 /* SP data */
527 if ( bIsGroup )
529 /* Player count */
530 /* Player IDs */
531 /* ParentID */
532 /* Shortcut ID Count */
533 /* Shortcut IDs */
535 return size;
538 lpPackedPlayer->Size = sizeof(DPLAYI_SUPERPACKEDPLAYER);
540 lpPackedPlayer->Flags = 0x000000FF & PLAYER_OR_GROUP( bIsGroup, lpData, dwFlags );
541 lpPackedPlayer->ID = PLAYER_OR_GROUP( bIsGroup, lpData, dpid );
543 if ( lpPackedPlayer->Flags & DPLAYI_PLAYER_SYSPLAYER )
545 lpPackedPlayer->VersionOrSystemPlayerID = DX61AVERSION;
547 else
549 lpPackedPlayer->VersionOrSystemPlayerID =
550 DPQ_FIRST( lpDP->dp2->lpSysGroup->players )->lpPData->dpid;
553 offset = lpPackedPlayer->Size;
555 /* Short name */
556 length = DP_CopyString( ((LPBYTE) lpPackedPlayer) + offset,
557 PLAYER_OR_GROUP( bIsGroup, lpData, name.lpszShortName ),
558 bAnsi );
559 if ( length )
560 lpPackedPlayer->PlayerInfoMask |= SPP_SN;
561 offset += length;
563 /* Long name */
564 length = DP_CopyString( ((LPBYTE) lpPackedPlayer) + offset,
565 PLAYER_OR_GROUP( bIsGroup, lpData, name.lpszLongName ),
566 bAnsi );
567 if ( length )
568 lpPackedPlayer->PlayerInfoMask |= SPP_LN;
569 offset += length;
571 /* Player data */
572 length = PLAYER_OR_GROUP( bIsGroup, lpData, dwRemoteDataSize);
573 if ( length )
575 size = spp_get_optimum_size( length );
576 lpPackedPlayer->PlayerInfoMask |= spp_size2flags( size, SPP_PD_OFFSET );
577 CopyMemory( ((LPBYTE) lpPackedPlayer) + offset, &length, size );
578 offset += size;
580 CopyMemory( ((LPBYTE) lpPackedPlayer) + offset,
581 PLAYER_OR_GROUP( bIsGroup, lpData, lpRemoteData ),
582 length );
583 offset += length;
586 /* Service provider data */
587 IDirectPlaySP_GetSPPlayerData( lpDP->dp2->spData.lpISP, lpPackedPlayer->ID,
588 &playerSPData, &length, DPGET_REMOTE );
589 if ( length )
591 size = spp_get_optimum_size( length );
592 lpPackedPlayer->PlayerInfoMask |= spp_size2flags( size, SPP_SL_OFFSET );
593 CopyMemory( ((LPBYTE) lpPackedPlayer) + offset, &length, size );
594 offset += size;
595 CopyMemory( ((LPBYTE) lpPackedPlayer) + offset, playerSPData, length );
596 offset += length;
599 if ( bIsGroup )
601 /* Player IDs */
602 if( !DPQ_IS_EMPTY( ((lpGroupData)lpData)->players ) )
604 DWORD player_count, offset_PC;
605 lpPlayerList lpPList;
607 offset_PC = offset; /* Space for PlayerCount */
608 offset += 4;
609 player_count = 0;
611 /* PlayerIDs */
612 if ( (lpPList = DPQ_FIRST( ((lpGroupData)lpData)->players )) )
616 CopyMemory( ((LPBYTE) lpPackedPlayer) + offset,
617 &lpPList->lpPData->dpid,
618 sizeof(DPID) );
619 offset += sizeof(DPID);
620 player_count++;
622 while( (lpPList = DPQ_NEXT( lpPList->players )) );
625 /* PlayerCount */
626 size = spp_get_optimum_size( player_count );
627 lpPackedPlayer->PlayerInfoMask |= spp_size2flags( size, SPP_PC_OFFSET );
629 CopyMemory( ((LPBYTE) lpPackedPlayer) + offset_PC, &player_count, size );
632 /* ParentID */
633 if ( ((lpGroupData)lpData)->parent )
635 lpPackedPlayer->PlayerInfoMask |= SPP_PI;
636 CopyMemory( ((LPBYTE) lpPackedPlayer) + offset,
637 &((lpGroupData)lpData)->parent,
638 sizeof(DWORD) );
639 offset += sizeof(DWORD);
642 /* Shortcut IDs */
643 /*size = spp_get_optimum_size( ___ );
644 lpPackedPlayer->PlayerInfoMask |= spp_size2flags( size, SPP_SC_OFFSET );*/
645 FIXME( "TODO: Add shortcut IDs\n" );
648 return offset;
651 static DWORD DP_MSG_FillPackedPlayer( IDirectPlay2Impl* lpDP,
652 LPDPLAYI_PACKEDPLAYER lpPackedPlayer,
653 LPVOID lpData,
654 BOOL bIsGroup,
655 BOOL bAnsi )
657 /* Fills lpPackedPlayer with the information in lpData.
658 * lpData can be a player or a group.
659 * If lpPackedPlayer is NULL just returns the size
660 * needed to pack the player.
662 * Returns: the size of the written data in bytes */
664 LPVOID playerSPData;
665 DWORD offset;
667 if ( lpPackedPlayer == NULL )
669 DWORD size;
670 size = sizeof(DPLAYI_PACKEDPLAYER); /* Fixed data */
671 size += DP_CopyString( NULL, /* Short name */
672 PLAYER_OR_GROUP( bIsGroup, lpData, name.lpszShortName ),
673 bAnsi );
674 size += DP_CopyString( NULL, /* Long name */
675 PLAYER_OR_GROUP( bIsGroup, lpData, name.lpszLongName ),
676 bAnsi );
677 /* SP data */
678 /* Player data */
680 if ( bIsGroup )
682 /* Player IDs */
684 return size;
687 lpPackedPlayer->SystemPlayerID =
688 DPQ_FIRST( lpDP->dp2->lpSysGroup->players )->lpPData->dpid;
689 lpPackedPlayer->FixedSize = sizeof(DPLAYI_PACKEDPLAYER);
690 lpPackedPlayer->PlayerVersion = DX61AVERSION;
692 lpPackedPlayer->Flags = 0x000000FF & PLAYER_OR_GROUP( bIsGroup, lpData, dwFlags );
693 lpPackedPlayer->PlayerID = PLAYER_OR_GROUP( bIsGroup, lpData, dpid );
694 lpPackedPlayer->ParentID = ( bIsGroup
695 ? ((lpGroupData)lpData)->parent
696 : 0 );
698 offset = lpPackedPlayer->FixedSize;
700 /* Short name */
701 lpPackedPlayer->ShortNameLength =
702 DP_CopyString( ((LPBYTE) lpPackedPlayer) + offset,
703 PLAYER_OR_GROUP( bIsGroup, lpData, name.lpszShortName ),
704 bAnsi );
705 offset += lpPackedPlayer->ShortNameLength;
707 /* Long name */
708 lpPackedPlayer->LongNameLength =
709 DP_CopyString( ((LPBYTE) lpPackedPlayer) + offset,
710 PLAYER_OR_GROUP( bIsGroup, lpData, name.lpszLongName ),
711 bAnsi );
712 offset += lpPackedPlayer->LongNameLength;
714 /* Service provider data */
715 IDirectPlaySP_GetSPPlayerData( lpDP->dp2->spData.lpISP,
716 lpPackedPlayer->PlayerID,
717 &playerSPData,
718 &lpPackedPlayer->ServiceProviderDataSize,
719 DPGET_REMOTE );
720 if ( lpPackedPlayer->ServiceProviderDataSize )
722 CopyMemory( ((LPBYTE) lpPackedPlayer) + offset, playerSPData,
723 lpPackedPlayer->ServiceProviderDataSize );
724 offset += lpPackedPlayer->ServiceProviderDataSize;
727 /* Player data */
728 lpPackedPlayer->PlayerDataSize = PLAYER_OR_GROUP( bIsGroup, lpData, dwRemoteDataSize );
729 if ( lpPackedPlayer->PlayerDataSize )
731 CopyMemory( ((LPBYTE) lpPackedPlayer) + offset,
732 PLAYER_OR_GROUP( bIsGroup, lpData, lpRemoteData ),
733 lpPackedPlayer->PlayerDataSize );
734 offset += lpPackedPlayer->PlayerDataSize;
737 if ( bIsGroup )
739 lpPlayerList lpPList;
741 /* Player IDs */
742 lpPackedPlayer->NumberOfPlayers = 0;
744 if ( (lpPList = DPQ_FIRST( ((lpGroupData)lpData)->players )) )
748 CopyMemory( ((LPBYTE) lpPackedPlayer) + offset,
749 &lpPList->lpPData->dpid,
750 sizeof(DPID) );
751 offset += sizeof(DPID);
752 lpPackedPlayer->NumberOfPlayers++;
754 while( (lpPList = DPQ_NEXT( lpPList->players )) );
759 /* Total size */
760 lpPackedPlayer->Size = offset;
762 return lpPackedPlayer->Size;
765 void DP_MSG_ReplyToEnumPlayersRequest( IDirectPlay2Impl* lpDP,
766 LPVOID* lplpReply,
767 LPDWORD lpdwMsgSize )
769 /* Depending on the type of session, builds either a EnumPlayers
770 reply or a SuperEnumPlayers reply, and places it in lplpReply */
772 /* TODO: Find size to allocate *lplpReply
773 * Check if we're releasing that memory */
775 LPDPSP_MSG_ENUMPLAYERSREPLY lpReply; /* Also valid in the SUPER case */
776 lpPlayerList lpPList;
777 lpGroupList lpGList;
778 DWORD offset;
779 BOOL bAnsi = TRUE; /* FIXME: This needs to be in the DPLAY interface */
782 *lplpReply = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 1024 );
783 lpReply = (LPDPSP_MSG_ENUMPLAYERSREPLY)( (LPBYTE)(*lplpReply) +
784 lpDP->dp2->spData.dwSPHeaderSize );
786 lpReply->envelope.dwMagic = DPMSG_SIGNATURE;
787 lpReply->envelope.wVersion = DX61AVERSION;
789 if ( lpDP->dp2->lpSessionDesc->dwFlags & DPSESSION_CLIENTSERVER )
791 lpReply->envelope.wCommandId = DPMSGCMD_ENUMPLAYERSREPLY;
793 else
795 lpReply->envelope.wCommandId = DPMSGCMD_SUPERENUMPLAYERSREPLY;
799 /* Session description */
800 lpReply->DescriptionOffset = ( sizeof(DPSP_MSG_ENUMPLAYERSREPLY) -
801 sizeof(DPSESSIONDESC2) );
802 CopyMemory( &lpReply->DPSessionDesc, lpDP->dp2->lpSessionDesc,
803 sizeof(DPSESSIONDESC2) );
805 offset = sizeof(DPSP_MSG_ENUMPLAYERSREPLY);
807 /* Session name */
808 if ( lpDP->dp2->lpSessionDesc->lpszSessionName )
810 lpReply->NameOffset = offset;
811 offset += DP_CopyString( ((LPBYTE) lpReply) + offset,
812 lpDP->dp2->lpSessionDesc->lpszSessionName,
813 bAnsi );
816 /* Session password */
817 if ( lpDP->dp2->lpSessionDesc->lpszPassword )
819 lpReply->PasswordOffset = offset;
820 offset += DP_CopyString( ((LPBYTE) lpReply) + offset,
821 lpDP->dp2->lpSessionDesc->lpszPassword,
822 bAnsi );
825 /* Populate PlayerInfo list */
827 /* - Players */
828 if ( (lpPList = DPQ_FIRST( lpDP->dp2->lpSysGroup->players )) )
832 if ( lpReply->envelope.wCommandId == DPMSGCMD_SUPERENUMPLAYERSREPLY )
834 offset += DP_MSG_FillSuperPackedPlayer(
835 lpDP, (LPDPLAYI_SUPERPACKEDPLAYER)( ((LPBYTE)lpReply) + offset ),
836 lpPList->lpPData, FALSE, bAnsi );
838 else
840 offset += DP_MSG_FillPackedPlayer(
841 lpDP, (LPDPLAYI_PACKEDPLAYER)( ((LPBYTE)lpReply) + offset ),
842 lpPList->lpPData, FALSE, bAnsi );
845 lpReply->PlayerCount++;
847 while( (lpPList = DPQ_NEXT( lpPList->players )) );
850 /* - Groups */
851 if ( (lpGList = DPQ_FIRST( lpDP->dp2->lpSysGroup->groups )) )
855 if ( lpReply->envelope.wCommandId == DPMSGCMD_SUPERENUMPLAYERSREPLY )
857 offset += DP_MSG_FillSuperPackedPlayer(
858 lpDP, (LPDPLAYI_SUPERPACKEDPLAYER)( ((LPBYTE)lpReply) + offset ),
859 lpGList->lpGData, TRUE, bAnsi );
861 else
863 offset += DP_MSG_FillPackedPlayer(
864 lpDP, (LPDPLAYI_PACKEDPLAYER)( ((LPBYTE)lpReply) + offset ),
865 lpGList->lpGData, TRUE, bAnsi );
868 lpReply->GroupCount++;
870 while( (lpGList = DPQ_NEXT( lpGList->groups )) );
873 if ( lpReply->envelope.wCommandId == DPMSGCMD_SUPERENUMPLAYERSREPLY )
875 /* - Groups with shortcuts */
876 FIXME( "TODO: Add shortcut IDs\n" );
879 *lpdwMsgSize = lpDP->dp2->spData.dwSPHeaderSize + offset;