Add missing LeaveCriticalSection's in error path.
[wine/multimedia.git] / dlls / rpcrt4 / rpcss_np_client.c
blobffbeadb6e5578d9eaedd26b9a04a59b7563745b6
1 /*
2 * RPCSS named pipe client implementation
4 * Copyright (C) 2002 Greg Turner
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
21 #include <assert.h>
23 #include "winbase.h"
24 #include "wine/rpcss_shared.h"
25 #include "wine/debug.h"
27 #include "rpc_binding.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(ole);
31 HANDLE RPCRT4_RpcssNPConnect(void)
33 HANDLE the_pipe = NULL;
34 DWORD dwmode, wait_result;
35 HANDLE master_mutex = RPCRT4_GetMasterMutex();
37 TRACE("\n");
39 while (TRUE) {
41 wait_result = WaitForSingleObject(master_mutex, MASTER_MUTEX_TIMEOUT);
42 switch (wait_result) {
43 case WAIT_ABANDONED:
44 case WAIT_OBJECT_0:
45 break;
46 case WAIT_FAILED:
47 case WAIT_TIMEOUT:
48 default:
49 ERR("This should never happen: couldn't enter mutex.\n");
50 return NULL;
53 /* try to open the client side of the named pipe. */
54 the_pipe = CreateFileA(
55 NAME_RPCSS_NAMED_PIPE, /* pipe name */
56 GENERIC_READ | GENERIC_WRITE, /* r/w access */
57 0, /* no sharing */
58 NULL, /* no security attributes */
59 OPEN_EXISTING, /* open an existing pipe */
60 0, /* default attributes */
61 NULL /* no template file */
64 if (the_pipe != INVALID_HANDLE_VALUE)
65 break;
67 if (GetLastError() != ERROR_PIPE_BUSY) {
68 WARN("Unable to open named pipe %s (assuming unavailable).\n",
69 debugstr_a(NAME_RPCSS_NAMED_PIPE));
70 the_pipe = NULL;
71 break;
74 WARN("Named pipe busy (will wait)\n");
76 if (!ReleaseMutex(master_mutex))
77 ERR("Failed to release master mutex. Expect deadlock.\n");
79 /* wait for the named pipe. We are only
80 willing to wait only 5 seconds. It should be available /very/ soon. */
81 if (! WaitNamedPipeA(NAME_RPCSS_NAMED_PIPE, MASTER_MUTEX_WAITNAMEDPIPE_TIMEOUT))
83 ERR("Named pipe unavailable after waiting. Something is probably wrong.\n");
84 return NULL;
89 if (the_pipe) {
90 dwmode = PIPE_READMODE_MESSAGE;
91 /* SetNamedPipeHandleState not implemented ATM, but still seems to work somehow. */
92 if (! SetNamedPipeHandleState(the_pipe, &dwmode, NULL, NULL))
93 WARN("Failed to set pipe handle state\n");
96 if (!ReleaseMutex(master_mutex))
97 ERR("Uh oh, failed to leave the RPC Master Mutex!\n");
99 return the_pipe;
102 BOOL RPCRT4_SendReceiveNPMsg(HANDLE np, PRPCSS_NP_MESSAGE msg, char *vardata, PRPCSS_NP_REPLY reply)
104 DWORD count;
105 UINT32 payload_offset;
106 RPCSS_NP_MESSAGE vardata_payload_msg;
108 TRACE("(np == %p, msg == %p, vardata == %p, reply == %p)\n",
109 np, msg, vardata, reply);
111 if (! WriteFile(np, msg, sizeof(RPCSS_NP_MESSAGE), &count, NULL)) {
112 ERR("write failed.\n");
113 return FALSE;
116 if (count != sizeof(RPCSS_NP_MESSAGE)) {
117 ERR("write count mismatch.\n");
118 return FALSE;
121 /* process the vardata payload if neccesary */
122 vardata_payload_msg.message_type = RPCSS_NP_MESSAGE_TYPEID_VARDATAPAYLOADMSG;
123 vardata_payload_msg.vardata_payload_size = 0; /* meaningless */
124 for ( payload_offset = 0; payload_offset < msg->vardata_payload_size;
125 payload_offset += VARDATA_PAYLOAD_BYTES ) {
126 TRACE("sending vardata payload. vd=%p, po=%d, ps=%d\n", vardata,
127 payload_offset, msg->vardata_payload_size);
128 ZeroMemory(vardata_payload_msg.message.vardatapayloadmsg.payload, VARDATA_PAYLOAD_BYTES);
129 CopyMemory(vardata_payload_msg.message.vardatapayloadmsg.payload,
130 vardata,
131 min( VARDATA_PAYLOAD_BYTES, msg->vardata_payload_size - payload_offset ));
132 vardata += VARDATA_PAYLOAD_BYTES;
133 if (! WriteFile(np, &vardata_payload_msg, sizeof(RPCSS_NP_MESSAGE), &count, NULL)) {
134 ERR("vardata write failed at %u bytes.\n", payload_offset);
135 return FALSE;
139 if (! ReadFile(np, reply, sizeof(RPCSS_NP_REPLY), &count, NULL)) {
140 ERR("read failed.\n");
141 return FALSE;
144 if (count != sizeof(RPCSS_NP_REPLY)) {
145 ERR("read count mismatch. got %ld, expected %u.\n", count, sizeof(RPCSS_NP_REPLY));
146 return FALSE;
149 /* message execution was successful */
150 return TRUE;