示例#1
0
文件: simproto.cpp 项目: jimzshi/fw
 SimSession(tcp::socket socket, tcp::endpoint peer) :
         start_tp_(hr_clock_t::now()), socket_(std::move(socket)), peer_(std::move(peer)), req_len_(0), res_len_(0) {
     zks::HashCode32 h;
     asio::ip::address peer_addr = peer_.address();
     h += peer_addr.to_string().c_str();
     h += peer_.port();
     session_id_ = zks::to_u8string(h);
     ZKS_INFO(g_logger, session_id_.c_str(), "new session: [%s] from %s:%d started",
             session_id_.c_str(), peer_addr.to_string().c_str(), peer_.port());
 }
bool OlsrEventClient::connect(tcp::endpoint& endpoint )
{
    // Synchronous connection to server
    tcp::socket& sock = m_tcpConnection->socket();
    boost::system::error_code error;
    sock.connect ( endpoint,error );
    if (!error)
    {
        DBG_DEV(1,"Connection with OLSR event server established");
        
        m_tcpConnection->async_read ( m_messageRead,
                                      boost::bind ( &OlsrEventClient::handleRead, this,
                                                    boost::asio::placeholders::error )
                                    );
        return true;
    }
    else
    {
        MSG_FAILED("Could not connect to OLSR event server %s on port %d",
          endpoint.address().to_string().c_str(),
          endpoint.port()
        ); 
    
//       std::cerr << "Could not connect to OLSR event server " << 
//                 << " on port " << endpoint.port() << std::endl;

      if (error==boost::asio::error::connection_refused)
      {
        MSG_FAILED("Please verfiy that OLSR server is running and sereadmo plugin is loaded");
        return false;
      }
      else
        throw boost::system::system_error(error);
    }
}
示例#3
0
int main(int argc, char **argv)
{
    if (argc > 1) 
        if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
            printf("\n    Usage: %s [ThreadCount] [QueryDataLength]\n", argv[0]);
            printf("\n    Default: %s %d 4\n", argv[0], thread_count);
            printf("\n    For example:\n         %s 2 32\n", argv[0]);
            printf("\n    That's means: start server with 2 threads, and per data-package is 32 bytes.\n\n");
            exit(1);
        }

    if (argc > 1)
        thread_count = atoi(argv[1]);
    if (argc > 2)
        qdata = atoi(argv[2]);

    rlimit of = {65536, 65536};
    if (-1 == setrlimit(RLIMIT_NOFILE, &of)) {
        perror("setrlimit");
        exit(1);
    }

    printf("startup server, thread:%d, qdata:%d, listen %s:%d\n", thread_count,
            qdata, addr.address().to_string().c_str(), addr.port());

    echo_server();

    boost::thread_group tg;
    for (int i = 0; i < thread_count; ++i)
        tg.create_thread([]{ ios.run(); });
    tg.join_all();
    return 0;
}
		void set_peer(tcp::endpoint const& ep)
		{
#if TORRENT_USE_IPV6
			is_v6_addr = ep.address().is_v6();
			if (is_v6_addr)
				addr.v6 = ep.address().to_v6().to_bytes();
			else
#endif
				addr.v4 = ep.address().to_v4().to_bytes();
			port = ep.port();
		}
	ipv6_peer::ipv6_peer(
		tcp::endpoint const& ep, bool c, int src
	)
		: torrent_peer(ep.port(), c, src)
		, addr(ep.address().to_v6().to_bytes())
	{
		is_v6_addr = true;
#if TORRENT_USE_I2P
		is_i2p_addr = false;
#endif
	}
示例#6
0
	bool was_introduced_by(peer_plugin const* pp, tcp::endpoint const& ep)
	{
		ut_pex_peer_plugin const* p = static_cast<ut_pex_peer_plugin const*>(pp);
#if TORRENT_USE_IPV6
		if (ep.address().is_v4())
		{
#endif
			ut_pex_peer_plugin::peers4_t::value_type v(ep.address().to_v4().to_bytes(), ep.port());
			ut_pex_peer_plugin::peers4_t::const_iterator i
				= std::lower_bound(p->m_peers.begin(), p->m_peers.end(), v);
			return i != p->m_peers.end() && *i == v;
#if TORRENT_USE_IPV6
		}
		else
		{
			ut_pex_peer_plugin::peers6_t::value_type v(ep.address().to_v6().to_bytes(), ep.port());
			ut_pex_peer_plugin::peers6_t::const_iterator i
				= std::lower_bound(p->m_peers6.begin(), p->m_peers6.end(), v);
			return i != p->m_peers6.end() && *i == v;
		}
#endif
	}
	ipv4_peer::ipv4_peer(
		tcp::endpoint const& ep, bool c, int src
	)
		: torrent_peer(ep.port(), c, src)
		, addr(ep.address().to_v4())
	{
#if TORRENT_USE_IPV6
		is_v6_addr = false;
#endif
#if TORRENT_USE_I2P
		is_i2p_addr = false;
#endif
	}
