blob: c52ca5fa3239bc7816f3c63e0f5890ab90ed61c6 [file] [log] [blame]
Siva Velusamydb974682011-11-30 15:05:37 -08001/*
2 * Copyright 2011, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdlib.h>
18#include <unistd.h>
19
20#include <sys/socket.h>
21#include <netinet/in.h>
22#include <arpa/inet.h>
23
24#include <cutils/log.h>
25
26#include "gltrace_transport.h"
27
28namespace android {
29namespace gltrace {
30
31int gServerSocket, gClientSocket;
32
33void startServer(int port) {
34 if (gServerSocket > 0) {
35 LOGD("startServer: server socket already open!");
36 return;
37 }
38
39 gServerSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
40 if (gServerSocket < 0) {
41 LOGE("Error (%d) while creating socket. Check if app has network permissions.",
42 gServerSocket);
43 exit(-1);
44 }
45
46 struct sockaddr_in server, client;
47
48 server.sin_family = AF_INET;
49 server.sin_addr.s_addr = htonl(INADDR_ANY);
50 server.sin_port = htons(port);
51
52 socklen_t sockaddr_len = sizeof(sockaddr_in);
53 if (bind(gServerSocket, (struct sockaddr *) &server, sizeof(server)) < 0) {
54 close(gServerSocket);
55 LOGE("Failed to bind the server socket");
56 exit(-1);
57 }
58
59 if (listen(gServerSocket, 1) < 0) {
60 close(gServerSocket);
61 LOGE("Failed to listen on server socket");
62 exit(-1);
63 }
64
65 LOGD("startServer: server started on %d", port);
66
67 /* Wait for client connection */
68 if ((gClientSocket = accept(gServerSocket, (struct sockaddr *)&client, &sockaddr_len)) < 0) {
69 close(gServerSocket);
70 LOGE("Failed to accept client connection");
71 exit(-1);
72 }
73
74 LOGD("startServer: client connected: %s", inet_ntoa(client.sin_addr));
75}
76
77void stopServer() {
78 if (gServerSocket > 0) {
79 close(gServerSocket);
80 close(gClientSocket);
81 gServerSocket = gClientSocket = 0;
82 }
83}
84
85/** Send GLMessage to the receiver on the host. */
86void traceGLMessage(GLMessage *call) {
87 if (gClientSocket <= 0) {
88 LOGE("traceGLMessage: Attempt to send while client connection is not established");
89 return;
90 }
91
92 std::string str;
93 call->SerializeToString(&str);
94 const uint32_t len = str.length();
95
96 int n = write(gClientSocket, &len, sizeof len);
97 if (n != sizeof len) {
98 LOGE("traceGLMessage: Error (%d) while writing message length\n", n);
99 stopServer();
100 exit(-1);
101 }
102
103 n = write(gClientSocket, str.data(), str.length());
104 if (n != (int) str.length()) {
105 LOGE("traceGLMessage: Error while writing out message, result = %d, length = %d\n",
106 n, str.length());
107 stopServer();
108 exit(-1);
109 }
110}
111
112}; // namespace gltrace
113}; // namespace android