aRts audio server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

datapacket.h 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. Copyright (C) 2000 Stefan Westerfeld
  3. stefan@space.twc.de
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. #ifndef DATAPACKET_H
  18. #define DATAPACKET_H
  19. #include "arts_export.h"
  20. #include "buffer.h"
  21. /*
  22. * BC - Status (2002-03-08): GenericDataChannel, DataPacket types
  23. *
  24. * These classes must be kept binary compatible, as the do interact with
  25. * generated code. So you MUST KNOW WHAT YOU ARE DOING, once you start
  26. * using the provided d pointers for extensions.
  27. */
  28. namespace Arts {
  29. class GenericDataChannelPrivate;
  30. class GenericDataPacket;
  31. /*
  32. * The GenericDataChannel interface is to be implemented by the flowsystem
  33. */
  34. class ARTS_EXPORT GenericDataChannel {
  35. private:
  36. GenericDataChannelPrivate *d; // unused
  37. protected:
  38. friend class GenericDataPacket;
  39. /*
  40. * this is used internally by DataPacket
  41. */
  42. virtual void processedPacket(GenericDataPacket *packet) = 0;
  43. /*
  44. * used internally by DataPacket
  45. */
  46. virtual void sendPacket(GenericDataPacket *packet) = 0;
  47. public:
  48. /*
  49. * used to set pull delivery mode
  50. */
  51. virtual void setPull(int packets, int capacity) = 0;
  52. virtual void endPull() = 0;
  53. GenericDataChannel() : d(0)
  54. {
  55. }
  56. };
  57. /*
  58. * DataPackets are the heard of asynchronous streaming (MCOP has synchronous
  59. * and asynchronous streams). They are used
  60. *
  61. * - in the interface async streams expose to C++ implementations of MCOP
  62. * interfaces (they directly deal with datapackets)
  63. *
  64. * - from the FlowSystem implemenentations
  65. */
  66. /**
  67. * The GenericDataPacket class provides the interface the flow system can
  68. * use to deal with data packets.
  69. */
  70. class GenericDataPacketPrivate;
  71. class ARTS_EXPORT GenericDataPacket {
  72. private:
  73. GenericDataPacketPrivate *d;
  74. static long _staticDataPacketCount;
  75. public:
  76. /**
  77. * the amount of active data packets (memory leak debugging only)
  78. */
  79. static long _dataPacketCount() { return _staticDataPacketCount; }
  80. /**
  81. * the channel this datapacket belongs to
  82. */
  83. GenericDataChannel *channel;
  84. /**
  85. * ensureCapactity ensures that there is room for at least capacity
  86. * Elements in the packet. This is implemented destructive - that
  87. * means: you may not find your old contents in the packet after
  88. * calling ensureCapacity
  89. */
  90. virtual void ensureCapacity(int capacity) = 0;
  91. /**
  92. * read/write write the contents of the packet. Read will also
  93. * automatically ensure that capacity is adapted before reading.
  94. */
  95. virtual void read(Buffer& stream) = 0;
  96. virtual void write(Buffer& stream) = 0;
  97. /**
  98. * having size here (and not in the derived concrete DataPackets) is so
  99. * that we can see whether the sender can't supply more data (and starts
  100. * sending zero size packets
  101. */
  102. int size;
  103. /**
  104. * useCount is to be set from sendPacket
  105. */
  106. int useCount;
  107. inline void send()
  108. {
  109. channel->sendPacket(this);
  110. }
  111. inline void processed()
  112. {
  113. useCount--;
  114. if(useCount == 0)
  115. {
  116. if(channel)
  117. channel->processedPacket(this);
  118. else
  119. delete this;
  120. }
  121. }
  122. virtual ~GenericDataPacket()
  123. {
  124. _staticDataPacketCount--;
  125. }
  126. protected:
  127. GenericDataPacket(GenericDataChannel *channel)
  128. :d(0),channel(channel),useCount(0)
  129. {
  130. _staticDataPacketCount++;
  131. }
  132. };
  133. /**
  134. * The DataPacket<T> interface is what C++ implementations of MCOP interfaces
  135. * will need to use.
  136. */
  137. template<class T>
  138. class DataPacket : public GenericDataPacket {
  139. public:
  140. T *contents;
  141. protected:
  142. DataPacket(GenericDataChannel *channel)
  143. : GenericDataPacket(channel) {}
  144. ~DataPacket() {}
  145. };
  146. /**
  147. * The RawDataPacket<T> interface handles raw class T arrays of data
  148. */
  149. template<class T>
  150. class RawDataPacket : public DataPacket<T> {
  151. protected:
  152. int capacity;
  153. void ensureCapacity(int newCapacity)
  154. {
  155. if(newCapacity > capacity)
  156. {
  157. delete[] this->contents;
  158. capacity = newCapacity;
  159. this->contents = new T[capacity];
  160. }
  161. }
  162. RawDataPacket(int capacity, GenericDataChannel *channel)
  163. :DataPacket<T>(channel), capacity(capacity)
  164. {
  165. this->size = capacity;
  166. this->contents = new T[capacity];
  167. }
  168. ~RawDataPacket()
  169. {
  170. delete[] this->contents;
  171. }
  172. };
  173. /**
  174. * FloatDataPacket finally is one concrete DataPacket (which contains the
  175. * information how to marshal a datapacket of type float)
  176. */
  177. class ARTS_EXPORT FloatDataPacket : public RawDataPacket<float> {
  178. public:
  179. FloatDataPacket(int capacity, GenericDataChannel *channel)
  180. : RawDataPacket<float>(capacity, channel)
  181. {
  182. //
  183. }
  184. void read(Buffer& stream);
  185. void write(Buffer& stream);
  186. };
  187. class ARTS_EXPORT ByteDataPacket : public RawDataPacket<mcopbyte> {
  188. public:
  189. ByteDataPacket(int capacity, GenericDataChannel *channel)
  190. : RawDataPacket<mcopbyte>(capacity, channel)
  191. {
  192. //
  193. }
  194. void read(Buffer& stream);
  195. void write(Buffer& stream);
  196. };
  197. }
  198. #endif