1 /*****************************************************************
3 | Platinum - Ring Buffer Stream
5 | Copyright (c) 2004-2010, Plutinosoft, LLC.
7 | http://www.plutinosoft.com
9 | This program is free software; you can redistribute it and/or
10 | modify it under the terms of the GNU General Public License
11 | as published by the Free Software Foundation; either version 2
12 | of the License, or (at your option) any later version.
14 | OEMs, ISVs, VARs and other distributors that combine and
15 | distribute commercially licensed software with Platinum software
16 | and do not wish to distribute the source code for the commercially
17 | licensed software under version 2, or (at your option) any later
18 | version, of the GNU General Public License (the "GPL") must enter
19 | into a commercial license agreement with Plutinosoft, LLC.
20 | licensing@plutinosoft.com
22 | This program is distributed in the hope that it will be useful,
23 | but WITHOUT ANY WARRANTY; without even the implied warranty of
24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 | GNU General Public License for more details.
27 | You should have received a copy of the GNU General Public License
28 | along with this program; see the file LICENSE.txt. If not, write to
29 | the Free Software Foundation, Inc.,
30 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 | http://www.gnu.org/licenses/gpl-2.0.html
33 ****************************************************************/
35 /*----------------------------------------------------------------------
37 +---------------------------------------------------------------------*/
38 #include "PltRingBufferStream.h"
41 /*----------------------------------------------------------------------
43 +---------------------------------------------------------------------*/
47 #define max(a,b) (((a) > (b)) ? (a) : (b))
52 #define min(a,b) (((a) < (b)) ? (a) : (b))
54 /*----------------------------------------------------------------------
55 | PLT_RingBufferStream::PLT_RingBufferStream
56 +---------------------------------------------------------------------*/
57 PLT_RingBufferStream::PLT_RingBufferStream(NPT_Size buffer_size
,
58 bool blocking
/* = true */) :
60 m_TotalBytesWritten(0),
65 m_RingBuffer
= new NPT_RingBuffer(buffer_size
);
68 /*----------------------------------------------------------------------
69 | PLT_RingBufferStream::PLT_RingBufferStream
70 +---------------------------------------------------------------------*/
71 PLT_RingBufferStream::PLT_RingBufferStream(NPT_RingBufferReference
& buffer
,
72 bool blocking
/* = true */) :
75 m_TotalBytesWritten(0),
82 /*----------------------------------------------------------------------
83 | PLT_RingBufferStream::~PLT_RingBufferStream
84 +---------------------------------------------------------------------*/
85 PLT_RingBufferStream::~PLT_RingBufferStream()
89 /*----------------------------------------------------------------------
90 | PLT_RingBufferStream::Read
91 +---------------------------------------------------------------------*/
93 PLT_RingBufferStream::Read(void* buffer
,
94 NPT_Size max_bytes_to_read
,
95 NPT_Size
* _bytes_read
/*= NULL*/)
97 NPT_Size bytes_to_read
;
98 NPT_Size bytes_read
= 0;
100 // reset output param first
101 if (_bytes_read
) *_bytes_read
= 0;
106 NPT_AutoLock
autoLock(m_Lock
);
109 return NPT_ERROR_INTERRUPTED
;
113 if (m_RingBuffer
->GetAvailable())
117 return NPT_ERROR_EOS
;
118 } else if (!m_Blocking
) {
119 return NPT_ERROR_WOULD_BLOCK
;
123 // sleep and try again
124 NPT_System::Sleep(NPT_TimeInterval(.1));
128 NPT_AutoLock
autoLock(m_Lock
);
130 // try twice in case available data was not contiguous
131 for (int i
=0; i
<2; i
++) {
132 bytes_to_read
= min(max_bytes_to_read
- bytes_read
, m_RingBuffer
->GetContiguousAvailable());
134 // break if nothing to read the second time
135 if (bytes_to_read
== 0) break;
137 // read into buffer and advance
138 NPT_CHECK(m_RingBuffer
->Read((unsigned char*)buffer
+bytes_read
, bytes_to_read
));
140 // keep track of the total bytes we have read so far
141 m_TotalBytesRead
+= bytes_to_read
;
142 bytes_read
+= bytes_to_read
;
144 if (_bytes_read
) *_bytes_read
+= bytes_to_read
;
148 // we have read some chars, so return success
149 // even if we have read less than asked
153 /*----------------------------------------------------------------------
154 | PLT_RingBufferStream::Write
155 +---------------------------------------------------------------------*/
157 PLT_RingBufferStream::Write(const void* buffer
,
158 NPT_Size max_bytes_to_write
,
159 NPT_Size
* _bytes_written
/*= NULL*/)
161 NPT_Size bytes_to_write
;
162 NPT_Size bytes_written
= 0;
164 // reset output param first
165 if (_bytes_written
) *_bytes_written
= 0;
170 NPT_AutoLock
autoLock(m_Lock
);
173 return NPT_ERROR_INTERRUPTED
;
176 // return immediately if we are told we're finished
178 return NPT_ERROR_EOS
;
181 if (m_RingBuffer
->GetSpace())
185 return NPT_ERROR_WOULD_BLOCK
;
189 // sleep and try again
190 NPT_System::Sleep(NPT_TimeInterval(.1));
194 NPT_AutoLock
autoLock(m_Lock
);
196 // try twice in case available space was not contiguous
197 for (int i
=0; i
<2; i
++) {
198 bytes_to_write
= min(max_bytes_to_write
- bytes_written
, m_RingBuffer
->GetContiguousSpace());
200 // break if no space to write the second time
201 if (bytes_to_write
== 0) break;
204 NPT_CHECK(m_RingBuffer
->Write((unsigned char*)buffer
+bytes_written
, bytes_to_write
));
206 m_TotalBytesWritten
+= bytes_to_write
;
207 bytes_written
+= bytes_to_write
;
209 if (_bytes_written
) *_bytes_written
+= bytes_to_write
;
213 // we have written some chars, so return success
214 // even if we have written less than provided
218 /*----------------------------------------------------------------------
219 | PLT_RingBufferStream::Flush
220 +---------------------------------------------------------------------*/
222 PLT_RingBufferStream::Flush()
224 NPT_AutoLock
autoLock(m_Lock
);
226 m_RingBuffer
->Flush();
227 m_TotalBytesRead
= 0;
228 m_TotalBytesWritten
= 0;
232 /*----------------------------------------------------------------------
233 | PLT_RingBufferStream::SetEOS
234 +---------------------------------------------------------------------*/
236 PLT_RingBufferStream::SetEOS()
238 NPT_AutoLock
autoLock(m_Lock
);
245 /*----------------------------------------------------------------------
246 | PLT_RingBufferStream::Abort
247 +---------------------------------------------------------------------*/
249 PLT_RingBufferStream::Abort()
251 NPT_AutoLock
autoLock(m_Lock
);