Search in sources :

Example 1 with Message

use of com.biglybt.core.peermanager.messaging.Message in project BiglyBT by BiglySoftware.

the class HTTPMessageDecoder method destroy.

@Override
public ByteBuffer destroy() {
    paused = true;
    destroyed = true;
    if (http_connection != null) {
        http_connection.destroy();
    }
    try {
        for (int i = 0; i < messages.size(); i++) {
            Message msg = (Message) messages.get(i);
            msg.destroy();
        }
    } catch (IndexOutOfBoundsException e) {
    // as access to messages_last_read isn't synchronized we can get this error if we destroy the
    // decoder in parallel with messages being removed. We don't really want to synchornized access
    // to this so we'll take the hit here
    }
    messages.clear();
    return (null);
}
Also used : Message(com.biglybt.core.peermanager.messaging.Message)

Example 2 with Message

use of com.biglybt.core.peermanager.messaging.Message in project BiglyBT by BiglySoftware.

the class HTTPNetworkConnection method sendAndClose.

protected void sendAndClose(String data) {
    final Message http_message = new HTTPMessage(data);
    getConnection().getOutgoingMessageQueue().registerQueueListener(new OutgoingMessageQueue.MessageQueueListener() {

        @Override
        public boolean messageAdded(Message message) {
            return (true);
        }

        @Override
        public void messageQueued(Message message) {
        }

        @Override
        public void messageRemoved(Message message) {
        }

        @Override
        public void messageSent(Message message) {
            if (message == http_message) {
                close("Close after message send complete");
            }
        }

        @Override
        public void protocolBytesSent(int byte_count) {
        }

        @Override
        public void dataBytesSent(int byte_count) {
        }

        @Override
        public void flush() {
        }
    });
    getConnection().getOutgoingMessageQueue().addMessage(http_message, false);
}
Also used : RawMessage(com.biglybt.core.networkmanager.RawMessage) Message(com.biglybt.core.peermanager.messaging.Message) OutgoingMessageQueue(com.biglybt.core.networkmanager.OutgoingMessageQueue)

Example 3 with Message

use of com.biglybt.core.peermanager.messaging.Message in project BiglyBT by BiglySoftware.

the class PEPeerTransportProtocol method sendAZHandshake.

private void sendAZHandshake() {
    final Message[] avail_msgs = MessageManager.getSingleton().getRegisteredMessages();
    final String[] avail_ids = new String[avail_msgs.length];
    final byte[] avail_vers = new byte[avail_msgs.length];
    for (int i = 0; i < avail_msgs.length; i++) {
        avail_ids[i] = avail_msgs[i].getID();
        avail_vers[i] = avail_msgs[i].getVersion();
    }
    int local_tcp_port = TCPNetworkManager.getSingleton().getTCPListeningPortNumber();
    int local_udp_port = UDPNetworkManager.getSingleton().getUDPListeningPortNumber();
    int local_udp2_port = UDPNetworkManager.getSingleton().getUDPNonDataListeningPortNumber();
    String tcpPortOverride = COConfigurationManager.getStringParameter("TCP.Listen.Port.Override");
    try {
        local_tcp_port = Integer.parseInt(tcpPortOverride);
    } catch (NumberFormatException e) // ignore as invalid input
    {
    }
    boolean require_crypto = NetworkManager.getCryptoRequired(manager.getAdapter().getCryptoLevel());
    /*
		 * we always send the Az-handshake immediately after the BT-handshake, before decoding the
		 * other side's Az-handshake, thus there should be no peerSessionID unless this is a
		 * reconnect
		 */
    if (peerSessionID != null)
        Logger.log(new LogEvent(this, LOGID, LogEvent.LT_INFORMATION, "notifying peer of reconnect attempt"));
    InetAddress defaultV6 = null;
    NetworkAdmin na = NetworkAdmin.getSingleton();
    if (peer_item_identity.getNetwork() == AENetworkClassifier.AT_PUBLIC && !na.isSocksActive()) {
        // don't send public address in handshake
        defaultV6 = na.hasIPV6Potential(true) ? na.getDefaultPublicAddressV6() : null;
    }
    // String peer_name = Constants.BIGLY_PROTOCOL_NAME;
    String client_name = (String) ClientIDManagerImpl.getSingleton().getProperty(manager.getHash(), ClientIDGenerator.PR_CLIENT_NAME);
    int pos = client_name.indexOf(' ');
    String peer_name;
    String peer_version;
    if (pos == -1) {
        Debug.out("Unsupported client name: " + client_name);
        peer_name = Constants.BIGLY_PROTOCOL_NAME;
        peer_version = Constants.AZUREUS_VERSION;
    } else {
        peer_name = client_name.substring(0, pos);
        pos = client_name.lastIndexOf(' ');
        peer_version = client_name.substring(pos + 1);
    }
    AZHandshake az_handshake = new AZHandshake(AZPeerIdentityManager.getAZPeerIdentity(), mySessionID, peerSessionID, peer_name, peer_version, local_tcp_port, local_udp_port, local_udp2_port, defaultV6, manager.isPrivateTorrent() ? 0 : (is_metadata_download ? 0 : manager.getTorrentInfoDictSize()), avail_ids, avail_vers, require_crypto ? AZHandshake.HANDSHAKE_TYPE_CRYPTO : AZHandshake.HANDSHAKE_TYPE_PLAIN, other_peer_handshake_version, manager.isSeeding() && !(ENABLE_LAZY_BITFIELD || manual_lazy_bitfield_control));
    connection.getOutgoingMessageQueue().addMessage(az_handshake, false);
}
Also used : Message(com.biglybt.core.peermanager.messaging.Message) NetworkAdmin(com.biglybt.core.networkmanager.admin.NetworkAdmin) InetAddress(java.net.InetAddress)

