From eed5fc25d9e8083b717a78bdf9b166792db702be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjist=C3=B3f?= Date: Sun, 4 Dec 2016 14:00:39 +0100 Subject: [PATCH] added a little good old fashioned command-line chat action --- CMakeLists.txt | 2 +- chat_networking.hpp | 25 +--------------------- commandline_chat.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 25 deletions(-) create mode 100644 commandline_chat.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5390fdc..da0ed46 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,6 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic") find_package(Boost COMPONENTS system REQUIRED) include_directories(${Boost_INCLUDE_DIR}) -set(SOURCE_FILES teszt.cpp chat_messages.hpp chat_networking.hpp) +set(SOURCE_FILES commandline_chat.cpp chat_messages.hpp chat_networking.hpp) add_executable(cpp11NHF2_chat ${SOURCE_FILES}) target_link_libraries(cpp11NHF2_chat ${Boost_LIBRARIES}) \ No newline at end of file diff --git a/chat_networking.hpp b/chat_networking.hpp index 3499b41..790d568 100644 --- a/chat_networking.hpp +++ b/chat_networking.hpp @@ -16,30 +16,7 @@ namespace chat using boost::asio::ip::tcp; - struct receive_policy_stdout - { - static void message_do_what(chat_message msg) - { std::cout << msg.get_content() << '\n'; } - - static void handshake_do_what(chat_message msg) - { std::cout << "Handshake üzenetek:\n" << msg.get_content() << '\n'; } - - static void serverdirection_do_what(chat_message msg) - { std::cout << "Szerver üzenet: " << msg.get_content() << '\n'; } - - static void login_do_what(chat_message msg) - { std::cout << msg.get_content() << " belépett.\n"; } - - static void logout_do_what(chat_message msg) - { std::cout << msg.get_content() << " kilépett.\n"; } - }; - - - /* most ezt így üres template paraméterekkel kell példányosítani, - * már amennyiben nem akarunk valami custom receive policyt megadni, - * pl.: client_network_manager<> cnm; - * meg lehet oldani úgy, hogy ne kelljen? usingot/typedefet nem enged saját magára */ - template + template class client_network_manager { /* compile-time check for whether receive_policy is valid or not */ diff --git a/commandline_chat.cpp b/commandline_chat.cpp new file mode 100644 index 0000000..8a17ca3 --- /dev/null +++ b/commandline_chat.cpp @@ -0,0 +1,51 @@ +#include +#include +#include "chat_networking.hpp" + + + +struct receive_policy_stdout +{ + static void message_do_what(chat::chat_message msg) + { std::cout << msg.get_content() << '\n'; } + + static void handshake_do_what(chat::chat_message msg) + { std::cout << "Handshake üzenetek:\n" << msg.get_content() << '\n'; } + + static void serverdirection_do_what(chat::chat_message msg) + { std::cout << "Szerver üzenet: " << msg.get_content() << '\n'; } + + static void login_do_what(chat::chat_message msg) + { std::cout << msg.get_content() << " belépett.\n"; } + + static void logout_do_what(chat::chat_message msg) + { std::cout << msg.get_content() << " kilépett.\n"; } +}; + + +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 cnm(ios, epit, "BATMAN"); + std::thread t([&ios]{ ios.run(); }); + + + std::string input; + while (true) + { + std::cin >> input; + if (input == "exit") + break; + + chat::chat_message msg(chat::message::MESSAGE, input); + cnm.send(msg); + } + + cnm.close_connection(); + t.join(); +}