gltrace_transport.h revision 93a826f78f6313db791e6fc880439189897651b3
1/* 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#ifndef __GLTRACE_TRANSPORT_H_ 18#define __GLTRACE_TRANSPORT_H_ 19 20#include <pthread.h> 21 22#include "gltrace.pb.h" 23 24namespace android { 25namespace gltrace { 26 27/** 28 * TCPStream provides a TCP based communication channel from the device to 29 * the host for transferring GLMessages. 30 */ 31class TCPStream { 32 int mSocket; 33 pthread_mutex_t mSocketWriteMutex; 34public: 35 /** Create a TCP based communication channel over @socket */ 36 TCPStream(int socket); 37 ~TCPStream(); 38 39 /** Close the channel. */ 40 void closeStream(); 41 42 /** Send @data of size @len to host. . Returns -1 on error, 0 on success. */ 43 int send(void *data, size_t len); 44 45 /** Receive data into @buf from the remote end. This is a blocking call. */ 46 int receive(void *buf, size_t size); 47}; 48 49/** 50 * BufferedOutputStream provides buffering of data sent to the underlying 51 * unbuffered channel. 52 */ 53class BufferedOutputStream { 54 TCPStream *mStream; 55 56 size_t mBufferSize; 57 std::string mStringBuffer; 58 59 /** Enqueue message into internal buffer. */ 60 void enqueueMessage(GLMessage *msg); 61public: 62 /** 63 * Construct a Buffered stream of size @bufferSize, using @stream as 64 * its underlying channel for transport. 65 */ 66 BufferedOutputStream(TCPStream *stream, size_t bufferSize); 67 68 /** 69 * Send @msg. The message could be buffered and sent later with a 70 * subsequent message. Returns -1 on error, 0 on success. 71 */ 72 int send(GLMessage *msg); 73 74 /** Send any buffered messages, returns -1 on error, 0 on success. */ 75 int flush(); 76}; 77 78/** 79 * Utility method: start a server at @serverPort, and wait for a client 80 * connection. Returns the connected client socket on success, or -1 on failure. 81 */ 82int acceptClientConnection(int serverPort); 83 84}; 85}; 86 87#endif 88