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

@@ -33,6 +33,7 @@ public:
virtual void *allocBuffer(size_t minSize) = 0; virtual void *allocBuffer(size_t minSize) = 0;
virtual int commitBuffer(size_t size) = 0; virtual int commitBuffer(size_t size) = 0;
virtual const unsigned char *readFully( void *buf, size_t len) = 0; virtual const unsigned char *readFully( void *buf, size_t len) = 0;
virtual const unsigned char *read( void *buf, size_t *inout_len) = 0;
virtual ~IOStream() { virtual ~IOStream() {

View File

@@ -19,6 +19,7 @@ LOCAL_SRC_FILES := \
LOCAL_C_INCLUDES += \ LOCAL_C_INCLUDES += \
$(emulatorOpengl)/shared/OpenglCodecCommon \ $(emulatorOpengl)/shared/OpenglCodecCommon \
$(emulatorOpengl)/host/include/libOpenglRender \
$(emulatorOpengl)/system/GLESv1_enc $(emulatorOpengl)/system/GLESv1_enc
LOCAL_STATIC_LIBRARIES := \ LOCAL_STATIC_LIBRARIES := \

View File

@@ -14,7 +14,7 @@ OpenglCodecCommon := \
LOCAL_SRC_FILES := $(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_CFLAGS += -DLOG_TAG=\"eglCodecCommon\"
LOCAL_MODULE_TAGS := debug LOCAL_MODULE_TAGS := debug
@@ -27,7 +27,7 @@ include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(OpenglCodecCommon) LOCAL_SRC_FILES := $(OpenglCodecCommon)
LOCAL_C_INCLUDES += $(emulatorOpengl)/host/include/libOpenGLRender LOCAL_C_INCLUDES += $(emulatorOpengl)/host/include/libOpenglRender
LOCAL_MODULE_TAGS := debug LOCAL_MODULE_TAGS := debug
LOCAL_MODULE := libOpenglCodecCommon LOCAL_MODULE := libOpenglCodecCommon

View File

@@ -14,25 +14,23 @@
* limitations under the License. * limitations under the License.
*/ */
#include "TcpStream.h" #include "TcpStream.h"
#include <cutils/sockets.h>
#ifdef ANDROID
#include <netinet/in.h>
#endif
#include <errno.h> #include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <string.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) : TcpStream::TcpStream(int sock, size_t bufSize) :
@@ -41,7 +39,6 @@ TcpStream::TcpStream(int sock, size_t bufSize) :
m_bufsize(bufSize), m_bufsize(bufSize),
m_buf(NULL) m_buf(NULL)
{ {
} }
TcpStream::~TcpStream() 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); 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; return 0;
} }
@@ -117,29 +90,8 @@ TcpStream * TcpStream::accept()
int TcpStream::connect(const char *hostname, unsigned short port) int TcpStream::connect(const char *hostname, unsigned short port)
{ {
struct addrinfo *ai; m_sock = socket_network_client(hostname, port, SOCK_STREAM);
char portstr[10]; if (!valid()) return -1;
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;
return 0; return 0;
} }
@@ -178,10 +130,11 @@ int TcpStream::writeFully(const void *buf, size_t len)
int retval = 0; int retval = 0;
while (res > 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 (stat < 0) {
if (errno != EINTR) { if (errno != EINTR) {
retval = stat; retval = stat;
ERR("TcpStream::writeFully failed, errno = %d\n", errno);
break; break;
} }
} else { } else {
@@ -194,10 +147,13 @@ int TcpStream::writeFully(const void *buf, size_t len)
const unsigned char *TcpStream::readFully(void *buf, size_t len) const unsigned char *TcpStream::readFully(void *buf, size_t len)
{ {
if (!valid()) return NULL; 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; size_t res = len;
while (res > 0) { 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) { if (stat == 0) {
// client shutdown; // client shutdown;
return NULL; return NULL;
@@ -205,6 +161,7 @@ const unsigned char *TcpStream::readFully(void *buf, size_t len)
if (errno == EINTR) { if (errno == EINTR) {
continue; continue;
} else { } else {
ERR("TcpStream::readFully failed, errno = %d 0x%x \n", errno,buf);
return NULL; return NULL;
} }
} else { } else {
@@ -214,12 +171,33 @@ const unsigned char *TcpStream::readFully(void *buf, size_t len)
return (const unsigned char *)buf; 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) int TcpStream::recv(void *buf, size_t len)
{ {
if (!valid()) return int(ERR_INVALID_SOCKET); if (!valid()) return int(ERR_INVALID_SOCKET);
int res = 0; int res = 0;
while(true) { while(true) {
res = ::recv(m_sock, buf, len, 0); res = ::recv(m_sock, (char *)buf, len, 0);
if (res < 0) { if (res < 0) {
if (errno == EINTR) { if (errno == EINTR) {
continue; continue;

View File

@@ -26,13 +26,14 @@ public:
explicit TcpStream(size_t bufsize = 10000); explicit TcpStream(size_t bufsize = 10000);
~TcpStream(); ~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(); TcpStream *accept();
int connect(const char *hostname, unsigned short port); int connect(const char *hostname, unsigned short port);
virtual void *allocBuffer(size_t minSize); virtual void *allocBuffer(size_t minSize);
virtual int commitBuffer(size_t size); virtual int commitBuffer(size_t size);
virtual const unsigned char *readFully( void *buf, size_t len); 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; } bool valid() { return m_sock >= 0; }
int recv(void *buf, size_t len); int recv(void *buf, size_t len);

View File

@@ -19,6 +19,7 @@ LOCAL_PRELINK_MODULE := false
LOCAL_CFLAGS += -DLOG_TAG=\"egl_GLESv1_enc\" LOCAL_CFLAGS += -DLOG_TAG=\"egl_GLESv1_enc\"
LOCAL_C_INCLUDES += \ LOCAL_C_INCLUDES += \
$(emulatorOpengl)/shared/OpenglCodecCommon \ $(emulatorOpengl)/shared/OpenglCodecCommon \
$(emulatorOpengl)/host/include/libOpenglRender \
$(glesv1_intermediates) $(glesv1_intermediates)
LOCAL_STATIC_LIBRARIES := \ LOCAL_STATIC_LIBRARIES := \

View File

@@ -21,6 +21,7 @@ LOCAL_ADDITIONAL_DEPENDENCIES := \
emulatorOpengl := $(LOCAL_PATH)/../.. emulatorOpengl := $(LOCAL_PATH)/../..
LOCAL_C_INCLUDES := $(emulatorOpengl)/shared/OpenglCodecCommon \ LOCAL_C_INCLUDES := $(emulatorOpengl)/shared/OpenglCodecCommon \
$(emulatorOpengl)/host/include/libOpenglRender \
$(call intermediates-dir-for, SHARED_LIBRARIES, libut_rendercontrol_enc) \ $(call intermediates-dir-for, SHARED_LIBRARIES, libut_rendercontrol_enc) \
$(call intermediates-dir-for, SHARED_LIBRARIES, libGLESv1_enc) \ $(call intermediates-dir-for, SHARED_LIBRARIES, libGLESv1_enc) \
$(emulatorOpengl)/system/GLESv1_enc \ $(emulatorOpengl)/system/GLESv1_enc \

View File

@@ -18,7 +18,11 @@ intermediates := $(local-intermediates-dir)
LOCAL_STATIC_LIBRARIES := \ LOCAL_STATIC_LIBRARIES := \
libOpenglCodecCommon \ libOpenglCodecCommon \
liblog liblog
LOCAL_C_INCLUDES += $(emulatorOpengl)/shared/OpenglCodecCommon $(emulatorOpengl)/tests/ut_rendercontrol_enc
LOCAL_C_INCLUDES += \
$(emulatorOpengl)/shared/OpenglCodecCommon \
$(emulatorOpengl)/host/include/libOpenglRender \
$(emulatorOpengl)/tests/ut_rendercontrol_enc
#we use only *_dec.h as a sentinel for the other generated headers #we use only *_dec.h as a sentinel for the other generated headers
GEN := $(intermediates)/ut_rendercontrol_dec.cpp $(intermediates)/ut_rendercontrol_dec.h GEN := $(intermediates)/ut_rendercontrol_dec.cpp $(intermediates)/ut_rendercontrol_dec.h

View File

@@ -12,7 +12,9 @@ LOCAL_MODULE_CLASS := SHARED_LIBRARIES
ut_intermediates := $(local-intermediates-dir) ut_intermediates := $(local-intermediates-dir)
LOCAL_C_INCLUDES += $(emulatorOpengl)/shared/OpenglCodecCommon LOCAL_C_INCLUDES += \
$(emulatorOpengl)/shared/OpenglCodecCommon \
$(emulatorOpengl)/host/include/libOpenglRender
LOCAL_STATIC_LIBRARIES := \ LOCAL_STATIC_LIBRARIES := \
libOpenglCodecCommon libOpenglCodecCommon

View File

@@ -43,6 +43,7 @@ LOCAL_CFLAGS := -DPVR_WAR
#LOCAL_CFLAGS += -g -O0 #LOCAL_CFLAGS += -g -O0
LOCAL_C_INCLUDES := $(emulatorOpengl)/shared/OpenglCodecCommon \ LOCAL_C_INCLUDES := $(emulatorOpengl)/shared/OpenglCodecCommon \
$(emulatorOpengl)/host/include/libOpenglRender \
$(call intermediates-dir-for, SHARED_LIBRARIES, libut_rendercontrol_dec, HOST) \ $(call intermediates-dir-for, SHARED_LIBRARIES, libut_rendercontrol_dec, HOST) \
$(call intermediates-dir-for, SHARED_LIBRARIES, libGLESv1_dec, HOST) \ $(call intermediates-dir-for, SHARED_LIBRARIES, libGLESv1_dec, HOST) \
$(emulatorOpengl)/host/libs/GLESv1_dec \ $(emulatorOpengl)/host/libs/GLESv1_dec \
@@ -50,7 +51,10 @@ LOCAL_C_INCLUDES := $(emulatorOpengl)/shared/OpenglCodecCommon \
$(emulatorOpengl)/tests/ut_rendercontrol_enc $(emulatorOpengl)/tests/ut_rendercontrol_enc
LOCAL_SHARED_LIBRARIES := libut_rendercontrol_dec libGLESv1_dec libEGL_host_wrapper LOCAL_SHARED_LIBRARIES := libut_rendercontrol_dec libGLESv1_dec libEGL_host_wrapper
LOCAL_STATIC_LIBRARIES := libOpenglCodecCommon LOCAL_STATIC_LIBRARIES := \
libOpenglCodecCommon \
libcutils
LOCAL_LDLIBS := -lpthread -lX11 -lrt LOCAL_LDLIBS := -lpthread -lX11 -lrt
include $(BUILD_HOST_EXECUTABLE) include $(BUILD_HOST_EXECUTABLE)