Moved IOStream.h to be used in external API for libOpenGLRender.

This change includes four changes:
1) moved IOStream.h into host/include/libOpenGLRender, this directory
   will include the api interface into the libOpenGLRender which will be
   used later by the emulator and we need this interface to use IOStream.h
2) Updated Andorid.mk files to include the new directory location of IOStream.h
   in the LOCAL_C_INCLUDE.
3) Added new function "read" to IOStream which reads a message without a givven
   size.
4) Updated TcpStream to use "cutils/sockets.h" instead of using directly the socket api for portability reasons. (It now compiles on windows as well).

Change-Id: I30eb40c8dcd5aacf0d993aff9cdb90b283b12dde
This commit is contained in:
Guy Zadikario
2011-04-08 11:12:35 +03:00
parent b24b247080
commit caafd4df2e
10 changed files with 69 additions and 76 deletions

View File

@@ -14,7 +14,7 @@ OpenglCodecCommon := \
LOCAL_SRC_FILES := $(OpenglCodecCommon)
LOCAL_C_INCLUDES += $(emulatorOpengl)/host/include/libOpenGLRender
LOCAL_C_INCLUDES += $(emulatorOpengl)/host/include/libOpenglRender
LOCAL_CFLAGS += -DLOG_TAG=\"eglCodecCommon\"
LOCAL_MODULE_TAGS := debug
@@ -27,7 +27,7 @@ include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(OpenglCodecCommon)
LOCAL_C_INCLUDES += $(emulatorOpengl)/host/include/libOpenGLRender
LOCAL_C_INCLUDES += $(emulatorOpengl)/host/include/libOpenglRender
LOCAL_MODULE_TAGS := debug
LOCAL_MODULE := libOpenglCodecCommon

View File

@@ -1,90 +0,0 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __IO_STREAM_H__
#define __IO_STREAM_H__
#include <stdlib.h>
#include <stdio.h>
#include "ErrorLog.h"
class IOStream {
public:
IOStream(size_t bufSize) {
m_buf = NULL;
m_bufsize = bufSize;
m_free = 0;
}
virtual void *allocBuffer(size_t minSize) = 0;
virtual int commitBuffer(size_t size) = 0;
virtual const unsigned char *readFully( void *buf, size_t len) = 0;
virtual ~IOStream() {
// NOTE: m_buf is 'owned' by the child class thus we expect it to be released by it
}
unsigned char *alloc(size_t len) {
if (m_buf && len > m_free) {
if (flush() < 0) {
ERR("Failed to flush in alloc\n");
return NULL; // we failed to flush so something is wrong
}
}
if (!m_buf || len > m_bufsize) {
int allocLen = m_bufsize < len ? len : m_bufsize;
m_buf = (unsigned char *)allocBuffer(allocLen);
if (!m_buf) {
ERR("Alloc (%u bytes) failed\n", allocLen);
return NULL;
}
m_bufsize = m_free = allocLen;
}
unsigned char *ptr;
ptr = m_buf + (m_bufsize - m_free);
m_free -= len;
return ptr;
}
int flush() {
if (!m_buf || m_free == m_bufsize) return 0;
int stat = commitBuffer(m_bufsize - m_free);
m_buf = NULL;
m_free = 0;
return stat;
}
const unsigned char *readback(void *buf, size_t len) {
flush();
return readFully(buf, len);
}
private:
unsigned char *m_buf;
size_t m_bufsize;
size_t m_free;
};
#endif

View File

