Counter Strike : Global Offensive Source Code
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.

309 lines
7.2 KiB

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