+/*\r
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
+*\r
+* This file is part of CasparCG (www.casparcg.com).\r
+*\r
+* CasparCG is free software: you can redistribute it and/or modify\r
+* it under the terms of the GNU General Public License as published by\r
+* the Free Software Foundation, either version 3 of the License, or\r
+* (at your option) any later version.\r
+*\r
+* CasparCG is distributed in the hope that it will be useful,\r
+* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+* GNU General Public License for more details.\r
+*\r
+* You should have received a copy of the GNU General Public License\r
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
+*\r
+* Author: Helge Norberg, helge.norberg@svt.se\r
+*/\r
+\r
+#include "../StdAfx.h"\r
+\r
+#include "stateful_protocol_strategy_wrapper.h"\r
+\r
+namespace caspar { namespace IO {\r
+\r
+stateful_protocol_strategy_wrapper::stateful_protocol_strategy_wrapper(\r
+ const std::function<ProtocolStrategyPtr ()>& factory)\r
+ : factory_(factory)\r
+ , codepage_(factory()->GetCodepage())\r
+{\r
+}\r
+\r
+void stateful_protocol_strategy_wrapper::purge_dead_clients()\r
+{\r
+ for (auto it = strategies_.begin(); it != strategies_.end();)\r
+ {\r
+ auto strong = it->first.lock();\r
+\r
+ if (strong)\r
+ ++it;\r
+ else\r
+ it = strategies_.erase(it);\r
+ }\r
+}\r
+\r
+void stateful_protocol_strategy_wrapper::Parse(\r
+ const wchar_t* pData, int charCount, ClientInfoPtr pClientInfo)\r
+{\r
+ purge_dead_clients();\r
+\r
+ std::weak_ptr<ClientInfo> weak(pClientInfo);\r
+ ProtocolStrategyPtr strategy;\r
+ auto strategy_iter = strategies_.find(weak);\r
+\r
+ if (strategy_iter == strategies_.end())\r
+ {\r
+ strategy = factory_();\r
+ strategies_.insert(std::make_pair(weak, strategy));\r
+ }\r
+ else\r
+ {\r
+ strategy = strategy_iter->second;\r
+ }\r
+\r
+ strategy->Parse(pData, charCount, pClientInfo);\r
+}\r
+\r
+unsigned int stateful_protocol_strategy_wrapper::GetCodepage()\r
+{\r
+ return codepage_;\r
+}\r
+\r
+}}\r