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 typedef struct _wine_rpc_request {
55 HANDLE hPipe; /* temp copy of handle */
56 wine_rpc_request_header reqh;
57 wine_rpc_response_header resph;
61 static wine_rpc_request **reqs = NULL;
62 static int nrofreqs = 0;
64 /* This pipe is _thread_ based */
65 typedef struct _wine_pipe {
66 wine_marshal_id mid; /* target mid */
67 DWORD tid; /* thread in which we execute */
72 CRITICAL_SECTION crit;
75 static wine_pipe *pipes = NULL;
76 static int nrofpipes = 0;
78 typedef struct _PipeBuf {
79 ICOM_VTABLE(IRpcChannelBuffer) *lpVtbl;
87 _xread(HANDLE hf, LPVOID ptr, DWORD size) {
89 if (!ReadFile(hf,ptr,size,&res,NULL)) {
90 FIXME("Failed to read from %p, le is %lx\n",hf,GetLastError());
94 FIXME("Read only %ld of %ld bytes from %p.\n",res,size,hf);
103 static int nrofreaders = 0;
107 memset(states,0,sizeof(states));
108 for (i=nrofreqs;i--;)
109 states[reqs[i]->state]++;
110 FIXME("%lx/%s/%d: rq %d, w %d, rg %d, rsq %d, rsg %d, d %d\n",
111 GetCurrentProcessId(),
114 states[REQSTATE_REQ_QUEUED],
115 states[REQSTATE_REQ_WAITING_FOR_REPLY],
116 states[REQSTATE_REQ_GOT],
117 states[REQSTATE_RESP_QUEUED],
118 states[REQSTATE_RESP_GOT],
119 states[REQSTATE_DONE]
126 static HRESULT WINAPI
127 _xwrite(HANDLE hf, LPVOID ptr, DWORD size) {
129 if (!WriteFile(hf,ptr,size,&res,NULL)) {
130 FIXME("Failed to write to %p, le is %lx\n",hf,GetLastError());
134 FIXME("Wrote only %ld of %ld bytes to %p.\n",res,size,hf);
140 static DWORD WINAPI _StubReaderThread(LPVOID);
143 PIPE_RegisterPipe(wine_marshal_id *mid, HANDLE hPipe, BOOL startreader) {
146 wine_pipe *new_pipes;
148 for (i=0;i<nrofpipes;i++)
149 if (pipes[i].mid.processid==mid->processid)
152 new_pipes=(wine_pipe*)HeapReAlloc(GetProcessHeap(),0,pipes,sizeof(pipes[0])*(nrofpipes+1));
154 new_pipes=(wine_pipe*)HeapAlloc(GetProcessHeap(),0,sizeof(pipes[0]));
155 if (!new_pipes) return E_OUTOFMEMORY;
157 sprintf(pipefn,OLESTUBMGR"_%08lx",mid->processid);
158 memcpy(&(pipes[nrofpipes].mid),mid,sizeof(*mid));
159 pipes[nrofpipes].hPipe = hPipe;
160 InitializeCriticalSection(&(pipes[nrofpipes].crit));
163 pipes[nrofpipes-1].hThread = CreateThread(NULL,0,_StubReaderThread,(LPVOID)(pipes+(nrofpipes-1)),0,&(pipes[nrofpipes-1].tid));
165 pipes[nrofpipes-1].tid = GetCurrentThreadId();
171 PIPE_FindByMID(wine_marshal_id *mid) {
173 for (i=0;i<nrofpipes;i++)
174 if ((pipes[i].mid.processid==mid->processid) &&
175 (GetCurrentThreadId()==pipes[i].tid)
177 return pipes[i].hPipe;
178 return INVALID_HANDLE_VALUE;
182 PIPE_GetFromMID(wine_marshal_id *mid) {
184 for (i=0;i<nrofpipes;i++) {
185 if ((pipes[i].mid.processid==mid->processid) &&
186 (GetCurrentThreadId()==pipes[i].tid)
194 RPC_GetRequest(wine_rpc_request **req) {
195 static int reqid = 0xdeadbeef;
198 for (i=0;i<nrofreqs;i++) { /* try to reuse */
199 if (reqs[i]->state == REQSTATE_DONE) {
200 reqs[i]->reqh.reqid = reqid++;
201 reqs[i]->resph.reqid = reqs[i]->reqh.reqid;
202 reqs[i]->hPipe = INVALID_HANDLE_VALUE;
204 reqs[i]->state = REQSTATE_START;
210 reqs = (wine_rpc_request**)HeapReAlloc(
214 sizeof(wine_rpc_request*)*(nrofreqs+1)
217 reqs = (wine_rpc_request**)HeapAlloc(
220 sizeof(wine_rpc_request*)
223 return E_OUTOFMEMORY;
224 reqs[nrofreqs] = (wine_rpc_request*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(wine_rpc_request));
225 reqs[nrofreqs]->reqh.reqid = reqid++;
226 reqs[nrofreqs]->resph.reqid = reqs[nrofreqs]->reqh.reqid;
227 reqs[nrofreqs]->hPipe = INVALID_HANDLE_VALUE;
228 *req = reqs[nrofreqs];
229 reqs[nrofreqs]->state = REQSTATE_START;
235 RPC_FreeRequest(wine_rpc_request *req) {
236 req->state = REQSTATE_DONE; /* Just reuse slot. */
240 static HRESULT WINAPI
241 PipeBuf_QueryInterface(
242 LPRPCCHANNELBUFFER iface,REFIID riid,LPVOID *ppv
245 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown)) {
246 *ppv = (LPVOID)iface;
247 IUnknown_AddRef(iface);
250 return E_NOINTERFACE;
254 PipeBuf_AddRef(LPRPCCHANNELBUFFER iface) {
255 ICOM_THIS(PipeBuf,iface);
261 PipeBuf_Release(LPRPCCHANNELBUFFER iface) {
262 ICOM_THIS(PipeBuf,iface);
266 ERR("Free all stuff.\n");
267 HeapFree(GetProcessHeap(),0,This);
271 static HRESULT WINAPI
273 LPRPCCHANNELBUFFER iface,RPCOLEMESSAGE* msg,REFIID riid
275 /*ICOM_THIS(PipeBuf,iface);*/
277 TRACE("(%p,%s), slightly wrong.\n",msg,debugstr_guid(riid));
278 /* probably reuses IID in real. */
279 if (msg->cbBuffer && (msg->Buffer == NULL))
280 msg->Buffer = HeapAlloc(GetProcessHeap(),0,msg->cbBuffer);
285 _invoke_onereq(wine_rpc_request *req) {
286 IRpcStubBuffer *stub;
291 hres = MARSHAL_Find_Stub_Buffer(&(req->reqh.mid),&stub);
293 ERR("Stub not found?\n");
296 msg.Buffer = req->Buffer;
297 msg.iMethod = req->reqh.iMethod;
298 msg.cbBuffer = req->reqh.cbBuffer;
299 req->state = REQSTATE_INVOKING;
300 req->resph.retval = IRpcStubBuffer_Invoke(stub,&msg,NULL);
301 req->Buffer = msg.Buffer;
302 req->resph.cbBuffer = msg.cbBuffer;
303 reqtype = REQTYPE_RESPONSE;
304 hres = _xwrite(req->hPipe,&reqtype,sizeof(reqtype));
305 if (hres) return hres;
306 hres = _xwrite(req->hPipe,&(req->resph),sizeof(req->resph));
307 if (hres) return hres;
308 hres = _xwrite(req->hPipe,req->Buffer,req->resph.cbBuffer);
309 if (hres) return hres;
310 req->state = REQSTATE_DONE;
315 static HRESULT _read_one(wine_pipe *xpipe);
318 RPC_QueueRequestAndWait(wine_rpc_request *req) {
320 wine_rpc_request *xreq;
323 wine_pipe *xpipe = PIPE_GetFromMID(&(req->reqh.mid));
326 FIXME("no pipe found.\n");
329 if (GetCurrentProcessId() == req->reqh.mid.processid) {
330 ERR("In current process?\n");
333 req->hPipe = xpipe->hPipe;
334 req->state = REQSTATE_REQ_WAITING_FOR_REPLY;
335 reqtype = REQTYPE_REQUEST;
336 hres = _xwrite(req->hPipe,&reqtype,sizeof(reqtype));
337 if (hres) return hres;
338 hres = _xwrite(req->hPipe,&(req->reqh),sizeof(req->reqh));
339 if (hres) return hres;
340 hres = _xwrite(req->hPipe,req->Buffer,req->reqh.cbBuffer);
341 if (hres) return hres;
344 /*WaitForSingleObject(hRpcChanged,INFINITE);*/
345 hres = _read_one(xpipe);
348 for (i=0;i<nrofreqs;i++) {
350 if ((xreq->state==REQSTATE_REQ_GOT) && (xreq->hPipe==req->hPipe)) {
351 _invoke_onereq(xreq);
354 if (req->state == REQSTATE_RESP_GOT)
360 static HRESULT WINAPI
362 LPRPCCHANNELBUFFER iface,RPCOLEMESSAGE* msg,ULONG *status
364 ICOM_THIS(PipeBuf,iface);
365 wine_rpc_request *req;
370 if (This->mid.processid == GetCurrentProcessId()) {
371 ERR("Need to call directly!\n");
375 hres = RPC_GetRequest(&req);
376 if (hres) return hres;
377 req->reqh.iMethod = msg->iMethod;
378 req->reqh.cbBuffer = msg->cbBuffer;
379 memcpy(&(req->reqh.mid),&(This->mid),sizeof(This->mid));
380 req->Buffer = msg->Buffer;
381 hres = RPC_QueueRequestAndWait(req);
383 RPC_FreeRequest(req);
386 msg->cbBuffer = req->resph.cbBuffer;
387 msg->Buffer = req->Buffer;
388 *status = req->resph.retval;
389 RPC_FreeRequest(req);
394 static HRESULT WINAPI
395 PipeBuf_FreeBuffer(LPRPCCHANNELBUFFER iface,RPCOLEMESSAGE* msg) {
396 FIXME("(%p), stub!\n",msg);
400 static HRESULT WINAPI
402 LPRPCCHANNELBUFFER iface,DWORD* pdwDestContext,void** ppvDestContext
404 FIXME("(%p,%p), stub!\n",pdwDestContext,ppvDestContext);
408 static HRESULT WINAPI
409 PipeBuf_IsConnected(LPRPCCHANNELBUFFER iface) {
410 FIXME("(), stub!\n");
414 static ICOM_VTABLE(IRpcChannelBuffer) pipebufvt = {
415 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
416 PipeBuf_QueryInterface,
427 PIPE_GetNewPipeBuf(wine_marshal_id *mid, IRpcChannelBuffer **pipebuf) {
428 wine_marshal_id ourid;
434 hPipe = PIPE_FindByMID(mid);
435 if (hPipe == INVALID_HANDLE_VALUE) {
437 sprintf(pipefn,OLESTUBMGR"_%08lx",mid->processid);
440 GENERIC_READ|GENERIC_WRITE,
447 if (hPipe == INVALID_HANDLE_VALUE) {
448 FIXME("Could not open named pipe %s, le is %lx\n",pipefn,GetLastError());
451 hres = PIPE_RegisterPipe(mid, hPipe, FALSE);
452 if (hres) return hres;
453 memset(&ourid,0,sizeof(ourid));
454 ourid.processid = GetCurrentProcessId();
455 if (!WriteFile(hPipe,&ourid,sizeof(ourid),&res,NULL)||(res!=sizeof(ourid))) {
456 ERR("Failed writing startup mid!\n");
460 pbuf = (PipeBuf*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(PipeBuf));
461 pbuf->lpVtbl = &pipebufvt;
463 memcpy(&(pbuf->mid),mid,sizeof(*mid));
464 *pipebuf = (IRpcChannelBuffer*)pbuf;
469 create_server(REFCLSID rclsid) {
472 HRESULT hres = E_UNEXPECTED;
474 WCHAR dllName[MAX_PATH+1];
475 DWORD dllNameLen = sizeof(dllName);
477 PROCESS_INFORMATION pinfo;
479 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
481 sprintf(buf,"CLSID\\%s\\LocalServer32",xclsid);
482 hres = RegOpenKeyExA(HKEY_CLASSES_ROOT, buf, 0, KEY_READ, &key);
484 if (hres != ERROR_SUCCESS)
485 return REGDB_E_READREGDB; /* Probably */
487 memset(dllName,0,sizeof(dllName));
488 hres= RegQueryValueExW(key,NULL,NULL,NULL,(LPBYTE)dllName,&dllNameLen);
491 return REGDB_E_CLASSNOTREG; /* FIXME: check retval */
492 memset(&sinfo,0,sizeof(sinfo));
493 sinfo.cb = sizeof(sinfo);
494 if (!CreateProcessW(NULL,dllName,NULL,NULL,FALSE,0,NULL,NULL,&sinfo,&pinfo))
498 /* http://msdn.microsoft.com/library/en-us/dnmsj99/html/com0199.asp, Figure 4 */
499 HRESULT create_marshalled_proxy(REFCLSID rclsid, REFIID iid, LPVOID *ppv) {
504 char marshalbuffer[200];
506 LARGE_INTEGER seekto;
507 ULARGE_INTEGER newpos;
509 #define MAXTRIES 10000
511 strcpy(pipefn,PIPEPREF);
512 WINE_StringFromCLSID(rclsid,pipefn+strlen(PIPEPREF));
514 while (tries++<MAXTRIES) {
517 GENERIC_READ|GENERIC_WRITE,
524 if (hPipe == INVALID_HANDLE_VALUE) {
526 if ((hres = create_server(rclsid)))
530 WARN("Could not open named pipe to broker %s, le is %lx\n",pipefn,GetLastError());
536 if (!ReadFile(hPipe,marshalbuffer,sizeof(marshalbuffer),&bufferlen,NULL)) {
537 FIXME("Failed to read marshal id from classfactory of %s.\n",debugstr_guid(rclsid));
545 return E_NOINTERFACE;
546 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
547 if (hres) return hres;
548 hres = IStream_Write(pStm,marshalbuffer,bufferlen,&res);
550 seekto.u.LowPart = 0;seekto.u.HighPart = 0;
551 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
552 hres = CoUnmarshalInterface(pStm,&IID_IClassFactory,ppv);
554 IStream_Release(pStm);
560 PIPE_StartRequestThread(HANDLE xhPipe) {
561 wine_marshal_id remoteid;
564 hres = _xread(xhPipe,&remoteid,sizeof(remoteid));
566 ERR("Failed to read remote mid!\n");
569 PIPE_RegisterPipe(&remoteid,xhPipe, TRUE);
573 _read_one(wine_pipe *xpipe) {
576 HANDLE xhPipe = xpipe->hPipe;
578 /*FIXME("%lx %d reading reqtype\n",GetCurrentProcessId(),xhPipe);*/
579 hres = _xread(xhPipe,&reqtype,sizeof(reqtype));
581 EnterCriticalSection(&(xpipe->crit));
582 /*FIXME("%lx got reqtype %ld\n",GetCurrentProcessId(),reqtype);*/
584 if (reqtype == REQTYPE_REQUEST) {
585 wine_rpc_request *xreq;
586 RPC_GetRequest(&xreq);
587 xreq->hPipe = xhPipe;
588 hres = _xread(xhPipe,&(xreq->reqh),sizeof(xreq->reqh));
590 xreq->resph.reqid = xreq->reqh.reqid;
591 xreq->Buffer = HeapAlloc(GetProcessHeap(),0, xreq->reqh.cbBuffer);
592 hres = _xread(xhPipe,xreq->Buffer,xreq->reqh.cbBuffer);
594 xreq->state = REQSTATE_REQ_GOT;
597 if (reqtype == REQTYPE_RESPONSE) {
598 wine_rpc_response_header resph;
601 hres = _xread(xhPipe,&resph,sizeof(resph));
603 for (i=nrofreqs;i--;) {
604 wine_rpc_request *xreq = reqs[i];
605 if (xreq->state != REQSTATE_REQ_WAITING_FOR_REPLY)
607 if (xreq->reqh.reqid == resph.reqid) {
608 memcpy(&(xreq->resph),&resph,sizeof(resph));
611 xreq->Buffer = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,xreq->Buffer,xreq->resph.cbBuffer);
613 xreq->Buffer = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,xreq->resph.cbBuffer);
615 hres = _xread(xhPipe,xreq->Buffer,xreq->resph.cbBuffer);
617 xreq->state = REQSTATE_RESP_GOT;
618 /*PulseEvent(hRpcChanged);*/
622 ERR("Did not find request for id %lx\n",resph.reqid);
626 ERR("Unknown reqtype %ld\n",reqtype);
629 LeaveCriticalSection(&(xpipe->crit));
634 _StubReaderThread(LPVOID param) {
635 wine_pipe *xpipe = (wine_pipe*)param;
636 HANDLE xhPipe = xpipe->hPipe;
639 TRACE("STUB reader thread %lx\n",GetCurrentProcessId());
642 hres = _read_one(xpipe);
645 for (i=nrofreqs;i--;) {
646 wine_rpc_request *xreq = reqs[i];
647 if ((xreq->state == REQSTATE_REQ_GOT) && (xreq->hPipe == xhPipe)) {
648 _invoke_onereq(xreq);
652 FIXME("Failed with hres %lx\n",hres);
658 _StubMgrThread(LPVOID param) {
662 sprintf(pipefn,OLESTUBMGR"_%08lx",GetCurrentProcessId());
663 TRACE("Stub Manager Thread starting on (%s)\n",pipefn);
666 listenPipe = CreateNamedPipeA(
669 PIPE_TYPE_BYTE|PIPE_WAIT,
670 PIPE_UNLIMITED_INSTANCES,
673 NMPWAIT_USE_DEFAULT_WAIT,
676 if (listenPipe == INVALID_HANDLE_VALUE) {
677 FIXME("pipe creation failed for %s, le is %lx\n",pipefn,GetLastError());
678 return 1; /* permanent failure, so quit stubmgr thread */
680 if (!ConnectNamedPipe(listenPipe,NULL)) {
681 ERR("Failure during ConnectNamedPipe %lx!\n",GetLastError());
682 CloseHandle(listenPipe);
685 PIPE_StartRequestThread(listenPipe);
692 static BOOL stubMgrRunning = FALSE;
695 if (!stubMgrRunning) {
696 stubMgrRunning = TRUE;
697 CreateThread(NULL,0,_StubMgrThread,NULL,0,&tid);
698 Sleep(2000); /* actually we just try opening the pipe until it succeeds */