1ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall/* 2ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall* Copyright (C) 2011 The Android Open Source Project 3ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall* 4ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall* Licensed under the Apache License, Version 2.0 (the "License"); 5ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall* you may not use this file except in compliance with the License. 6ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall* You may obtain a copy of the License at 7ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall* 8ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall* http://www.apache.org/licenses/LICENSE-2.0 9ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall* 10ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall* Unless required by applicable law or agreed to in writing, software 11ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall* distributed under the License is distributed on an "AS IS" BASIS, 12ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall* See the License for the specific language governing permissions and 14ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall* limitations under the License. 15ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall*/ 16ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall#ifndef __SOCKET_STREAM_H 17ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall#define __SOCKET_STREAM_H 18ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall 19ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall#include <stdlib.h> 20ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall#include "IOStream.h" 21ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall 22ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hallclass SocketStream : public IOStream { 23ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hallpublic: 24ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall typedef enum { ERR_INVALID_SOCKET = -1000 } SocketStreamError; 25dca7f2a6ac56567850e0e03a063e1739e43c5ce2Jesse Hall static const size_t MAX_ADDRSTR_LEN = 256; 26ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall 27ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall explicit SocketStream(size_t bufsize = 10000); 28ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall virtual ~SocketStream(); 29ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall 30dca7f2a6ac56567850e0e03a063e1739e43c5ce2Jesse Hall virtual int listen(char addrstr[MAX_ADDRSTR_LEN]) = 0; 31ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall virtual SocketStream *accept() = 0; 32dca7f2a6ac56567850e0e03a063e1739e43c5ce2Jesse Hall virtual int connect(const char* addr) = 0; 33ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall 34ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall virtual void *allocBuffer(size_t minSize); 35ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall virtual int commitBuffer(size_t size); 36ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall virtual const unsigned char *readFully(void *buf, size_t len); 37ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall virtual const unsigned char *read(void *buf, size_t *inout_len); 38ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall 39ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall bool valid() { return m_sock >= 0; } 40ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall virtual int recv(void *buf, size_t len); 41ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall virtual int writeFully(const void *buf, size_t len); 42ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall 43ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hallprotected: 44ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall int m_sock; 45ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall size_t m_bufsize; 46ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall unsigned char *m_buf; 47ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall 48ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall SocketStream(int sock, size_t bufSize); 49ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall}; 50ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall 51ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall#endif /* __SOCKET_STREAM_H */ 52