----------------------------------------------------------------------------- Add data to the buffer -----------------------------------------------------------------------------
| 130 | // Add data to the buffer |
| 131 | //----------------------------------------------------------------------------- |
| 132 | bool Stream::Put |
| 133 | ( |
| 134 | uint8* _buffer, |
| 135 | uint32 _size |
| 136 | ) |
| 137 | { |
| 138 | if( (m_bufferSize-m_dataSize) < _size ) |
| 139 | { |
| 140 | // There is not enough space left in the buffer for the data |
| 141 | Log::Write( LogLevel_Error, "ERROR: Not enough space in stream buffer"); |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | m_mutex->Lock(); |
| 146 | if( (m_head + _size) > m_bufferSize ) |
| 147 | { |
| 148 | // We will have to wrap around |
| 149 | uint32 block1 = m_bufferSize - m_head; |
| 150 | uint32 block2 = _size - block1; |
| 151 | |
| 152 | memcpy( &m_buffer[m_head], _buffer, block1 ); |
| 153 | memcpy( m_buffer, &_buffer[block1], block2 ); |
| 154 | uint8 * logpos = m_buffer + m_head; |
| 155 | m_head = block2; |
| 156 | LogData( logpos, block1, " Read (controller->buffer): "); |
| 157 | LogData( m_buffer, block2, " Read (controller->buffer): "); |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | // There is enough space before we reach the end of the buffer |
| 162 | memcpy( &m_buffer[m_head], _buffer, _size ); |
| 163 | m_head += _size; |
| 164 | LogData(m_buffer+m_head-_size, _size, " Read (controller->buffer): "); |
| 165 | } |
| 166 | |
| 167 | m_dataSize += _size; |
| 168 | |
| 169 | if( IsSignalled() ) |
| 170 | { |
| 171 | // We now have more data than we are waiting for, so notify the watchers |
| 172 | Notify(); |
| 173 | } |
| 174 | |
| 175 | m_mutex->Unlock(); |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | //----------------------------------------------------------------------------- |
| 180 | // <Stream::Purge> |