|
| 1 | +// Copyright (c) 2012 A. Linunix (linunixinception@gmail.com). |
| 2 | +// Distributed under the Boost Software License, Version 1.0. |
| 3 | +// (See accompanying file LICENSE_1_0.txt or copy at |
| 4 | +// http://www.boost.org/LICENSE_1_0.txt) |
| 5 | + |
| 6 | +#ifndef SYN_CNNECT_HPP |
| 7 | +#define SYN_CNNECT_HPP |
| 8 | + |
| 9 | +#include <boost/asio/ip/tcp.hpp> |
| 10 | +#include <boost/system/error_code.hpp> |
| 11 | +#include <boost/asio.hpp> |
| 12 | + |
| 13 | +#include <chrono> // later |
| 14 | +#include <functional> |
| 15 | +#include <utility> |
| 16 | +#include <iostream> |
| 17 | +#include <mutex> // later |
| 18 | + |
| 19 | +using namespace boost; |
| 20 | + |
| 21 | +namespace boost { namespace network { namespace TCP { |
| 22 | + |
| 23 | +class syn_cnnect |
| 24 | +{ |
| 25 | + |
| 26 | +public: |
| 27 | + |
| 28 | + syn_cnnect(asio::io_service & io) : sock(io) { } |
| 29 | + |
| 30 | +public: |
| 31 | + |
| 32 | + void connect(const std::string &host, unsigned short port, system::error_code & erc) |
| 33 | + { |
| 34 | + if(!erc) |
| 35 | + sock.connect(asio::ip::tcp::endpoint( |
| 36 | + asio::ip::address::from_string(host), port)); |
| 37 | + |
| 38 | + else |
| 39 | + std::cout << "The TCP Connexion was failed, try again" << std::endl; |
| 40 | + sock.shutdown(boost::asio::ip::tcp::socket::shutdown_both); |
| 41 | + } |
| 42 | + |
| 43 | + void connect_end(asio::ip::tcp::endpoint & end) |
| 44 | + { |
| 45 | + sock.connect(end); |
| 46 | + } |
| 47 | + |
| 48 | + void connect_v4(const std::string &host, unsigned short port, system::error_code & erc) |
| 49 | + { |
| 50 | + if(!erc) |
| 51 | + sock.connect(boost::asio::ip::tcp::endpoint( |
| 52 | + boost::asio::ip::address_v4::from_string(host), port)); |
| 53 | + |
| 54 | + boost::asio::detail::throw_error(erc); |
| 55 | + } |
| 56 | + |
| 57 | + void connect_v6(const std::string &host , unsigned short port, system::error_code & erc) |
| 58 | + { |
| 59 | + if(!erc) |
| 60 | + sock.connect(boost::asio::ip::tcp::endpoint( |
| 61 | + boost::asio::ip::address_v6::from_string(host), port)); |
| 62 | + |
| 63 | + else |
| 64 | + std::cout << "Failed to connected" << std::endl; |
| 65 | + sock.shutdown(boost::asio::ip::tcp::socket::shutdown_both); |
| 66 | + } |
| 67 | + |
| 68 | + void cancel () |
| 69 | + { |
| 70 | + sock.cancel(); |
| 71 | + } |
| 72 | + |
| 73 | + void close () |
| 74 | + { |
| 75 | + sock.close(); |
| 76 | + } |
| 77 | + |
| 78 | + ~syn_cnnect() |
| 79 | + { |
| 80 | + sock.shutdown(boost::asio::ip::tcp::socket::shutdown_both); |
| 81 | + sock.close(); |
| 82 | + } |
| 83 | + |
| 84 | +private: |
| 85 | + |
| 86 | + boost::asio::ip::tcp::socket sock; |
| 87 | + |
| 88 | +}; |
| 89 | + |
| 90 | +} } } |
| 91 | + |
| 92 | +#endif // SYN_CNNECT_HPP |
0 commit comments