4 * Copyright 2002 Marcus Meissner
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
42 #include "wine/unicode.h"
43 #include "wine/winbase16.h"
44 #include "compobj_private.h"
47 #include "compobj_private.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
53 #define REQTYPE_REQUEST 0
54 typedef struct _wine_rpc_request_header
{
59 } wine_rpc_request_header
;
61 #define REQTYPE_RESPONSE 1
62 typedef struct _wine_rpc_response_header
{
66 } wine_rpc_response_header
;
68 #define REQSTATE_START 0
69 #define REQSTATE_REQ_QUEUED 1
70 #define REQSTATE_REQ_WAITING_FOR_REPLY 2
71 #define REQSTATE_REQ_GOT 3
72 #define REQSTATE_INVOKING 4
73 #define REQSTATE_RESP_QUEUED 5
74 #define REQSTATE_RESP_GOT 6
75 #define REQSTATE_DONE 6
77 typedef struct _wine_rpc_request
{
79 HANDLE hPipe
; /* temp copy of handle */
80 wine_rpc_request_header reqh
;
81 wine_rpc_response_header resph
;
85 static wine_rpc_request
**reqs
= NULL
;
86 static int nrofreqs
= 0;
88 /* This pipe is _thread_ based */
89 typedef struct _wine_pipe
{
90 wine_marshal_id mid
; /* target mid */
91 DWORD tid
; /* thread in which we execute */
96 CRITICAL_SECTION crit
;
99 static wine_pipe
*pipes
= NULL
;
100 static int nrofpipes
= 0;
102 typedef struct _PipeBuf
{
103 ICOM_VTABLE(IRpcChannelBuffer
) *lpVtbl
;
110 static HRESULT WINAPI
111 _xread(HANDLE hf
, LPVOID ptr
, DWORD size
) {
113 if (!ReadFile(hf
,ptr
,size
,&res
,NULL
)) {
114 FIXME("Failed to read from %p, le is %lx\n",hf
,GetLastError());
118 FIXME("Read only %ld of %ld bytes from %p.\n",res
,size
,hf
);
127 static int nrofreaders
= 0;
131 memset(states
,0,sizeof(states
));
132 for (i
=nrofreqs
;i
--;)
133 states
[reqs
[i
]->state
]++;
134 FIXME("%lx/%s/%d: rq %d, w %d, rg %d, rsq %d, rsg %d, d %d\n",
135 GetCurrentProcessId(),
138 states
[REQSTATE_REQ_QUEUED
],
139 states
[REQSTATE_REQ_WAITING_FOR_REPLY
],
140 states
[REQSTATE_REQ_GOT
],
141 states
[REQSTATE_RESP_QUEUED
],
142 states
[REQSTATE_RESP_GOT
],
143 states
[REQSTATE_DONE
]
150 static HRESULT WINAPI
151 _xwrite(HANDLE hf
, LPVOID ptr
, DWORD size
) {
153 if (!WriteFile(hf
,ptr
,size
,&res
,NULL
)) {
154 FIXME("Failed to write to %p, le is %lx\n",hf
,GetLastError());
158 FIXME("Wrote only %ld of %ld bytes to %p.\n",res
,size
,hf
);
164 static DWORD WINAPI
_StubReaderThread(LPVOID
);
167 PIPE_RegisterPipe(wine_marshal_id
*mid
, HANDLE hPipe
, BOOL startreader
) {
170 wine_pipe
*new_pipes
;
172 for (i
=0;i
<nrofpipes
;i
++)
173 if (pipes
[i
].mid
.processid
==mid
->processid
)
176 new_pipes
=(wine_pipe
*)HeapReAlloc(GetProcessHeap(),0,pipes
,sizeof(pipes
[0])*(nrofpipes
+1));
178 new_pipes
=(wine_pipe
*)HeapAlloc(GetProcessHeap(),0,sizeof(pipes
[0]));
179 if (!new_pipes
) return E_OUTOFMEMORY
;
181 sprintf(pipefn
,OLESTUBMGR
"_%08lx",mid
->processid
);
182 memcpy(&(pipes
[nrofpipes
].mid
),mid
,sizeof(*mid
));
183 pipes
[nrofpipes
].hPipe
= hPipe
;
184 InitializeCriticalSection(&(pipes
[nrofpipes
].crit
));
187 pipes
[nrofpipes
-1].hThread
= CreateThread(NULL
,0,_StubReaderThread
,(LPVOID
)(pipes
+(nrofpipes
-1)),0,&(pipes
[nrofpipes
-1].tid
));
189 pipes
[nrofpipes
-1].tid
= GetCurrentThreadId();
195 PIPE_FindByMID(wine_marshal_id
*mid
) {
197 for (i
=0;i
<nrofpipes
;i
++)
198 if ((pipes
[i
].mid
.processid
==mid
->processid
) &&
199 (GetCurrentThreadId()==pipes
[i
].tid
)
201 return pipes
[i
].hPipe
;
202 return INVALID_HANDLE_VALUE
;
206 PIPE_GetFromMID(wine_marshal_id
*mid
) {
208 for (i
=0;i
<nrofpipes
;i
++) {
209 if ((pipes
[i
].mid
.processid
==mid
->processid
) &&
210 (GetCurrentThreadId()==pipes
[i
].tid
)
218 RPC_GetRequest(wine_rpc_request
**req
) {
219 static int reqid
= 0xdeadbeef;
222 for (i
=0;i
<nrofreqs
;i
++) { /* try to reuse */
223 if (reqs
[i
]->state
== REQSTATE_DONE
) {
224 reqs
[i
]->reqh
.reqid
= reqid
++;
225 reqs
[i
]->resph
.reqid
= reqs
[i
]->reqh
.reqid
;
226 reqs
[i
]->hPipe
= INVALID_HANDLE_VALUE
;
228 reqs
[i
]->state
= REQSTATE_START
;
234 reqs
= (wine_rpc_request
**)HeapReAlloc(
238 sizeof(wine_rpc_request
*)*(nrofreqs
+1)
241 reqs
= (wine_rpc_request
**)HeapAlloc(
244 sizeof(wine_rpc_request
*)
247 return E_OUTOFMEMORY
;
248 reqs
[nrofreqs
] = (wine_rpc_request
*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(wine_rpc_request
));
249 reqs
[nrofreqs
]->reqh
.reqid
= reqid
++;
250 reqs
[nrofreqs
]->resph
.reqid
= reqs
[nrofreqs
]->reqh
.reqid
;
251 reqs
[nrofreqs
]->hPipe
= INVALID_HANDLE_VALUE
;
252 *req
= reqs
[nrofreqs
];
253 reqs
[nrofreqs
]->state
= REQSTATE_START
;
259 RPC_FreeRequest(wine_rpc_request
*req
) {
260 req
->state
= REQSTATE_DONE
; /* Just reuse slot. */
264 static HRESULT WINAPI
265 PipeBuf_QueryInterface(
266 LPRPCCHANNELBUFFER iface
,REFIID riid
,LPVOID
*ppv
269 if (IsEqualIID(riid
,&IID_IRpcChannelBuffer
) || IsEqualIID(riid
,&IID_IUnknown
)) {
270 *ppv
= (LPVOID
)iface
;
271 IUnknown_AddRef(iface
);
274 return E_NOINTERFACE
;
278 PipeBuf_AddRef(LPRPCCHANNELBUFFER iface
) {
279 ICOM_THIS(PipeBuf
,iface
);
285 PipeBuf_Release(LPRPCCHANNELBUFFER iface
) {
286 ICOM_THIS(PipeBuf
,iface
);
290 ERR("Free all stuff.\n");
291 HeapFree(GetProcessHeap(),0,This
);
295 static HRESULT WINAPI
297 LPRPCCHANNELBUFFER iface
,RPCOLEMESSAGE
* msg
,REFIID riid
299 /*ICOM_THIS(PipeBuf,iface);*/
301 TRACE("(%p,%s), slightly wrong.\n",msg
,debugstr_guid(riid
));
302 /* probably reuses IID in real. */
303 if (msg
->cbBuffer
&& (msg
->Buffer
== NULL
))
304 msg
->Buffer
= HeapAlloc(GetProcessHeap(),0,msg
->cbBuffer
);
309 _invoke_onereq(wine_rpc_request
*req
) {
310 IRpcStubBuffer
*stub
;
315 hres
= MARSHAL_Find_Stub_Buffer(&(req
->reqh
.mid
),&stub
);
317 ERR("Stub not found?\n");
320 msg
.Buffer
= req
->Buffer
;
321 msg
.iMethod
= req
->reqh
.iMethod
;
322 msg
.cbBuffer
= req
->reqh
.cbBuffer
;
323 req
->state
= REQSTATE_INVOKING
;
324 req
->resph
.retval
= IRpcStubBuffer_Invoke(stub
,&msg
,NULL
);
325 IUnknown_Release(stub
);
326 req
->Buffer
= msg
.Buffer
;
327 req
->resph
.cbBuffer
= msg
.cbBuffer
;
328 reqtype
= REQTYPE_RESPONSE
;
329 hres
= _xwrite(req
->hPipe
,&reqtype
,sizeof(reqtype
));
330 if (hres
) return hres
;
331 hres
= _xwrite(req
->hPipe
,&(req
->resph
),sizeof(req
->resph
));
332 if (hres
) return hres
;
333 hres
= _xwrite(req
->hPipe
,req
->Buffer
,req
->resph
.cbBuffer
);
334 if (hres
) return hres
;
335 req
->state
= REQSTATE_DONE
;
340 static HRESULT
_read_one(wine_pipe
*xpipe
);
343 RPC_QueueRequestAndWait(wine_rpc_request
*req
) {
345 wine_rpc_request
*xreq
;
348 wine_pipe
*xpipe
= PIPE_GetFromMID(&(req
->reqh
.mid
));
351 FIXME("no pipe found.\n");
354 if (GetCurrentProcessId() == req
->reqh
.mid
.processid
) {
355 ERR("In current process?\n");
358 req
->hPipe
= xpipe
->hPipe
;
359 req
->state
= REQSTATE_REQ_WAITING_FOR_REPLY
;
360 reqtype
= REQTYPE_REQUEST
;
361 hres
= _xwrite(req
->hPipe
,&reqtype
,sizeof(reqtype
));
362 if (hres
) return hres
;
363 hres
= _xwrite(req
->hPipe
,&(req
->reqh
),sizeof(req
->reqh
));
364 if (hres
) return hres
;
365 hres
= _xwrite(req
->hPipe
,req
->Buffer
,req
->reqh
.cbBuffer
);
366 if (hres
) return hres
;
369 /*WaitForSingleObject(hRpcChanged,INFINITE);*/
370 hres
= _read_one(xpipe
);
373 for (i
=0;i
<nrofreqs
;i
++) {
375 if ((xreq
->state
==REQSTATE_REQ_GOT
) && (xreq
->hPipe
==req
->hPipe
)) {
376 _invoke_onereq(xreq
);
379 if (req
->state
== REQSTATE_RESP_GOT
)
385 static HRESULT WINAPI
387 LPRPCCHANNELBUFFER iface
,RPCOLEMESSAGE
* msg
,ULONG
*status
389 ICOM_THIS(PipeBuf
,iface
);
390 wine_rpc_request
*req
;
395 if (This
->mid
.processid
== GetCurrentProcessId()) {
396 ERR("Need to call directly!\n");
400 hres
= RPC_GetRequest(&req
);
401 if (hres
) return hres
;
402 req
->reqh
.iMethod
= msg
->iMethod
;
403 req
->reqh
.cbBuffer
= msg
->cbBuffer
;
404 memcpy(&(req
->reqh
.mid
),&(This
->mid
),sizeof(This
->mid
));
405 req
->Buffer
= msg
->Buffer
;
406 hres
= RPC_QueueRequestAndWait(req
);
408 RPC_FreeRequest(req
);
411 msg
->cbBuffer
= req
->resph
.cbBuffer
;
412 msg
->Buffer
= req
->Buffer
;
413 *status
= req
->resph
.retval
;
414 RPC_FreeRequest(req
);
419 static HRESULT WINAPI
420 PipeBuf_FreeBuffer(LPRPCCHANNELBUFFER iface
,RPCOLEMESSAGE
* msg
) {
421 FIXME("(%p), stub!\n",msg
);
425 static HRESULT WINAPI
427 LPRPCCHANNELBUFFER iface
,DWORD
* pdwDestContext
,void** ppvDestContext
429 FIXME("(%p,%p), stub!\n",pdwDestContext
,ppvDestContext
);
433 static HRESULT WINAPI
434 PipeBuf_IsConnected(LPRPCCHANNELBUFFER iface
) {
435 FIXME("(), stub!\n");
439 static ICOM_VTABLE(IRpcChannelBuffer
) pipebufvt
= {
440 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
441 PipeBuf_QueryInterface
,
452 PIPE_GetNewPipeBuf(wine_marshal_id
*mid
, IRpcChannelBuffer
**pipebuf
) {
453 wine_marshal_id ourid
;
459 hPipe
= PIPE_FindByMID(mid
);
460 if (hPipe
== INVALID_HANDLE_VALUE
) {
462 sprintf(pipefn
,OLESTUBMGR
"_%08lx",mid
->processid
);
465 GENERIC_READ
|GENERIC_WRITE
,
472 if (hPipe
== INVALID_HANDLE_VALUE
) {
473 FIXME("Could not open named pipe %s, le is %lx\n",pipefn
,GetLastError());
476 hres
= PIPE_RegisterPipe(mid
, hPipe
, FALSE
);
477 if (hres
) return hres
;
478 memset(&ourid
,0,sizeof(ourid
));
479 ourid
.processid
= GetCurrentProcessId();
480 if (!WriteFile(hPipe
,&ourid
,sizeof(ourid
),&res
,NULL
)||(res
!=sizeof(ourid
))) {
481 ERR("Failed writing startup mid!\n");
485 pbuf
= (PipeBuf
*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(PipeBuf
));
486 pbuf
->lpVtbl
= &pipebufvt
;
488 memcpy(&(pbuf
->mid
),mid
,sizeof(*mid
));
489 *pipebuf
= (IRpcChannelBuffer
*)pbuf
;
494 create_server(REFCLSID rclsid
) {
497 HRESULT hres
= E_UNEXPECTED
;
499 WCHAR dllName
[MAX_PATH
+1];
500 DWORD dllNameLen
= sizeof(dllName
);
502 PROCESS_INFORMATION pinfo
;
504 WINE_StringFromCLSID((LPCLSID
)rclsid
,xclsid
);
506 sprintf(buf
,"CLSID\\%s\\LocalServer32",xclsid
);
507 hres
= RegOpenKeyExA(HKEY_CLASSES_ROOT
, buf
, 0, KEY_READ
, &key
);
509 if (hres
!= ERROR_SUCCESS
)
510 return REGDB_E_READREGDB
; /* Probably */
512 memset(dllName
,0,sizeof(dllName
));
513 hres
= RegQueryValueExW(key
,NULL
,NULL
,NULL
,(LPBYTE
)dllName
,&dllNameLen
);
516 return REGDB_E_CLASSNOTREG
; /* FIXME: check retval */
517 memset(&sinfo
,0,sizeof(sinfo
));
518 sinfo
.cb
= sizeof(sinfo
);
519 if (!CreateProcessW(NULL
,dllName
,NULL
,NULL
,FALSE
,0,NULL
,NULL
,&sinfo
,&pinfo
))
523 /* http://msdn.microsoft.com/library/en-us/dnmsj99/html/com0199.asp, Figure 4 */
524 HRESULT
create_marshalled_proxy(REFCLSID rclsid
, REFIID iid
, LPVOID
*ppv
) {
529 char marshalbuffer
[200];
531 LARGE_INTEGER seekto
;
532 ULARGE_INTEGER newpos
;
534 #define MAXTRIES 10000
536 strcpy(pipefn
,PIPEPREF
);
537 WINE_StringFromCLSID(rclsid
,pipefn
+strlen(PIPEPREF
));
539 while (tries
++<MAXTRIES
) {
540 WaitNamedPipeA( pipefn
, NMPWAIT_WAIT_FOREVER
);
543 GENERIC_READ
|GENERIC_WRITE
,
550 if (hPipe
== INVALID_HANDLE_VALUE
) {
552 if ((hres
= create_server(rclsid
)))
556 WARN("Could not open named pipe to broker %s, le is %lx\n",pipefn
,GetLastError());
562 if (!ReadFile(hPipe
,marshalbuffer
,sizeof(marshalbuffer
),&bufferlen
,NULL
)) {
563 FIXME("Failed to read marshal id from classfactory of %s.\n",debugstr_guid(rclsid
));
571 return E_NOINTERFACE
;
572 hres
= CreateStreamOnHGlobal(0,TRUE
,&pStm
);
573 if (hres
) return hres
;
574 hres
= IStream_Write(pStm
,marshalbuffer
,bufferlen
,&res
);
576 seekto
.u
.LowPart
= 0;seekto
.u
.HighPart
= 0;
577 hres
= IStream_Seek(pStm
,seekto
,SEEK_SET
,&newpos
);
578 hres
= CoUnmarshalInterface(pStm
,&IID_IClassFactory
,ppv
);
580 IStream_Release(pStm
);
586 PIPE_StartRequestThread(HANDLE xhPipe
) {
587 wine_marshal_id remoteid
;
590 hres
= _xread(xhPipe
,&remoteid
,sizeof(remoteid
));
592 ERR("Failed to read remote mid!\n");
595 PIPE_RegisterPipe(&remoteid
,xhPipe
, TRUE
);
599 _read_one(wine_pipe
*xpipe
) {
602 HANDLE xhPipe
= xpipe
->hPipe
;
604 /*FIXME("%lx %d reading reqtype\n",GetCurrentProcessId(),xhPipe);*/
605 hres
= _xread(xhPipe
,&reqtype
,sizeof(reqtype
));
607 EnterCriticalSection(&(xpipe
->crit
));
608 /*FIXME("%lx got reqtype %ld\n",GetCurrentProcessId(),reqtype);*/
610 if (reqtype
== REQTYPE_REQUEST
) {
611 wine_rpc_request
*xreq
;
612 RPC_GetRequest(&xreq
);
613 xreq
->hPipe
= xhPipe
;
614 hres
= _xread(xhPipe
,&(xreq
->reqh
),sizeof(xreq
->reqh
));
616 xreq
->resph
.reqid
= xreq
->reqh
.reqid
;
617 xreq
->Buffer
= HeapAlloc(GetProcessHeap(),0, xreq
->reqh
.cbBuffer
);
618 hres
= _xread(xhPipe
,xreq
->Buffer
,xreq
->reqh
.cbBuffer
);
620 xreq
->state
= REQSTATE_REQ_GOT
;
623 if (reqtype
== REQTYPE_RESPONSE
) {
624 wine_rpc_response_header resph
;
627 hres
= _xread(xhPipe
,&resph
,sizeof(resph
));
629 for (i
=nrofreqs
;i
--;) {
630 wine_rpc_request
*xreq
= reqs
[i
];
631 if (xreq
->state
!= REQSTATE_REQ_WAITING_FOR_REPLY
)
633 if (xreq
->reqh
.reqid
== resph
.reqid
) {
634 memcpy(&(xreq
->resph
),&resph
,sizeof(resph
));
637 xreq
->Buffer
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,xreq
->Buffer
,xreq
->resph
.cbBuffer
);
639 xreq
->Buffer
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,xreq
->resph
.cbBuffer
);
641 hres
= _xread(xhPipe
,xreq
->Buffer
,xreq
->resph
.cbBuffer
);
643 xreq
->state
= REQSTATE_RESP_GOT
;
644 /*PulseEvent(hRpcChanged);*/
648 ERR("Did not find request for id %lx\n",resph
.reqid
);
652 ERR("Unknown reqtype %ld\n",reqtype
);
655 LeaveCriticalSection(&(xpipe
->crit
));
660 _StubReaderThread(LPVOID param
) {
661 wine_pipe
*xpipe
= (wine_pipe
*)param
;
662 HANDLE xhPipe
= xpipe
->hPipe
;
665 TRACE("STUB reader thread %lx\n",GetCurrentProcessId());
668 hres
= _read_one(xpipe
);
671 for (i
=nrofreqs
;i
--;) {
672 wine_rpc_request
*xreq
= reqs
[i
];
673 if ((xreq
->state
== REQSTATE_REQ_GOT
) && (xreq
->hPipe
== xhPipe
)) {
674 _invoke_onereq(xreq
);
678 FIXME("Failed with hres %lx\n",hres
);
684 _StubMgrThread(LPVOID param
) {
688 sprintf(pipefn
,OLESTUBMGR
"_%08lx",GetCurrentProcessId());
689 TRACE("Stub Manager Thread starting on (%s)\n",pipefn
);
692 listenPipe
= CreateNamedPipeA(
695 PIPE_TYPE_BYTE
|PIPE_WAIT
,
696 PIPE_UNLIMITED_INSTANCES
,
699 NMPWAIT_USE_DEFAULT_WAIT
,
702 if (listenPipe
== INVALID_HANDLE_VALUE
) {
703 FIXME("pipe creation failed for %s, le is %lx\n",pipefn
,GetLastError());
704 return 1; /* permanent failure, so quit stubmgr thread */
706 if (!ConnectNamedPipe(listenPipe
,NULL
)) {
707 ERR("Failure during ConnectNamedPipe %lx!\n",GetLastError());
708 CloseHandle(listenPipe
);
711 PIPE_StartRequestThread(listenPipe
);
718 static BOOL stubMgrRunning
= FALSE
;
721 if (!stubMgrRunning
) {
722 stubMgrRunning
= TRUE
;
723 CreateThread(NULL
,0,_StubMgrThread
,NULL
,0,&tid
);
724 Sleep(2000); /* actually we just try opening the pipe until it succeeds */