Team Fortress 2 Source Code as on 22/4/2020
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.

312 lines
7.3 KiB

  1. // channels.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #ifndef CRYPTOPP_IMPORTS
  4. #include "cryptlib.h"
  5. #include "channels.h"
  6. NAMESPACE_BEGIN(CryptoPP)
  7. USING_NAMESPACE(std)
  8. #if 0
  9. void MessageSwitch::AddDefaultRoute(BufferedTransformation &destination, const std::string &channel)
  10. {
  11. m_defaultRoutes.push_back(Route(&destination, channel));
  12. }
  13. void MessageSwitch::AddRoute(unsigned int begin, unsigned int end, BufferedTransformation &destination, const std::string &channel)
  14. {
  15. RangeRoute route(begin, end, Route(&destination, channel));
  16. RouteList::iterator it = upper_bound(m_routes.begin(), m_routes.end(), route);
  17. m_routes.insert(it, route);
  18. }
  19. /*
  20. class MessageRouteIterator
  21. {
  22. public:
  23. typedef MessageSwitch::RouteList::const_iterator RouteIterator;
  24. typedef MessageSwitch::DefaultRouteList::const_iterator DefaultIterator;
  25. bool m_useDefault;
  26. RouteIterator m_itRouteCurrent, m_itRouteEnd;
  27. DefaultIterator m_itDefaultCurrent, m_itDefaultEnd;
  28. MessageRouteIterator(MessageSwitch &ms, const std::string &channel)
  29. : m_channel(channel)
  30. {
  31. pair<MapIterator, MapIterator> range = cs.m_routeMap.equal_range(channel);
  32. if (range.first == range.second)
  33. {
  34. m_useDefault = true;
  35. m_itListCurrent = cs.m_defaultRoutes.begin();
  36. m_itListEnd = cs.m_defaultRoutes.end();
  37. }
  38. else
  39. {
  40. m_useDefault = false;
  41. m_itMapCurrent = range.first;
  42. m_itMapEnd = range.second;
  43. }
  44. }
  45. bool End() const
  46. {
  47. return m_useDefault ? m_itListCurrent == m_itListEnd : m_itMapCurrent == m_itMapEnd;
  48. }
  49. void Next()
  50. {
  51. if (m_useDefault)
  52. ++m_itListCurrent;
  53. else
  54. ++m_itMapCurrent;
  55. }
  56. BufferedTransformation & Destination()
  57. {
  58. return m_useDefault ? *m_itListCurrent->first : *m_itMapCurrent->second.first;
  59. }
  60. const std::string & Message()
  61. {
  62. if (m_useDefault)
  63. return m_itListCurrent->second.get() ? *m_itListCurrent->second.get() : m_channel;
  64. else
  65. return m_itMapCurrent->second.second;
  66. }
  67. };
  68. void MessageSwitch::Put(byte inByte);
  69. void MessageSwitch::Put(const byte *inString, unsigned int length);
  70. void MessageSwitch::Flush(bool completeFlush, int propagation=-1);
  71. void MessageSwitch::MessageEnd(int propagation=-1);
  72. void MessageSwitch::PutMessageEnd(const byte *inString, unsigned int length, int propagation=-1);
  73. void MessageSwitch::MessageSeriesEnd(int propagation=-1);
  74. */
  75. #endif
  76. //
  77. // ChannelRouteIterator
  78. //////////////////////////
  79. void ChannelRouteIterator::Reset(const std::string &channel)
  80. {
  81. m_channel = channel;
  82. pair<MapIterator, MapIterator> range = m_cs.m_routeMap.equal_range(channel);
  83. if (range.first == range.second)
  84. {
  85. m_useDefault = true;
  86. m_itListCurrent = m_cs.m_defaultRoutes.begin();
  87. m_itListEnd = m_cs.m_defaultRoutes.end();
  88. }
  89. else
  90. {
  91. m_useDefault = false;
  92. m_itMapCurrent = range.first;
  93. m_itMapEnd = range.second;
  94. }
  95. }
  96. bool ChannelRouteIterator::End() const
  97. {
  98. return m_useDefault ? m_itListCurrent == m_itListEnd : m_itMapCurrent == m_itMapEnd;
  99. }
  100. void ChannelRouteIterator::Next()
  101. {
  102. if (m_useDefault)
  103. ++m_itListCurrent;
  104. else
  105. ++m_itMapCurrent;
  106. }
  107. BufferedTransformation & ChannelRouteIterator::Destination()
  108. {
  109. return m_useDefault ? *m_itListCurrent->first : *m_itMapCurrent->second.first;
  110. }
  111. const std::string & ChannelRouteIterator::Channel()
  112. {
  113. if (m_useDefault)
  114. return m_itListCurrent->second.get() ? *m_itListCurrent->second.get() : m_channel;
  115. else
  116. return m_itMapCurrent->second.second;
  117. }
  118. //
  119. // ChannelSwitch
  120. ///////////////////
  121. size_t ChannelSwitch::ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
  122. {
  123. if (m_blocked)
  124. {
  125. m_blocked = false;
  126. goto WasBlocked;
  127. }
  128. m_it.Reset(channel);
  129. while (!m_it.End())
  130. {
  131. WasBlocked:
  132. if (m_it.Destination().ChannelPut2(m_it.Channel(), begin, length, messageEnd, blocking))
  133. {
  134. m_blocked = true;
  135. return 1;
  136. }
  137. m_it.Next();
  138. }
  139. return 0;
  140. }
  141. void ChannelSwitch::IsolatedInitialize(const NameValuePairs& parameters)
  142. {
  143. CRYPTOPP_UNUSED(parameters);
  144. m_routeMap.clear();
  145. m_defaultRoutes.clear();
  146. m_blocked = false;
  147. }
  148. bool ChannelSwitch::ChannelFlush(const std::string &channel, bool completeFlush, int propagation, bool blocking)
  149. {
  150. if (m_blocked)
  151. {
  152. m_blocked = false;
  153. goto WasBlocked;
  154. }
  155. m_it.Reset(channel);
  156. while (!m_it.End())
  157. {
  158. WasBlocked:
  159. if (m_it.Destination().ChannelFlush(m_it.Channel(), completeFlush, propagation, blocking))
  160. {
  161. m_blocked = true;
  162. return true;
  163. }
  164. m_it.Next();
  165. }
  166. return false;
  167. }
  168. bool ChannelSwitch::ChannelMessageSeriesEnd(const std::string &channel, int propagation, bool blocking)
  169. {
  170. CRYPTOPP_UNUSED(blocking);
  171. if (m_blocked)
  172. {
  173. m_blocked = false;
  174. goto WasBlocked;
  175. }
  176. m_it.Reset(channel);
  177. while (!m_it.End())
  178. {
  179. WasBlocked:
  180. if (m_it.Destination().ChannelMessageSeriesEnd(m_it.Channel(), propagation))
  181. {
  182. m_blocked = true;
  183. return true;
  184. }
  185. m_it.Next();
  186. }
  187. return false;
  188. }
  189. byte * ChannelSwitch::ChannelCreatePutSpace(const std::string &channel, size_t &size)
  190. {
  191. m_it.Reset(channel);
  192. if (!m_it.End())
  193. {
  194. BufferedTransformation &target = m_it.Destination();
  195. const std::string &ch = m_it.Channel();
  196. m_it.Next();
  197. if (m_it.End()) // there is only one target channel
  198. return target.ChannelCreatePutSpace(ch, size);
  199. }
  200. size = 0;
  201. return NULL;
  202. }
  203. size_t ChannelSwitch::ChannelPutModifiable2(const std::string &channel, byte *inString, size_t length, int messageEnd, bool blocking)
  204. {
  205. ChannelRouteIterator it(*this);
  206. it.Reset(channel);
  207. if (!it.End())
  208. {
  209. BufferedTransformation &target = it.Destination();
  210. const std::string &targetChannel = it.Channel();
  211. it.Next();
  212. if (it.End()) // there is only one target channel
  213. return target.ChannelPutModifiable2(targetChannel, inString, length, messageEnd, blocking);
  214. }
  215. return ChannelPut2(channel, inString, length, messageEnd, blocking);
  216. }
  217. void ChannelSwitch::AddDefaultRoute(BufferedTransformation &destination)
  218. {
  219. m_defaultRoutes.push_back(DefaultRoute(&destination, value_ptr<std::string>(NULL)));
  220. }
  221. void ChannelSwitch::RemoveDefaultRoute(BufferedTransformation &destination)
  222. {
  223. for (DefaultRouteList::iterator it = m_defaultRoutes.begin(); it != m_defaultRoutes.end(); ++it)
  224. if (it->first == &destination && !it->second.get())
  225. {
  226. m_defaultRoutes.erase(it);
  227. break;
  228. }
  229. }
  230. void ChannelSwitch::AddDefaultRoute(BufferedTransformation &destination, const std::string &outChannel)
  231. {
  232. m_defaultRoutes.push_back(DefaultRoute(&destination, outChannel));
  233. }
  234. void ChannelSwitch::RemoveDefaultRoute(BufferedTransformation &destination, const std::string &outChannel)
  235. {
  236. for (DefaultRouteList::iterator it = m_defaultRoutes.begin(); it != m_defaultRoutes.end(); ++it)
  237. if (it->first == &destination && (it->second.get() && *it->second == outChannel))
  238. {
  239. m_defaultRoutes.erase(it);
  240. break;
  241. }
  242. }
  243. void ChannelSwitch::AddRoute(const std::string &inChannel, BufferedTransformation &destination, const std::string &outChannel)
  244. {
  245. m_routeMap.insert(RouteMap::value_type(inChannel, Route(&destination, outChannel)));
  246. }
  247. void ChannelSwitch::RemoveRoute(const std::string &inChannel, BufferedTransformation &destination, const std::string &outChannel)
  248. {
  249. typedef ChannelSwitch::RouteMap::iterator MapIterator;
  250. pair<MapIterator, MapIterator> range = m_routeMap.equal_range(inChannel);
  251. for (MapIterator it = range.first; it != range.second; ++it)
  252. if (it->second.first == &destination && it->second.second == outChannel)
  253. {
  254. m_routeMap.erase(it);
  255. break;
  256. }
  257. }
  258. NAMESPACE_END
  259. #endif