blob: 7758e48baaa12a8e07bbf2a862a11f14e9757e6a [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
Siva Velusamy1e81e712011-12-14 12:19:56 -080031int acceptClientConnection(int serverPort) {
32 int serverSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
33 if (serverSocket < 0) {
Siva Velusamydb974682011-11-30 15:05:37 -080034 LOGE("Error (%d) while creating socket. Check if app has network permissions.",
Siva Velusamy1e81e712011-12-14 12:19:56 -080035 serverSocket);
36 return -1;
Siva Velusamydb974682011-11-30 15:05:37 -080037 }
38
39 struct sockaddr_in server, client;
40
41 server.sin_family = AF_INET;
42 server.sin_addr.s_addr = htonl(INADDR_ANY);
Siva Velusamy1e81e712011-12-14 12:19:56 -080043 server.sin_port = htons(serverPort);
Siva Velusamydb974682011-11-30 15:05:37 -080044
45 socklen_t sockaddr_len = sizeof(sockaddr_in);
Siva Velusamy1e81e712011-12-14 12:19:56 -080046 if (bind(serverSocket, (struct sockaddr *) &server, sizeof(server)) < 0) {
47 close(serverSocket);
Siva Velusamydb974682011-11-30 15:05:37 -080048 LOGE("Failed to bind the server socket");
Siva Velusamy1e81e712011-12-14 12:19:56 -080049 return -1;
Siva Velusamydb974682011-11-30 15:05:37 -080050 }
51
Siva Velusamy1e81e712011-12-14 12:19:56 -080052 if (listen(serverSocket, 1) < 0) {
53 close(serverSocket);
Siva Velusamydb974682011-11-30 15:05:37 -080054 LOGE("Failed to listen on server socket");
Siva Velusamy1e81e712011-12-14 12:19:56 -080055 return -1;
Siva Velusamydb974682011-11-30 15:05:37 -080056 }
57
Siva Velusamy1e81e712011-12-14 12:19:56 -080058 ALOGD("gltrace::waitForClientConnection: server listening @ port %d", serverPort);
Siva Velusamydb974682011-11-30 15:05:37 -080059
Siva Velusamy1e81e712011-12-14 12:19:56 -080060 int clientSocket = accept(serverSocket, (struct sockaddr *)&client, &sockaddr_len);
61 if (clientSocket < 0) {
62 close(serverSocket);
Siva Velusamydb974682011-11-30 15:05:37 -080063 LOGE("Failed to accept client connection");
Siva Velusamy1e81e712011-12-14 12:19:56 -080064 return -1;
Siva Velusamydb974682011-11-30 15:05:37 -080065 }
66
Siva Velusamy1e81e712011-12-14 12:19:56 -080067 ALOGD("gltrace::waitForClientConnection: client connected: %s", inet_ntoa(client.sin_addr));
68
69 // do not accept any more incoming connections
70 close(serverSocket);
71
72 return clientSocket;
Siva Velusamydb974682011-11-30 15:05:37 -080073}
74
Siva Velusamy1e81e712011-12-14 12:19:56 -080075TCPStream::TCPStream(int socket) {
76 mSocket = socket;
77 pthread_mutex_init(&mSocketWriteMutex, NULL);
78}
79
80TCPStream::~TCPStream() {
81 pthread_mutex_destroy(&mSocketWriteMutex);
82}
83
84void TCPStream::closeStream() {
85 if (mSocket > 0) {
86 close(mSocket);
87 mSocket = 0;
Siva Velusamydb974682011-11-30 15:05:37 -080088 }
89}
90
Siva Velusamy1e81e712011-12-14 12:19:56 -080091int TCPStream::send(void *buf, size_t len) {
92 if (mSocket <= 0) {
93 return -1;
Siva Velusamydb974682011-11-30 15:05:37 -080094 }
95
Siva Velusamy1e81e712011-12-14 12:19:56 -080096 pthread_mutex_lock(&mSocketWriteMutex);
97 int n = write(mSocket, buf, len);
98 pthread_mutex_unlock(&mSocketWriteMutex);
Siva Velusamydb974682011-11-30 15:05:37 -080099
Siva Velusamy1e81e712011-12-14 12:19:56 -0800100 return n;
101}
102
103int TCPStream::receive(void *data, size_t len) {
104 if (mSocket <= 0) {
105 return -1;
Siva Velusamydb974682011-11-30 15:05:37 -0800106 }
107
Siva Velusamy1e81e712011-12-14 12:19:56 -0800108 return read(mSocket, data, len);
109}
110
111BufferedOutputStream::BufferedOutputStream(TCPStream *stream, size_t bufferSize) {
112 mStream = stream;
113
114 mBufferSize = bufferSize;
115 mStringBuffer = "";
116 mStringBuffer.reserve(bufferSize);
117}
118
119int BufferedOutputStream::flush() {
120 if (mStringBuffer.size() == 0) {
121 return 0;
Siva Velusamydb974682011-11-30 15:05:37 -0800122 }
Siva Velusamy1e81e712011-12-14 12:19:56 -0800123
124 int n = mStream->send((void *)mStringBuffer.data(), mStringBuffer.size());
125 mStringBuffer.clear();
126 return n;
127}
128
129void BufferedOutputStream::enqueueMessage(GLMessage *msg) {
130 const uint32_t len = msg->ByteSize();
131
132 mStringBuffer.append((const char *)&len, sizeof(len)); // append header
133 msg->AppendToString(&mStringBuffer); // append message
134}
135
136int BufferedOutputStream::send(GLMessage *msg) {
137 enqueueMessage(msg);
138
139 if (mStringBuffer.size() > mBufferSize) {
140 return flush();
141 }
142
143 return 0;
Siva Velusamydb974682011-11-30 15:05:37 -0800144}
145
146}; // namespace gltrace
147}; // namespace android