Mercurial > projects > hoofbaby
annotate deps/Platinum/Source/Core/PltRingBufferStream.cpp @ 0:3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
author | fraserofthenight |
---|---|
date | Mon, 06 Jul 2009 08:06:28 -0700 |
parents | |
children |
rev | line source |
---|---|
0
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
1 /***************************************************************** |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
2 | |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
3 | Platinum - Ring Buffer Stream |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
4 | |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
5 | Copyright (c) 2004-2008, Plutinosoft, LLC. |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
6 | All rights reserved. |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
7 | http://www.plutinosoft.com |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
8 | |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
9 | This program is free software; you can redistribute it and/or |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
10 | modify it under the terms of the GNU General Public License |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
11 | as published by the Free Software Foundation; either version 2 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
12 | of the License, or (at your option) any later version. |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
13 | |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
14 | OEMs, ISVs, VARs and other distributors that combine and |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
15 | distribute commercially licensed software with Platinum software |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
16 | and do not wish to distribute the source code for the commercially |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
17 | licensed software under version 2, or (at your option) any later |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
18 | version, of the GNU General Public License (the "GPL") must enter |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
19 | into a commercial license agreement with Plutinosoft, LLC. |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
20 | |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
21 | This program is distributed in the hope that it will be useful, |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
22 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
24 | GNU General Public License for more details. |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
25 | |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
26 | You should have received a copy of the GNU General Public License |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
27 | along with this program; see the file LICENSE.txt. If not, write to |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
28 | the Free Software Foundation, Inc., |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
29 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
30 | http://www.gnu.org/licenses/gpl-2.0.html |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
31 | |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
32 ****************************************************************/ |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
33 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
34 /*---------------------------------------------------------------------- |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
35 | includes |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
36 +---------------------------------------------------------------------*/ |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
37 #include "PltRingBufferStream.h" |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
38 #include "Neptune.h" |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
39 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
40 /*---------------------------------------------------------------------- |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
41 | defines |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
42 +---------------------------------------------------------------------*/ |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
43 #ifdef max |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
44 #undef max |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
45 #endif |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
46 #define max(a,b) (((a) > (b)) ? (a) : (b)) |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
47 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
48 #ifdef min |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
49 #undef min |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
50 #endif |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
51 #define min(a,b) (((a) < (b)) ? (a) : (b)) |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
52 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
53 /*---------------------------------------------------------------------- |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
54 | PLT_RingBufferStream::PLT_RingBufferStream |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
55 +---------------------------------------------------------------------*/ |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
56 PLT_RingBufferStream::PLT_RingBufferStream(NPT_Size buffer_size, |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
57 bool blocking /* = true */) : |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
58 m_TotalBytesRead(0), |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
59 m_TotalBytesWritten(0), |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
60 m_Eos(false), |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
61 m_Blocking(blocking) |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
62 { |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
63 m_RingBuffer = new NPT_RingBuffer(buffer_size); |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
64 } |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
65 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
66 /*---------------------------------------------------------------------- |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
67 | PLT_RingBufferStream::PLT_RingBufferStream |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
68 +---------------------------------------------------------------------*/ |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
69 PLT_RingBufferStream::PLT_RingBufferStream(NPT_RingBufferReference& buffer, |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
70 bool blocking /* = true */) : |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
71 m_RingBuffer(buffer), |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
72 m_TotalBytesRead(0), |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
73 m_TotalBytesWritten(0), |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
74 m_Eos(false), |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
75 m_Blocking(blocking) |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
76 { |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
77 } |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
78 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
79 /*---------------------------------------------------------------------- |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
80 | PLT_RingBufferStream::~PLT_RingBufferStream |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
81 +---------------------------------------------------------------------*/ |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
82 PLT_RingBufferStream::~PLT_RingBufferStream() |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
83 { |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
84 } |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
85 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
86 /*---------------------------------------------------------------------- |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
87 | PLT_RingBufferStream::Read |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
88 +---------------------------------------------------------------------*/ |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
89 NPT_Result |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
90 PLT_RingBufferStream::Read(void* buffer, |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
91 NPT_Size max_bytes_to_read, |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
92 NPT_Size* bytes_read /*= NULL*/) |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
93 { |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
94 NPT_Size bytes_avail, to_read; |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
95 NPT_Size last_read = 0; |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
96 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
97 // reset output param first |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
98 if (bytes_read) *bytes_read = 0; |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
99 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
100 // wait for data |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
101 do { |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
102 { |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
103 NPT_AutoLock autoLock(m_Lock); |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
104 if (m_RingBuffer->GetContiguousAvailable()) break; |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
105 } |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
106 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
107 if (m_Eos || m_RingBuffer->IsClosed()) { |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
108 return NPT_ERROR_EOS; |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
109 } else if (!m_Blocking) { |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
110 return NPT_ERROR_WOULD_BLOCK; |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
111 } |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
112 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
113 // sleep and try again |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
114 NPT_System::Sleep(NPT_TimeInterval(0, 10000)); |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
115 } while (1); |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
116 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
117 { |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
118 NPT_AutoLock autoLock(m_Lock); |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
119 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
120 // try twice in case we looped |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
121 for (int i=0; i<2; i++) { |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
122 bytes_avail = m_RingBuffer->GetContiguousAvailable(); |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
123 to_read = min(max_bytes_to_read - last_read, bytes_avail); |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
124 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
125 // break if nothing to read |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
126 if (to_read == 0) break; |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
127 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
128 // read into buffer and advance |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
129 NPT_CHECK(m_RingBuffer->Read((unsigned char*)buffer+last_read, to_read)); |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
130 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
131 // keep track of the total bytes we have read so far |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
132 m_TotalBytesRead += to_read; |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
133 last_read += to_read; |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
134 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
135 if (bytes_read) *bytes_read += to_read; |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
136 } |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
137 } |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
138 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
139 // we have read some chars, so return success |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
140 // even if we have read less than asked |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
141 return NPT_SUCCESS; |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
142 } |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
143 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
144 /*---------------------------------------------------------------------- |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
145 | PLT_RingBufferStream::Write |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
146 +---------------------------------------------------------------------*/ |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
147 NPT_Result |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
148 PLT_RingBufferStream::Write(const void* buffer, |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
149 NPT_Size bytes_to_write, |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
150 NPT_Size* bytes_written /*= NULL*/) |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
151 { |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
152 NPT_Size space_avail, to_write; |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
153 NPT_Size last_written = 0; |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
154 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
155 // reset output param first |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
156 if (bytes_written) *bytes_written = 0; |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
157 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
158 // wait for space |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
159 do { |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
160 { |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
161 NPT_AutoLock autoLock(m_Lock); |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
162 if (m_RingBuffer->GetContiguousSpace()) break; |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
163 } |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
164 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
165 if (m_Eos || m_RingBuffer->IsClosed()) { |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
166 return NPT_ERROR_EOS; |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
167 } else if (!m_Blocking) { |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
168 return NPT_ERROR_WOULD_BLOCK; |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
169 } |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
170 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
171 // sleep and try again |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
172 NPT_System::Sleep(NPT_TimeInterval(0, 10000)); |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
173 } while (1); |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
174 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
175 { |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
176 NPT_AutoLock autoLock(m_Lock); |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
177 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
178 // try twice in case we looped |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
179 for (int i=0; i<2; i++) { |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
180 space_avail = m_RingBuffer->GetContiguousSpace(); |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
181 to_write = min(bytes_to_write - last_written, space_avail); |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
182 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
183 // break if no space to write |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
184 if (to_write == 0) break; |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
185 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
186 // write into buffer |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
187 NPT_CHECK(m_RingBuffer->Write((unsigned char*)buffer+last_written, to_write)); |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
188 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
189 m_TotalBytesWritten += to_write; |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
190 last_written += to_write; |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
191 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
192 if (bytes_written) *bytes_written += to_write; |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
193 } |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
194 } |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
195 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
196 // we have written some chars, so return success |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
197 // even if we have written less than provided |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
198 return NPT_SUCCESS; |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
199 } |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
200 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
201 /*---------------------------------------------------------------------- |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
202 | PLT_RingBufferStream::Flush |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
203 +---------------------------------------------------------------------*/ |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
204 NPT_Result |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
205 PLT_RingBufferStream::Flush() |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
206 { |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
207 NPT_AutoLock autoLock(m_Lock); |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
208 |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
209 m_RingBuffer->Flush(); |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
210 m_TotalBytesRead = 0; |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
211 m_TotalBytesWritten = 0; |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
212 return NPT_SUCCESS; |
3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
fraserofthenight
parents:
diff
changeset
|
213 } |