Search in sources :

Example 11 with MessageException

use of com.biglybt.pif.messaging.MessageException in project BiglyBT by BiglySoftware.

the class GenericMessageConnectionImpl method send.

@Override
public void send(PooledByteBuffer message) throws MessageException {
    int size = ((PooledByteBufferImpl) message).getBuffer().remaining(DirectByteBuffer.SS_EXTERNAL);
    if (size > getMaximumMessageSize()) {
        throw (new MessageException("Message is too large: supplied is " + size + ", maximum is " + getMaximumMessageSize()));
    }
    delegate.send(message);
}
Also used : MessageException(com.biglybt.pif.messaging.MessageException) GenericMessageEndpoint(com.biglybt.pif.messaging.generic.GenericMessageEndpoint)

Example 12 with MessageException

use of com.biglybt.pif.messaging.MessageException 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)

Example 13 with MessageException

use of com.biglybt.pif.messaging.MessageException in project BiglyBT by BiglySoftware.

the class GenericMessageConnectionIndirect method send.

@Override
public void send(PooledByteBuffer pbb) throws MessageException {
    byte[] bytes = pbb.toByteArray();
    if (TRACE) {
        trace("send " + bytes.length);
    }
    if (incoming) {
        synchronized (send_queue) {
            if (send_queue.size() > 64) {
                throw (new MessageException("Send queue limit exceeded"));
            }
            send_queue.add(bytes);
        }
        send_queue_sem.release();
    } else {
        List messages = new ArrayList();
        messages.add(bytes);
        send(messages);
    }
}
Also used : MessageException(com.biglybt.pif.messaging.MessageException)

Example 14 with MessageException

use of com.biglybt.pif.messaging.MessageException in project BiglyBT by BiglySoftware.

the class GenericMessageConnectionIndirect method receive.

