Marsyas
0.6.0-alpha
|
00001 /* 00002 ** Copyright (C) 2014 George Tzanetakis <gtzan@cs.uvic.ca> 00003 ** 00004 ** This program is free software; you can redistribute it and/or modify 00005 ** it under the terms of the GNU General Public License as published by 00006 ** the Free Software Foundation; either version 2 of the License, or 00007 ** (at your option) any later version. 00008 ** 00009 ** This program is distributed in the hope that it will be useful, 00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 ** GNU General Public License for more details. 00013 ** 00014 ** You should have received a copy of the GNU General Public License 00015 ** along with this program; if not, write to the Free Software 00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 */ 00018 00019 #ifndef MARSYAS_REALTIME_PACKET_QUEUE_INCLUDED 00020 #define MARSYAS_REALTIME_PACKET_QUEUE_INCLUDED 00021 00022 #include "queue.h" 00023 #include <iostream> 00024 #include <algorithm> 00025 00026 namespace Marsyas { 00027 namespace RealTime { 00028 00029 using std::size_t; 00030 00031 class packet_queue 00032 { 00033 00034 queue<char> m_byte_queue; 00035 00036 public: 00037 packet_queue(size_t capacity): 00038 m_byte_queue(capacity) 00039 {} 00040 00041 void clear() { m_byte_queue.clear(); } 00042 00043 bool push ( const char *data, size_t count ) 00044 { 00045 queue_producer<char> producer(m_byte_queue, sizeof(size_t) + count); 00046 00047 if (!producer.capacity()) 00048 return false; 00049 00050 producer.write(0, reinterpret_cast<char*>(&count), sizeof(size_t)); 00051 producer.write(sizeof(size_t), data, count); 00052 00053 return true; 00054 } 00055 00056 size_t pop ( char *data, size_t max_count ) 00057 { 00058 size_t count; 00059 00060 queue_consumer<char> consumer(m_byte_queue, sizeof(size_t)); 00061 00062 if (!consumer.capacity()) 00063 return 0; 00064 00065 consumer.read(0, reinterpret_cast<char*>(&count), sizeof(size_t)); 00066 00067 if (!consumer.reserve(sizeof(size_t) + count)) 00068 { 00069 consumer.abort(); 00070 return 0; 00071 } 00072 00073 consumer.read( sizeof(size_t), data, std::min(count, max_count) ); 00074 00075 return count; 00076 } 00077 }; 00078 00079 } 00080 } 00081 00082 #endif // MARSYAS_REALTIME_PACKET_QUEUE_INCLUDED