use of com.biglybt.core.nat.NATTraversalObserver in project BiglyBT by BiglySoftware.
the class GenericMessageConnectionImpl method connectUDP.
protected void connectUDP(final ByteBuffer initial_data, final InetSocketAddress udp_ep, boolean nat_traversal) {
if (TRACE) {
System.out.println("UDP connection attempt to " + udp_ep + " (nat=" + nat_traversal + ")");
}
final GenericMessageEndpointImpl gen_udp = new GenericMessageEndpointImpl(endpoint.getNotionalAddress());
gen_udp.addUDP(udp_ep);
final GenericMessageConnectionAdapter udp_delegate = new GenericMessageConnectionDirect(msg_id, msg_desc, gen_udp, stream_crypto, shared_secrets);
udp_delegate.setOwner(this);
if (nat_traversal || TEST_TUNNEL) {
final NATTraverser nat_traverser = message_manager.getNATTraverser();
Map request = new HashMap();
nat_traverser.attemptTraversal(message_manager, udp_ep, request, false, new NATTraversalObserver() {
@Override
public void succeeded(final InetSocketAddress rendezvous, final InetSocketAddress target, Map reply) {
if (closed) {
reportFailed(new MessageException("Connection has been closed"));
} else {
connect_method_count++;
if (TEST_TUNNEL) {
initial_data.rewind();
connectTunnel(initial_data, gen_udp, rendezvous, target);
} else {
udp_delegate.connect(initial_data, new GenericMessageConnectionAdapter.ConnectionListener() {
private boolean connected;
@Override
public void connectSuccess() {
connected = true;
setDelegate(udp_delegate);
if (closed) {
try {
delegate.close();
} catch (Throwable e) {
}
reportFailed(new MessageException("Connection has been closed"));
} else {
reportConnected();
}
}
@Override
public void connectFailure(Throwable failure_msg) {
if (connected) {
reportFailed(failure_msg);
} else {
initial_data.rewind();
connectTunnel(initial_data, gen_udp, rendezvous, target);
}
}
});
}
}
}
@Override
public void failed(int failure_type) {
reportFailed(new MessageException("UDP connection attempt failed - NAT traversal failed (" + NATTraversalObserver.FT_STRINGS[failure_type] + ")"));
}
@Override
public void failed(Throwable cause) {
reportFailed(cause);
}
@Override
public void disabled() {
reportFailed(new MessageException("UDP connection attempt failed as DDB is disabled"));
}
});
} else {
udp_delegate.connect(initial_data, new GenericMessageConnectionAdapter.ConnectionListener() {
private boolean connected;
@Override
public void connectSuccess() {
connected = true;
setDelegate(udp_delegate);
if (closed) {
try {
delegate.close();
} catch (Throwable e) {
}
reportFailed(new MessageException("Connection has been closed"));
} else {
reportConnected();
}
}
@Override
public void connectFailure(Throwable failure_msg) {
if (connected) {
reportFailed(failure_msg);
} else {
initial_data.rewind();
connectUDP(initial_data, udp_ep, true);
}
}
});
}
}
Aggregations