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
diff --git a/tools/emulator/opengl/shared/OpenglCodecCommon/IOStream.h b/tools/emulator/opengl/host/include/libOpenglRender/IOStream.h
similarity index 96%
rename from tools/emulator/opengl/shared/OpenglCodecCommon/IOStream.h
rename to tools/emulator/opengl/host/include/libOpenglRender/IOStream.h
index 7cfccc3..ed8c266 100644
--- a/tools/emulator/opengl/shared/OpenglCodecCommon/IOStream.h
+++ b/tools/emulator/opengl/host/include/libOpenglRender/IOStream.h
@@ -33,6 +33,7 @@
     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 const unsigned char *read( void *buf, size_t *inout_len) = 0;
 
     virtual ~IOStream() {
 
diff --git a/tools/emulator/opengl/host/libs/GLESv1_dec/Android.mk b/tools/emulator/opengl/host/libs/GLESv1_dec/Android.mk
index 08fd3a3..c86c292 100644
--- a/tools/emulator/opengl/host/libs/GLESv1_dec/Android.mk
+++ b/tools/emulator/opengl/host/libs/GLESv1_dec/Android.mk
@@ -19,6 +19,7 @@
 
 LOCAL_C_INCLUDES += \
     $(emulatorOpengl)/shared/OpenglCodecCommon \
+    $(emulatorOpengl)/host/include/libOpenglRender \
     $(emulatorOpengl)/system/GLESv1_enc
 
 LOCAL_STATIC_LIBRARIES := \
diff --git a/tools/emulator/opengl/shared/OpenglCodecCommon/Android.mk b/tools/emulator/opengl/shared/OpenglCodecCommon/Android.mk
index e569270..af8c163 100644
--- a/tools/emulator/opengl/shared/OpenglCodecCommon/Android.mk
+++ b/tools/emulator/opengl/shared/OpenglCodecCommon/Android.mk
@@ -14,7 +14,7 @@
 
 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 @@
 
 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
diff --git a/tools/emulator/opengl/shared/OpenglCodecCommon/TcpStream.cpp b/tools/emulator/opengl/shared/OpenglCodecCommon/TcpStream.cpp
index 86202d0..b33d24f 100644
--- a/tools/emulator/opengl/shared/OpenglCodecCommon/TcpStream.cpp
+++ b/tools/emulator/opengl/shared/OpenglCodecCommon/TcpStream.cpp
@@ -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 @@
     m_bufsize(bufSize),
     m_buf(NULL)
 {
-
 }
 
 TcpStream::~TcpStream()
@@ -55,39 +52,15 @@
 }
 
 
-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 @@
 
 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 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 @@
 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 @@
             if (errno == EINTR) {
                 continue;
             } else {
+                ERR("TcpStream::readFully failed, errno = %d 0x%x \n", errno,buf);
                 return NULL;
             }
         } else {
@@ -214,12 +171,33 @@
     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;
diff --git a/tools/emulator/opengl/shared/OpenglCodecCommon/TcpStream.h b/tools/emulator/opengl/shared/OpenglCodecCommon/TcpStream.h
index aa65b7e..7521843 100644
--- a/tools/emulator/opengl/shared/OpenglCodecCommon/TcpStream.h
+++ b/tools/emulator/opengl/shared/OpenglCodecCommon/TcpStream.h
@@ -26,13 +26,14 @@
 
     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);
diff --git a/tools/emulator/opengl/system/GLESv1_enc/Android.mk b/tools/emulator/opengl/system/GLESv1_enc/Android.mk
index bf4943b..c8fd18b 100644
--- a/tools/emulator/opengl/system/GLESv1_enc/Android.mk
+++ b/tools/emulator/opengl/system/GLESv1_enc/Android.mk
@@ -19,6 +19,7 @@
 LOCAL_CFLAGS += -DLOG_TAG=\"egl_GLESv1_enc\"
 LOCAL_C_INCLUDES +=  \
     $(emulatorOpengl)/shared/OpenglCodecCommon \
+    $(emulatorOpengl)/host/include/libOpenglRender \
     $(glesv1_intermediates)
 
 LOCAL_STATIC_LIBRARIES := \
diff --git a/tools/emulator/opengl/tests/gles_android_wrapper/Android.mk b/tools/emulator/opengl/tests/gles_android_wrapper/Android.mk
index 5ae3344..15e2e98 100644
--- a/tools/emulator/opengl/tests/gles_android_wrapper/Android.mk
+++ b/tools/emulator/opengl/tests/gles_android_wrapper/Android.mk
@@ -21,6 +21,7 @@
 emulatorOpengl := $(LOCAL_PATH)/../..
 
 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, libGLESv1_enc) \
         $(emulatorOpengl)/system/GLESv1_enc \
diff --git a/tools/emulator/opengl/tests/ut_rendercontrol_dec/Android.mk b/tools/emulator/opengl/tests/ut_rendercontrol_dec/Android.mk
index a3a2c4a..673fd11 100644
--- a/tools/emulator/opengl/tests/ut_rendercontrol_dec/Android.mk
+++ b/tools/emulator/opengl/tests/ut_rendercontrol_dec/Android.mk
@@ -18,7 +18,11 @@
 LOCAL_STATIC_LIBRARIES := \
 	libOpenglCodecCommon \
     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
 GEN := $(intermediates)/ut_rendercontrol_dec.cpp $(intermediates)/ut_rendercontrol_dec.h
diff --git a/tools/emulator/opengl/tests/ut_rendercontrol_enc/Android.mk b/tools/emulator/opengl/tests/ut_rendercontrol_enc/Android.mk
index bfcd699..d9beb85 100644
--- a/tools/emulator/opengl/tests/ut_rendercontrol_enc/Android.mk
+++ b/tools/emulator/opengl/tests/ut_rendercontrol_enc/Android.mk
@@ -12,7 +12,9 @@
 
 ut_intermediates := $(local-intermediates-dir)
 
-LOCAL_C_INCLUDES += $(emulatorOpengl)/shared/OpenglCodecCommon 
+LOCAL_C_INCLUDES += \
+    $(emulatorOpengl)/shared/OpenglCodecCommon \
+    $(emulatorOpengl)/host/include/libOpenglRender
 
 LOCAL_STATIC_LIBRARIES := \
         libOpenglCodecCommon
diff --git a/tools/emulator/opengl/tests/ut_renderer/Android.mk b/tools/emulator/opengl/tests/ut_renderer/Android.mk
index d51beab..28317e9 100644
--- a/tools/emulator/opengl/tests/ut_renderer/Android.mk
+++ b/tools/emulator/opengl/tests/ut_renderer/Android.mk
@@ -43,6 +43,7 @@
 #LOCAL_CFLAGS += -g -O0
 
 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, libGLESv1_dec, HOST) \
         $(emulatorOpengl)/host/libs/GLESv1_dec \
@@ -50,7 +51,10 @@
         $(emulatorOpengl)/tests/ut_rendercontrol_enc
 
 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
 include $(BUILD_HOST_EXECUTABLE)