Changeset 31:bbd688924703 in xplcommon for src


Ignore:
Timestamp:
01/02/13 13:58:25 (12 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Implemented the data stream

Location:
src/xplcommon
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • src/xplcommon/BlockingStream.cc

    r30 r31  
    4646//------------------------------------------------------------------------------
    4747
    48 bool BlockingStream::isInterrupted()
     48inline bool BlockingStream::checkInterrupted()
    4949{
    50     if (failed()) return false;
    51 
    5250    if (!interrupted) {
    5351        interrupted = event.check();
     
    8381//------------------------------------------------------------------------------
    8482
     83bool BlockingStream::skip(size_t length)
     84{
     85    ReadingBuffer& readingBuffer = stream.getReadingBuffer();
     86    while (length>0) {
     87        size_t toSkip = min(length, readingBuffer.getLength() - readingOffset);
     88        readingOffset += toSkip;
     89        length -= toSkip;
     90
     91        if (length!=0) {
     92            if (!fill()) return false;
     93        }
     94    }
     95
     96    return true;
     97}
     98
     99//------------------------------------------------------------------------------
     100
    85101bool BlockingStream::write(const void* src, size_t length)
    86102{
     
    105121{
    106122    WritingBuffer& writingBuffer = stream.getWritingBuffer();
    107     while (!failed()) {
    108         if (isInterrupted()) return false;
     123    while (*this) {
     124        if (checkInterrupted()) return false;
    109125
    110126        if (writingBuffer.write()) {
     
    112128        } else if (writingBuffer.failed()) {
    113129            setErrorCode(writingBuffer.getErrorCode());
    114             return false;
    115130        } else {
    116131            Waiter* waiter = stream.getWaiter();
     
    132147    readingBuffer.reset();
    133148    readingOffset = 0;
    134     while (true) {
    135         if (isInterrupted()) return false;
     149    while (*this) {
     150        if (checkInterrupted()) return false;
    136151
    137152        if (readingBuffer.read()) {
    138             return !readingBuffer.isEmpty();
     153            eof = readingBuffer.isEmpty();
     154            return !eof;
    139155        } else if (readingBuffer.failed()) {
    140156            setErrorCode(readingBuffer.getErrorCode());
    141             return false;
    142157        } else {
    143158            Waiter* waiter = stream.getWaiter();
     
    149164        }
    150165    }
     166    return false;
    151167}
    152168
  • src/xplcommon/BlockingStream.h

    r30 r31  
    3535
    3636#include "BufferedStream.h"
     37
    3738#include "Waiter.h"
    3839#include "WaitableEvent.h"
     
    7677    size_t readingOffset;
    7778
     79    /**
     80     * Indicate if the end-of-file has been reached while reading.
     81     */
     82    bool eof;
     83
    7884public:
    7985    /**
     
    8389     */
    8490    BlockingStream(BufferedStream& stream);
     91
     92    /**
     93     * Determine if the stream has neither failed nor been
     94     * interrupted.
     95     */
     96    operator bool() const;
    8597
    8698    /**
     
    102114     */
    103115    bool read(void* dest, size_t length);
     116
     117    /**
     118     * Skip the given number of bytes.
     119     */
     120    bool skip(size_t length);
    104121
    105122    /**
     
    125142     */
    126143    bool fill();
     144
     145    /**
     146     * Check for the stream being interrupted.
     147     *
     148     * @return if the stream is interrupted, false otherwise
     149     */
     150    bool checkInterrupted();
    127151};
    128152
     
    135159    event(stream.getWaiter()),
    136160    interrupted(false),
    137     readingOffset(0)
     161    readingOffset(0),
     162    eof(false)
    138163{
     164}
     165
     166//------------------------------------------------------------------------------
     167
     168inline BlockingStream::operator bool() const
     169{
     170    return !failed() && !interrupted && !eof;
    139171}
    140172
     
    144176{
    145177    event.fire();
     178}
     179
     180//------------------------------------------------------------------------------
     181
     182inline bool BlockingStream::isInterrupted()
     183{
     184    return interrupted;
    146185}
    147186
  • src/xplcommon/Makefile.am

    r30 r31  
    1111libxplcommon_la_SOURCES= \
    1212        PseudoRandom.cc         \
    13         BlockingStream.cc
     13        BlockingStream.cc       \
     14        DataStream.cc
    1415
    1516if TARGET_API_POSIX
     
    3940        LocalConnector.h        \
    4041        LocalClientSocket.h     \
    41         BlockingStream.h
     42        BlockingStream.h        \
     43        DataStream.h
Note: See TracChangeset for help on using the changeset viewer.