24,860
社区成员




#ifndef _NET_CONNECTOR_H
#define _NET_CONNECTOR_H
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <memory>
#include <string>
#include <boost/date_time.hpp>
/**
* Net_connector is a class implementing asynchronous connection to remote
* location.
*/
class Net_connector {
public:
/**
* Construct Net_connector object with io_service object.
*
* @param[in] io_service boost::asio::io_service object for deadline_time
*/
Net_connector(boost::asio::io_service& io_service,
boost::asio::ip::tcp::socket& socket);
/**
* connect to specified remote location within a period time.
*
* @param[in] ip the ip address to connect
* @param[in] port the port on remote location to connect
* @param[in] seconds a period time in second
*
* @return true if successfully made connection to remote location
*/
bool connect(const std::string& ip, const unsigned short& port,
const boost::posix_time::seconds& seconds);
/** return true if the connection is established, or false. */
operator bool() const;
private:
/** asynchronous connection handler. */
void handle_connect(const boost::system::error_code& error);
/** asynchronous wait handler. */
void handle_timeout(const boost::system::error_code& error);
/** reference socket for asynchronous connection. */
boost::asio::ip::tcp::socket& _socket_ref;
/** deadline_timer object for implementing asynchronous connection. */
boost::asio::deadline_timer _dealine_timer;
/** time point to computing time period for specified operation. */
//std::chrono::system_clock::time_point _last_time_point;
/** flag indicates whether or not connection is made with a period time. */
bool _connected;
/** mechanism for implementing asynchronous connection. */
boost::mutex _mutex;
boost::condition_variable _cond_var;
};
#endif // _NET_CONNECTOR_
#include "Net_connector.h"
#include <boost/lexical_cast.hpp>
#include <boost/bind.hpp>
Net_connector::Net_connector(boost::asio::io_service& io_service,
boost::asio::ip::tcp::socket& socket)
: _socket_ref(socket)
, _dealine_timer(io_service)
, _connected(false)
{
}
bool Net_connector::connect(const std::string& ip, const unsigned short& port,
const boost::posix_time::seconds& seconds)
{
boost::mutex::scoped_lock lock(_mutex);
boost::asio::ip::tcp::endpoint endpoint(
boost::asio::ip::address::from_string(ip), port);
_connected = false;
_dealine_timer.expires_from_now(seconds);
_dealine_timer.async_wait(boost::bind(&Net_connector::handle_timeout,
this, boost::asio::placeholders::error));
_socket_ref.async_connect(endpoint,
boost::bind(&Net_connector::handle_connect, this,
boost::asio::placeholders::error));
_cond_var.wait(lock);
return _connected;
}
void Net_connector::handle_connect(const boost::system::error_code& error)
{
if (boost::asio::error::operation_aborted == error) {
return;
}
if (!error) {
_connected = true;
}
_dealine_timer.cancel();
}
void Net_connector::handle_timeout(const boost::system::error_code& error)
{
boost::mutex::scoped_lock lock(_mutex);
// timeout, cancel asynchronous connect operation
if (boost::asio::error::operation_aborted != error) {
boost::system::error_code ec;
_socket_ref.close(ec);
}
_cond_var.notify_one();
}
Net_connector::operator bool() const
{
return _connected;
}
#include "Net_connector.h"
#include <boost/thread.hpp>
void run_service(boost::asio::io_service& io_service)
{
io_service.run();
}
int main(int, char**)
{
boost::asio::io_service io_service;
boost::asio::io_service::work work(io_service);
boost::thread t(run_service, boost::ref(io_service));
while (true) {
boost::asio::ip::tcp::socket socket(io_service);
boost::system::error_code ec;
Net_connector connector(io_service, socket);
if (!connector.connect("61.135.169.125", 80, boost::posix_time::seconds(5))) {
std::cout << "failed to connect server" << std::endl;
}
socket.shutdown(boost::asio::ip::tcp::socket::shutdown_send, ec);
socket.close(ec);
}
return 0;
}