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 "QemuPipeStream.h"
17 #if PLATFORM_SDK_VERSION > 24
18 #include <qemu_pipe.h>
19 #else // PLATFORM_SDK_VERSION
20 #include <hardware/qemu_pipe.h>
21 #endif //PLATFORM_SDK_VERSION
28 QemuPipeStream::QemuPipeStream(size_t bufSize) :
36 QemuPipeStream::QemuPipeStream(int sock, size_t bufSize) :
44 QemuPipeStream::~QemuPipeStream()
56 int QemuPipeStream::connect(void)
58 #if PLATFORM_SDK_VERSION > 24
59 m_sock = qemu_pipe_open("pipe:opengles");
60 #else // PLATFORM_SDK_VERSION
61 m_sock = qemu_pipe_open("opengles");
62 #endif // PLATFORM_SDK_VERSION
63 if (!valid()) return -1;
67 void *QemuPipeStream::allocBuffer(size_t minSize)
69 size_t allocSize = (m_bufsize < minSize ? minSize : m_bufsize);
71 m_buf = (unsigned char *)malloc(allocSize);
73 else if (m_bufsize < allocSize) {
74 unsigned char *p = (unsigned char *)realloc(m_buf, allocSize);
77 m_bufsize = allocSize;
79 ERR("realloc (%d) failed\n", allocSize);
89 int QemuPipeStream::commitBuffer(size_t size)
91 return writeFully(m_buf, size);
94 int QemuPipeStream::writeFully(const void *buf, size_t len)
96 //DBG(">> QemuPipeStream::writeFully %d\n", len);
97 if (!valid()) return -1;
100 // If len is non-zero, buf must not be NULL. Otherwise the pipe would be
101 // in a corrupted state, which is lethal for the emulator.
102 ERR("QemuPipeStream::writeFully failed, buf=NULL, len %d,"
103 " lethal error, exiting", len);
113 ssize_t stat = ::write(m_sock, (const char *)(buf) + (len - res), res);
118 if (stat == 0) { /* EOF */
119 ERR("QemuPipeStream::writeFully failed: premature EOF\n");
123 if (errno == EINTR) {
127 ERR("QemuPipeStream::writeFully failed: %s, lethal error, exiting.\n",
131 //DBG("<< QemuPipeStream::writeFully %d\n", len );
135 const unsigned char *QemuPipeStream::readFully(void *buf, size_t len)
137 //DBG(">> QemuPipeStream::readFully %d\n", len);
138 if (!valid()) return NULL;
141 // If len is non-zero, buf must not be NULL. Otherwise the pipe would be
142 // in a corrupted state, which is lethal for the emulator.
143 ERR("QemuPipeStream::readFully failed, buf=NULL, len %zu, lethal"
144 " error, exiting.", len);
147 return NULL; // do not allow NULL buf in that implementation
151 ssize_t stat = ::read(m_sock, (char *)(buf) + len - res, res);
155 } else if (stat < 0) {
156 if (errno == EINTR) {
159 ERR("QemuPipeStream::readFully failed (buf %p, len %zu"
160 ", res %zu): %s, lethal error, exiting.", buf, len, res,
168 //DBG("<< QemuPipeStream::readFully %d\n", len);
169 return (const unsigned char *)buf;
172 const unsigned char *QemuPipeStream::read( void *buf, size_t *inout_len)
174 //DBG(">> QemuPipeStream::read %d\n", *inout_len);
175 if (!valid()) return NULL;
177 ERR("QemuPipeStream::read failed, buf=NULL");
178 return NULL; // do not allow NULL buf in that implementation
181 int n = recv(buf, *inout_len);
185 return (const unsigned char *)buf;
188 //DBG("<< QemuPipeStream::read %d\n", *inout_len);
192 int QemuPipeStream::recv(void *buf, size_t len)
194 if (!valid()) return int(ERR_INVALID_SOCKET);
195 char* p = (char *)buf;
198 int res = ::read(m_sock, p, len);
205 if (res == 0) { /* EOF */