Sync to Wine-20050830:
[reactos.git] / reactos / lib / rpcrt4 / rpcss_np_client.c
1 /*
2 * RPCSS named pipe client implementation
3 *
4 * Copyright (C) 2002 Greg Turner
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include <assert.h>
22 #include <stdarg.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "ntstatus.h"
27 #include "wine/rpcss_shared.h"
28 #include "wine/debug.h"
29
30 #include "rpc_binding.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(ole);
33
34 HANDLE RPCRT4_RpcssNPConnect(void)
35 {
36 HANDLE the_pipe = NULL;
37 DWORD dwmode, wait_result;
38 HANDLE master_mutex = RPCRT4_GetMasterMutex();
39
40 TRACE("\n");
41
42 while (TRUE) {
43
44 wait_result = WaitForSingleObject(master_mutex, MASTER_MUTEX_TIMEOUT);
45 switch (wait_result) {
46 case WAIT_ABANDONED:
47 case WAIT_OBJECT_0:
48 break;
49 case WAIT_FAILED:
50 case WAIT_TIMEOUT:
51 default:
52 ERR("This should never happen: couldn't enter mutex.\n");
53 return NULL;
54 }
55
56 /* try to open the client side of the named pipe. */
57 the_pipe = CreateFileA(
58 NAME_RPCSS_NAMED_PIPE, /* pipe name */
59 GENERIC_READ | GENERIC_WRITE, /* r/w access */
60 0, /* no sharing */
61 NULL, /* no security attributes */
62 OPEN_EXISTING, /* open an existing pipe */
63 0, /* default attributes */
64 NULL /* no template file */
65 );
66
67 if (the_pipe != INVALID_HANDLE_VALUE)
68 break;
69
70 if (GetLastError() != ERROR_PIPE_BUSY) {
71 WARN("Unable to open named pipe %s (assuming unavailable).\n",
72 debugstr_a(NAME_RPCSS_NAMED_PIPE));
73 the_pipe = NULL;
74 break;
75 }
76
77 WARN("Named pipe busy (will wait)\n");
78
79 if (!ReleaseMutex(master_mutex))
80 ERR("Failed to release master mutex. Expect deadlock.\n");
81
82 /* wait for the named pipe. We are only
83 willing to wait only 5 seconds. It should be available /very/ soon. */
84 if (! WaitNamedPipeA(NAME_RPCSS_NAMED_PIPE, MASTER_MUTEX_WAITNAMEDPIPE_TIMEOUT))
85 {
86 ERR("Named pipe unavailable after waiting. Something is probably wrong.\n");
87 the_pipe = NULL;
88 break;
89 }
90
91 }
92
93 if (the_pipe) {
94 dwmode = PIPE_READMODE_MESSAGE;
95 /* SetNamedPipeHandleState not implemented ATM, but still seems to work somehow. */
96 if (! SetNamedPipeHandleState(the_pipe, &dwmode, NULL, NULL))
97 WARN("Failed to set pipe handle state\n");
98 }
99
100 if (!ReleaseMutex(master_mutex))
101 ERR("Uh oh, failed to leave the RPC Master Mutex!\n");
102
103 return the_pipe;
104 }
105
106 BOOL RPCRT4_SendReceiveNPMsg(HANDLE np, PRPCSS_NP_MESSAGE msg, char *vardata, PRPCSS_NP_REPLY reply)
107 {
108 DWORD count;
109 UINT32 payload_offset;
110 RPCSS_NP_MESSAGE vardata_payload_msg;
111
112 TRACE("(np == %p, msg == %p, vardata == %p, reply == %p)\n",
113 np, msg, vardata, reply);
114
115 if (! WriteFile(np, msg, sizeof(RPCSS_NP_MESSAGE), &count, NULL)) {
116 ERR("write failed.\n");
117 return FALSE;
118 }
119
120 if (count != sizeof(RPCSS_NP_MESSAGE)) {
121 ERR("write count mismatch.\n");
122 return FALSE;
123 }
124
125 /* process the vardata payload if necessary */
126 vardata_payload_msg.message_type = RPCSS_NP_MESSAGE_TYPEID_VARDATAPAYLOADMSG;
127 vardata_payload_msg.vardata_payload_size = 0; /* meaningless */
128 for ( payload_offset = 0; payload_offset < msg->vardata_payload_size;
129 payload_offset += VARDATA_PAYLOAD_BYTES ) {
130 TRACE("sending vardata payload. vd=%p, po=%d, ps=%d\n", vardata,
131 payload_offset, msg->vardata_payload_size);
132 ZeroMemory(vardata_payload_msg.message.vardatapayloadmsg.payload, VARDATA_PAYLOAD_BYTES);
133 CopyMemory(vardata_payload_msg.message.vardatapayloadmsg.payload,
134 vardata,
135 min( VARDATA_PAYLOAD_BYTES, msg->vardata_payload_size - payload_offset ));
136 vardata += VARDATA_PAYLOAD_BYTES;
137 if (! WriteFile(np, &vardata_payload_msg, sizeof(RPCSS_NP_MESSAGE), &count, NULL)) {
138 ERR("vardata write failed at %u bytes.\n", payload_offset);
139 return FALSE;
140 }
141 }
142
143 if (! ReadFile(np, reply, sizeof(RPCSS_NP_REPLY), &count, NULL)) {
144 ERR("read failed.\n");
145 return FALSE;
146 }
147
148 if (count != sizeof(RPCSS_NP_REPLY)) {
149 ERR("read count mismatch. got %ld, expected %u.\n", count, sizeof(RPCSS_NP_REPLY));
150 return FALSE;
151 }
152
153 /* message execution was successful */
154 return TRUE;
155 }