source: box/trunk/lib/server/ProtocolUncertainStream.cpp @ 2312

Revision 2312, 5.5 KB checked in by chris, 4 years ago (diff)

Spacing and additional stream header byte logging.

  • Property svn:eol-style set to native
Line 
1// --------------------------------------------------------------------------
2//
3// File
4//              Name:    ProtocolUncertainStream.h
5//              Purpose: Read part of another stream
6//              Created: 2003/12/05
7//
8// --------------------------------------------------------------------------
9
10#include "Box.h"
11#include "ProtocolUncertainStream.h"
12#include "ServerException.h"
13#include "Protocol.h"
14
15#include "MemLeakFindOn.h"
16
17// --------------------------------------------------------------------------
18//
19// Function
20//              Name:    ProtocolUncertainStream::ProtocolUncertainStream(IOStream &, int)
21//              Purpose: Constructor, taking another stream.
22//              Created: 2003/12/05
23//
24// --------------------------------------------------------------------------
25ProtocolUncertainStream::ProtocolUncertainStream(IOStream &rSource)
26        : mrSource(rSource),
27          mBytesLeftInCurrentBlock(0),
28          mFinished(false)
29{
30}
31
32// --------------------------------------------------------------------------
33//
34// Function
35//              Name:    ProtocolUncertainStream::~ProtocolUncertainStream()
36//              Purpose: Destructor. Won't absorb any unread bytes.
37//              Created: 2003/12/05
38//
39// --------------------------------------------------------------------------
40ProtocolUncertainStream::~ProtocolUncertainStream()
41{
42        if(!mFinished)
43        {
44                BOX_WARNING("ProtocolUncertainStream destroyed before "
45                        "stream finished");
46        }
47}
48
49// --------------------------------------------------------------------------
50//
51// Function
52//              Name:    ProtocolUncertainStream::Read(void *, int, int)
53//              Purpose: As interface.
54//              Created: 2003/12/05
55//
56// --------------------------------------------------------------------------
57int ProtocolUncertainStream::Read(void *pBuffer, int NBytes, int Timeout)
58{
59        // Finished?
60        if(mFinished)
61        {
62                return 0;
63        }
64       
65        int read = 0;
66        while(read < NBytes)
67        {
68                // Anything we can get from the current block?
69                ASSERT(mBytesLeftInCurrentBlock >= 0);
70                if(mBytesLeftInCurrentBlock > 0)
71                {
72                        // Yes, let's use some of these up
73                        int toRead = (NBytes - read);
74                        if(toRead > mBytesLeftInCurrentBlock)
75                        {
76                                // Adjust downwards to only read stuff out of the current block
77                                toRead = mBytesLeftInCurrentBlock;
78                        }
79                       
80                        BOX_TRACE("Reading " << toRead << " bytes from stream");
81       
82                        // Read it
83                        int r = mrSource.Read(((uint8_t*)pBuffer) + read, toRead, Timeout);
84                        // Give up now if it didn't return anything
85                        if(r == 0)
86                        {
87                                BOX_TRACE("Read " << r << " bytes from "
88                                        "stream, returning");
89                                return read;
90                        }
91                       
92                        // Adjust counts of bytes by the bytes recieved
93                        read += r;
94                        mBytesLeftInCurrentBlock -= r;
95                       
96                        // stop now if the stream returned less than we asked for -- avoid blocking
97                        if(r != toRead)
98                        {
99                                BOX_TRACE("Read " << r << " bytes from "
100                                        "stream, returning");
101                                return read;
102                        }
103                }
104                else
105                {
106                        // Read the header byte to find out how much there is
107                        // in the next block
108                        uint8_t header;
109                        if(mrSource.Read(&header, 1, Timeout) == 0)
110                        {
111                                // Didn't get the byte, return now
112                                BOX_TRACE("Read 0 bytes of block header, "
113                                        "returning with " << read << " bytes "
114                                        "read this time");
115                                return read;
116                        }
117                       
118                        // Interpret the byte...
119                        if(header == Protocol::ProtocolStreamHeader_EndOfStream)
120                        {
121                                // All done.
122                                mFinished = true;
123                                BOX_TRACE("Stream finished, returning with " <<
124                                        read << " bytes read this time");
125                                return read;
126                        }
127                        else if(header <= Protocol::ProtocolStreamHeader_MaxEncodedSizeValue)
128                        {
129                                // get size of the block from the Protocol's lovely list
130                                mBytesLeftInCurrentBlock = Protocol::sProtocolStreamHeaderLengths[header];
131                        }
132                        else if(header == Protocol::ProtocolStreamHeader_SizeIs64k)
133                        {
134                                // 64k
135                                mBytesLeftInCurrentBlock = (64*1024);
136                        }
137                        else
138                        {
139                                // Bad. It used the reserved values.
140                                THROW_EXCEPTION(ServerException, ProtocolUncertainStreamBadBlockHeader) 
141                        }
142
143                        BOX_TRACE("Read header byte " << (int)header << ", "
144                                "next block has " << 
145                                mBytesLeftInCurrentBlock << " bytes");
146                }
147        }
148
149        // Return the number read
150        return read;
151}
152
153// --------------------------------------------------------------------------
154//
155// Function
156//              Name:    ProtocolUncertainStream::BytesLeftToRead()
157//              Purpose: As interface.
158//              Created: 2003/12/05
159//
160// --------------------------------------------------------------------------
161IOStream::pos_type ProtocolUncertainStream::BytesLeftToRead()
162{
163        // Only know how much is left if everything is finished
164        return mFinished?(0):(IOStream::SizeOfStreamUnknown);
165}
166
167// --------------------------------------------------------------------------
168//
169// Function
170//              Name:    ProtocolUncertainStream::Write(const void *, int)
171//              Purpose: As interface. But will exception.
172//              Created: 2003/12/05
173//
174// --------------------------------------------------------------------------
175void ProtocolUncertainStream::Write(const void *pBuffer, int NBytes)
176{
177        THROW_EXCEPTION(ServerException, CantWriteToProtocolUncertainStream)
178}
179
180// --------------------------------------------------------------------------
181//
182// Function
183//              Name:    ProtocolUncertainStream::StreamDataLeft()
184//              Purpose: As interface.
185//              Created: 2003/12/05
186//
187// --------------------------------------------------------------------------
188bool ProtocolUncertainStream::StreamDataLeft()
189{
190        return !mFinished;
191}
192
193// --------------------------------------------------------------------------
194//
195// Function
196//              Name:    ProtocolUncertainStream::StreamClosed()
197//              Purpose: As interface.
198//              Created: 2003/12/05
199//
200// --------------------------------------------------------------------------
201bool ProtocolUncertainStream::StreamClosed()
202{
203        // always closed
204        return true;
205}
206
Note: See TracBrowser for help on using the repository browser.