gltrace_transport.cpp revision 0469dd6d55fa331bfd7de9431da98b6340d82271
10469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy/* 20469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy * Copyright 2011, The Android Open Source Project 30469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy * 40469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy * Licensed under the Apache License, Version 2.0 (the "License"); 50469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy * you may not use this file except in compliance with the License. 60469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy * You may obtain a copy of the License at 70469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy * 80469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy * http://www.apache.org/licenses/LICENSE-2.0 90469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy * 100469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy * Unless required by applicable law or agreed to in writing, software 110469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy * distributed under the License is distributed on an "AS IS" BASIS, 120469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 130469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy * See the License for the specific language governing permissions and 140469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy * limitations under the License. 150469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy */ 160469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy 170469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#include <stdlib.h> 180469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#include <unistd.h> 190469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy 200469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#include <sys/socket.h> 210469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#include <netinet/in.h> 220469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#include <arpa/inet.h> 230469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy 240469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#include <cutils/log.h> 250469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy 260469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#include "gltrace_transport.h" 270469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy 280469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamynamespace android { 290469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamynamespace gltrace { 300469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy 310469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamyint gServerSocket, gClientSocket; 320469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy 330469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamyvoid startServer(int port) { 340469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy if (gServerSocket > 0) { 350469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy LOGD("startServer: server socket already open!"); 360469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy return; 370469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy } 380469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy 390469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy gServerSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); 400469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy if (gServerSocket < 0) { 410469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy LOGE("Error (%d) while creating socket. Check if app has network permissions.", 420469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy gServerSocket); 430469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy exit(-1); 440469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy } 450469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy 460469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy struct sockaddr_in server, client; 470469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy 480469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy server.sin_family = AF_INET; 490469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy server.sin_addr.s_addr = htonl(INADDR_ANY); 500469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy server.sin_port = htons(port); 510469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy 520469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy socklen_t sockaddr_len = sizeof(sockaddr_in); 530469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy if (bind(gServerSocket, (struct sockaddr *) &server, sizeof(server)) < 0) { 540469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy close(gServerSocket); 550469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy LOGE("Failed to bind the server socket"); 560469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy exit(-1); 570469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy } 580469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy 590469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy if (listen(gServerSocket, 1) < 0) { 600469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy close(gServerSocket); 610469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy LOGE("Failed to listen on server socket"); 620469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy exit(-1); 630469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy } 640469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy 650469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy LOGD("startServer: server started on %d", port); 660469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy 670469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy /* Wait for client connection */ 680469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy if ((gClientSocket = accept(gServerSocket, (struct sockaddr *)&client, &sockaddr_len)) < 0) { 690469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy close(gServerSocket); 700469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy LOGE("Failed to accept client connection"); 710469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy exit(-1); 720469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy } 730469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy 740469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy LOGD("startServer: client connected: %s", inet_ntoa(client.sin_addr)); 750469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy} 760469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy 770469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamyvoid stopServer() { 780469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy if (gServerSocket > 0) { 790469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy close(gServerSocket); 800469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy close(gClientSocket); 810469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy gServerSocket = gClientSocket = 0; 820469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy } 830469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy} 840469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy 850469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy/** Send GLMessage to the receiver on the host. */ 860469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamyvoid traceGLMessage(GLMessage *call) { 870469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy if (gClientSocket <= 0) { 880469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy LOGE("traceGLMessage: Attempt to send while client connection is not established"); 890469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy return; 900469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy } 910469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy 920469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy std::string str; 930469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy call->SerializeToString(&str); 940469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy const uint32_t len = str.length(); 950469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy 960469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy int n = write(gClientSocket, &len, sizeof len); 970469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy if (n != sizeof len) { 980469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy LOGE("traceGLMessage: Error (%d) while writing message length\n", n); 990469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy stopServer(); 1000469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy exit(-1); 1010469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy } 1020469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy 1030469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy n = write(gClientSocket, str.data(), str.length()); 1040469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy if (n != (int) str.length()) { 1050469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy LOGE("traceGLMessage: Error while writing out message, result = %d, length = %d\n", 1060469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy n, str.length()); 1070469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy stopServer(); 1080469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy exit(-1); 1090469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy } 1100469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy} 1110469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy 1120469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy}; // namespace gltrace 1130469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy}; // namespace android 114