@@ -14,25 +14,23 @@
* limitations under the License.
*/
#include "TcpStream.h"
#ifdef ANDROID
#include <netinet/in.h>
#endif
#include <cutils/sockets.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
TcpStream::TcpStream(size_t bufSize) : IOStream(bufSize)
#ifdef __linux__
#include <netinet/in.h>
#endif
TcpStream::TcpStream(size_t bufSize) :
IOStream(bufSize),
m_sock(-1),
m_bufsize(bufSize),
m_buf(NULL)
{
m_sock = socket(AF_INET, SOCK_STREAM, 0);
m_bufsize = bufSize;
m_buf = NULL;
}
TcpStream::TcpStream(int sock, size_t bufSize) :
@@ -41,7 +39,6 @@ TcpStream::TcpStream(int sock, size_t bufSize) :
m_bufsize(bufSize),
m_buf(NULL)
{
}
TcpStream::~TcpStream()
@@ -55,39 +52,15 @@ TcpStream::~TcpStream()
}
int TcpStream::listen(unsigned short port, bool localhost_only, bool reuse_address)
int TcpStream::listen(unsigned short port, bool localhost_only)
{
if (localhost_only) {
m_sock = socket_loopback_server(port, SOCK_STREAM);
} else {
m_sock = socket_inaddr_any_server(port, SOCK_STREAM);
}
if (!valid()) return int(ERR_INVALID_SOCKET);
// NOTE: This is a potential security issue. However, since we accept connection
// from local host only, this should be reasonably OK.
if (reuse_address) {
int one = 1;
if (setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) {
perror("setsockopt resuseaddr");
}
}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
if (localhost_only) {
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
} else {
addr.sin_addr.s_addr = INADDR_ANY;
}
if (::bind(m_sock, (const sockaddr *) &addr, sizeof(addr)) < 0) {
perror("bind");
return -1;
}
if (::listen(m_sock, 5) < 0) {
perror("listen");
return -1;
}
return 0;
}
@@ -117,29 +90,8 @@ TcpStream * TcpStream::accept()
int TcpStream::connect(const char *hostname, unsigned short port)
{
struct addrinfo *ai;
char portstr[10];
snprintf(portstr, sizeof(portstr), "%d", port);
if (getaddrinfo(hostname, portstr, NULL, &ai) != 0) {
return -1;
}
struct addrinfo *i;
i = ai;
while (i != NULL) {
if (::connect(m_sock, i->ai_addr, i->ai_addrlen) >= 0) {
break;
} else {
if (errno != EINTR) {
i = i->ai_next;
}
}
}
freeaddrinfo(ai);
if (i == NULL) return -1;
m_sock = socket_network_client(hostname, port, SOCK_STREAM);
if (!valid()) return -1;
return 0;
}
@@ -178,10 +130,11 @@ int TcpStream::writeFully(const void *buf, size_t len)
int retval = 0;
while (res > 0) {
ssize_t stat = ::send(m_sock, (unsigned char *)(buf) + (len - res), res, 0);
ssize_t stat = ::send(m_sock, (const char *)(buf) + (len - res), res, 0);
if (stat < 0) {
if (errno != EINTR) {
retval = stat;
ERR("TcpStream::writeFully failed, errno = %d\n", errno);
break;
}
} else {
@@ -194,10 +147,13 @@ int TcpStream::writeFully(const void *buf, size_t len)
const unsigned char *TcpStream::readFully(void *buf, size_t len)
{
if (!valid()) return NULL;
if (!buf) return NULL; // do not allow NULL buf in that implementation
if (!buf) {
ERR("TcpStream::readFully failed, buf=NULL");
return NULL; // do not allow NULL buf in that implementation
}
size_t res = len;
while (res > 0) {
ssize_t stat = ::recv(m_sock, (unsigned char *)(buf) + len - res, len, MSG_WAITALL);
ssize_t stat = ::recv(m_sock, (char *)(buf) + len - res, len, 0);
if (stat == 0) {
// client shutdown;
return NULL;
@@ -205,6 +161,7 @@ const unsigned char *TcpStream::readFully(void *buf, size_t len)
if (errno == EINTR) {
continue;
} else {
ERR("TcpStream::readFully failed, errno = %d 0x%x \n", errno,buf);
return NULL;
}
} else {
@@ -214,12 +171,33 @@ const unsigned char *TcpStream::readFully(void *buf, size_t len)
return (const unsigned char *)buf;
}
const unsigned char *TcpStream::read( void *buf, size_t *inout_len)
{
if (!valid()) return NULL;
if (!buf) {
ERR("TcpStream::read failed, buf=NULL");
return NULL; // do not allow NULL buf in that implementation
}
int n;
do {
n = recv(buf, *inout_len);
} while( n < 0 && errno == EINTR );
if (n > 0) {
*inout_len = n;
return (const unsigned char *)buf;
}
return NULL;
}
int TcpStream::recv(void *buf, size_t len)
{
if (!valid()) return int(ERR_INVALID_SOCKET);
int res = 0;
while(true) {
res = ::recv(m_sock, buf, len, 0);
res = ::recv(m_sock, (char *)buf, len, 0);
if (res < 0) {
if (errno == EINTR) {
continue;

View File

@@ -26,13 +26,14 @@ public:
explicit TcpStream(size_t bufsize = 10000);
~TcpStream();
int listen(unsigned short port, bool localhost_only = true, bool reuse_address = true);
int listen(unsigned short port, bool localhost_only = true);
TcpStream *accept();
int connect(const char *hostname, unsigned short port);
virtual void *allocBuffer(size_t minSize);
virtual int commitBuffer(size_t size);
virtual const unsigned char *readFully( void *buf, size_t len);
virtual const unsigned char *read( void *buf, size_t *inout_len);
bool valid() { return m_sock >= 0; }
int recv(void *buf, size_t len);