channels.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // channels.h - originally written and placed in the public domain by Wei Dai
  2. /// \file channels.h
  3. /// \brief Classes for multiple named channels
  4. #ifndef CRYPTOPP_CHANNELS_H
  5. #define CRYPTOPP_CHANNELS_H
  6. #include "cryptlib.h"
  7. #include "simple.h"
  8. #include "smartptr.h"
  9. #include "stdcpp.h"
  10. #if CRYPTOPP_MSC_VERSION
  11. # pragma warning(push)
  12. # pragma warning(disable: 4355)
  13. #endif
  14. NAMESPACE_BEGIN(CryptoPP)
  15. #if 0
  16. /// Route input on default channel to different and/or multiple channels based on message sequence number
  17. class MessageSwitch : public Sink
  18. {
  19. public:
  20. void AddDefaultRoute(BufferedTransformation &destination, const std::string &channel);
  21. void AddRoute(unsigned int begin, unsigned int end, BufferedTransformation &destination, const std::string &channel);
  22. void Put(byte inByte);
  23. void Put(const byte *inString, unsigned int length);
  24. void Flush(bool completeFlush, int propagation=-1);
  25. void MessageEnd(int propagation=-1);
  26. void PutMessageEnd(const byte *inString, unsigned int length, int propagation=-1);
  27. void MessageSeriesEnd(int propagation=-1);
  28. private:
  29. typedef std::pair<BufferedTransformation *, std::string> Route;
  30. struct RangeRoute
  31. {
  32. RangeRoute(unsigned int begin, unsigned int end, const Route &route)
  33. : begin(begin), end(end), route(route) {}
  34. bool operator<(const RangeRoute &rhs) const {return begin < rhs.begin;}
  35. unsigned int begin, end;
  36. Route route;
  37. };
  38. typedef std::list<RangeRoute> RouteList;
  39. typedef std::list<Route> DefaultRouteList;
  40. RouteList m_routes;
  41. DefaultRouteList m_defaultRoutes;
  42. unsigned int m_nCurrentMessage;
  43. };
  44. #endif
  45. class ChannelSwitchTypedefs
  46. {
  47. public:
  48. typedef std::pair<BufferedTransformation *, std::string> Route;
  49. typedef std::multimap<std::string, Route> RouteMap;
  50. typedef std::pair<BufferedTransformation *, value_ptr<std::string> > DefaultRoute;
  51. typedef std::list<DefaultRoute> DefaultRouteList;
  52. // SunCC workaround: can't use const_iterator here
  53. typedef RouteMap::iterator MapIterator;
  54. typedef DefaultRouteList::iterator ListIterator;
  55. };
  56. class ChannelSwitch;
  57. class ChannelRouteIterator : public ChannelSwitchTypedefs
  58. {
  59. public:
  60. ChannelRouteIterator(ChannelSwitch &cs) : m_cs(cs), m_useDefault(false) {}
  61. void Reset(const std::string &channel);
  62. bool End() const;
  63. void Next();
  64. BufferedTransformation & Destination();
  65. const std::string & Channel();
  66. ChannelSwitch& m_cs;
  67. std::string m_channel;
  68. bool m_useDefault;
  69. MapIterator m_itMapCurrent, m_itMapEnd;
  70. ListIterator m_itListCurrent, m_itListEnd;
  71. protected:
  72. // Hide this to see if we break something...
  73. ChannelRouteIterator();
  74. };
  75. /// Route input to different and/or multiple channels based on channel ID
  76. class CRYPTOPP_DLL ChannelSwitch : public Multichannel<Sink>, public ChannelSwitchTypedefs
  77. {
  78. public:
  79. ChannelSwitch() : m_it(*this), m_blocked(false) {}
  80. ChannelSwitch(BufferedTransformation &destination) : m_it(*this), m_blocked(false)
  81. {
  82. AddDefaultRoute(destination);
  83. }
  84. ChannelSwitch(BufferedTransformation &destination, const std::string &outChannel) : m_it(*this), m_blocked(false)
  85. {
  86. AddDefaultRoute(destination, outChannel);
  87. }
  88. void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
  89. size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking);
  90. size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking);
  91. bool ChannelFlush(const std::string &channel, bool completeFlush, int propagation=-1, bool blocking=true);
  92. bool ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1, bool blocking=true);
  93. byte * ChannelCreatePutSpace(const std::string &channel, size_t &size);
  94. void AddDefaultRoute(BufferedTransformation &destination);
  95. void RemoveDefaultRoute(BufferedTransformation &destination);
  96. void AddDefaultRoute(BufferedTransformation &destination, const std::string &outChannel);
  97. void RemoveDefaultRoute(BufferedTransformation &destination, const std::string &outChannel);
  98. void AddRoute(const std::string &inChannel, BufferedTransformation &destination, const std::string &outChannel);
  99. void RemoveRoute(const std::string &inChannel, BufferedTransformation &destination, const std::string &outChannel);
  100. private:
  101. RouteMap m_routeMap;
  102. DefaultRouteList m_defaultRoutes;
  103. ChannelRouteIterator m_it;
  104. bool m_blocked;
  105. friend class ChannelRouteIterator;
  106. };
  107. NAMESPACE_END
  108. #if CRYPTOPP_MSC_VERSION
  109. # pragma warning(pop)
  110. #endif
  111. #endif