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 #include <hardware/qemu_pipe.h>
24 QemuPipeStream::QemuPipeStream(size_t bufSize) :
32 QemuPipeStream::QemuPipeStream(int sock, size_t bufSize) :
40 QemuPipeStream::~QemuPipeStream()
52 int QemuPipeStream::connect(void)
54 m_sock = qemu_pipe_open("opengles");
55 if (!valid()) return -1;
59 void *QemuPipeStream::allocBuffer(size_t minSize)
61 size_t allocSize = (m_bufsize < minSize ? minSize : m_bufsize);
63 m_buf = (unsigned char *)malloc(allocSize);
65 else if (m_bufsize < allocSize) {
66 unsigned char *p = (unsigned char *)realloc(m_buf, allocSize);
69 m_bufsize = allocSize;
71 ERR("realloc (%d) failed\n", allocSize);
81 int QemuPipeStream::commitBuffer(size_t size)
83 return writeFully(m_buf, size);
86 int QemuPipeStream::writeFully(const void *buf, size_t len)
88 //DBG(">> QemuPipeStream::writeFully %d\n", len);
89 if (!valid()) return -1;
92 // If len is non-zero, buf must not be NULL. Otherwise the pipe would be
93 // in a corrupted state, which is lethal for the emulator.
94 ERR("QemuPipeStream::writeFully failed, buf=NULL, len %d,"
95 " lethal error, exiting", len);
105 ssize_t stat = ::write(m_sock, (const char *)(buf) + (len - res), res);
110 if (stat == 0) { /* EOF */
111 ERR("QemuPipeStream::writeFully failed: premature EOF\n");
115 if (errno == EINTR) {
119 ERR("QemuPipeStream::writeFully failed: %s, lethal error, exiting.\n",
123 //DBG("<< QemuPipeStream::writeFully %d\n", len );
127 const unsigned char *QemuPipeStream::readFully(void *buf, size_t len)
129 //DBG(">> QemuPipeStream::readFully %d\n", len);
130 if (!valid()) return NULL;
133 // If len is non-zero, buf must not be NULL. Otherwise the pipe would be
134 // in a corrupted state, which is lethal for the emulator.
135 ERR("QemuPipeStream::readFully failed, buf=NULL, len %zu, lethal"
136 " error, exiting.", len);
139 return NULL; // do not allow NULL buf in that implementation
143 ssize_t stat = ::read(m_sock, (char *)(buf) + len - res, res);
147 } else if (stat < 0) {
148 if (errno == EINTR) {
151 ERR("QemuPipeStream::readFully failed (buf %p, len %zu"
152 ", res %zu): %s, lethal error, exiting.", buf, len, res,
160 //DBG("<< QemuPipeStream::readFully %d\n", len);
161 return (const unsigned char *)buf;
164 const unsigned char *QemuPipeStream::read( void *buf, size_t *inout_len)
166 //DBG(">> QemuPipeStream::read %d\n", *inout_len);
167 if (!valid()) return NULL;
169 ERR("QemuPipeStream::read failed, buf=NULL");
170 return NULL; // do not allow NULL buf in that implementation
173 int n = recv(buf, *inout_len);
177 return (const unsigned char *)buf;
180 //DBG("<< QemuPipeStream::read %d\n", *inout_len);
184 int QemuPipeStream::recv(void *buf, size_t len)
186 if (!valid()) return int(ERR_INVALID_SOCKET);
187 char* p = (char *)buf;
190 int res = ::read(m_sock, p, len);
197 if (res == 0) { /* EOF */