rpcrt4: Tests for CStdStubBuffer_Disconnect and a tidy up of the implementation.
[wine] / dlls / rpcrt4 / cstub.c
1 /*
2  * COM stub (CStdStubBuffer) implementation
3  *
4  * Copyright 2001 Ove Kåven, TransGaming Technologies
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winerror.h"
28 #include "excpt.h"
29
30 #include "objbase.h"
31 #include "rpcproxy.h"
32
33 #include "wine/debug.h"
34 #include "wine/exception.h"
35
36 #include "cpsf.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(ole);
39
40 #define STUB_HEADER(This) (((CInterfaceStubHeader*)((This)->lpVtbl))[-1])
41
42 static WINE_EXCEPTION_FILTER(stub_filter)
43 {
44     if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
45         return EXCEPTION_CONTINUE_SEARCH;
46     return EXCEPTION_EXECUTE_HANDLER;
47 }
48
49 HRESULT WINAPI CStdStubBuffer_Construct(REFIID riid,
50                                        LPUNKNOWN pUnkServer,
51                                        PCInterfaceName name,
52                                        CInterfaceStubVtbl *vtbl,
53                                        LPPSFACTORYBUFFER pPSFactory,
54                                        LPRPCSTUBBUFFER *ppStub)
55 {
56   CStdStubBuffer *This;
57   IUnknown *pvServer;
58   HRESULT r;
59   TRACE("(%p,%p,%p,%p) %s\n", pUnkServer, vtbl, pPSFactory, ppStub, name);
60   TRACE("iid=%s\n", debugstr_guid(vtbl->header.piid));
61   TRACE("vtbl=%p\n", &vtbl->Vtbl);
62
63   if (!IsEqualGUID(vtbl->header.piid, riid)) {
64     ERR("IID mismatch during stub creation\n");
65     return RPC_E_UNEXPECTED;
66   }
67
68   r = IUnknown_QueryInterface(pUnkServer, riid, (void**)&pvServer);
69   if(FAILED(r))
70     return r;
71
72   This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CStdStubBuffer));
73   if (!This) {
74     IUnknown_Release(pvServer);
75     return E_OUTOFMEMORY;
76   }
77
78   This->lpVtbl = &vtbl->Vtbl;
79   This->RefCount = 1;
80   This->pvServerObject = pvServer;
81   This->pPSFactory = pPSFactory;
82   *ppStub = (LPRPCSTUBBUFFER)This;
83
84   IPSFactoryBuffer_AddRef(pPSFactory);
85   return S_OK;
86 }
87
88 HRESULT WINAPI CStdStubBuffer_QueryInterface(LPRPCSTUBBUFFER iface,
89                                             REFIID riid,
90                                             LPVOID *obj)
91 {
92   CStdStubBuffer *This = (CStdStubBuffer *)iface;
93   TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
94
95   if (IsEqualGUID(&IID_IUnknown,riid) ||
96       IsEqualGUID(&IID_IRpcStubBuffer,riid)) {
97     *obj = This;
98     This->RefCount++;
99     return S_OK;
100   }
101   return E_NOINTERFACE;
102 }
103
104 ULONG WINAPI CStdStubBuffer_AddRef(LPRPCSTUBBUFFER iface)
105 {
106   CStdStubBuffer *This = (CStdStubBuffer *)iface;
107   TRACE("(%p)->AddRef()\n",This);
108   return ++(This->RefCount);
109 }
110
111 ULONG WINAPI NdrCStdStubBuffer_Release(LPRPCSTUBBUFFER iface,
112                                       LPPSFACTORYBUFFER pPSF)
113 {
114   CStdStubBuffer *This = (CStdStubBuffer *)iface;
115   TRACE("(%p)->Release()\n",This);
116
117   if (!--(This->RefCount)) {
118     IRpcStubBuffer_Disconnect(iface);
119     if(This->pPSFactory)
120         IPSFactoryBuffer_Release(This->pPSFactory);
121     HeapFree(GetProcessHeap(),0,This);
122     return 0;
123   }
124   return This->RefCount;
125 }
126
127 ULONG WINAPI NdrCStdStubBuffer2_Release(LPRPCSTUBBUFFER iface,
128                                         LPPSFACTORYBUFFER pPSF)
129 {
130     FIXME("Not implemented\n");
131     return 0;
132 }
133
134 HRESULT WINAPI CStdStubBuffer_Connect(LPRPCSTUBBUFFER iface,
135                                      LPUNKNOWN lpUnkServer)
136 {
137     CStdStubBuffer *This = (CStdStubBuffer *)iface;
138     HRESULT r;
139     IUnknown *new = NULL;
140
141     TRACE("(%p)->Connect(%p)\n",This,lpUnkServer);
142
143     r = IUnknown_QueryInterface(lpUnkServer, STUB_HEADER(This).piid, (void**)&new);
144     new = InterlockedExchangePointer((void**)&This->pvServerObject, new);
145     if(new)
146         IUnknown_Release(new);
147     return r;
148 }
149
150 void WINAPI CStdStubBuffer_Disconnect(LPRPCSTUBBUFFER iface)
151 {
152     CStdStubBuffer *This = (CStdStubBuffer *)iface;
153     IUnknown *old;
154     TRACE("(%p)->Disconnect()\n",This);
155
156     old = InterlockedExchangePointer((void**)&This->pvServerObject, NULL);
157
158     if(old)
159         IUnknown_Release(old);
160 }
161
162 HRESULT WINAPI CStdStubBuffer_Invoke(LPRPCSTUBBUFFER iface,
163                                     PRPCOLEMESSAGE pMsg,
164                                     LPRPCCHANNELBUFFER pChannel)
165 {
166   CStdStubBuffer *This = (CStdStubBuffer *)iface;
167   DWORD dwPhase = STUB_UNMARSHAL;
168   HRESULT hr = S_OK;
169
170   TRACE("(%p)->Invoke(%p,%p)\n",This,pMsg,pChannel);
171
172   __TRY
173   {
174     if (STUB_HEADER(This).pDispatchTable)
175       STUB_HEADER(This).pDispatchTable[pMsg->iMethod](iface, pChannel, (PRPC_MESSAGE)pMsg, &dwPhase);
176     else /* pure interpreted */
177       NdrStubCall2(iface, pChannel, (PRPC_MESSAGE)pMsg, &dwPhase);
178   }
179   __EXCEPT(stub_filter)
180   {
181     DWORD dwExceptionCode = GetExceptionCode();
182     WARN("a stub call failed with exception 0x%08lx (%ld)\n", dwExceptionCode, dwExceptionCode);
183     if (FAILED(dwExceptionCode))
184       hr = dwExceptionCode;
185     else
186       hr = HRESULT_FROM_WIN32(dwExceptionCode);
187   }
188   __ENDTRY
189
190   return hr;
191 }
192
193 LPRPCSTUBBUFFER WINAPI CStdStubBuffer_IsIIDSupported(LPRPCSTUBBUFFER iface,
194                                                     REFIID riid)
195 {
196   CStdStubBuffer *This = (CStdStubBuffer *)iface;
197   TRACE("(%p)->IsIIDSupported(%s)\n",This,debugstr_guid(riid));
198   return IsEqualGUID(STUB_HEADER(This).piid, riid) ? iface : NULL;
199 }
200
201 ULONG WINAPI CStdStubBuffer_CountRefs(LPRPCSTUBBUFFER iface)
202 {
203   CStdStubBuffer *This = (CStdStubBuffer *)iface;
204   TRACE("(%p)->CountRefs()\n",This);
205   return This->RefCount;
206 }
207
208 HRESULT WINAPI CStdStubBuffer_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,
209                                                        LPVOID *ppv)
210 {
211   CStdStubBuffer *This = (CStdStubBuffer *)iface;
212   TRACE("(%p)->DebugServerQueryInterface(%p)\n",This,ppv);
213   return S_OK;
214 }
215
216 void WINAPI CStdStubBuffer_DebugServerRelease(LPRPCSTUBBUFFER iface,
217                                              LPVOID pv)
218 {
219   CStdStubBuffer *This = (CStdStubBuffer *)iface;
220   TRACE("(%p)->DebugServerRelease(%p)\n",This,pv);
221 }
222
223 const IRpcStubBufferVtbl CStdStubBuffer_Vtbl =
224 {
225     CStdStubBuffer_QueryInterface,
226     CStdStubBuffer_AddRef,
227     NULL,
228     CStdStubBuffer_Connect,
229     CStdStubBuffer_Disconnect,
230     CStdStubBuffer_Invoke,
231     CStdStubBuffer_IsIIDSupported,
232     CStdStubBuffer_CountRefs,
233     CStdStubBuffer_DebugServerQueryInterface,
234     CStdStubBuffer_DebugServerRelease
235 };
236
237 const MIDL_SERVER_INFO *CStdStubBuffer_GetServerInfo(IRpcStubBuffer *iface)
238 {
239   CStdStubBuffer *This = (CStdStubBuffer *)iface;
240   return STUB_HEADER(This).pServerInfo;
241 }
242
243 /************************************************************************
244  *           NdrStubForwardingFunction [RPCRT4.@]
245  */
246 void __RPC_STUB NdrStubForwardingFunction( IRpcStubBuffer *This, IRpcChannelBuffer *pChannel,
247                                            PRPC_MESSAGE pMsg, DWORD *pdwStubPhase )
248 {
249     /* Once stub delegation is implemented, this should call
250        IRpcStubBuffer_Invoke on the stub's base interface.  The
251        IRpcStubBuffer for this interface is stored at (void**)This-1.
252        The pChannel and pMsg parameters are passed intact
253        (RPCOLEMESSAGE is basically a RPC_MESSAGE).  If Invoke returns
254        with a failure then an exception is raised (to see this, change
255        the return value in the test).
256
257     IRpcStubBuffer *base_this = *(IRpcStubBuffer**)((void**)This - 1);
258     HRESULT r = IRpcStubBuffer_Invoke(base_this, (RPCOLEMESSAGE*)pMsg, pChannel);
259     if(FAILED(r)) RpcRaiseException(r);
260     return;
261     */
262
263     FIXME("Not implemented\n");
264     return;
265 }