Search in sources :

Example 1 with NATTraversalObserver

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);
                }
            }
        });
    }
}
Also used : HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) GenericMessageConnectionListener(com.biglybt.pif.messaging.generic.GenericMessageConnectionListener) NATTraversalObserver(com.biglybt.core.nat.NATTraversalObserver) GenericMessageEndpoint(com.biglybt.pif.messaging.generic.GenericMessageEndpoint) MessageException(com.biglybt.pif.messaging.MessageException) NATTraverser(com.biglybt.core.nat.NATTraverser) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

NATTraversalObserver (com.biglybt.core.nat.NATTraversalObserver)1 NATTraverser (com.biglybt.core.nat.NATTraverser)1 MessageException (com.biglybt.pif.messaging.MessageException)1 GenericMessageConnectionListener (com.biglybt.pif.messaging.generic.GenericMessageConnectionListener)1 GenericMessageEndpoint (com.biglybt.pif.messaging.generic.GenericMessageEndpoint)1 InetSocketAddress (java.net.InetSocketAddress)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1