Пинги пошли!

master
const an teen 2021-05-10 17:41:01 +03:00
parent ccfcaacf6a
commit 1647b969f2
2 changed files with 90 additions and 27 deletions

View File

@ -1,33 +1,87 @@
#include "tcpsyncclient.h"
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);
bool connectStatus = connect_to_server();
if(!connectStatus) {
log("Connection error");
}
}
bool TcpSyncClient::connect_to_server()
{
m_sock.connect(m_ep);
return true;
}
template <typename T>
void TcpSyncClient::log(T message)
{
std::cout << "[TSC] " << message << std::endl;
}
size_t TcpSyncClient::read_complete(char * buf, const error_code & err, size_t bytes) {
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);
connect_to_ep(); //FIXME return code ?
connect_to_server();
}
bool TcpSyncClient::write(std::string msg)
{
try {
m_sock.write_some(boost::asio::buffer(msg + '\n'));
} catch (boost::system::system_error & err) {
log("Write("+msg+") error");
std::cerr << err.what() <<std::endl;
return false;
}
return true;
}
bool TcpSyncClient::connect_to_ep()
{
try {
m_sock.connect(m_ep);
} catch (boost::system::system_error & err) {
log("Connect_to_socket error");
std::cerr << err.what() <<std::endl;
return false;
}
return true;
}
size_t TcpSyncClient::read_complete(const error_code & err, size_t bytes) {
if ( err) return 0;
bool found = std::find(buf, buf + bytes, '\n') < buf + bytes;
m_already_read = bytes;
bool found = std::find(m_buff, m_buff + bytes, '\n') < m_buff + bytes;
return found ? 0 : 1;
}
void TcpSyncClient::read_answer()
{
m_already_read = 0;
read(m_sock, boost::asio::buffer(m_buff),
boost::bind(&TcpSyncClient::read_complete, this, _1, _2));
process_msg();
}
void TcpSyncClient::answer_to_ping(std::string ping)
{
write("PONG :" + ping);
}
bool TcpSyncClient::connect_to_server()
{
write("USER " + USER + " . . :" + REALNAME);
write("NICK " + NICK);
read_answer();
return true;
}
void TcpSyncClient::loop()
{
}
void TcpSyncClient::process_msg()
{
std::string msg(m_buff, m_already_read);
log("Readed: " + msg);
if (msg.find("PING :") == 0) answer_to_ping(msg.substr(6));
}

View File

@ -2,33 +2,42 @@
#define TCPSYNCCLIENT_H
#include <iostream>
#include <boost/bind/bind.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
using namespace boost::placeholders;
const static std::string USER = "acetonebot";
const static std::string NICK = "bot";
const static std::string REALNAME = "IRC bot in C++";
using boost::system::error_code;
class TcpSyncClient
{
public:
TcpSyncClient(boost::asio::ip::tcp::endpoint, boost::asio::io_service&, const std::string);
bool write(std::string);
private:
template <typename T>
void log(T);
bool connect_to_server();
size_t read_complete(char*, const error_code&, size_t);
bool connect_to_ep();
size_t read_complete(const error_code&, size_t);
void read_answer();
void process_msg();
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;
std::string m_channel;
void loop();
void answer_to_ping(std::string);
bool connect_to_server();
};
#endif // TCPSYNCCLIENT_H