示例#8
0
	inline std::string print_endpoint(tcp::endpoint const& ep)
	{
		error_code ec;
		std::string ret;
		address const& addr = ep.address();
#if TORRENT_USE_IPV6
		if (addr.is_v6())
		{
			ret += '[';
			ret += addr.to_string(ec);
			ret += ']';
			ret += ':';
			ret += to_string(ep.port()).elems;
		}
		else
#endif
		{
			ret += addr.to_string(ec);
			ret += ':';
			ret += to_string(ep.port()).elems;
		}
		return ret;
	}
示例#9
0
文件: tcp.cpp 项目: FOXTTER/mavros
static bool resolve_address_tcp(io_service &io, size_t chan, std::string host, unsigned short port, tcp::endpoint &ep)
{
	bool result = false;
	tcp::resolver resolver(io);
	error_code ec;

	tcp::resolver::query query(host, "");
	std::for_each(resolver.resolve(query, ec), tcp::resolver::iterator(),
			[&](const tcp::endpoint & q_ep) {
				ep = q_ep;
				ep.port(port);
				result = true;
				logDebug(PFXd "host %s resolved as %s", chan, host.c_str(), to_string_ss(ep).c_str());
			});

	if (ec) {
		logWarn(PFXd "resolve error: %s", chan, ec.message().c_str());
		result = false;
	}

	return result;
}
    void run(unsigned sleepMillis)
    {
		std::cout << "in GUIMessageSenderThread: run "
                  << sleepMillis << "ms"
                  << std::endl;

		while (running_) {
			if (!connected_) {
				std::cout << "In GUIMessageSenderThread trying to connect to: " << host_ << " " << port_ << std::endl;
				//tcp::endpoint endpoint_(boost::asio::ip::address::from_string(host_), port_);
				//tcp::endpoint endpoint_(boost::asio::ip::address::from_string(host_), port_);
				endpoint_.address(boost::asio::ip::address::from_string(host_));
				endpoint_.port(port_);
				tcp::acceptor acceptor(*io_service_, endpoint_);

				getClientConnection(acceptor);
				try {
				
					sendMessages(sleepMillis);

					acceptor.close();
					stream_.clear();
					stream_.close();
				}
				catch (std::exception& e)
				{
					std::cerr << "GUIMessageSenderThread Error in connection: " << e.what() << std::endl;
				}
				connected_ = false;
				//boost::asio::deadline_timer timer(*io_service_,boost::posix_time::milliseconds(1000));
				//timer.expires_from_now(boost::posix_time::milliseconds(1000));
				//timer.wait();
			}
		}
		std::cout << "done GUIMessageSenderThread: run "
                  << sleepMillis << "ms"
                  << std::endl;
	}
示例#11
0
tuple endpoint_to_tuple(tcp::endpoint const& ep)
{
    return boost::python::make_tuple(ep.address().to_string(), ep.port());
}
	// 1. if the IP addresses are identical, hash the ports in 16 bit network-order
	//    binary representation, ordered lowest first.
	// 2. if the IPs are in the same /24, hash the IPs ordered, lowest first.
	// 3. if the IPs are in the ame /16, mask the IPs by 0xffffff55, hash them
	//    ordered, lowest first.
	// 4. if IPs are not in the same /16, mask the IPs by 0xffff5555, hash them
	//    ordered, lowest first.
	//
	// * for IPv6 peers, just use the first 64 bits and widen the masks.
	//   like this: 0xffff5555 -> 0xffffffff55555555
	//   the lower 64 bits are always unmasked
	//
	// * for IPv6 addresses, compare /32 and /48 instead of /16 and /24
	// 
	// * the two IP addresses that are used to calculate the rank must
	//   always be of the same address family
	//
	// * all IP addresses are in network byte order when hashed
	boost::uint32_t peer_priority(tcp::endpoint e1, tcp::endpoint e2)
	{
		TORRENT_ASSERT(e1.address().is_v4() == e2.address().is_v4());

		using std::swap;

		boost::uint32_t ret;
		if (e1.address() == e2.address())
		{
			if (e1.port() > e2.port())
				swap(e1, e2);
			boost::uint32_t p;
			reinterpret_cast<boost::uint16_t*>(&p)[0] = htons(e1.port());
			reinterpret_cast<boost::uint16_t*>(&p)[1] = htons(e2.port());
			ret = crc32c_32(p);
		}
#if TORRENT_USE_IPV6
		else if (e1.address().is_v6())
		{
			const static boost::uint8_t v6mask[][8] = {
				{ 0xff, 0xff, 0xff, 0xff, 0x55, 0x55, 0x55, 0x55 },
				{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x55, 0x55 },
				{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }
			};

			if (e1 > e2) swap(e1, e2);
			address_v6::bytes_type b1 = e1.address().to_v6().to_bytes();
			address_v6::bytes_type b2 = e2.address().to_v6().to_bytes();
			int mask = memcmp(&b1[0], &b2[0], 4) ? 0
				: memcmp(&b1[0], &b2[0], 6) ? 1 : 2;
			apply_mask(&b1[0], v6mask[mask], 8);
			apply_mask(&b2[0], v6mask[mask], 8);
			boost::uint64_t addrbuf[4];
			memcpy(&addrbuf[0], &b1[0], 16);
			memcpy(&addrbuf[2], &b2[0], 16);
			ret = crc32c(addrbuf, 4);
		}
#endif
		else
		{
			const static boost::uint8_t v4mask[][4] = {
				{ 0xff, 0xff, 0x55, 0x55 },
				{ 0xff, 0xff, 0xff, 0x55 },
				{ 0xff, 0xff, 0xff, 0xff }
			};

			if (e1 > e2) swap(e1, e2);
			address_v4::bytes_type b1 = e1.address().to_v4().to_bytes();
			address_v4::bytes_type b2 = e2.address().to_v4().to_bytes();
			int mask = memcmp(&b1[0], &b2[0], 2) ? 0
				: memcmp(&b1[0], &b2[0], 3) ? 1 : 2;
			apply_mask(&b1[0], v4mask[mask], 4);
			apply_mask(&b2[0], v4mask[mask], 4);
			boost::uint64_t addrbuf;
			memcpy(&addrbuf, &b1[0], 4);
			memcpy(reinterpret_cast<char*>(&addrbuf) + 4, &b2[0], 4);
			ret = crc32c(&addrbuf, 1);
		}

		return ret;
	}
