[AMSTREAM] We don't need to define WIDL_C_INLINE_WRAPPERS here anymore.
[reactos.git] / dll / directx / wine / dplayx / name_server.c
1 /* DPLAYX.DLL name server implementation
2 *
3 * Copyright 2000-2001 - Peter Hunnisett
4 *
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.
9 *
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.
14 *
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
18 */
19
20 /* NOTE: Methods with the NS_ prefix are name server methods */
21
22 #include "dplayx_global.h"
23
24 #include <mmsystem.h>
25
26 /* FIXME: Need to create a crit section, store and use it */
27
28 /* NS specific structures */
29 struct NSCacheData
30 {
31 DPQ_ENTRY(NSCacheData) next;
32
33 DWORD dwTime; /* Time at which data was last known valid */
34 LPDPSESSIONDESC2 data;
35
36 LPVOID lpNSAddrHdr;
37
38 };
39 typedef struct NSCacheData NSCacheData, *lpNSCacheData;
40
41 struct NSCache
42 {
43 lpNSCacheData present; /* keep track of what is to be looked at when walking */
44
45 DPQ_HEAD(NSCacheData) first;
46
47 BOOL bNsIsLocal;
48 LPVOID lpLocalAddrHdr; /* FIXME: Not yet used */
49 LPVOID lpRemoteAddrHdr; /* FIXME: Not yet used */
50 };
51 typedef struct NSCache NSCache, *lpNSCache;
52
53 /* Function prototypes */
54 static DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData );
55
56 /* Name Server functions
57 * ---------------------
58 */
59 void NS_SetLocalComputerAsNameServer( LPCDPSESSIONDESC2 lpsd, LPVOID lpNSInfo )
60 {
61 lpNSCache lpCache = (lpNSCache)lpNSInfo;
62
63 lpCache->bNsIsLocal = TRUE;
64 }
65
66 static DPQ_DECL_COMPARECB( cbUglyPig, GUID )
67 {
68 return IsEqualGUID( elem1, elem2 );
69 }
70
71 /* Store the given NS remote address for future reference */
72 void NS_AddRemoteComputerAsNameServer( LPCVOID lpcNSAddrHdr,
73 DWORD dwHdrSize,
74 LPCDPMSG_ENUMSESSIONSREPLY lpcMsg,
75 LPVOID lpNSInfo )
76 {
77 DWORD len;
78 lpNSCache lpCache = (lpNSCache)lpNSInfo;
79 lpNSCacheData lpCacheNode;
80
81 TRACE( "%p, %p, %p\n", lpcNSAddrHdr, lpcMsg, lpNSInfo );
82
83 /* See if we can find this session. If we can, remove it as it's a dup */
84 DPQ_REMOVE_ENTRY_CB( lpCache->first, next, data->guidInstance, cbUglyPig,
85 lpcMsg->sd.guidInstance, lpCacheNode );
86
87 if( lpCacheNode != NULL )
88 {
89 TRACE( "Duplicate session entry for %s removed - updated version kept\n",
90 debugstr_guid( &lpCacheNode->data->guidInstance ) );
91 cbDeleteNSNodeFromHeap( lpCacheNode );
92 }
93
94 /* Add this to the list */
95 lpCacheNode = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *lpCacheNode ) );
96
97 if( lpCacheNode == NULL )
98 {
99 ERR( "no memory for NS node\n" );
100 return;
101 }
102
103 lpCacheNode->lpNSAddrHdr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
104 dwHdrSize );
105 CopyMemory( lpCacheNode->lpNSAddrHdr, lpcNSAddrHdr, dwHdrSize );
106
107 lpCacheNode->data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *(lpCacheNode->data) ) );
108
109 if( lpCacheNode->data == NULL )
110 {
111 ERR( "no memory for SESSIONDESC2\n" );
112 HeapFree( GetProcessHeap(), 0, lpCacheNode );
113 return;
114 }
115
116 *lpCacheNode->data = lpcMsg->sd;
117 len = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)(lpcMsg+1), -1, NULL, 0, NULL, NULL );
118 if ((lpCacheNode->data->u1.lpszSessionNameA = HeapAlloc( GetProcessHeap(), 0, len )))
119 {
120 WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)(lpcMsg+1), -1,
121 lpCacheNode->data->u1.lpszSessionNameA, len, NULL, NULL );
122 }
123
124 lpCacheNode->dwTime = timeGetTime();
125
126 DPQ_INSERT(lpCache->first, lpCacheNode, next );
127
128 lpCache->present = lpCacheNode;
129
130 /* Use this message as an opportunity to weed out any old sessions so
131 * that we don't enum them again
132 */
133 NS_PruneSessionCache( lpNSInfo );
134 }
135
136 LPVOID NS_GetNSAddr( LPVOID lpNSInfo )
137 {
138 lpNSCache lpCache = (lpNSCache)lpNSInfo;
139
140 FIXME( ":quick stub\n" );
141
142 /* Ok. Cheat and don't search for the correct stuff just take the first.
143 * FIXME: In the future how are we to know what is _THE_ enum we used?
144 * This is going to have to go into dplay somehow. Perhaps it
145 * comes back with app server id for the join command! Oh... that
146 * must be it. That would make this method obsolete once that's
147 * in place.
148 */
149 #if 1
150 if ( lpCache->first.lpQHFirst )
151 return lpCache->first.lpQHFirst->lpNSAddrHdr;
152
153 return NULL;
154 #else
155 /* FIXME: Should convert over to this */
156 return lpCache->bNsIsLocal ? lpCache->lpLocalAddrHdr
157 : lpCache->lpRemoteAddrHdr;
158 #endif
159 }
160
161 /* Get the magic number associated with the Name Server */
162 DWORD NS_GetNsMagic( LPVOID lpNSInfo )
163 {
164 LPDWORD lpHdrInfo = NS_GetNSAddr( lpNSInfo );
165
166 return lpHdrInfo[1];
167 }
168
169 void NS_SetLocalAddr( LPVOID lpNSInfo, LPCVOID lpHdr, DWORD dwHdrSize )
170 {
171 lpNSCache lpCache = (lpNSCache)lpNSInfo;
172
173 lpCache->lpLocalAddrHdr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, dwHdrSize );
174
175 CopyMemory( lpCache->lpLocalAddrHdr, lpHdr, dwHdrSize );
176 }
177
178 /* This function is responsible for sending a request for all other known
179 nameservers to send us what sessions they have registered locally
180 */
181 HRESULT NS_SendSessionRequestBroadcast( LPCGUID lpcGuid,
182 DWORD dwFlags,
183 const SPINITDATA *lpSpData )
184
185 {
186 DPSP_ENUMSESSIONSDATA data;
187 LPDPMSG_ENUMSESSIONSREQUEST lpMsg;
188
189 TRACE( "enumerating for guid %s\n", debugstr_guid( lpcGuid ) );
190
191 /* Get the SP to deal with sending the EnumSessions request */
192 FIXME( ": not all data fields are correct\n" );
193
194 data.dwMessageSize = lpSpData->dwSPHeaderSize + sizeof( *lpMsg ); /*FIXME!*/
195 data.lpMessage = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
196 data.dwMessageSize );
197 data.lpISP = lpSpData->lpISP;
198 data.bReturnStatus = (dwFlags & DPENUMSESSIONS_RETURNSTATUS) != 0;
199
200
201 lpMsg = (LPDPMSG_ENUMSESSIONSREQUEST)(((BYTE*)data.lpMessage)+lpSpData->dwSPHeaderSize);
202
203 /* Setup EnumSession request message */
204 lpMsg->envelope.dwMagic = DPMSGMAGIC_DPLAYMSG;
205 lpMsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREQUEST;
206 lpMsg->envelope.wVersion = DPMSGVER_DP6;
207
208 lpMsg->dwPasswordSize = 0; /* FIXME: If enumerating passwords..? */
209 lpMsg->dwFlags = dwFlags;
210
211 lpMsg->guidApplication = *lpcGuid;
212
213 return (lpSpData->lpCB->EnumSessions)( &data );
214 }
215
216 /* Delete a name server node which has been allocated on the heap */
217 static DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData )
218 {
219 /* NOTE: This proc doesn't deal with the walking pointer */
220
221 /* FIXME: Memory leak on data (contained ptrs) */
222 HeapFree( GetProcessHeap(), 0, elem->data );
223 HeapFree( GetProcessHeap(), 0, elem->lpNSAddrHdr );
224 HeapFree( GetProcessHeap(), 0, elem );
225 }
226
227 /* Render all data in a session cache invalid */
228 void NS_InvalidateSessionCache( LPVOID lpNSInfo )
229 {
230 lpNSCache lpCache = (lpNSCache)lpNSInfo;
231
232 if( lpCache == NULL )
233 {
234 ERR( ": invalidate nonexistent cache\n" );
235 return;
236 }
237
238 DPQ_DELETEQ( lpCache->first, next, lpNSCacheData, cbDeleteNSNodeFromHeap );
239
240 /* NULL out the walking pointer */
241 lpCache->present = NULL;
242
243 lpCache->bNsIsLocal = FALSE;
244
245 }
246
247 /* Create and initialize a session cache */
248 BOOL NS_InitializeSessionCache( LPVOID* lplpNSInfo )
249 {
250 lpNSCache lpCache = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *lpCache ) );
251
252 *lplpNSInfo = lpCache;
253
254 if( lpCache == NULL )
255 {
256 return FALSE;
257 }
258
259 DPQ_INIT(lpCache->first);
260 lpCache->present = NULL;
261
262 lpCache->bNsIsLocal = FALSE;
263
264 return TRUE;
265 }
266
267 /* Delete a session cache */
268 void NS_DeleteSessionCache( LPVOID lpNSInfo )
269 {
270 NS_InvalidateSessionCache( (lpNSCache)lpNSInfo );
271 }
272
273 /* Reinitialize the present pointer for this cache */
274 void NS_ResetSessionEnumeration( LPVOID lpNSInfo )
275 {
276 ((lpNSCache)lpNSInfo)->present = ((lpNSCache)lpNSInfo)->first.lpQHFirst;
277 }
278
279 LPDPSESSIONDESC2 NS_WalkSessions( LPVOID lpNSInfo )
280 {
281 LPDPSESSIONDESC2 lpSessionDesc;
282 lpNSCache lpCache = (lpNSCache)lpNSInfo;
283
284 /* FIXME: The pointers could disappear when walking if a prune happens */
285
286 /* Test for end of the list */
287 if( lpCache->present == NULL )
288 {
289 return NULL;
290 }
291
292 lpSessionDesc = lpCache->present->data;
293
294 /* Advance tracking pointer */
295 lpCache->present = lpCache->present->next.lpQNext;
296
297 return lpSessionDesc;
298 }
299
300 /* This method should check to see if there are any sessions which are
301 * older than the criteria. If so, just delete that information.
302 */
303 /* FIXME: This needs to be called by some periodic timer */
304 void NS_PruneSessionCache( LPVOID lpNSInfo )
305 {
306 lpNSCache lpCache = lpNSInfo;
307
308 const DWORD dwPresentTime = timeGetTime();
309 const DWORD dwPrunePeriod = DPMSG_WAIT_60_SECS; /* is 60 secs enough? */
310
311 /* This silly little algorithm is based on the fact we keep entries in
312 * the queue in a time based order. It also assumes that it is not possible
313 * to wrap around over yourself (which is not unreasonable).
314 * The if statements verify if the first entry in the queue is less
315 * than dwPrunePeriod old depending on the "clock" roll over.
316 */
317 for( ;; )
318 {
319 lpNSCacheData lpFirstData;
320
321 if( DPQ_IS_EMPTY(lpCache->first) )
322 {
323 /* Nothing to prune */
324 break;
325 }
326
327 /* Deal with time in a wrap around safe manner - unsigned arithmetic.
328 * Check the difference in time */
329 if( (dwPresentTime - (DPQ_FIRST(lpCache->first)->dwTime)) < dwPrunePeriod )
330 {
331 /* First entry has not expired yet; don't prune */
332 break;
333 }
334
335 lpFirstData = DPQ_FIRST(lpCache->first);
336 DPQ_REMOVE( lpCache->first, DPQ_FIRST(lpCache->first), next );
337 cbDeleteNSNodeFromHeap( lpFirstData );
338 }
339
340 }
341
342 /* NAME SERVER Message stuff */
343 void NS_ReplyToEnumSessionsRequest( const void *lpcMsg, void **lplpReplyData, DWORD *lpdwReplySize,
344 IDirectPlayImpl *lpDP )
345 {
346 LPDPMSG_ENUMSESSIONSREPLY rmsg;
347 DWORD dwVariableSize;
348 DWORD dwVariableLen;
349 /* LPCDPMSG_ENUMSESSIONSREQUEST msg = (LPDPMSG_ENUMSESSIONSREQUEST)lpcMsg; */
350
351 /* FIXME: Should handle ANSI or WIDECHAR input. Currently just ANSI input */
352 FIXME( ": few fixed + need to check request for response, might need UNICODE input ability.\n" );
353
354 dwVariableLen = MultiByteToWideChar( CP_ACP, 0,
355 lpDP->dp2->lpSessionDesc->u1.lpszSessionNameA,
356 -1, NULL, 0 );
357 dwVariableSize = dwVariableLen * sizeof( WCHAR );
358
359 *lpdwReplySize = lpDP->dp2->spData.dwSPHeaderSize +
360 sizeof( *rmsg ) + dwVariableSize;
361 *lplpReplyData = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
362 *lpdwReplySize );
363
364 rmsg = (LPDPMSG_ENUMSESSIONSREPLY)( (BYTE*)*lplpReplyData +
365 lpDP->dp2->spData.dwSPHeaderSize);
366
367 rmsg->envelope.dwMagic = DPMSGMAGIC_DPLAYMSG;
368 rmsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREPLY;
369 rmsg->envelope.wVersion = DPMSGVER_DP6;
370
371 CopyMemory( &rmsg->sd, lpDP->dp2->lpSessionDesc,
372 lpDP->dp2->lpSessionDesc->dwSize );
373 rmsg->dwUnknown = 0x0000005c;
374 MultiByteToWideChar( CP_ACP, 0, lpDP->dp2->lpSessionDesc->u1.lpszSessionNameA, -1,
375 (LPWSTR)(rmsg+1), dwVariableLen );
376 }