]> git.sesse.net Git - casparcg/blob - protocol/util/strategy_adapters.cpp
[ffmpeg_consumer] Retired old implementation in favour of the now updated streaming_c...
[casparcg] / protocol / util / strategy_adapters.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Helge Norberg, helge.norberg@svt.se\r
20 */\r
21 \r
22 #include "../StdAfx.h"\r
23 \r
24 #include "strategy_adapters.h"\r
25 \r
26 #include <boost/locale.hpp>\r
27 #include <boost/algorithm/string/replace.hpp>\r
28 \r
29 namespace caspar { namespace IO {\r
30 \r
31 class to_unicode_adapter : public protocol_strategy<char>\r
32 {\r
33         std::string codepage_;\r
34         protocol_strategy<wchar_t>::ptr unicode_strategy_;\r
35 public:\r
36         to_unicode_adapter(\r
37                         const std::string& codepage, \r
38                         const protocol_strategy<wchar_t>::ptr& unicode_strategy)\r
39                 : codepage_(codepage)\r
40                 , unicode_strategy_(unicode_strategy)\r
41         {\r
42         }\r
43 \r
44         void parse(const std::basic_string<char>& data) override\r
45         {\r
46                 auto utf_data = boost::locale::conv::to_utf<wchar_t>(data, codepage_);\r
47 \r
48                 unicode_strategy_->parse(utf_data);\r
49         }\r
50 };\r
51 \r
52 class from_unicode_client_connection : public client_connection<wchar_t>\r
53 {\r
54         client_connection<char>::ptr client_;\r
55         std::string codepage_;\r
56 public:\r
57         from_unicode_client_connection(\r
58                         const client_connection<char>::ptr& client, const std::string& codepage)\r
59                 : client_(client)\r
60                 , codepage_(codepage)\r
61         {\r
62         }\r
63         ~from_unicode_client_connection()\r
64         {\r
65         }\r
66 \r
67         void send(std::basic_string<wchar_t>&& data) override\r
68         {\r
69                 auto str = boost::locale::conv::from_utf<wchar_t>(data, codepage_);\r
70 \r
71                 client_->send(std::move(str));\r
72 \r
73                 if (data.length() < 512)\r
74                 {\r
75                         boost::replace_all(data, L"\n", L"\\n");\r
76                         boost::replace_all(data, L"\r", L"\\r");\r
77                         CASPAR_LOG_COMMUNICATION(info) << L"Sent message to " << client_->address() << L":" << data;\r
78                 }\r
79                 else\r
80                         CASPAR_LOG_COMMUNICATION(info) << L"Sent more than 512 bytes to " << client_->address();\r
81         }\r
82 \r
83         void disconnect() override\r
84         {\r
85                 client_->disconnect();\r
86         }\r
87 \r
88         std::wstring address() const override\r
89         {\r
90                 return client_->address();\r
91         }\r
92 \r
93         void add_lifecycle_bound_object(const std::wstring& key, const std::shared_ptr<void>& lifecycle_bound) override\r
94         {\r
95                 client_->add_lifecycle_bound_object(key, lifecycle_bound);\r
96         }\r
97         std::shared_ptr<void> remove_lifecycle_bound_object(const std::wstring& key) override\r
98         {\r
99                 return client_->remove_lifecycle_bound_object(key);\r
100         }\r
101 };\r
102 \r
103 to_unicode_adapter_factory::to_unicode_adapter_factory(\r
104                 const std::string& codepage, \r
105                 const protocol_strategy_factory<wchar_t>::ptr& unicode_strategy_factory)\r
106         : codepage_(codepage)\r
107         , unicode_strategy_factory_(unicode_strategy_factory)\r
108 {\r
109 }\r
110 \r
111 protocol_strategy<char>::ptr to_unicode_adapter_factory::create(\r
112         const client_connection<char>::ptr& client_connection)\r
113 {\r
114         auto client = spl::make_shared<from_unicode_client_connection>(client_connection, codepage_);\r
115 \r
116         return spl::make_shared<to_unicode_adapter>(codepage_, unicode_strategy_factory_->create(client));\r
117 }\r
118 \r
119 class legacy_strategy_adapter : public protocol_strategy<wchar_t>\r
120 {\r
121         ProtocolStrategyPtr strategy_;\r
122         ClientInfoPtr client_info_;\r
123 public:\r
124         legacy_strategy_adapter(\r
125                         const ProtocolStrategyPtr& strategy, \r
126                         const client_connection<wchar_t>::ptr& client_connection)\r
127                 : strategy_(strategy)\r
128                 , client_info_(client_connection)\r
129         {\r
130         }\r
131         ~legacy_strategy_adapter()\r
132         {\r
133         }\r
134 \r
135         void parse(const std::basic_string<wchar_t>& data) override\r
136         {\r
137                 strategy_->Parse(data, client_info_);\r
138         }\r
139 };\r
140 \r
141 legacy_strategy_adapter_factory::legacy_strategy_adapter_factory(\r
142                 const ProtocolStrategyPtr& strategy)\r
143         : strategy_(strategy)\r
144 {\r
145 }\r
146 \r
147 protocol_strategy<wchar_t>::ptr legacy_strategy_adapter_factory::create(\r
148                 const client_connection<wchar_t>::ptr& client_connection)\r
149 {\r
150         return spl::make_shared<legacy_strategy_adapter>(strategy_, client_connection);\r
151 }\r
152 \r
153 protocol_strategy_factory<char>::ptr wrap_legacy_protocol(\r
154                 const std::string& delimiter, \r
155                 const ProtocolStrategyPtr& strategy)\r
156 {\r
157         return spl::make_shared<delimiter_based_chunking_strategy_factory<char>>(\r
158                         delimiter,\r
159                         spl::make_shared<to_unicode_adapter_factory>(\r
160                                         strategy->GetCodepage(),\r
161                                         spl::make_shared<legacy_strategy_adapter_factory>(strategy)));\r
162 }\r
163 \r
164 }}\r