OSDN Git Service

f6ec11c116534e6619932b2cbe7e4db5d7c8ca72
[android-x86/device-generic-goldfish-opengl.git] / system / OpenglSystemCommon / QemuPipeStream.cpp
1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
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
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <string.h>
27
28 QemuPipeStream::QemuPipeStream(size_t bufSize) :
29     IOStream(bufSize),
30     m_sock(-1),
31     m_bufsize(bufSize),
32     m_buf(NULL)
33 {
34 }
35
36 QemuPipeStream::QemuPipeStream(int sock, size_t bufSize) :
37     IOStream(bufSize),
38     m_sock(sock),
39     m_bufsize(bufSize),
40     m_buf(NULL)
41 {
42 }
43
44 QemuPipeStream::~QemuPipeStream()
45 {
46     if (m_sock >= 0) {
47         flush();
48         ::close(m_sock);
49     }
50     if (m_buf != NULL) {
51         free(m_buf);
52     }
53 }
54
55
56 int QemuPipeStream::connect(void)
57 {
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;
64     return 0;
65 }
66
67 void *QemuPipeStream::allocBuffer(size_t minSize)
68 {
69     size_t allocSize = (m_bufsize < minSize ? minSize : m_bufsize);
70     if (!m_buf) {
71         m_buf = (unsigned char *)malloc(allocSize);
72     }
73     else if (m_bufsize < allocSize) {
74         unsigned char *p = (unsigned char *)realloc(m_buf, allocSize);
75         if (p != NULL) {
76             m_buf = p;
77             m_bufsize = allocSize;
78         } else {
79             ERR("realloc (%d) failed\n", allocSize);
80             free(m_buf);
81             m_buf = NULL;
82             m_bufsize = 0;
83         }
84     }
85
86     return m_buf;
87 };
88
89 int QemuPipeStream::commitBuffer(size_t size)
90 {
91     return writeFully(m_buf, size);
92 }
93
94 int QemuPipeStream::writeFully(const void *buf, size_t len)
95 {
96     //DBG(">> QemuPipeStream::writeFully %d\n", len);
97     if (!valid()) return -1;
98     if (!buf) {
99        if (len>0) {
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);
104            abort();
105        }
106        return 0;
107     }
108
109     size_t res = len;
110     int retval = 0;
111
112     while (res > 0) {
113         ssize_t stat = ::write(m_sock, (const char *)(buf) + (len - res), res);
114         if (stat > 0) {
115             res -= stat;
116             continue;
117         }
118         if (stat == 0) { /* EOF */
119             ERR("QemuPipeStream::writeFully failed: premature EOF\n");
120             retval = -1;
121             break;
122         }
123         if (errno == EINTR) {
124             continue;
125         }
126         retval =  stat;
127         ERR("QemuPipeStream::writeFully failed: %s, lethal error, exiting.\n",
128                 strerror(errno));
129         abort();
130     }
131     //DBG("<< QemuPipeStream::writeFully %d\n", len );
132     return retval;
133 }
134
135 const unsigned char *QemuPipeStream::readFully(void *buf, size_t len)
136 {
137     //DBG(">> QemuPipeStream::readFully %d\n", len);
138     if (!valid()) return NULL;
139     if (!buf) {
140         if (len > 0) {
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);
145             abort();
146         }
147         return NULL;  // do not allow NULL buf in that implementation
148     }
149     size_t res = len;
150     while (res > 0) {
151         ssize_t stat = ::read(m_sock, (char *)(buf) + len - res, res);
152         if (stat == 0) {
153             // client shutdown;
154             return NULL;
155         } else if (stat < 0) {
156             if (errno == EINTR) {
157                 continue;
158             } else {
159                 ERR("QemuPipeStream::readFully failed (buf %p, len %zu"
160                     ", res %zu): %s, lethal error, exiting.", buf, len, res,
161                     strerror(errno));
162                 abort();
163             }
164         } else {
165             res -= stat;
166         }
167     }
168     //DBG("<< QemuPipeStream::readFully %d\n", len);
169     return (const unsigned char *)buf;
170 }
171
172 const unsigned char *QemuPipeStream::read( void *buf, size_t *inout_len)
173 {
174     //DBG(">> QemuPipeStream::read %d\n", *inout_len);
175     if (!valid()) return NULL;
176     if (!buf) {
177       ERR("QemuPipeStream::read failed, buf=NULL");
178       return NULL;  // do not allow NULL buf in that implementation
179     }
180
181     int n = recv(buf, *inout_len);
182
183     if (n > 0) {
184         *inout_len = n;
185         return (const unsigned char *)buf;
186     }
187
188     //DBG("<< QemuPipeStream::read %d\n", *inout_len);
189     return NULL;
190 }
191
192 int QemuPipeStream::recv(void *buf, size_t len)
193 {
194     if (!valid()) return int(ERR_INVALID_SOCKET);
195     char* p = (char *)buf;
196     int ret = 0;
197     while(len > 0) {
198         int res = ::read(m_sock, p, len);
199         if (res > 0) {
200             p += res;
201             ret += res;
202             len -= res;
203             continue;
204         }
205         if (res == 0) { /* EOF */
206              break;
207         }
208         if (errno == EINTR)
209             continue;
210
211         /* A real error */
212         if (ret == 0)
213             ret = -1;
214         break;
215     }
216     return ret;
217 }