コード例 #1
0
ファイル: peer.cpp プロジェクト: AshKarath/bumo
	utils::InetAddress Peer::GetRemoteAddress() const {
		utils::InetAddress address = GetPeerAddress();
		if (InBound()) {
			address.SetPort((uint16_t)peer_listen_port_);
		}
		return address;
	}
コード例 #2
0
ファイル: peer.cpp プロジェクト: AshKarath/bumo
	bool Peer::OnNetworkTimer(int64_t current_time) {
		if (!IsActive() && current_time - connect_start_time_ > 10 * utils::MICRO_UNITS_PER_SEC) {
			LOG_ERROR("Peer(%s) active timeout", GetPeerAddress().ToIpPort().c_str());
			return false;
		} 

		return true;
	}
コード例 #3
0
ファイル: connection.hpp プロジェクト: ShauryaRawat/thrill
    //! make ostreamable
    std::ostream & OutputOstream(std::ostream& os) const final {
        os << "[tcp::Connection"
           << " fd=" << GetSocket().fd();

        if (IsValid())
            os << " peer=" << GetPeerAddress();

        return os << "]";
    }
コード例 #4
0
//
/// This version of GetPeerAddress() works on our own socket descriptor.
//
int TSocket::GetPeerAddress(TSocketAddress& socketAddress, int& addressLength)
{
  return GetPeerAddress(socketAddress, addressLength, Handle);
}
コード例 #5
0
	Bool TcpSocketWin32::Connect(const Address & p_Address, const Uint16 p_Port, const Time & p_Timeout, const Uint16 p_EndpointPort)
	{
		// Create the socket
		if ((m_Handle = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
		{
			bitLogNetErr(  "Can not create the socket. Error: " << static_cast<Int32>(GetLastError()) );
			return false;
		}

		// Bind the socket to a port
		sockaddr_in service;

		if (p_EndpointPort != 0)
		{
			service.sin_family = AF_INET;
			service.sin_addr.s_addr = htonl(INADDR_ANY);
			service.sin_port = htons(static_cast<u_short>(p_EndpointPort));

			const int optVal = 1;
			const int optLen = sizeof(optVal);
			int rtn = setsockopt(m_Handle, SOL_SOCKET, SO_REUSEADDR, (const char*)&optVal, optLen);
			if( rtn != 0 )
			{
				bitLogNetErr(  "Can not set reusable socket. Error: " << static_cast<Int32>(GetLastError()) );
				return false;
			}

			// Bind
			if (bind(m_Handle, reinterpret_cast<const sockaddr *>(&service), sizeof(service)) == SOCKET_ERROR)
			{
				bitLogNetErr(  "Can not bind the socket. Error: " << static_cast<Int32>(GetLastError()) );
				return false;
			}
		}
		
		// Create an object that's holding the host data
		service.sin_family = AF_INET;
		service.sin_addr.s_addr = htonl( static_cast<u_long>( p_Address.GetAddress( ) ) );
		service.sin_port = htons( static_cast<u_short>( p_Port ) );

		// We are using timeout
		// Get the blocking status and disable it.
		Bool blocking = GetBlocking( );
		SetBlocking( false );

		// Connect
		if( connect( m_Handle, ( const sockaddr * )&service, sizeof (sockaddr_in ) ) != 0 )
		{
		   // Ignore the WSAEWOULDBLOCK error
			DWORD lastError = GetLastError( );
			if( lastError != WSAEWOULDBLOCK )
			{
				Disconnect( );
				return false;
			}
		}

		// We failed to connect, but we are waiting for the connection to establish
		struct timeval tv;
		if( p_Timeout.AsMicroseconds( ) / 1000000ULL > g_MaxTimeout )
		{
			tv.tv_sec	= static_cast<long>( g_MaxTimeout );
			tv.tv_usec	= static_cast<long>( 0 );
		}
		else
		{
			tv.tv_sec	= static_cast<long>( p_Timeout.AsMicroseconds( ) / 1000000ULL );
			tv.tv_usec
			= static_cast<long>( p_Timeout.AsMicroseconds( ) % 1000000ULL );
		}

		// Create a FD_SET, and add the m_Handle to the set
		fd_set fdset;
		FD_ZERO(&fdset);
		FD_SET(m_Handle, &fdset);

		// Select from the set
		if (select(static_cast<int>(m_Handle)+1, NULL, &fdset, NULL, &tv) > 0)
		{
			// Check if the address is valid.
			Address address = GetPeerAddress( );
			if( address == p_Address )
			{
				// The address is not 0, we successfully connected.
				SetBlocking( blocking );
				return true;
			}
		}

		DWORD lastError = GetLastError();

		// Failed to connect. Close the socket.
		Disconnect( );

		// Failed.
		return false;
	}