added a little good old fashioned command-line chat action

This commit is contained in:
Kjistóf 2016-12-04 14:00:39 +01:00
parent d0a995d6da
commit eed5fc25d9
3 changed files with 53 additions and 25 deletions

View File

@ -7,6 +7,6 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic")
find_package(Boost COMPONENTS system REQUIRED) find_package(Boost COMPONENTS system REQUIRED)
include_directories(${Boost_INCLUDE_DIR}) 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}) add_executable(cpp11NHF2_chat ${SOURCE_FILES})
target_link_libraries(cpp11NHF2_chat ${Boost_LIBRARIES}) target_link_libraries(cpp11NHF2_chat ${Boost_LIBRARIES})

View File

@ -16,30 +16,7 @@ namespace chat
using boost::asio::ip::tcp; using boost::asio::ip::tcp;
struct receive_policy_stdout template <class receive_policy>
{
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 <class receive_policy = receive_policy_stdout>
class client_network_manager class client_network_manager
{ {
/* compile-time check for whether receive_policy is valid or not */ /* compile-time check for whether receive_policy is valid or not */

51
commandline_chat.cpp Normal file
View File

@ -0,0 +1,51 @@
#include <iostream>
#include <thread>
#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<receive_policy_stdout> 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();
}