From ab4a102bad18954a326ec38c6ea9237745012df6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjist=C3=B3f?= Date: Sun, 4 Dec 2016 14:31:26 +0100 Subject: [PATCH] fixed a bug, where messages would only be read till whitespaces --- chat_networking.hpp | 12 +++++++++--- check_policy.hpp | 24 ++++++++++++++++++++++-- commandline_chat.cpp | 15 +++++++++++++-- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/chat_networking.hpp b/chat_networking.hpp index 790d568..5db8788 100644 --- a/chat_networking.hpp +++ b/chat_networking.hpp @@ -16,12 +16,15 @@ namespace chat using boost::asio::ip::tcp; - template + template class client_network_manager { - /* compile-time check for whether receive_policy is valid or not */ - static_assert(is_valid_policy::value, + /* compile-time checks for whether policies are valid or not */ + static_assert(is_valid_receive_policy::value, "Receive policy does not supply neccessary methods!"); + static_assert(is_valid_send_policy::value, + "Send policy does not supply neccessary methods!"); + /* members */ private: @@ -45,6 +48,9 @@ namespace chat void send(chat_message message) { + if (!send_policy::check_msg_length(message)) + return; + _ios.post([this, message] { asio::async_write(_socket, asio::buffer(message.get()), diff --git a/check_policy.hpp b/check_policy.hpp index 9f5a51b..3fc045b 100644 --- a/check_policy.hpp +++ b/check_policy.hpp @@ -12,10 +12,10 @@ * - static void login_do_what(chat_message) * - static void logout_do_what(chat_message) */ template -struct is_valid_policy : std::false_type {}; +struct is_valid_receive_policy : std::false_type {}; template -struct is_valid_policy< +struct is_valid_receive_policy< PolicyCandidate, typename std::enable_if< @@ -49,4 +49,24 @@ struct is_valid_policy< void >::value >::type> +: std::true_type {}; + + +/* SFINAE compile-type checker for send policies (as defined in chat_networking.hpp) + * it checks whether PolicyCandidate supplies all of the following methods: + * - static bool check_msg_length(chat_message) */ +template +struct is_valid_send_policy : std::false_type {}; + +template +struct is_valid_send_policy< + PolicyCandidate, + typename + std::enable_if< + std::is_same< + decltype(PolicyCandidate + ::check_msg_length(std::declval())), + bool + >::value + >::type> : std::true_type {}; \ No newline at end of file diff --git a/commandline_chat.cpp b/commandline_chat.cpp index 8a17ca3..b40160f 100644 --- a/commandline_chat.cpp +++ b/commandline_chat.cpp @@ -22,6 +22,17 @@ struct receive_policy_stdout { std::cout << msg.get_content() << " kilépett.\n"; } }; +struct send_policy_stdout +{ + static bool check_msg_length(chat::chat_message msg) + { + bool good = msg.length() <= (256-34); + if (!good) + std::cout << "Az üzenet túl hosszú!\n"; + return good; + } +}; + int main() { @@ -31,14 +42,14 @@ int main() auto epit = resolver.resolve(query); - chat::client_network_manager cnm(ios, epit, "BATMAN"); + chat::client_network_manager cnm(ios, epit, "BATMAN"); std::thread t([&ios]{ ios.run(); }); std::string input; while (true) { - std::cin >> input; + std::getline(std::cin, input); if (input == "exit") break;