cpp11chat/commandline_chat.cpp

66 lines
1.7 KiB
C++
Raw Normal View History

#include <iostream>
#include <thread>
#include <stdexcept>
#include "chat_networking.hpp"
struct receive_policy_stdout
{
static void message_do_what(const chat::chat_message& msg)
{ std::cout << msg.get_content() << '\n'; }
static void handshake_do_what(const chat::chat_message& msg)
{ std::cout << "Handshake üzenetek:\n" << msg.get_content() << '\n'; }
static void serverdirection_do_what(const chat::chat_message& msg)
{ std::cout << "Szerver üzenet: " << msg.get_content() << '\n'; }
static void login_do_what(const chat::chat_message& msg)
{ std::cout << msg.get_content() << " belépett.\n"; }
static void logout_do_what(const chat::chat_message& msg)
{ std::cout << msg.get_content() << " kilépett.\n"; }
static void invalid_msg_do_what(const chat::chat_message& msg)
{ throw std::runtime_error("Invalid message received!"); }
};
struct send_policy_stdout
{
static bool check_msg_length(const chat::chat_message& msg)
{
bool good = msg.length() <= (256-34);
if (!good)
std::cout << "Az üzenet túl hosszú!\n";
return good;
}
};
int main()
{
boost::asio::io_service ios;
boost::asio::ip::tcp::resolver resolver(ios);
boost::asio::ip::tcp::resolver::query query("infoc.eet.bme.hu", "8888");
auto epit = resolver.resolve(query);
chat::client_network_manager<receive_policy_stdout, send_policy_stdout> cnm(ios, epit, "BATMAN");
std::thread t([&ios]{ ios.run(); });
std::string input;
while (true)
{
std::getline(std::cin, input);
if (input == "exit")
break;
chat::chat_message msg(chat::message::MESSAGE, input);
cnm.send(msg);
}
cnm.close_connection();
t.join();
}