Path: blob/a-new-beginning/SharedDependencies/Sources/cryptopp/channels.cpp
2 views
// channels.cpp - originally written and placed in the public domain by Wei Dai1// CryptoPP::Test namespace added by JW in February 201723#include "pch.h"45#ifndef CRYPTOPP_IMPORTS67#include "cryptlib.h"8#include "channels.h"910#if CRYPTOPP_MSC_VERSION11# pragma warning(disable: 4355)12#endif1314NAMESPACE_BEGIN(CryptoPP)1516#if 017void MessageSwitch::AddDefaultRoute(BufferedTransformation &destination, const std::string &channel)18{19m_defaultRoutes.push_back(Route(&destination, channel));20}2122void MessageSwitch::AddRoute(unsigned int begin, unsigned int end, BufferedTransformation &destination, const std::string &channel)23{24RangeRoute route(begin, end, Route(&destination, channel));25RouteList::iterator it = upper_bound(m_routes.begin(), m_routes.end(), route);26m_routes.insert(it, route);27}2829/*30class MessageRouteIterator31{32public:33typedef MessageSwitch::RouteList::const_iterator RouteIterator;34typedef MessageSwitch::DefaultRouteList::const_iterator DefaultIterator;3536bool m_useDefault;37RouteIterator m_itRouteCurrent, m_itRouteEnd;38DefaultIterator m_itDefaultCurrent, m_itDefaultEnd;3940MessageRouteIterator(MessageSwitch &ms, const std::string &channel)41: m_channel(channel)42{43std::pair<MapIterator, MapIterator> range = cs.m_routeMap.equal_range(channel);44if (range.first == range.second)45{46m_useDefault = true;47m_itListCurrent = cs.m_defaultRoutes.begin();48m_itListEnd = cs.m_defaultRoutes.end();49}50else51{52m_useDefault = false;53m_itMapCurrent = range.first;54m_itMapEnd = range.second;55}56}5758bool End() const59{60return m_useDefault ? m_itListCurrent == m_itListEnd : m_itMapCurrent == m_itMapEnd;61}6263void Next()64{65if (m_useDefault)66++m_itListCurrent;67else68++m_itMapCurrent;69}7071BufferedTransformation & Destination()72{73return m_useDefault ? *m_itListCurrent->first : *m_itMapCurrent->second.first;74}7576const std::string & Message()77{78if (m_useDefault)79return m_itListCurrent->second.get() ? *m_itListCurrent->second.get() : m_channel;80else81return m_itMapCurrent->second.second;82}83};8485void MessageSwitch::Put(byte inByte);86void MessageSwitch::Put(const byte *inString, unsigned int length);8788void MessageSwitch::Flush(bool completeFlush, int propagation=-1);89void MessageSwitch::MessageEnd(int propagation=-1);90void MessageSwitch::PutMessageEnd(const byte *inString, unsigned int length, int propagation=-1);91void MessageSwitch::MessageSeriesEnd(int propagation=-1);92*/93#endif949596//97// ChannelRouteIterator98//////////////////////////99100void ChannelRouteIterator::Reset(const std::string &channel)101{102m_channel = channel;103std::pair<MapIterator, MapIterator> range = m_cs.m_routeMap.equal_range(channel);104if (range.first == range.second)105{106m_useDefault = true;107m_itListCurrent = m_cs.m_defaultRoutes.begin();108m_itListEnd = m_cs.m_defaultRoutes.end();109}110else111{112m_useDefault = false;113m_itMapCurrent = range.first;114m_itMapEnd = range.second;115}116}117118bool ChannelRouteIterator::End() const119{120return m_useDefault ? m_itListCurrent == m_itListEnd : m_itMapCurrent == m_itMapEnd;121}122123void ChannelRouteIterator::Next()124{125if (m_useDefault)126++m_itListCurrent;127else128++m_itMapCurrent;129}130131BufferedTransformation & ChannelRouteIterator::Destination()132{133return m_useDefault ? *m_itListCurrent->first : *m_itMapCurrent->second.first;134}135136const std::string & ChannelRouteIterator::Channel()137{138if (m_useDefault)139return m_itListCurrent->second.get() ? *m_itListCurrent->second.get() : m_channel;140else141return m_itMapCurrent->second.second;142}143144145//146// ChannelSwitch147///////////////////148149size_t ChannelSwitch::ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)150{151if (m_blocked)152{153m_blocked = false;154goto WasBlocked;155}156157m_it.Reset(channel);158159while (!m_it.End())160{161WasBlocked:162if (m_it.Destination().ChannelPut2(m_it.Channel(), begin, length, messageEnd, blocking))163{164m_blocked = true;165return 1;166}167168m_it.Next();169}170171return 0;172}173174void ChannelSwitch::IsolatedInitialize(const NameValuePairs& parameters)175{176CRYPTOPP_UNUSED(parameters);177m_routeMap.clear();178m_defaultRoutes.clear();179m_blocked = false;180}181182bool ChannelSwitch::ChannelFlush(const std::string &channel, bool completeFlush, int propagation, bool blocking)183{184if (m_blocked)185{186m_blocked = false;187goto WasBlocked;188}189190m_it.Reset(channel);191192while (!m_it.End())193{194WasBlocked:195if (m_it.Destination().ChannelFlush(m_it.Channel(), completeFlush, propagation, blocking))196{197m_blocked = true;198return true;199}200201m_it.Next();202}203204return false;205}206207bool ChannelSwitch::ChannelMessageSeriesEnd(const std::string &channel, int propagation, bool blocking)208{209CRYPTOPP_UNUSED(blocking);210if (m_blocked)211{212m_blocked = false;213goto WasBlocked;214}215216m_it.Reset(channel);217218while (!m_it.End())219{220WasBlocked:221if (m_it.Destination().ChannelMessageSeriesEnd(m_it.Channel(), propagation))222{223m_blocked = true;224return true;225}226227m_it.Next();228}229230return false;231}232233byte * ChannelSwitch::ChannelCreatePutSpace(const std::string &channel, size_t &size)234{235m_it.Reset(channel);236if (!m_it.End())237{238BufferedTransformation &target = m_it.Destination();239const std::string &ch = m_it.Channel();240m_it.Next();241if (m_it.End()) // there is only one target channel242return target.ChannelCreatePutSpace(ch, size);243}244size = 0;245return NULLPTR;246}247248size_t ChannelSwitch::ChannelPutModifiable2(const std::string &channel, byte *inString, size_t length, int messageEnd, bool blocking)249{250ChannelRouteIterator it(*this);251it.Reset(channel);252253if (!it.End())254{255BufferedTransformation &target = it.Destination();256const std::string &targetChannel = it.Channel();257it.Next();258if (it.End()) // there is only one target channel259return target.ChannelPutModifiable2(targetChannel, inString, length, messageEnd, blocking);260}261262return ChannelPut2(channel, inString, length, messageEnd, blocking);263}264265void ChannelSwitch::AddDefaultRoute(BufferedTransformation &destination)266{267m_defaultRoutes.push_back(DefaultRoute(&destination, value_ptr<std::string>(NULLPTR)));268}269270void ChannelSwitch::RemoveDefaultRoute(BufferedTransformation &destination)271{272for (DefaultRouteList::iterator it = m_defaultRoutes.begin(); it != m_defaultRoutes.end(); ++it)273if (it->first == &destination && !it->second.get())274{275m_defaultRoutes.erase(it);276break;277}278}279280void ChannelSwitch::AddDefaultRoute(BufferedTransformation &destination, const std::string &outChannel)281{282m_defaultRoutes.push_back(DefaultRoute(&destination, outChannel));283}284285void ChannelSwitch::RemoveDefaultRoute(BufferedTransformation &destination, const std::string &outChannel)286{287for (DefaultRouteList::iterator it = m_defaultRoutes.begin(); it != m_defaultRoutes.end(); ++it)288if (it->first == &destination && (it->second.get() && *it->second == outChannel))289{290m_defaultRoutes.erase(it);291break;292}293}294295void ChannelSwitch::AddRoute(const std::string &inChannel, BufferedTransformation &destination, const std::string &outChannel)296{297m_routeMap.insert(RouteMap::value_type(inChannel, Route(&destination, outChannel)));298}299300void ChannelSwitch::RemoveRoute(const std::string &inChannel, BufferedTransformation &destination, const std::string &outChannel)301{302typedef ChannelSwitch::RouteMap::iterator MapIterator;303std::pair<MapIterator, MapIterator> range = m_routeMap.equal_range(inChannel);304305for (MapIterator it = range.first; it != range.second; ++it)306if (it->second.first == &destination && it->second.second == outChannel)307{308m_routeMap.erase(it);309break;310}311}312313NAMESPACE_END314315#endif316317318