From dca7f2a6ac56567850e0e03a063e1739e43c5ce2 Mon Sep 17 00:00:00 2001 From: Jesse Hall Date: Wed, 11 Jul 2012 16:25:37 -0700 Subject: Use a per-process server address for the GLES server Previously we used a hardcoded address (tcp port, unix pipe path, etc.) for the OpenGLRender system. Multiple emulators would all try to listen on the same address, with the system non-deterministically (?) choosing which one accepted each new connection. This resulted in frames going to the wrong emulator window, one emulator shutting down another's OpenGL system, etc. Now the OpenGLRender server requests an unused tcp port or derives a path from the pid, and reports the address back to the emulator client to use for future connections from the guest. Change-Id: I6af2eac0c7f27670a3b6595772eebc7aa2b24688 --- .../opengl/shared/OpenglCodecCommon/TcpStream.cpp | 23 ++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'emulator/opengl/shared/OpenglCodecCommon/TcpStream.cpp') diff --git a/emulator/opengl/shared/OpenglCodecCommon/TcpStream.cpp b/emulator/opengl/shared/OpenglCodecCommon/TcpStream.cpp index 4da2cec..8a6e56e 100644 --- a/emulator/opengl/shared/OpenglCodecCommon/TcpStream.cpp +++ b/emulator/opengl/shared/OpenglCodecCommon/TcpStream.cpp @@ -28,6 +28,8 @@ #include #endif +#define LISTEN_BACKLOG 4 + TcpStream::TcpStream(size_t bufSize) : SocketStream(bufSize) { @@ -47,10 +49,22 @@ TcpStream::TcpStream(int sock, size_t bufSize) : setsockopt( sock, IPPROTO_TCP, TCP_NODELAY, (const char*)&flag, sizeof(flag) ); } -int TcpStream::listen(unsigned short port) +int TcpStream::listen(char addrstr[MAX_ADDRSTR_LEN]) { - m_sock = socket_loopback_server(port, SOCK_STREAM); - if (!valid()) return int(ERR_INVALID_SOCKET); + m_sock = socket_loopback_server(0, SOCK_STREAM); + if (!valid()) + return int(ERR_INVALID_SOCKET); + + /* get the actual port number assigned by the system */ + struct sockaddr_in addr; + socklen_t addrLen = sizeof(addr); + memset(&addr, 0, sizeof(addr)); + if (getsockname(m_sock, (struct sockaddr*)&addr, &addrLen) < 0) { + close(m_sock); + return int(ERR_INVALID_SOCKET); + } + snprintf(addrstr, MAX_ADDRSTR_LEN - 1, "%hu", ntohs(addr.sin_port)); + addrstr[MAX_ADDRSTR_LEN-1] = '\0'; return 0; } @@ -78,8 +92,9 @@ SocketStream * TcpStream::accept() return clientStream; } -int TcpStream::connect(unsigned short port) +int TcpStream::connect(const char* addr) { + int port = atoi(addr); return connect("127.0.0.1",port); } -- cgit v1.1