source: branches/blt4/packages/vizservers/nanovis/ReadBuffer.h @ 3892

Last change on this file since 3892 was 3892, checked in by gah, 11 years ago
File size: 1.8 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * Copyright (C) 2004-2012  HUBzero Foundation, LLC
4 *
5 * Author: George A. Howlett <gah@purdue.edu>
6 */
7
8#include <cstdlib>
9
10#include <RpBuffer.h>
11
12#ifndef NV_READBUFFER_H
13#define NV_READBUFFER_H
14
15namespace nv {
16
17/**
18 * \brief Buffered input for reading socket
19 */
20class ReadBuffer
21{
22public:
23    enum BufferStatus {
24        ENDFILE=-1,
25        OK,
26        ERROR,
27        CONTINUE,
28    };
29
30    ReadBuffer(int fd, size_t bufferSize);
31
32    virtual ~ReadBuffer();
33
34    BufferStatus getLine(size_t *numBytesPtr, unsigned char **bytesPtr);
35
36    BufferStatus followingData(unsigned char *out, size_t numBytes);
37
38    BufferStatus followingData(Rappture::Buffer& out, size_t numBytes);
39
40    /**
41     * \brief Check if buffer contains data ending in a newline
42     *
43     * \return bool indicating if getLine can be called without blocking
44     */
45    bool isLineAvailable()
46    {
47        return (nextNewLine() > 0);
48    }
49
50    /**
51     * \brief Get the file descriptor this object reads from
52     */
53    int file()
54    {
55        return _fd;
56    }
57
58    /**
59     * \brief Get the status of the last read
60     */
61    BufferStatus status()
62    {
63        return _lastStatus;
64    }
65
66private:
67    unsigned char *_bytes;              /**< New-ed buffer to hold read
68                                         * characters. */
69    size_t _bufferSize;                 /**< # of bytes allocated for buffer */
70    size_t _fill;                       /**< # of bytes used in the buffer */
71    size_t _mark;                       /**< Starting point of the unconsumed
72                                         * data in the buffer. */
73    int _fd;                            /**< File descriptor to get data. */
74    BufferStatus _lastStatus;           /**< Status of last read operation. */
75
76    BufferStatus doFill();
77
78    void flush()
79    {
80        _fill = _mark = 0;
81    }
82
83    size_t nextNewLine();
84};
85
86}
87
88#endif
Note: See TracBrowser for help on using the repository browser.