2 * Copyright (C) 2011 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 #include "Win32PipeStream.h"
26 #error ONLY BUILD THIS SOURCE FILE FOR WINDOWS!
29 /* The official documentation states that the name of a given named
30 * pipe cannot be more than 256 characters long.
32 #define NAMED_PIPE_MAX 256
34 Win32PipeStream::Win32PipeStream(size_t bufSize) :
35 SocketStream(bufSize),
36 m_pipe(INVALID_HANDLE_VALUE)
40 Win32PipeStream::Win32PipeStream(HANDLE pipe, size_t bufSize) :
41 SocketStream(-1, bufSize),
46 Win32PipeStream::~Win32PipeStream()
48 if (m_pipe != INVALID_HANDLE_VALUE) {
50 m_pipe = INVALID_HANDLE_VALUE;
54 /* Initialize the pipe name corresponding to a given port
57 make_pipe_name(char *path, size_t pathlen, int port_number)
59 snprintf(path, pathlen, "\\\\.\\pipe\\qemu-gles-%d", port_number);
63 /* Technical note: Named pipes work differently from BSD Sockets.
64 * One does not create/bind a pipe, and collect a new handle each
65 * time a client connects with accept().
67 * Instead, the server creates a new pipe instance each time it wants
68 * to get a new client connection, then calls ConnectNamedPipe() to
69 * wait for a connection.
71 * So listen() is a no-op, and accept() really creates the pipe handle.
73 * Also, connect() must create a pipe handle with CreateFile() and
74 * wait for a server instance with WaitNamedPipe()
76 int Win32PipeStream::listen(unsigned short port)
78 // just save the port number for accept()
83 SocketStream * Win32PipeStream::accept()
85 char path[NAMED_PIPE_MAX+1];
86 SocketStream* clientStream;
89 make_pipe_name(path, sizeof(path), m_port);
91 pipe = ::CreateNamedPipe(
93 PIPE_ACCESS_DUPLEX, // read-write access
94 PIPE_TYPE_BYTE | // byte-oriented writes
95 PIPE_READMODE_BYTE | // byte-oriented reads
96 PIPE_WAIT, // blocking operations
97 PIPE_UNLIMITED_INSTANCES, // no limit on clients
98 4096, // input buffer size
99 4096, // output buffer size
100 0, // client time-out
101 NULL); // default security attributes
103 if (pipe == INVALID_HANDLE_VALUE) {
104 ERR("%s: CreateNamedPipe failed %d\n", __FUNCTION__, (int)GetLastError());
108 // Stupid Win32 API design: If a client is already connected, then
109 // ConnectNamedPipe will return 0, and GetLastError() will return
110 // ERROR_PIPE_CONNECTED. This is not an error! It just means that the
111 // function didn't have to wait.
113 if (::ConnectNamedPipe(pipe, NULL) == 0 && GetLastError() != ERROR_PIPE_CONNECTED) {
114 ERR("%s: ConnectNamedPipe failed: %d\n", __FUNCTION__, (int)GetLastError());
119 clientStream = new Win32PipeStream(pipe, m_bufsize);
123 int Win32PipeStream::connect(unsigned short port)
125 char path[NAMED_PIPE_MAX+1];
129 make_pipe_name(path, sizeof(path), port);
131 /* We're going to loop in order to wait for the pipe server to
134 for (; tries > 0; tries--) {
137 GENERIC_READ | GENERIC_WRITE, // read & write
139 NULL, // default security attrs
140 OPEN_EXISTING, // open existing pipe
141 0, // default attributes
142 NULL); // no template file
144 /* If we have a valid pipe handle, break from the loop */
145 if (pipe != INVALID_HANDLE_VALUE) {
149 /* We can get here if the pipe is busy, i.e. if the server hasn't
150 * create a new pipe instance to service our request. In which case
151 * GetLastError() will return ERROR_PIPE_BUSY.
153 * If so, then use WaitNamedPipe() to wait for a decent time
156 if (GetLastError() != ERROR_PIPE_BUSY) {
157 /* Not ERROR_PIPE_BUSY */
158 ERR("%s: CreateFile failed: %d\n", __FUNCTION__, (int)GetLastError());
163 /* Wait for 5 seconds */
164 if ( !WaitNamedPipe(path, 5000) ) {
165 ERR("%s: WaitNamedPipe failed: %d\n", __FUNCTION__, (int)GetLastError());
175 /* Special buffer methods, since we can't use socket functions here */
177 int Win32PipeStream::commitBuffer(size_t size)
179 if (m_pipe == INVALID_HANDLE_VALUE)
187 if (! ::WriteFile(m_pipe, (const char *)m_buf + (size - res), res, &written, NULL)) {
189 ERR("%s: failed: %d\n", __FUNCTION__, (int)GetLastError());
197 const unsigned char *Win32PipeStream::readFully(void *buf, size_t len)
199 const unsigned char* ret = NULL;
201 if (m_pipe == INVALID_HANDLE_VALUE)
205 return NULL; // do not allow NULL buf in that implementation
211 if (! ::ReadFile(m_pipe, (char *)buf + (len - res), res, &readcount, NULL) || readcount == 0) {
212 errno = (int)GetLastError();
217 return (const unsigned char *)buf;
220 const unsigned char *Win32PipeStream::read( void *buf, size_t *inout_len)
222 size_t len = *inout_len;
225 if (m_pipe == INVALID_HANDLE_VALUE)
229 return NULL; // do not allow NULL buf in that implementation
232 if (!::ReadFile(m_pipe, (char *)buf, len, &readcount, NULL)) {
233 errno = (int)GetLastError();
237 *inout_len = (size_t)readcount;
238 return (const unsigned char *)buf;