protected static Map receive(MessageManagerImpl message_manager, InetSocketAddress originator, Map message) {
    if (TRACE) {
        System.out.println("receive:" + originator + "/" + message);
    }
    if (!message.containsKey("type")) {
        return (null);
    }
    int type = ((Long) message.get("type")).intValue();
    if (type == MESSAGE_TYPE_CONNECT) {
        String msg_id = new String((byte[]) message.get("msg_id"));
        String msg_desc = new String((byte[]) message.get("msg_desc"));
        GenericMessageEndpointImpl endpoint = new GenericMessageEndpointImpl(originator);
        endpoint.addUDP(originator);
        GenericMessageHandler handler = message_manager.getHandler(msg_id);
        if (handler == null) {
            Debug.out("No message handler registered for '" + msg_id + "'");
            return (null);
        }
        try {
            Long con_id;
            synchronized (remote_connections) {
                if (remote_connections.size() >= MAX_REMOTE_CONNECTIONS) {
                    Debug.out("Maximum remote connections exceeded - request from " + originator + " denied [" + getRemoteConnectionStatus() + "]");
                    return (null);
                }
                int num_from_this_ip = 0;
                Iterator it = remote_connections.values().iterator();
                while (it.hasNext()) {
                    GenericMessageConnectionIndirect con = (GenericMessageConnectionIndirect) it.next();
                    if (con.getEndpoint().getNotionalAddress().getAddress().equals(originator.getAddress())) {
                        num_from_this_ip++;
                    }
                }
                if (num_from_this_ip >= MAX_REMOTE_CONNECTIONS_PER_IP) {
                    Debug.out("Maximum remote connections per-ip exceeded - request from " + originator + " denied [" + getRemoteConnectionStatus() + "]");
                    return (null);
                }
                con_id = new Long(connection_id_next++);
            }
            GenericMessageConnectionIndirect indirect_connection = new GenericMessageConnectionIndirect(message_manager, msg_id, msg_desc, endpoint, con_id.longValue());
            GenericMessageConnectionImpl new_connection = new GenericMessageConnectionImpl(message_manager, indirect_connection);
            if (handler.accept(new_connection)) {
                new_connection.accepted();
                synchronized (remote_connections) {
                    remote_connections.put(con_id, indirect_connection);
                }
                List replies = indirect_connection.receive((List) message.get("data"));
                Map reply = new HashMap();
                reply.put("type", new Long(MESSAGE_TYPE_CONNECT));
                reply.put("con_id", con_id);
                reply.put("data", replies);
                return (reply);
            } else {
                return (null);
            }
        } catch (MessageException e) {
            Debug.out("Error accepting message", e);
            return (null);
        }
    } else if (type == MESSAGE_TYPE_DATA) {
        Long con_id = (Long) message.get("con_id");
        GenericMessageConnectionIndirect indirect_connection;
        synchronized (remote_connections) {
            indirect_connection = (GenericMessageConnectionIndirect) remote_connections.get(con_id);
        }
        if (indirect_connection == null) {
            return (null);
        }
        Map reply = new HashMap();
        if (indirect_connection.isClosed()) {
            reply.put("type", new Long(MESSAGE_TYPE_DISCONNECT));
        } else {
            List replies = indirect_connection.receive((List) message.get("data"));
            reply.put("type", new Long(MESSAGE_TYPE_DATA));
            reply.put("data", replies);
            if (indirect_connection.receiveIncomplete()) {
                reply.put("more_data", new Long(1));
            }
        }
        return (reply);
    } else {
        // error or disconnect
        Long con_id = (Long) message.get("con_id");
        GenericMessageConnectionIndirect indirect_connection;
        synchronized (remote_connections) {
            indirect_connection = (GenericMessageConnectionIndirect) remote_connections.get(con_id);
        }
        if (indirect_connection != null) {
            try {
                indirect_connection.close(new Throwable("Remote closed connection"));
            } catch (Throwable e) {
                Debug.printStackTrace(e);
            }
        }
        return (null);
    }
}
Also used : GenericMessageEndpoint(com.biglybt.pif.messaging.generic.GenericMessageEndpoint) MessageException(com.biglybt.pif.messaging.MessageException) GenericMessageHandler(com.biglybt.pif.messaging.generic.GenericMessageHandler)

Aggregations

MessageException (com.biglybt.pif.messaging.MessageException)14 GenericMessageEndpoint (com.biglybt.pif.messaging.generic.GenericMessageEndpoint)6 PooledByteBuffer (com.biglybt.pif.utils.PooledByteBuffer)5 PooledByteBufferImpl (com.biglybt.pifimpl.local.utils.PooledByteBufferImpl)4 GenericMessageConnectionListener (com.biglybt.pif.messaging.generic.GenericMessageConnectionListener)3 InetSocketAddress (java.net.InetSocketAddress)3 ByteBuffer (java.nio.ByteBuffer)3 NATTraversalObserver (com.biglybt.core.nat.NATTraversalObserver)1 NATTraverser (com.biglybt.core.nat.NATTraverser)1 NetworkConnection (com.biglybt.core.networkmanager.NetworkConnection)1 NetworkManager (com.biglybt.core.networkmanager.NetworkManager)1 TransportHelper (com.biglybt.core.networkmanager.impl.TransportHelper)1 MessageStreamDecoder (com.biglybt.core.peermanager.messaging.MessageStreamDecoder)1 MessageStreamEncoder (com.biglybt.core.peermanager.messaging.MessageStreamEncoder)1 MessageStreamFactory (com.biglybt.core.peermanager.messaging.MessageStreamFactory)1 CryptoManagerException (com.biglybt.core.security.CryptoManagerException)1 SHA1Simple (com.biglybt.core.util.SHA1Simple)1 GenericMessageConnection (com.biglybt.pif.messaging.generic.GenericMessageConnection)1 GenericMessageHandler (com.biglybt.pif.messaging.generic.GenericMessageHandler)1 GenericMessageRegistration (com.biglybt.pif.messaging.generic.GenericMessageRegistration)1