mirror of https://notabug.org/acetone/ircabot.git
Проверка переданных значений
parent
9359e8824e
commit
ccfcaacf6a
38
main.cpp
38
main.cpp
|
@ -1,20 +1,46 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "tcpsyncclient.h"
|
#include "tcpsyncclient.h"
|
||||||
|
|
||||||
void usage(std::string argv)
|
boost::asio::io_service service;
|
||||||
|
|
||||||
|
void usage(std::string path)
|
||||||
{
|
{
|
||||||
std::cout << argv << " <address> <port>" << std::endl;
|
std::cout << "Usage:\n" << path << " <address> <port> <#channel>" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char * argv[])
|
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::tcp::endpoint ep(
|
||||||
boost::asio::ip::address::from_string(
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,23 @@
|
||||||
#include "tcpsyncclient.h"
|
#include "tcpsyncclient.h"
|
||||||
|
|
||||||
TcpSyncClient::TcpSyncClient(boost::asio::ip::tcp::endpoint ep, boost::asio::io_service& service) :
|
TcpSyncClient::TcpSyncClient(boost::asio::ip::tcp::endpoint ep, boost::asio::io_service& service,
|
||||||
m_ep(ep), m_sock(service)
|
const std::string channel) : m_started(true), m_ep(ep), m_sock(service),
|
||||||
|
m_channel(channel)
|
||||||
{
|
{
|
||||||
log(ep.address().to_string());
|
log(ep.address().to_string());
|
||||||
log(ep.port());
|
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 <typename T>
|
template <typename T>
|
||||||
|
|
|
@ -15,13 +15,18 @@ using boost::system::error_code;
|
||||||
class TcpSyncClient
|
class TcpSyncClient
|
||||||
{
|
{
|
||||||
public:
|
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:
|
private:
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void log(T);
|
void log(T);
|
||||||
|
bool connect_to_server();
|
||||||
size_t read_complete(char*, const error_code&, size_t);
|
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::endpoint m_ep;
|
||||||
boost::asio::ip::tcp::socket m_sock;
|
boost::asio::ip::tcp::socket m_sock;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue