Crypto++
mqueue.h
1 #ifndef CRYPTOPP_MQUEUE_H
2 #define CRYPTOPP_MQUEUE_H
3 
4 #include "queue.h"
5 #include "filters.h"
6 #include <deque>
7 
8 NAMESPACE_BEGIN(CryptoPP)
9 
10 //! Message Queue
12 {
13 public:
14  MessageQueue(unsigned int nodeSize=256);
15 
16  void IsolatedInitialize(const NameValuePairs &parameters)
17  {m_queue.IsolatedInitialize(parameters); m_lengths.assign(1, 0U); m_messageCounts.assign(1, 0U);}
18  size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
19  {
20  m_queue.Put(begin, length);
21  m_lengths.back() += length;
22  if (messageEnd)
23  {
24  m_lengths.push_back(0);
25  m_messageCounts.back()++;
26  }
27  return 0;
28  }
29  bool IsolatedFlush(bool hardFlush, bool blocking) {return false;}
30  bool IsolatedMessageSeriesEnd(bool blocking)
31  {m_messageCounts.push_back(0); return false;}
32 
33  lword MaxRetrievable() const
34  {return m_lengths.front();}
35  bool AnyRetrievable() const
36  {return m_lengths.front() > 0;}
37 
38  size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
39  size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
40 
41  lword TotalBytesRetrievable() const
42  {return m_queue.MaxRetrievable();}
43  unsigned int NumberOfMessages() const
44  {return (unsigned int)m_lengths.size()-1;}
45  bool GetNextMessage();
46 
47  unsigned int NumberOfMessagesInThisSeries() const
48  {return m_messageCounts[0];}
49  unsigned int NumberOfMessageSeries() const
50  {return (unsigned int)m_messageCounts.size()-1;}
51 
52  unsigned int CopyMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX, const std::string &channel=DEFAULT_CHANNEL) const;
53 
54  const byte * Spy(size_t &contiguousSize) const;
55 
56  void swap(MessageQueue &rhs);
57 
58 private:
59  ByteQueue m_queue;
60  std::deque<lword> m_lengths;
61  std::deque<unsigned int> m_messageCounts;
62 };
63 
64 
65 //! A filter that checks messages on two channels for equality
66 class CRYPTOPP_DLL EqualityComparisonFilter : public Unflushable<Multichannel<Filter> >
67 {
68 public:
69  struct MismatchDetected : public Exception {MismatchDetected() : Exception(DATA_INTEGRITY_CHECK_FAILED, "EqualityComparisonFilter: did not receive the same data on two channels") {}};
70 
71  /*! if throwIfNotEqual is false, this filter will output a '\\0' byte when it detects a mismatch, '\\1' otherwise */
72  EqualityComparisonFilter(BufferedTransformation *attachment=NULL, bool throwIfNotEqual=true, const std::string &firstChannel="0", const std::string &secondChannel="1")
73  : m_throwIfNotEqual(throwIfNotEqual), m_mismatchDetected(false)
74  , m_firstChannel(firstChannel), m_secondChannel(secondChannel)
75  {Detach(attachment);}
76 
77  size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking);
78  bool ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1, bool blocking=true);
79 
80 private:
81  unsigned int MapChannel(const std::string &channel) const;
82  bool HandleMismatchDetected(bool blocking);
83 
84  bool m_throwIfNotEqual, m_mismatchDetected;
85  std::string m_firstChannel, m_secondChannel;
86  MessageQueue m_q[2];
87 };
88 
89 NAMESPACE_END
90 
91 #ifndef __BORLANDC__
92 NAMESPACE_BEGIN(std)
93 template<> inline void swap(CryptoPP::MessageQueue &a, CryptoPP::MessageQueue &b)
94 {
95  a.swap(b);
96 }
97 NAMESPACE_END
98 #endif
99 
100 #endif
base class for all exceptions thrown by Crypto++
Definition: cryptlib.h:109
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
input multiple bytes for blocking or non-blocking processing
Definition: mqueue.h:18
Message Queue.
Definition: mqueue.h:11
unsigned int NumberOfMessages() const
number of times MessageEnd() has been received minus messages retrieved or skipped ...
Definition: mqueue.h:43
interface for buffered transformations
Definition: cryptlib.h:770
lword MaxRetrievable() const
returns number of bytes that is currently ready for retrieval
Definition: mqueue.h:33
EqualityComparisonFilter(BufferedTransformation *attachment=NULL, bool throwIfNotEqual=true, const std::string &firstChannel="0", const std::string &secondChannel="1")
Definition: mqueue.h:72
bool AnyRetrievable() const
returns whether any bytes are currently ready for retrieval
Definition: mqueue.h:35
const std::string DEFAULT_CHANNEL
the default channel for BufferedTransformation, equal to the empty string
Byte Queue.
Definition: queue.h:16
A filter that checks messages on two channels for equality.
Definition: mqueue.h:66
interface for retrieving values given their names
Definition: cryptlib.h:224