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#ifndef __GLTRACE_TRANSPORT_H_
180469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#define __GLTRACE_TRANSPORT_H_
190469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy
2093a826f78f6313db791e6fc880439189897651b3Siva Velusamy#include <pthread.h>
2193a826f78f6313db791e6fc880439189897651b3Siva Velusamy
220469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#include "gltrace.pb.h"
230469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy
240469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamynamespace android {
250469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamynamespace gltrace {
260469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy
2793a826f78f6313db791e6fc880439189897651b3Siva Velusamy/**
2893a826f78f6313db791e6fc880439189897651b3Siva Velusamy * TCPStream provides a TCP based communication channel from the device to
2993a826f78f6313db791e6fc880439189897651b3Siva Velusamy * the host for transferring GLMessages.
3093a826f78f6313db791e6fc880439189897651b3Siva Velusamy */
3193a826f78f6313db791e6fc880439189897651b3Siva Velusamyclass TCPStream {
3293a826f78f6313db791e6fc880439189897651b3Siva Velusamy    int mSocket;
3393a826f78f6313db791e6fc880439189897651b3Siva Velusamy    pthread_mutex_t mSocketWriteMutex;
3493a826f78f6313db791e6fc880439189897651b3Siva Velusamypublic:
3593a826f78f6313db791e6fc880439189897651b3Siva Velusamy    /** Create a TCP based communication channel over @socket */
3693a826f78f6313db791e6fc880439189897651b3Siva Velusamy    TCPStream(int socket);
3793a826f78f6313db791e6fc880439189897651b3Siva Velusamy    ~TCPStream();
3893a826f78f6313db791e6fc880439189897651b3Siva Velusamy
3993a826f78f6313db791e6fc880439189897651b3Siva Velusamy    /** Close the channel. */
4093a826f78f6313db791e6fc880439189897651b3Siva Velusamy    void closeStream();
4193a826f78f6313db791e6fc880439189897651b3Siva Velusamy
4293a826f78f6313db791e6fc880439189897651b3Siva Velusamy    /** Send @data of size @len to host. . Returns -1 on error, 0 on success. */
4393a826f78f6313db791e6fc880439189897651b3Siva Velusamy    int send(void *data, size_t len);
4493a826f78f6313db791e6fc880439189897651b3Siva Velusamy
452fdcc81ddfdbdfbbde63bd64e9ac9272b5417553Siva Velusamy    /**
462fdcc81ddfdbdfbbde63bd64e9ac9272b5417553Siva Velusamy     * Receive @len bytes of data into @buf from the remote end. This is a blocking call.
472fdcc81ddfdbdfbbde63bd64e9ac9272b5417553Siva Velusamy     * Returns -1 on failure, 0 on success.
482fdcc81ddfdbdfbbde63bd64e9ac9272b5417553Siva Velusamy     */
492fdcc81ddfdbdfbbde63bd64e9ac9272b5417553Siva Velusamy    int receive(void *buf, size_t len);
5093a826f78f6313db791e6fc880439189897651b3Siva Velusamy};
5193a826f78f6313db791e6fc880439189897651b3Siva Velusamy
5293a826f78f6313db791e6fc880439189897651b3Siva Velusamy/**
5393a826f78f6313db791e6fc880439189897651b3Siva Velusamy * BufferedOutputStream provides buffering of data sent to the underlying
5493a826f78f6313db791e6fc880439189897651b3Siva Velusamy * unbuffered channel.
5593a826f78f6313db791e6fc880439189897651b3Siva Velusamy */
5693a826f78f6313db791e6fc880439189897651b3Siva Velusamyclass BufferedOutputStream {
5793a826f78f6313db791e6fc880439189897651b3Siva Velusamy    TCPStream *mStream;
5893a826f78f6313db791e6fc880439189897651b3Siva Velusamy
5993a826f78f6313db791e6fc880439189897651b3Siva Velusamy    size_t mBufferSize;
6093a826f78f6313db791e6fc880439189897651b3Siva Velusamy    std::string mStringBuffer;
6193a826f78f6313db791e6fc880439189897651b3Siva Velusamy
6293a826f78f6313db791e6fc880439189897651b3Siva Velusamy    /** Enqueue message into internal buffer. */
6393a826f78f6313db791e6fc880439189897651b3Siva Velusamy    void enqueueMessage(GLMessage *msg);
6493a826f78f6313db791e6fc880439189897651b3Siva Velusamypublic:
6593a826f78f6313db791e6fc880439189897651b3Siva Velusamy    /**
6693a826f78f6313db791e6fc880439189897651b3Siva Velusamy     * Construct a Buffered stream of size @bufferSize, using @stream as
6793a826f78f6313db791e6fc880439189897651b3Siva Velusamy     * its underlying channel for transport.
6893a826f78f6313db791e6fc880439189897651b3Siva Velusamy     */
6993a826f78f6313db791e6fc880439189897651b3Siva Velusamy    BufferedOutputStream(TCPStream *stream, size_t bufferSize);
7093a826f78f6313db791e6fc880439189897651b3Siva Velusamy
7193a826f78f6313db791e6fc880439189897651b3Siva Velusamy    /**
7293a826f78f6313db791e6fc880439189897651b3Siva Velusamy     * Send @msg. The message could be buffered and sent later with a
7393a826f78f6313db791e6fc880439189897651b3Siva Velusamy     * subsequent message. Returns -1 on error, 0 on success.
7493a826f78f6313db791e6fc880439189897651b3Siva Velusamy     */
7593a826f78f6313db791e6fc880439189897651b3Siva Velusamy    int send(GLMessage *msg);
760469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy
7793a826f78f6313db791e6fc880439189897651b3Siva Velusamy    /** Send any buffered messages, returns -1 on error, 0 on success. */
7893a826f78f6313db791e6fc880439189897651b3Siva Velusamy    int flush();
7993a826f78f6313db791e6fc880439189897651b3Siva Velusamy};
8093a826f78f6313db791e6fc880439189897651b3Siva Velusamy
8193a826f78f6313db791e6fc880439189897651b3Siva Velusamy/**
820832fb6407d1c85cba20a8cc0aff828db3c134deSiva Velusamy * Utility method: start a server listening at @sockName (unix domain socket,
830832fb6407d1c85cba20a8cc0aff828db3c134deSiva Velusamy * abstract namespace path), and wait for a client connection.
840832fb6407d1c85cba20a8cc0aff828db3c134deSiva Velusamy * Returns the connected client socket on success, or -1 on failure.
8593a826f78f6313db791e6fc880439189897651b3Siva Velusamy */
860832fb6407d1c85cba20a8cc0aff828db3c134deSiva Velusamyint acceptClientConnection(char *sockName);
870469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy
880469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy};
890469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy};
900469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy
910469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#endif
92