fixed a bug, where messages would only be read till whitespaces

This commit is contained in:
Kjistóf 2016-12-04 14:31:26 +01:00
parent 09c2dafba5
commit ab4a102bad
3 changed files with 44 additions and 7 deletions

View File

@ -16,12 +16,15 @@ namespace chat
using boost::asio::ip::tcp; using boost::asio::ip::tcp;
template <class receive_policy> template <class receive_policy, class send_policy>
class client_network_manager class client_network_manager
{ {
/* compile-time check for whether receive_policy is valid or not */ /* compile-time checks for whether policies are valid or not */
static_assert(is_valid_policy<receive_policy>::value, static_assert(is_valid_receive_policy<receive_policy>::value,
"Receive policy does not supply neccessary methods!"); "Receive policy does not supply neccessary methods!");
static_assert(is_valid_send_policy<send_policy>::value,
"Send policy does not supply neccessary methods!");
/* members */ /* members */
private: private:
@ -45,6 +48,9 @@ namespace chat
void send(chat_message message) void send(chat_message message)
{ {
if (!send_policy::check_msg_length(message))
return;
_ios.post([this, message] _ios.post([this, message]
{ {
asio::async_write(_socket, asio::buffer(message.get()), asio::async_write(_socket, asio::buffer(message.get()),

View File

@ -12,10 +12,10 @@
* - static void login_do_what(chat_message) * - static void login_do_what(chat_message)
* - static void logout_do_what(chat_message) */ * - static void logout_do_what(chat_message) */
template <class PolicyCandidate, typename = void> template <class PolicyCandidate, typename = void>
struct is_valid_policy : std::false_type {}; struct is_valid_receive_policy : std::false_type {};
template <class PolicyCandidate> template <class PolicyCandidate>
struct is_valid_policy< struct is_valid_receive_policy<
PolicyCandidate, PolicyCandidate,
typename typename
std::enable_if< std::enable_if<
@ -49,4 +49,24 @@ struct is_valid_policy<
void void
>::value >::value
>::type> >::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 <class PolicyCandidate, typename = void>
struct is_valid_send_policy : std::false_type {};
template <class PolicyCandidate>
struct is_valid_send_policy<
PolicyCandidate,
typename
std::enable_if<
std::is_same<
decltype(PolicyCandidate
::check_msg_length(std::declval<chat::chat_message>())),
bool
>::value
>::type>
: std::true_type {}; : std::true_type {};

View File

@ -22,6 +22,17 @@ struct receive_policy_stdout
{ std::cout << msg.get_content() << " kilépett.\n"; } { 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() int main()
{ {
@ -31,14 +42,14 @@ int main()
auto epit = resolver.resolve(query); auto epit = resolver.resolve(query);
chat::client_network_manager<receive_policy_stdout> cnm(ios, epit, "BATMAN"); chat::client_network_manager<receive_policy_stdout, send_policy_stdout> cnm(ios, epit, "BATMAN");
std::thread t([&ios]{ ios.run(); }); std::thread t([&ios]{ ios.run(); });
std::string input; std::string input;
while (true) while (true)
{ {
std::cin >> input; std::getline(std::cin, input);
if (input == "exit") if (input == "exit")
break; break;