Example 4 with Message

use of com.biglybt.core.peermanager.messaging.Message in project BiglyBT by BiglySoftware.

the class PEPeerTransportProtocol method decodeAZHandshake.

protected void decodeAZHandshake(AZHandshake handshake) {
    if (getConnectionState() == CONNECTION_FULLY_ESTABLISHED) {
        handshake.destroy();
        closeConnectionInternally("peer sent another az-handshake after the intial connect");
    }
    this.client_handshake = StringInterner.intern(handshake.getClient());
    this.client_handshake_version = StringInterner.intern(handshake.getClientVersion());
    this.client = StringInterner.intern(ClientIdentifier.identifyAZMP(this.client_peer_id, client_handshake, client_handshake_version, this.peer_id));
    if (handshake.getTCPListenPort() > 0) {
        // use the ports given in handshake
        tcp_listen_port = handshake.getTCPListenPort();
        udp_listen_port = handshake.getUDPListenPort();
        udp_non_data_port = handshake.getUDPNonDataListenPort();
        final byte type = handshake.getHandshakeType() == AZHandshake.HANDSHAKE_TYPE_CRYPTO ? PeerItemFactory.HANDSHAKE_TYPE_CRYPTO : PeerItemFactory.HANDSHAKE_TYPE_PLAIN;
        // remake the id using the peer's remote listen port instead of
        // their random local port
        peer_item_identity = PeerItemFactory.createPeerItem(ip, tcp_listen_port, PeerItem.convertSourceID(peer_source), type, udp_listen_port, crypto_level, 0);
    }
    if (AddressUtils.isGlobalAddressV6(handshake.getIPv6()))
        alternativeAddress = handshake.getIPv6();
    if (handshake.getReconnectSessionID() != null) {
        if (Logger.isEnabled()) {
            Logger.log(new LogEvent(this, LOGID, LogEvent.LT_INFORMATION, "received reconnect request ID: " + handshake.getReconnectSessionID().toBase32String()));
        }
        checkForReconnect(handshake.getReconnectSessionID());
    }
    if (handshake.getRemoteSessionID() != null)
        peerSessionID = handshake.getRemoteSessionID();
    if (handshake.isUploadOnly()) {
        relativeSeeding |= RELATIVE_SEEDING_UPLOAD_ONLY_INDICATED;
        checkSeed();
    }
    String[] supported_message_ids = handshake.getMessageIDs();
    byte[] supported_message_versions = handshake.getMessageVersions();
    // find mutually available message types
    final ArrayList messages = new ArrayList();
    for (int i = 0; i < handshake.getMessageIDs().length; i++) {
        Message msg = MessageManager.getSingleton().lookupMessage(supported_message_ids[i]);
        if (msg != null) {
            // mutual support!
            messages.add(msg);
            String id = msg.getID();
            byte supported_version = supported_message_versions[i];
            // we can use == safely
            if (id == BTMessage.ID_BT_BITFIELD)
                other_peer_bitfield_version = supported_version;
            else if (id == BTMessage.ID_BT_CANCEL)
                other_peer_cancel_version = supported_version;
            else if (id == BTMessage.ID_BT_CHOKE)
                other_peer_choke_version = supported_version;
            else if (id == BTMessage.ID_BT_HANDSHAKE)
                other_peer_handshake_version = supported_version;
            else if (id == BTMessage.ID_BT_HAVE)
                other_peer_bt_have_version = supported_version;
            else if (id == BTMessage.ID_BT_INTERESTED)
                other_peer_interested_version = supported_version;
            else if (id == BTMessage.ID_BT_KEEP_ALIVE)
                other_peer_keep_alive_version = supported_version;
            else if (id == BTMessage.ID_BT_PIECE)
                other_peer_piece_version = supported_version;
            else if (id == BTMessage.ID_BT_UNCHOKE)
                other_peer_unchoke_version = supported_version;
            else if (id == BTMessage.ID_BT_UNINTERESTED)
                other_peer_uninterested_version = supported_version;
            else if (id == BTMessage.ID_BT_REQUEST)
                other_peer_request_version = supported_version;
            else if (id == BTMessage.ID_BT_SUGGEST_PIECE)
                other_peer_suggest_piece_version = supported_version;
            else if (id == BTMessage.ID_BT_HAVE_ALL)
                other_peer_have_all_version = supported_version;
            else if (id == BTMessage.ID_BT_HAVE_NONE)
                other_peer_have_none_version = supported_version;
            else if (id == BTMessage.ID_BT_REJECT_REQUEST)
                other_peer_reject_request_version = supported_version;
            else if (id == BTMessage.ID_BT_ALLOWED_FAST)
                other_peer_allowed_fast_version = supported_version;
            else if (id == AZMessage.ID_AZ_PEER_EXCHANGE)
                other_peer_pex_version = supported_version;
            else if (id == AZMessage.ID_AZ_REQUEST_HINT)
                other_peer_az_request_hint_version = supported_version;
            else if (id == AZMessage.ID_AZ_HAVE)
                other_peer_az_have_version = supported_version;
            else if (id == AZMessage.ID_AZ_BAD_PIECE)
                other_peer_az_bad_piece_version = supported_version;
            else if (id == AZMessage.ID_AZ_STAT_REQUEST)
                other_peer_az_stats_request_version = supported_version;
            else if (id == AZMessage.ID_AZ_STAT_REPLY)
                other_peer_az_stats_reply_version = supported_version;
            else if (id == AZMessage.ID_AZ_METADATA)
                other_peer_az_metadata_version = supported_version;
            else if (id == BTMessage.ID_BT_DHT_PORT)
                this.ml_dht_enabled = true;
            else {
            // we expect unmatched ones here at the moment as we're not
            // dealing with them yet or they don't make sense.
            // for example AZVER
            }
        }
    }
    if (is_metadata_download) {
        int mds = handshake.getMetadataSize();
        if (mds > 0) {
            manager.setTorrentInfoDictSize(mds);
        }
    }
    supported_messages = (Message[]) messages.toArray(new Message[messages.size()]);
    if (outgoing_piece_message_handler != null) {
        outgoing_piece_message_handler.setPieceVersion(other_peer_piece_version);
    }
    if (outgoing_have_message_aggregator != null) {
        outgoing_have_message_aggregator.setHaveVersion(other_peer_bt_have_version, other_peer_az_have_version);
    }
    this.initPostConnection(handshake);
}
Also used : Message(com.biglybt.core.peermanager.messaging.Message)