示例#13
0
	// 1. if the IP addresses are identical, hash the ports in 16 bit network-order
	//    binary representation, ordered lowest first.
	// 2. if the IPs are in the same /24, hash the IPs ordered, lowest first.
	// 3. if the IPs are in the ame /16, mask the IPs by 0xffffff55, hash them
	//    ordered, lowest first.
	// 4. if IPs are not in the same /16, mask the IPs by 0xffff5555, hash them
	//    ordered, lowest first.
	//
	// * for IPv6 peers, just use the first 64 bits and widen the masks.
	//   like this: 0xffff5555 -> 0xffffffff55555555
	//   the lower 64 bits are always unmasked
	//
	// * for IPv6 addresses, compare /32 and /48 instead of /16 and /24
	// 
	// * the two IP addresses that are used to calculate the rank must
	//   always be of the same address family
	//
	// * all IP addresses are in network byte order when hashed
	std::uint32_t peer_priority(tcp::endpoint e1, tcp::endpoint e2)
	{
		TORRENT_ASSERT(e1.address().is_v4() == e2.address().is_v4());

		using std::swap;

		std::uint32_t ret;
		if (e1.address() == e2.address())
		{
			if (e1.port() > e2.port())
				swap(e1, e2);
			std::uint32_t p;
#if defined BOOST_BIG_ENDIAN
			p = e1.port() << 16;
			p |= e2.port();
#elif defined BOOST_LITTLE_ENDIAN
			p = aux::host_to_network(e2.port()) << 16;
			p |= aux::host_to_network(e1.port());
#else
#error unsupported endianness
#endif
			ret = crc32c_32(p);
		}
#if TORRENT_USE_IPV6
		else if (e1.address().is_v6())
		{
			static const std::uint8_t v6mask[][8] = {
				{ 0xff, 0xff, 0xff, 0xff, 0x55, 0x55, 0x55, 0x55 },
				{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x55, 0x55 },
				{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }
			};

			if (e1 > e2) swap(e1, e2);
			address_v6::bytes_type b1 = e1.address().to_v6().to_bytes();
			address_v6::bytes_type b2 = e2.address().to_v6().to_bytes();
			int mask = memcmp(&b1[0], &b2[0], 4) ? 0
				: memcmp(&b1[0], &b2[0], 6) ? 1 : 2;
			apply_mask(&b1[0], v6mask[mask], 8);
			apply_mask(&b2[0], v6mask[mask], 8);
			std::uint64_t addrbuf[4];
			memcpy(&addrbuf[0], &b1[0], 16);
			memcpy(&addrbuf[2], &b2[0], 16);
			ret = crc32c(addrbuf, 4);
		}
#endif
		else
		{
			static const std::uint8_t v4mask[][4] = {
				{ 0xff, 0xff, 0x55, 0x55 },
				{ 0xff, 0xff, 0xff, 0x55 },
				{ 0xff, 0xff, 0xff, 0xff }
			};

			if (e1 > e2) swap(e1, e2);
			address_v4::bytes_type b1 = e1.address().to_v4().to_bytes();
			address_v4::bytes_type b2 = e2.address().to_v4().to_bytes();
			int mask = memcmp(&b1[0], &b2[0], 2) ? 0
				: memcmp(&b1[0], &b2[0], 3) ? 1 : 2;
			apply_mask(&b1[0], v4mask[mask], 4);
			apply_mask(&b2[0], v4mask[mask], 4);
			std::uint64_t addrbuf;
			memcpy(&addrbuf, &b1[0], 4);
			memcpy(reinterpret_cast<char*>(&addrbuf) + 4, &b2[0], 4);
			ret = crc32c(&addrbuf, 1);
		}

		return ret;
	}