Esempio n. 1
0
void connectSocket(boost::asio::io_service& io_service, tcp::socket& socket, const std::string& url, const std::string &port) {
	boost::system::error_code error = boost::asio::error::host_not_found;

	// connect
	if (isIpAddress(url.c_str())) {
		boost::asio::ip::address addr = boost::asio::ip::address::from_string(url);
		boost::asio::ip::tcp::endpoint endpoint = boost::asio::ip::tcp::endpoint(addr, lexical_cast<int>(port));
		socket.close();
		socket.connect(endpoint);
	}
	else {
		tcp::resolver::query query(url, "http");
		tcp::resolver resolver(io_service);
		tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
		tcp::resolver::iterator end;

		while (error && endpoint_iterator != end)
		{
			socket.close();
			socket.connect(*endpoint_iterator++, error);
		}
		if (error)
			throw boost::system::system_error(error);
	}
}
		void EthernetRelayDriver::configure(std::string host, int port){

			tcp::endpoint endpoint(boost::asio::ip::address::from_string(host.c_str()), port);
			socket.connect(endpoint);

			if(socket.is_open()){
				ROS_INFO("TCP/IP socket opened.");
			}
		}
boost::shared_ptr<torrent_info> setup_peer(tcp::socket& s, sha1_hash& ih
	, boost::shared_ptr<lt::session>& ses, torrent_handle* th = NULL)
{
	boost::shared_ptr<torrent_info> t = ::create_torrent();
	ih = t->info_hash();
	settings_pack sett;
	sett.set_str(settings_pack::listen_interfaces, "0.0.0.0:48900");
	sett.set_int(settings_pack::alert_mask, alert::all_categories);
	sett.set_bool(settings_pack::enable_upnp, false);
	sett.set_bool(settings_pack::enable_natpmp, false);
	sett.set_bool(settings_pack::enable_lsd, false);
	sett.set_bool(settings_pack::enable_dht, false);
	ses.reset(new lt::session(sett, lt::session::add_default_plugins));

	error_code ec;
	add_torrent_params p;
	p.flags &= ~add_torrent_params::flag_paused;
	p.flags &= ~add_torrent_params::flag_auto_managed;
	p.ti = t;
	p.save_path = "./tmp1_fast";

	remove("./tmp1_fast/temporary", ec);
	if (ec) log("remove(): %s", ec.message().c_str());
	ec.clear();
	torrent_handle ret = ses->add_torrent(p, ec);
	if (th) *th = ret;

	// wait for the torrent to be ready
	wait_for_downloading(*ses, "ses");

	int const port = ses->listen_port();
	fprintf(stderr, "listen port: %d\n", port);
	s.connect(tcp::endpoint(address::from_string("127.0.0.1", ec), port), ec);
	if (ec) TEST_ERROR(ec.message());

	print_session_log(*ses);

	return t;
}
	NetworkDevice(Stats &stats, const char *address)
	: Device(stats), socket(io_service)
	{
		stringstream portstr;
		portstr << SERVER_PORT;

		tcp::resolver resolver(io_service);
		tcp::resolver::query query(address, portstr.str());
		tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
		tcp::resolver::iterator end;

		boost::system::error_code error = boost::asio::error::host_not_found;
		while(error && endpoint_iterator != end)
		{
			socket.close();
			socket.connect(*endpoint_iterator++, error);
		}

		if(error)
			throw boost::system::system_error(error);

		mem_counter = 0;
	}
boost::shared_ptr<torrent_info> setup_peer(tcp::socket& s, sha1_hash& ih
	, boost::shared_ptr<lt::session>& ses, bool incoming = true
	, int flags = 0, torrent_handle* th = nullptr)
{
	boost::shared_ptr<torrent_info> t = ::create_torrent();
	ih = t->info_hash();
	settings_pack sett;
	sett.set_str(settings_pack::listen_interfaces, "0.0.0.0:48900");
	sett.set_int(settings_pack::alert_mask, alert::all_categories);
	sett.set_bool(settings_pack::enable_upnp, false);
	sett.set_bool(settings_pack::enable_natpmp, false);
	sett.set_bool(settings_pack::enable_lsd, false);
	sett.set_bool(settings_pack::enable_dht, false);
	sett.set_int(settings_pack::in_enc_policy, settings_pack::pe_disabled);
	sett.set_int(settings_pack::out_enc_policy, settings_pack::pe_disabled);
	sett.set_bool(settings_pack::enable_outgoing_utp, false);
	sett.set_bool(settings_pack::enable_incoming_utp, false);
	ses.reset(new lt::session(sett, lt::session::add_default_plugins));

	error_code ec;
	add_torrent_params p;
	p.flags &= ~add_torrent_params::flag_paused;
	p.flags &= ~add_torrent_params::flag_auto_managed;
	p.flags |= flags;
	p.ti = t;
	p.save_path = "./tmp1_fast";

	remove("./tmp1_fast/temporary", ec);
	if (ec) log("remove(): %s", ec.message().c_str());
	ec.clear();
	torrent_handle ret = ses->add_torrent(p, ec);
	if (th) *th = ret;

	// wait for the torrent to be ready
	if ((flags & add_torrent_params::flag_seed_mode) == 0)
	{
		wait_for_downloading(*ses, "ses");
	}

	if (incoming)
	{
		s.connect(tcp::endpoint(address::from_string("127.0.0.1", ec), ses->listen_port()), ec);
		if (ec) TEST_ERROR(ec.message());
	}
	else
	{
		tcp::acceptor l(s.get_io_service());
		l.open(tcp::v4());
		l.bind(tcp::endpoint(address_v4::from_string("127.0.0.1"), 0));
		l.listen();
		tcp::endpoint addr = l.local_endpoint();

		ret.connect_peer(addr);
		print_session_log(*ses);
		l.accept(s);
	}

	print_session_log(*ses);

	return t;
}