Example 5 with Message

use of com.biglybt.core.peermanager.messaging.Message in project BiglyBT by BiglySoftware.

the class PEPeerTransportProtocol method cancelRequests.

private void cancelRequests() {
    if (!closing) {
        // cancel any unsent requests in the queue
        final Message[] type = { new BTRequest(-1, -1, -1, other_peer_request_version) };
        connection.getOutgoingMessageQueue().removeMessagesOfType(type, false);
    }
    if (requested != null && requested.size() > 0) {
        try {
            requested_mon.enter();
            if (!closing) {
                // may have unchoked us, gotten a request, then choked without filling it - snub them
                // if they actually have data coming in, they'll be unsnubbed as soon as it writes
                final long timeSinceGoodData = getTimeSinceGoodDataReceived();
                if (timeSinceGoodData == -1 || timeSinceGoodData > 60 * 1000)
                    setSnubbed(true);
            }
            for (int i = requested.size() - 1; i >= 0; i--) {
                final DiskManagerReadRequest request = (DiskManagerReadRequest) requested.remove(i);
                manager.requestCanceled(request);
            }
        } finally {
            requested_mon.exit();
        }
    }
}
Also used : DiskManagerReadRequest(com.biglybt.core.disk.DiskManagerReadRequest) Message(com.biglybt.core.peermanager.messaging.Message)

Aggregations

Message (com.biglybt.core.peermanager.messaging.Message)16 DirectByteBuffer (com.biglybt.core.util.DirectByteBuffer)5 RawMessage (com.biglybt.core.networkmanager.RawMessage)4 IOException (java.io.IOException)4 OutgoingMessageQueue (com.biglybt.core.networkmanager.OutgoingMessageQueue)2 RawMessageImpl (com.biglybt.core.networkmanager.impl.RawMessageImpl)2 DiskManagerReadRequest (com.biglybt.core.disk.DiskManagerReadRequest)1 NetworkAdmin (com.biglybt.core.networkmanager.admin.NetworkAdmin)1 MessageException (com.biglybt.core.peermanager.messaging.MessageException)1 MainlineDHTProvider (com.biglybt.pif.dht.mainline.MainlineDHTProvider)1 GenericMessageEndpoint (com.biglybt.pif.messaging.generic.GenericMessageEndpoint)1 MessageAdapter (com.biglybt.pifimpl.local.messaging.MessageAdapter)1 InetAddress (java.net.InetAddress)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1