PageRenderTime 28ms CodeModel.GetById 18ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/mordor/streams/test.cpp

http://github.com/mozy/mordor
C++ | 58 lines | 50 code | 7 blank | 1 comment | 17 complexity | 5685601cd8657ddb52d84d2225a47f66 MD5 | raw file
 1// Copyright (c) 2009 - Mozy, Inc.
 2
 3#include "test.h"
 4
 5namespace Mordor {
 6
 7void
 8TestStream::close(CloseType type)
 9{
10    if (m_onClose)
11        m_onClose(type);
12    if (ownsParent())
13        parent()->close(type);
14}
15
16size_t
17TestStream::read(Buffer &b, size_t len)
18{
19    len = (std::min)(len, m_maxReadSize);
20    if (m_onRead) {
21        if (m_onReadBytes == 0) {
22            m_onRead();
23        }
24    }
25    if (m_onRead && m_onReadBytes > 0)
26        len = (size_t)std::min<long long>(len, m_onReadBytes);
27    size_t result = parent()->read(b, len);
28    if (m_onRead && m_onReadBytes > 0)
29        m_onReadBytes -= result;
30    return result;
31}
32
33size_t
34TestStream::write(const Buffer &b, size_t len)
35{
36    len = (std::min)(len, m_maxWriteSize);
37    if (m_onWrite) {
38        if (m_onWriteBytes == 0) {
39            m_onWrite();
40        }
41    }
42    if (m_onWrite && m_onWriteBytes > 0)
43        len = (size_t)std::min<long long>(len, m_onWriteBytes);
44    size_t result = parent()->write(b, len);
45    if (m_onWrite && m_onWriteBytes > 0)
46        m_onWriteBytes -= result;
47    return result;
48}
49
50void
51TestStream::flush(bool flushParent)
52{
53    if (m_onFlush)
54        m_onFlush(flushParent);
55    parent()->flush(flushParent);
56}
57
58}