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
37 #include "wine/unicode.h"
38 #include "wine/obj_base.h"
39 #include "wine/obj_clientserver.h"
40 #include "wine/obj_misc.h"
41 #include "wine/obj_marshal.h"
42 #include "wine/obj_storage.h"
43 #include "wine/obj_channel.h"
44 #include "wine/winbase16.h"
45 #include "compobj_private.h"
48 #include "compobj_private.h"
50 #include "wine/debug.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(ole);
54 typedef struct _wine_rpc_request {
56 HANDLE hPipe; /* temp copy of handle */
57 wine_rpc_request_header reqh;
58 wine_rpc_response_header resph;
62 static wine_rpc_request **reqs = NULL;
63 static int nrofreqs = 0;
65 /* This pipe is _thread_ based */
66 typedef struct _wine_pipe {
67 wine_marshal_id mid; /* target mid */
68 DWORD tid; /* thread in which we execute */
73 CRITICAL_SECTION crit;
76 static wine_pipe *pipes = NULL;
77 static int nrofpipes = 0;
79 typedef struct _PipeBuf {
80 ICOM_VTABLE(IRpcChannelBuffer) *lpVtbl;
87 static int nrofreaders = 0;
90 _xread(HANDLE hf, LPVOID ptr, DWORD size) {
92 if (!ReadFile(hf,ptr,size,&res,NULL)) {
93 FIXME("Failed to read from %p, le is %lx\n",hf,GetLastError());
97 FIXME("Read only %ld of %ld bytes from %p.\n",res,size,hf);
109 memset(states,0,sizeof(states));
110 for (i=nrofreqs;i--;)
111 states[reqs[i]->state]++;
112 FIXME("%lx/%s/%d: rq %d, w %d, rg %d, rsq %d, rsg %d, d %d\n",
113 GetCurrentProcessId(),
116 states[REQSTATE_REQ_QUEUED],
117 states[REQSTATE_REQ_WAITING_FOR_REPLY],
118 states[REQSTATE_REQ_GOT],
119 states[REQSTATE_RESP_QUEUED],
120 states[REQSTATE_RESP_GOT],
121 states[REQSTATE_DONE]
125 static HRESULT WINAPI
126 _xwrite(HANDLE hf, LPVOID ptr, DWORD size) {
128 if (!WriteFile(hf,ptr,size,&res,NULL)) {
129 FIXME("Failed to write to %p, le is %lx\n",hf,GetLastError());
133 FIXME("Wrote only %ld of %ld bytes to %p.\n",res,size,hf);
139 static DWORD WINAPI _StubReaderThread(LPVOID);
142 PIPE_RegisterPipe(wine_marshal_id *mid, HANDLE hPipe, BOOL startreader) {
145 wine_pipe *new_pipes;
147 for (i=0;i<nrofpipes;i++)
148 if (pipes[i].mid.processid==mid->processid)
151 new_pipes=(wine_pipe*)HeapReAlloc(GetProcessHeap(),0,pipes,sizeof(pipes[0])*(nrofpipes+1));
153 new_pipes=(wine_pipe*)HeapAlloc(GetProcessHeap(),0,sizeof(pipes[0]));
154 if (!new_pipes) return E_OUTOFMEMORY;
156 sprintf(pipefn,OLESTUBMGR"_%08lx",mid->processid);
157 memcpy(&(pipes[nrofpipes].mid),mid,sizeof(*mid));
158 pipes[nrofpipes].hPipe = hPipe;
159 InitializeCriticalSection(&(pipes[nrofpipes].crit));
162 pipes[nrofpipes-1].hThread = CreateThread(NULL,0,_StubReaderThread,(LPVOID)(pipes+(nrofpipes-1)),0,&(pipes[nrofpipes-1].tid));
164 pipes[nrofpipes-1].tid = GetCurrentThreadId();
170 PIPE_FindByMID(wine_marshal_id *mid) {
172 for (i=0;i<nrofpipes;i++)
173 if ((pipes[i].mid.processid==mid->processid) &&
174 (GetCurrentThreadId()==pipes[i].tid)
176 return pipes[i].hPipe;
177 return INVALID_HANDLE_VALUE;
181 PIPE_GetFromMID(wine_marshal_id *mid) {
183 for (i=0;i<nrofpipes;i++) {
184 if ((pipes[i].mid.processid==mid->processid) &&
185 (GetCurrentThreadId()==pipes[i].tid)
193 RPC_GetRequest(wine_rpc_request **req) {
194 static int reqid = 0xdeadbeef;
197 for (i=0;i<nrofreqs;i++) { /* try to reuse */
198 if (reqs[i]->state == REQSTATE_DONE) {
199 reqs[i]->reqh.reqid = reqid++;
200 reqs[i]->resph.reqid = reqs[i]->reqh.reqid;
201 reqs[i]->hPipe = INVALID_HANDLE_VALUE;
203 reqs[i]->state = REQSTATE_START;
209 reqs = (wine_rpc_request**)HeapReAlloc(
213 sizeof(wine_rpc_request*)*(nrofreqs+1)
216 reqs = (wine_rpc_request**)HeapAlloc(
219 sizeof(wine_rpc_request*)
222 return E_OUTOFMEMORY;
223 reqs[nrofreqs] = (wine_rpc_request*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(wine_rpc_request));
224 reqs[nrofreqs]->reqh.reqid = reqid++;
225 reqs[nrofreqs]->resph.reqid = reqs[nrofreqs]->reqh.reqid;
226 reqs[nrofreqs]->hPipe = INVALID_HANDLE_VALUE;
227 *req = reqs[nrofreqs];
228 reqs[nrofreqs]->state = REQSTATE_START;
234 RPC_FreeRequest(wine_rpc_request *req) {
235 req->state = REQSTATE_DONE; /* Just reuse slot. */
239 static HRESULT WINAPI
240 PipeBuf_QueryInterface(
241 LPRPCCHANNELBUFFER iface,REFIID riid,LPVOID *ppv
244 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown)) {
245 *ppv = (LPVOID)iface;
246 IUnknown_AddRef(iface);
249 return E_NOINTERFACE;
253 PipeBuf_AddRef(LPRPCCHANNELBUFFER iface) {
254 ICOM_THIS(PipeBuf,iface);
260 PipeBuf_Release(LPRPCCHANNELBUFFER iface) {
261 ICOM_THIS(PipeBuf,iface);
265 ERR("Free all stuff.\n");
266 HeapFree(GetProcessHeap(),0,This);
270 static HRESULT WINAPI
272 LPRPCCHANNELBUFFER iface,RPCOLEMESSAGE* msg,REFIID riid
274 /*ICOM_THIS(PipeBuf,iface);*/
276 TRACE("(%p,%s), slightly wrong.\n",msg,debugstr_guid(riid));
277 /* probably reuses IID in real. */
278 if (msg->cbBuffer && (msg->Buffer == NULL))
279 msg->Buffer = HeapAlloc(GetProcessHeap(),0,msg->cbBuffer);
284 _invoke_onereq(wine_rpc_request *req) {
285 IRpcStubBuffer *stub;
290 hres = MARSHAL_Find_Stub_Buffer(&(req->reqh.mid),&stub);
292 ERR("Stub not found?\n");
295 msg.Buffer = req->Buffer;
296 msg.iMethod = req->reqh.iMethod;
297 msg.cbBuffer = req->reqh.cbBuffer;
298 req->state = REQSTATE_INVOKING;
299 req->resph.retval = IRpcStubBuffer_Invoke(stub,&msg,NULL);
300 req->Buffer = msg.Buffer;
301 req->resph.cbBuffer = msg.cbBuffer;
302 reqtype = REQTYPE_RESPONSE;
303 hres = _xwrite(req->hPipe,&reqtype,sizeof(reqtype));
304 if (hres) return hres;
305 hres = _xwrite(req->hPipe,&(req->resph),sizeof(req->resph));
306 if (hres) return hres;
307 hres = _xwrite(req->hPipe,req->Buffer,req->resph.cbBuffer);
308 if (hres) return hres;
309 req->state = REQSTATE_DONE;
314 static HRESULT _read_one(wine_pipe *xpipe);
317 RPC_QueueRequestAndWait(wine_rpc_request *req) {
319 wine_rpc_request *xreq;
322 wine_pipe *xpipe = PIPE_GetFromMID(&(req->reqh.mid));
325 FIXME("no pipe found.\n");
328 if (GetCurrentProcessId() == req->reqh.mid.processid) {
329 ERR("In current process?\n");
332 req->hPipe = xpipe->hPipe;
333 req->state = REQSTATE_REQ_WAITING_FOR_REPLY;
334 reqtype = REQTYPE_REQUEST;
335 hres = _xwrite(req->hPipe,&reqtype,sizeof(reqtype));
336 if (hres) return hres;
337 hres = _xwrite(req->hPipe,&(req->reqh),sizeof(req->reqh));
338 if (hres) return hres;
339 hres = _xwrite(req->hPipe,req->Buffer,req->reqh.cbBuffer);
340 if (hres) return hres;
343 /*WaitForSingleObject(hRpcChanged,INFINITE);*/
344 hres = _read_one(xpipe);
347 for (i=0;i<nrofreqs;i++) {
349 if ((xreq->state==REQSTATE_REQ_GOT) && (xreq->hPipe==req->hPipe)) {
350 _invoke_onereq(xreq);
353 if (req->state == REQSTATE_RESP_GOT)
359 static HRESULT WINAPI
361 LPRPCCHANNELBUFFER iface,RPCOLEMESSAGE* msg,ULONG *status
363 ICOM_THIS(PipeBuf,iface);
364 wine_rpc_request *req;
369 if (This->mid.processid == GetCurrentProcessId()) {
370 ERR("Need to call directly!\n");
374 hres = RPC_GetRequest(&req);
375 if (hres) return hres;
376 req->reqh.iMethod = msg->iMethod;
377 req->reqh.cbBuffer = msg->cbBuffer;
378 memcpy(&(req->reqh.mid),&(This->mid),sizeof(This->mid));
379 req->Buffer = msg->Buffer;
380 hres = RPC_QueueRequestAndWait(req);
382 RPC_FreeRequest(req);
385 msg->cbBuffer = req->resph.cbBuffer;
386 msg->Buffer = req->Buffer;
387 *status = req->resph.retval;
388 RPC_FreeRequest(req);
393 static HRESULT WINAPI
394 PipeBuf_FreeBuffer(LPRPCCHANNELBUFFER iface,RPCOLEMESSAGE* msg) {
395 FIXME("(%p), stub!\n",msg);
399 static HRESULT WINAPI
401 LPRPCCHANNELBUFFER iface,DWORD* pdwDestContext,void** ppvDestContext
403 FIXME("(%p,%p), stub!\n",pdwDestContext,ppvDestContext);
407 static HRESULT WINAPI
408 PipeBuf_IsConnected(LPRPCCHANNELBUFFER iface) {
409 FIXME("(), stub!\n");
413 static ICOM_VTABLE(IRpcChannelBuffer) pipebufvt = {
414 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
415 PipeBuf_QueryInterface,
426 PIPE_GetNewPipeBuf(wine_marshal_id *mid, IRpcChannelBuffer **pipebuf) {
427 wine_marshal_id ourid;
433 hPipe = PIPE_FindByMID(mid);
434 if (hPipe == INVALID_HANDLE_VALUE) {
436 sprintf(pipefn,OLESTUBMGR"_%08lx",mid->processid);
439 GENERIC_READ|GENERIC_WRITE,
446 if (hPipe == INVALID_HANDLE_VALUE) {
447 FIXME("Could not open named pipe %s, le is %lx\n",pipefn,GetLastError());
450 hres = PIPE_RegisterPipe(mid, hPipe, FALSE);
451 if (hres) return hres;
452 memset(&ourid,0,sizeof(ourid));
453 ourid.processid = GetCurrentProcessId();
454 if (!WriteFile(hPipe,&ourid,sizeof(ourid),&res,NULL)||(res!=sizeof(ourid))) {
455 ERR("Failed writing startup mid!\n");
459 pbuf = (PipeBuf*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(PipeBuf));
460 pbuf->lpVtbl = &pipebufvt;
462 memcpy(&(pbuf->mid),mid,sizeof(*mid));
463 *pipebuf = (IRpcChannelBuffer*)pbuf;
468 create_server(REFCLSID rclsid) {
471 HRESULT hres = E_UNEXPECTED;
473 WCHAR dllName[MAX_PATH+1];
474 DWORD dllNameLen = sizeof(dllName);
476 PROCESS_INFORMATION pinfo;
478 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
480 sprintf(buf,"CLSID\\%s\\LocalServer32",xclsid);
481 hres = RegOpenKeyExA(HKEY_CLASSES_ROOT, buf, 0, KEY_READ, &key);
483 if (hres != ERROR_SUCCESS)
484 return REGDB_E_READREGDB; /* Probably */
486 memset(dllName,0,sizeof(dllName));
487 hres= RegQueryValueExW(key,NULL,NULL,NULL,(LPBYTE)dllName,&dllNameLen);
490 return REGDB_E_CLASSNOTREG; /* FIXME: check retval */
491 memset(&sinfo,0,sizeof(sinfo));
492 sinfo.cb = sizeof(sinfo);
493 if (!CreateProcessW(NULL,dllName,NULL,NULL,FALSE,0,NULL,NULL,&sinfo,&pinfo))
497 /* http://msdn.microsoft.com/library/en-us/dnmsj99/html/com0199.asp, Figure 4 */
498 HRESULT create_marshalled_proxy(REFCLSID rclsid, REFIID iid, LPVOID *ppv) {
503 char marshalbuffer[200];
505 LARGE_INTEGER seekto;
506 ULARGE_INTEGER newpos;
508 #define MAXTRIES 10000
510 strcpy(pipefn,PIPEPREF);
511 WINE_StringFromCLSID(rclsid,pipefn+strlen(PIPEPREF));
513 while (tries++<MAXTRIES) {
516 GENERIC_READ|GENERIC_WRITE,
523 if (hPipe == INVALID_HANDLE_VALUE) {
525 if ((hres = create_server(rclsid)))
529 WARN("Could not open named pipe to broker %s, le is %lx\n",pipefn,GetLastError());
535 if (!ReadFile(hPipe,marshalbuffer,sizeof(marshalbuffer),&bufferlen,NULL)) {
536 FIXME("Failed to read marshal id from classfactory of %s.\n",debugstr_guid(rclsid));
544 return E_NOINTERFACE;
545 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
546 if (hres) return hres;
547 hres = IStream_Write(pStm,marshalbuffer,bufferlen,&res);
549 seekto.s.LowPart = 0;seekto.s.HighPart = 0;
550 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
551 hres = CoUnmarshalInterface(pStm,&IID_IClassFactory,ppv);
553 IStream_Release(pStm);
559 PIPE_StartRequestThread(HANDLE xhPipe) {
560 wine_marshal_id remoteid;
563 hres = _xread(xhPipe,&remoteid,sizeof(remoteid));
565 ERR("Failed to read remote mid!\n");
568 PIPE_RegisterPipe(&remoteid,xhPipe, TRUE);
572 _read_one(wine_pipe *xpipe) {
575 HANDLE xhPipe = xpipe->hPipe;
577 /*FIXME("%lx %d reading reqtype\n",GetCurrentProcessId(),xhPipe);*/
578 hres = _xread(xhPipe,&reqtype,sizeof(reqtype));
580 EnterCriticalSection(&(xpipe->crit));
581 /*FIXME("%lx got reqtype %ld\n",GetCurrentProcessId(),reqtype);*/
583 if (reqtype == REQTYPE_REQUEST) {
584 wine_rpc_request *xreq;
585 RPC_GetRequest(&xreq);
586 xreq->hPipe = xhPipe;
587 hres = _xread(xhPipe,&(xreq->reqh),sizeof(xreq->reqh));
589 xreq->resph.reqid = xreq->reqh.reqid;
590 xreq->Buffer = HeapAlloc(GetProcessHeap(),0, xreq->reqh.cbBuffer);
591 hres = _xread(xhPipe,xreq->Buffer,xreq->reqh.cbBuffer);
593 xreq->state = REQSTATE_REQ_GOT;
596 if (reqtype == REQTYPE_RESPONSE) {
597 wine_rpc_response_header resph;
600 hres = _xread(xhPipe,&resph,sizeof(resph));
602 for (i=nrofreqs;i--;) {
603 wine_rpc_request *xreq = reqs[i];
604 if (xreq->state != REQSTATE_REQ_WAITING_FOR_REPLY)
606 if (xreq->reqh.reqid == resph.reqid) {
607 memcpy(&(xreq->resph),&resph,sizeof(resph));
608 xreq->Buffer = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,xreq->Buffer,xreq->resph.cbBuffer);
609 hres = _xread(xhPipe,xreq->Buffer,xreq->resph.cbBuffer);
611 xreq->state = REQSTATE_RESP_GOT;
612 /*PulseEvent(hRpcChanged);*/
616 ERR("Did not find request for id %lx\n",resph.reqid);
620 ERR("Unknown reqtype %ld\n",reqtype);
623 LeaveCriticalSection(&(xpipe->crit));
628 _StubReaderThread(LPVOID param) {
629 wine_pipe *xpipe = (wine_pipe*)param;
630 HANDLE xhPipe = xpipe->hPipe;
633 TRACE("STUB reader thread %lx\n",GetCurrentProcessId());
636 hres = _read_one(xpipe);
639 for (i=nrofreqs;i--;) {
640 wine_rpc_request *xreq = reqs[i];
641 if ((xreq->state == REQSTATE_REQ_GOT) && (xreq->hPipe == xhPipe)) {
642 _invoke_onereq(xreq);
646 FIXME("Failed with hres %lx\n",hres);
652 _StubMgrThread(LPVOID param) {
656 sprintf(pipefn,OLESTUBMGR"_%08lx",GetCurrentProcessId());
657 TRACE("Stub Manager Thread starting on (%s)\n",pipefn);
659 listenPipe = CreateNamedPipeA(
662 PIPE_TYPE_BYTE|PIPE_WAIT,
663 PIPE_UNLIMITED_INSTANCES,
666 NMPWAIT_USE_DEFAULT_WAIT,
669 if (listenPipe == INVALID_HANDLE_VALUE) {
670 FIXME("pipe creation failed for %s, le is %lx\n",pipefn,GetLastError());
671 return 1; /* permanent failure, so quit stubmgr thread */
675 if (!ConnectNamedPipe(listenPipe,NULL)) {
676 ERR("Failure during ConnectNamedPipe %lx!\n",GetLastError());
677 CloseHandle(listenPipe);
680 PIPE_StartRequestThread(listenPipe);
681 listenPipe = CreateNamedPipeA(
684 PIPE_TYPE_BYTE|PIPE_WAIT,
685 PIPE_UNLIMITED_INSTANCES,
688 NMPWAIT_USE_DEFAULT_WAIT,
691 if (listenPipe == INVALID_HANDLE_VALUE) {
692 FIXME("pipe creation failed for %s, le is %lx\n",pipefn,GetLastError());
693 return 1; /* permanent failure, so quit stubmgr thread */
701 static BOOL stubMgrRunning = FALSE;
704 if (!stubMgrRunning) {
705 stubMgrRunning = TRUE;
706 CreateThread(NULL,0,_StubMgrThread,NULL,0,&tid);
707 Sleep(2000); /* actually we just try opening the pipe until it succeeds */