From ccfcaacf6a0487fd0b853e92f3a5332bc36a0616 Mon Sep 17 00:00:00 2001 From: acetone Date: Mon, 10 May 2021 11:40:42 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B?= =?UTF-8?q?=D1=85=20=D0=B7=D0=BD=D0=B0=D1=87=D0=B5=D0=BD=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 38 ++++++++++++++++++++++++++++++++------ tcpsyncclient.cpp | 16 +++++++++++++--- tcpsyncclient.h | 7 ++++++- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/main.cpp b/main.cpp index a61c60d..f1e9f62 100644 --- a/main.cpp +++ b/main.cpp @@ -1,20 +1,46 @@ #include #include "tcpsyncclient.h" -void usage(std::string argv) +boost::asio::io_service service; + +void usage(std::string path) { - std::cout << argv << "
" << std::endl; + std::cout << "Usage:\n" << path << "
<#channel>" << std::endl; } int main(int argc, char * argv[]) { - if (argc < 2) usage( std::string(argv[0])); +////// Проверка переданных данных + if (argc < 4) { + usage( std::string(argv[0])); + return -1; + } - boost::asio::io_service service; + std::string address(argv[1]); + std::string port(argv[2]); + std::string channel(argv[3]); + + if (argv[3][0] != '#') { + std::cerr << "Incorrect channel name. Maybe '#" << channel << "'?" << std::endl; + return -2; + } + + try { // Проверка переданных адрес:порт boost::asio::ip::tcp::endpoint ep( boost::asio::ip::address::from_string( - std::string(argv[1])), std::stoi(std::string(argv[2])) ); + address), std::stoi(port) ); + + } catch (boost::system::system_error & err) { + std::cerr << err.what() << ": " << address << " / " + << port << std::endl; + return -3; + } + +////// Начало работы + boost::asio::ip::tcp::endpoint ep( + boost::asio::ip::address::from_string( + address), std::stoi(port) ); + TcpSyncClient socket(ep, service, channel); - TcpSyncClient(ep, service); return 0; } diff --git a/tcpsyncclient.cpp b/tcpsyncclient.cpp index d24cdb9..3cdbd31 100644 --- a/tcpsyncclient.cpp +++ b/tcpsyncclient.cpp @@ -1,13 +1,23 @@ #include "tcpsyncclient.h" -TcpSyncClient::TcpSyncClient(boost::asio::ip::tcp::endpoint ep, boost::asio::io_service& service) : - m_ep(ep), m_sock(service) +TcpSyncClient::TcpSyncClient(boost::asio::ip::tcp::endpoint ep, boost::asio::io_service& service, + const std::string channel) : m_started(true), m_ep(ep), m_sock(service), + m_channel(channel) { log(ep.address().to_string()); log(ep.port()); + log(m_channel); - // m_sock.connect(ep); + bool connectStatus = connect_to_server(); + if(!connectStatus) { + log("Connection error"); + } +} +bool TcpSyncClient::connect_to_server() +{ + m_sock.connect(m_ep); + return true; } template diff --git a/tcpsyncclient.h b/tcpsyncclient.h index 563b00f..23780e2 100644 --- a/tcpsyncclient.h +++ b/tcpsyncclient.h @@ -15,13 +15,18 @@ using boost::system::error_code; class TcpSyncClient { public: - TcpSyncClient(boost::asio::ip::tcp::endpoint, boost::asio::io_service&); + TcpSyncClient(boost::asio::ip::tcp::endpoint, boost::asio::io_service&, const std::string); private: template void log(T); + bool connect_to_server(); size_t read_complete(char*, const error_code&, size_t); + int m_already_read; + bool m_started; + char m_buff[1024]; // Буффер 1Кб + std::string m_channel; boost::asio::ip::tcp::endpoint m_ep; boost::asio::ip::tcp::socket m_sock; };