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 "frameworks/native/opengl/libs/GLES_trace/proto/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    /**
46     * Receive @len bytes of data into @buf from the remote end. This is a blocking call.
47     * Returns -1 on failure, 0 on success.
48     */
49    int receive(void *buf, size_t len);
50};
51
52/**
53 * BufferedOutputStream provides buffering of data sent to the underlying
54 * unbuffered channel.
55 */
56class BufferedOutputStream {
57    TCPStream *mStream;
58
59    size_t mBufferSize;
60    std::string mStringBuffer;
61
62    /** Enqueue message into internal buffer. */
63    void enqueueMessage(GLMessage *msg);
64public:
65    /**
66     * Construct a Buffered stream of size @bufferSize, using @stream as
67     * its underlying channel for transport.
68     */
69    BufferedOutputStream(TCPStream *stream, size_t bufferSize);
70
71    /**
72     * Send @msg. The message could be buffered and sent later with a
73     * subsequent message. Returns -1 on error, 0 on success.
74     */
75    int send(GLMessage *msg);
76
77    /** Send any buffered messages, returns -1 on error, 0 on success. */
78    int flush();
79};
80
81/**
82 * Utility method: start a server listening at @sockName (unix domain socket,
83 * abstract namespace path), and wait for a client connection.
84 * Returns the connected client socket on success, or -1 on failure.
85 */
86int acceptClientConnection(char *sockName);
87
88};
89};
90
91#endif
92