Пример #1
0
void TCPClient::sendData(const char* data, std::size_t size, const net::Address& peerAddress) 
{
	TraceL << "Send data to " << peerAddress << endl;

	// Ensure permissions exist for the peer.
	if (!hasPermission(peerAddress.host()))	
		throw std::runtime_error("No permission exists for peer: " + peerAddress.host());	

	auto conn = connections().get(peerAddress, nullptr);
	if (!conn)	
		throw std::runtime_error("No peer exists for: " + peerAddress.toString());
	
	conn->send(data, size);
}
Пример #2
0
void UDPAllocation::onPeerDataReceived(void*, const MutableBuffer& buffer, const net::Address& peerAddress)
{	
	//auto source = reinterpret_cast<net::PacketInfo*>(packet.info);
	TraceL << "Received UDP Datagram from " << peerAddress << endl;	
	
	if (!hasPermission(peerAddress.host())) {
		TraceL << "No Permission: " << peerAddress.host() << endl;	
		return;
	}

	updateUsage(buffer.size());
	
	// Check that we have not exceeded out lifetime and bandwidth quota.
	if (IAllocation::deleted())
		return;
	
	stun::Message message(stun::Message::Indication, stun::Message::DataIndication);
		
	// Try to use the externalIP value for the XorPeerAddress 
	// attribute to overcome proxy and NAT issues.
	std::string peerHost(server().options().externalIP);
	if (peerHost.empty()) {
		peerHost.assign(peerAddress.host());
		assert(0 && "external IP not set");
	}
	
	auto peerAttr = new stun::XorPeerAddress;
	peerAttr->setAddress(net::Address(peerHost, peerAddress.port()));
	message.add(peerAttr);

	auto dataAttr = new stun::Data;
	dataAttr->copyBytes(bufferCast<const char*>(buffer), buffer.size());
	message.add(dataAttr);
	
	//Mutex::ScopedLock lock(_mutex);

	TraceL << "Send data indication:" 
		<< "\n\tFrom: " << peerAddress
		<< "\n\tTo: " << _tuple.remote()
		//<< "\n\tData: " << std::string(packet.data(), packet.size())
		<< endl;
		
	server().udpSocket().sendPacket(message, _tuple.remote());
	
	//net::Address tempAddress("58.7.41.244", _tuple.remote().port());
	//server().udpSocket().send(message, tempAddress);
}