Fixed some issues found by winapi_check.
[wine] / dlls / dplayx / name_server.c
1 /* DPLAYX.DLL name server implementation
2  *
3  * Copyright 2000 - Peter Hunnisett
4  *
5  * <presently under construction - contact hunnise@nortelnetworks.com>
6  *
7  */
8  
9 /* NOTE: Methods with the NS_ prefix are name server methods */
10
11 #include "winbase.h"
12 #include "debugtools.h"
13
14 #include "dplayx_global.h"
15 #include "name_server.h"
16
17 /* FIXME: Need to aquire the interface semaphore before didlling data */
18
19 DEFAULT_DEBUG_CHANNEL(dplay);
20
21 /* NS specific structures */
22 typedef struct tagNSCacheData
23 {
24   struct tagNSCacheData* next;
25
26   LPDPSESSIONDESC2 data;
27
28 } NSCacheData, *lpNSCacheData;
29
30 typedef struct tagNSCache
31 {
32   lpNSCacheData present; /* keep track of what is to be looked at */
33   lpNSCacheData first; 
34 } NSCache, *lpNSCache;
35
36 /* Local Prototypes */
37 static void NS_InvalidateSessionCache( lpNSCache lpCache );
38
39
40 /* Name Server functions 
41  * --------------------- 
42  */
43 void NS_SetLocalComputerAsNameServer( LPCDPSESSIONDESC2 lpsd )
44 {
45   DPLAYX_SetLocalSession( lpsd );
46 }
47
48 /* This function is responsible for sending a request for all other known
49    nameservers to send us what sessions they have registered locally
50  */
51 void NS_SendSessionRequestBroadcast( LPVOID lpNSInfo )
52 {
53   UINT index = 0;
54   lpNSCache lpCache = (lpNSCache)lpNSInfo;
55   LPDPSESSIONDESC2 lpTmp = NULL; 
56
57   /* Invalidate the session cache for the interface */
58   NS_InvalidateSessionCache( lpCache );
59  
60   /* Add the local known sessions to the cache */
61   if( ( lpTmp = DPLAYX_CopyAndAllocateLocalSession( &index ) ) != NULL )
62   {
63     lpCache->first = (lpNSCacheData)HeapAlloc( GetProcessHeap(), 
64                                                HEAP_ZERO_MEMORY,
65                                                sizeof( *(lpCache->first) ) );
66     lpCache->first->data = lpTmp;
67     lpCache->first->next = NULL;
68     lpCache->present = lpCache->first;
69
70     while( ( lpTmp = DPLAYX_CopyAndAllocateLocalSession( &index ) ) != NULL )
71     {
72       lpCache->present->next = (lpNSCacheData)HeapAlloc( GetProcessHeap(), 
73                                                          HEAP_ZERO_MEMORY,
74                                                          sizeof( *(lpCache->present) ) ); 
75       lpCache->present = lpCache->present->next;
76       lpCache->present->data = lpTmp;
77       lpCache->present->next = NULL;
78     }
79
80     lpCache->present = lpCache->first;
81   }
82
83   /* Send out requests for matching sessions to all other known computers */
84   FIXME( ": no remote requests sent\n" );
85   /* FIXME - how to handle responses to messages anyways? */
86 }
87
88 /* Render all data in a session cache invalid */
89 static void NS_InvalidateSessionCache( lpNSCache lpCache )
90 {
91   if( lpCache == NULL )
92   {
93     ERR( ": invalidate non existant cache\n" );
94     return;
95   }
96
97   /* Remove everything from the cache */
98   while( lpCache->first )
99   {
100     lpCache->present = lpCache->first;
101     lpCache->first = lpCache->first->next; 
102     HeapFree( GetProcessHeap(), 0, lpCache->present );
103   }
104
105   /* NULL out the cache pointers */
106   lpCache->present = NULL;
107   lpCache->first   = NULL;
108 }
109
110 /* Create and initialize a session cache */
111 BOOL NS_InitializeSessionCache( LPVOID* lplpNSInfo )
112 {
113   lpNSCache lpCache = (lpNSCache)HeapAlloc( GetProcessHeap(),
114                                             HEAP_ZERO_MEMORY,
115                                             sizeof( *lpCache ) );
116
117   *lplpNSInfo = lpCache;
118
119   if( lpCache == NULL )
120   {
121     return FALSE;
122   }
123
124   lpCache->first = lpCache->present = NULL;
125
126   return TRUE;
127 }
128
129 /* Delete a session cache */
130 void NS_DeleteSessionCache( LPVOID lpNSInfo )
131 {
132   NS_InvalidateSessionCache( (lpNSCache)lpNSInfo );
133 }
134
135 /* Reinitialize the present pointer for this cache */
136 void NS_ResetSessionEnumeration( LPVOID lpNSInfo )
137 {
138  
139   ((lpNSCache)lpNSInfo)->present = ((lpNSCache)lpNSInfo)->first;
140 }
141
142 LPDPSESSIONDESC2 NS_WalkSessions( LPVOID lpNSInfo )
143 {
144   LPDPSESSIONDESC2 lpSessionDesc;
145   lpNSCache lpCache = (lpNSCache)lpNSInfo;
146
147   /* Test for end of the list */ 
148   if( lpCache->present == NULL )
149   {
150     return NULL;
151   }
152
153   lpSessionDesc = lpCache->present->data;
154
155   /* Advance tracking pointer */
156   lpCache->present = lpCache->present->next;
157
158   return lpSessionDesc;
159 }