00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef BASICMUXCHANNEL_H
00029 #define BASICMUXCHANNEL_H
00030
00031 #include <BasicUtils/BasicCondition.h>
00032 #include "basicmuxmessage.h"
00033
00034 #include <list>
00035
00036 class BasicMsgQueueMUX;
00037
00038 class BasicMUXChannelBase {
00039 long key;
00040 long msgSize;
00041 long qLength;
00042
00043 BasicMsgQueueMUX *parent;
00044 long filled;
00045
00046 typedef std::list<BasicMUXMessageBase *> queue_t;
00047 queue_t readQ;
00048 queue_t writeQ;
00049
00050 BasicCondition readLock;
00051 BasicCondition writeLock;
00052
00053 public:
00054 BasicMUXChannelBase(long key, long msgSize, long qLength);
00055 virtual ~BasicMUXChannelBase();
00056
00057 long getKey() {return key;}
00058
00059 BasicMUXMessageBase *read(bool blocking = true);
00060
00061 bool write(BasicMUXMessageBase *msg, bool blocking = true);
00062 void flush();
00063
00064 virtual BasicMUXMessageBase *allocateMsg()
00065 {return new BasicMUXMessageBase(msgSize);}
00066
00067 protected:
00068 void setParent(BasicMsgQueueMUX *parent) {this->parent = parent;}
00069 void enqueue(BasicMUXMessageBase *msg);
00070 BasicMUXMessageBase *dequeue();
00071 bool writeQueueIsEmpty();
00072 void msgCleared();
00073
00074 friend class BasicMsgQueueMUX;
00075 };
00076
00077 template <class T>
00078 class BasicMUXChannel : public BasicMUXChannelBase {
00079 public:
00080 BasicMUXChannel(long key, long qLength) :
00081 BasicMUXChannelBase(key, sizeof(T), qLength) {
00082 }
00083
00084 T readT(bool blocking = true) {
00085 T data;
00086 BasicMUXMessage<T> *msg = (BasicMUXMessage<T> *)read(blocking);
00087
00088 if (msg) {
00089 data = msg->getDataRef();
00090 delete msg;
00091 } else memset(&data, 0, sizeof(T));
00092
00093 return data;
00094 }
00095
00096 bool writeT(T &data, bool blocking = true) {
00097 return write(new BasicMUXMessage<T>(data), blocking);
00098 }
00099
00100 virtual BasicMUXMessageBase *allocateMsg() {return new BasicMUXMessage<T>;}
00101 };
00102 #endif