📄 pepeertransportprotocol.java
字号:
{
transport.setTransportMode(2);
outgoing_piece_message_handler.setRequestReadAhead(256);
} else
if (send_rate >= 0x1312d0L)
{
transport.setTransportMode(2);
outgoing_piece_message_handler.setRequestReadAhead(128);
} else
if (send_rate >= 0x1e848L)
{
if (transport.getTransportMode() < 1)
transport.setTransportMode(1);
outgoing_piece_message_handler.setRequestReadAhead(32);
} else
if (send_rate >= 62500L)
outgoing_piece_message_handler.setRequestReadAhead(16);
else
if (send_rate >= 31250L)
outgoing_piece_message_handler.setRequestReadAhead(8);
else
if (send_rate >= 12500L)
outgoing_piece_message_handler.setRequestReadAhead(4);
else
outgoing_piece_message_handler.setRequestReadAhead(2);
long receive_rate = peer_stats.getDataReceiveRate() + peer_stats.getProtocolReceiveRate();
if (receive_rate >= 0x1312d0L)
transport.setTransportMode(2);
else
if (receive_rate >= 0x1e848L && transport.getTransportMode() < 1)
transport.setTransportMode(1);
}
}
public int getConnectionState()
{
return connection_state;
}
public long getTimeSinceLastDataMessageReceived()
{
if (last_data_message_received_time == -1L)
return -1L;
long now = SystemTime.getCurrentTime();
if (last_data_message_received_time > now)
last_data_message_received_time = now;
return now - last_data_message_received_time;
}
public long getTimeSinceGoodDataReceived()
{
if (last_good_data_time == -1L)
return -1L;
long now = SystemTime.getCurrentTime();
if (last_good_data_time > now)
last_good_data_time = now;
return now - last_good_data_time;
}
public long getTimeSinceLastDataMessageSent()
{
if (last_data_message_sent_time == -1L)
return -1L;
long now = SystemTime.getCurrentTime();
if (last_data_message_sent_time > now)
last_data_message_sent_time = now;
return now - last_data_message_sent_time;
}
public long getTimeSinceConnectionEstablished()
{
if (connection_established_time == 0L)
return 0L;
long now = SystemTime.getCurrentTime();
if (connection_established_time > now)
connection_established_time = now;
return now - connection_established_time;
}
public int getConsecutiveNoRequestCount()
{
return consecutive_no_request_count;
}
public void setConsecutiveNoRequestCount(int num)
{
consecutive_no_request_count = num;
}
protected void decodeBTHandshake(BTHandshake handshake)
{
org.gudy.azureus2.core3.peer.util.PeerIdentityDataID my_peer_data_id;
if (Logger.isEnabled())
Logger.log(new LogEvent(this, LOGID, (new StringBuilder()).append("Received handshake with reserved bytes: ").append(ByteFormatter.nicePrint(handshake.getReserved(), false)).toString()));
my_peer_data_id = manager.getPeerIdentityDataID();
if (getConnectionState() == 4)
{
handshake.destroy();
closeConnectionInternally("peer sent another handshake after the initial connect");
}
if (!Arrays.equals(manager.getHash(), handshake.getDataHash()))
{
closeConnectionInternally("handshake has wrong infohash");
handshake.destroy();
return;
}
peer_id = handshake.getPeerId();
client_peer_id = client = StringInterner.intern(PeerClassifier.getClientDescription(peer_id));
if (!PeerClassifier.isClientTypeAllowed(client))
{
closeConnectionInternally((new StringBuilder()).append(client).append(" client type not allowed to connect, banned").toString());
handshake.destroy();
return;
}
if (Arrays.equals(manager.getPeerId(), peer_id))
{
manager.peerVerifiedAsSelf(this);
closeConnectionInternally("given peer id matches myself");
handshake.destroy();
return;
}
boolean sameIdentity = PeerIdentityManager.containsIdentity(my_peer_data_id, peer_id, getPort());
boolean sameIP = false;
boolean same_allowed = COConfigurationManager.getBooleanParameter("Allow Same IP Peers") || ip.equals("127.0.0.1");
if (!same_allowed && PeerIdentityManager.containsIPAddress(my_peer_data_id, ip))
sameIP = true;
if (sameIdentity)
{
boolean close = true;
if (connection.isLANLocal())
{
PEPeerTransport existing = manager.getTransportFromIdentity(peer_id);
if (existing != null)
{
String existing_ip = existing.getIp();
if (!existing.isLANLocal() || existing_ip.endsWith(".1") && !existing_ip.equals(ip))
{
Debug.outNoStack((new StringBuilder()).append("Dropping existing non-lanlocal peer connection [").append(existing).append("] in favour of [").append(this).append("]").toString());
manager.removePeer(existing);
close = false;
}
}
}
if (close)
{
closeConnectionInternally("peer matches already-connected peer id");
handshake.destroy();
return;
}
}
if (sameIP)
{
closeConnectionInternally("peer matches already-connected IP address, duplicate connections not allowed");
handshake.destroy();
return;
}
int maxAllowed = manager.getMaxNewConnectionsAllowed();
if (maxAllowed == 0 && !manager.doOptimisticDisconnect(isLANLocal(), isPriorityConnection()))
{
String msg = (new StringBuilder()).append("too many existing peer connections [p").append(PeerIdentityManager.getIdentityCount(my_peer_data_id)).append("/g").append(PeerIdentityManager.getTotalIdentityCount()).append(", pmx").append(PeerUtils.MAX_CONNECTIONS_PER_TORRENT).append("/gmx").append(PeerUtils.MAX_CONNECTIONS_TOTAL).append("/dmx").append(manager.getMaxConnections()).append("]").toString();
closeConnectionInternally(msg);
handshake.destroy();
return;
}
closing_mon.enter();
if (!closing)
break MISSING_BLOCK_LABEL_620;
String msg = "connection already closing";
closeConnectionInternally("connection already closing");
handshake.destroy();
closing_mon.exit();
return;
if (PeerIdentityManager.addIdentity(my_peer_data_id, peer_id, getPort(), ip))
break MISSING_BLOCK_LABEL_658;
closeConnectionInternally("peer matches already-connected peer id");
handshake.destroy();
closing_mon.exit();
return;
identityAdded = true;
closing_mon.exit();
break MISSING_BLOCK_LABEL_685;
Exception exception;
exception;
closing_mon.exit();
throw exception;
if (Logger.isEnabled())
Logger.log(new LogEvent(this, LOGID, "In: has sent their handshake"));
handshake_reserved_bytes = handshake.getReserved();
ml_dht_enabled = (handshake_reserved_bytes[7] & 1) == 1;
messaging_mode = decideExtensionProtocol(handshake);
if (messaging_mode == 2)
{
if (Logger.isEnabled() && client.indexOf("Azureus") == -1)
Logger.log(new LogEvent(this, LOGID, "Handshake claims extended AZ messaging support... enabling AZ mode."));
ml_dht_enabled = false;
Transport transport = connection.getTransport();
boolean enable_padding = transport.isTCP() && transport.isEncrypted();
connection.getIncomingMessageQueue().setDecoder(new AZMessageDecoder());
connection.getOutgoingMessageQueue().setEncoder(new AZMessageEncoder(enable_padding));
sendAZHandshake();
handshake.destroy();
} else
if (messaging_mode == 3)
{
if (Logger.isEnabled())
Logger.log(new LogEvent(this, LOGID, "Enabling LT extension protocol support..."));
connection.getIncomingMessageQueue().setDecoder(new LTMessageDecoder());
connection.getOutgoingMessageQueue().setEncoder(new LTMessageEncoder(this));
generateSessionId();
initPostConnection(handshake);
sendLTHandshake();
} else
{
client = ClientIdentifier.identifyBTOnly(client_peer_id, handshake_reserved_bytes);
connection.getIncomingMessageQueue().getDecoder().resumeDecoding();
initPostConnection(handshake);
}
return;
}
private int decideExtensionProtocol(BTHandshake handshake)
{
boolean supports_azmp = (handshake.getReserved()[0] & 0x80) == 128;
boolean supports_ltep = (handshake.getReserved()[5] & 0x10) == 16;
if (!supports_azmp)
if (supports_ltep)
{
if (!manager.isExtendedMessagingEnabled())
{
if (Logger.isEnabled())
Logger.log(new LogEvent(this, LOGID, "Ignoring peer's LT extension protocol support, as disabled for this download."));
return 1;
} else
{
return 3;
}
} else
{
return 1;
}
if (!supports_ltep)
{
if (!manager.isExtendedMessagingEnabled())
{
if (Logger.isEnabled())
Logger.log(new LogEvent(this, LOGID, "Ignoring peer's extended AZ messaging support, as disabled for this download."));
return 1;
}
if (client.indexOf("Plus!") != -1)
{
if (Logger.isEnabled())
Logger.log(new LogEvent(this, LOGID, "Handshake mistakingly indicates extended AZ messaging support...ignoring."));
return 1;
} else
{
return 2;
}
}
boolean enp_major_bit = (handshake.getReserved()[5] & 2) == 2;
boolean enp_minor_bit = (handshake.getReserved()[5] & 1) == 1;
String their_ext_preference = (new StringBuilder()).append(enp_major_bit != enp_minor_bit ? "Prefer " : "Force ").append(enp_major_bit ? "AZMP" : "LTEP").toString();
String our_ext_preference = "Force AZMP";
boolean use_azmp = enp_major_bit || enp_minor_bit;
boolean we_decide = use_azmp;
if (Logger.isEnabled())
{
String msg = "Peer supports both AZMP and LTEP: ";
msg = (new StringBuilder()).append(msg).append("\"").append(our_ext_preference).append("\"").append(we_decide ? ">" : "<").append(our_ext_preference.equals(their_ext_preference) ? "= " : " ").toString();
msg = (new StringBuilder()).append(msg).append("\"").append(their_ext_preference).append("\" - using ").append(use_azmp ? "AZMP" : "LTEP").toString();
Logger.log(new LogEvent(this, LOGID, msg));
}
return use_azmp ? 2 : 3;
}
protected void decodeLTHandshake(LTHandshake handshake)
{
String lt_handshake_name = handshake.getClientName();
if (lt_handshake_name != null)
{
client_handshake = StringInterner.intern(lt_handshake_name);
client = StringInterner.intern(ClientIdentifier.identifyLTEP(client_peer_id, client_handshake, peer_id));
}
if (handshake.getTCPListeningPort() > 0)
{
Boolean crypto_requested = handshake.isCryptoRequested();
byte handshake_type = ((byte)(crypto_requested == null || !crypto_requested.booleanValue() ? 0 : 1));
tcp_listen_port = handshake.getTCPListeningPort();
peer_item_identity = PeerItemFactory.createPeerItem(ip, tcp_listen_port, PeerItem.convertSourceID(peer_source), handshake_type, udp_listen_port, crypto_level, 0);
}
if (handshake.isUploadOnly())
{
relativeSeeding |= 1;
checkSeed();
}
LTMessageEncoder encoder = (LTMessageEncoder)connection.getOutgoingMessageQueue().getEncoder();
encoder.updateSupportedExtensions(handshake.getExtensionMapping());
ut_pex_enabled = encoder.supportsUTPEX();
doPostHandshakeProcessing();
handshake.destroy();
}
protected void decodeAZHandshake(AZHandshake handshake)
{
if (getConnectionState() == 4)
{
handshake.destroy();
closeConnectionInternally("peer sent another az-handshake after the intial connect");
}
client_handshake = StringInterner.intern(handshake.getClient());
client_handshake_version = StringInterner.intern(handshake.getClientVersion());
client = StringInterner.intern(ClientIdentifier.identifyAZMP(client_peer_id, client_handshake, client_handshake_version, peer_id));
if (handshake.getTCPListenPort() > 0)
{
tcp_listen_port = handshake.getTCPListenPort();
udp_listen_port = handshake.getUDPListenPort();
udp_non_data_port = handshake.getUDPNonDataListenPort();
byte type = ((byte)(handshake.getHandshakeType() != 1 ? 0 : 1));
peer_item_identity = PeerItemFactory.createPeerItem(ip, tcp_listen_port, PeerItem.convertSourceID(peer_source), type, udp_listen_port, crypto_level, 0);
}
if (handshake.getReconnectSessionID() != null)
{
if (Logger.isEnabled())
Logger.log(new LogEvent(this, LOGID, 0, (new StringBuilder()).append("received reconnect request ID: ").append(handshake.getReconnectSessionID().toBase32String()).toString()));
checkForReconnect(handshake.getReconnectSessionID());
}
if (handshake.getRemoteSessionID() != null)
peerSessionID = handshake.getRemoteSessionID();
if (handshake.isUploadOnly())
{
relativeSeeding |= 1;
checkSeed();
}
String supported_message_ids[] = handshake.getMessageIDs();
byte supported_message_versions[] = handshake.getMessageVersions();
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)
continue;
messages.add(msg);
String id = msg.getID();
byte supported_version = supported_message_versions[i];
if (id == "BT_BITFIELD")
{
other_peer_bitfield_version = supported_version;
continue;
}
if (id == "BT_CANCEL")
{
other_peer_cancel_version = supported_version;
continue;
}
if (id == "BT_CHOKE")
{
other_peer_choke_version = supported_version;
continue;
}
if (id == "BT_HANDSHAKE")
{
other_peer_handshake_version = supported_version;
continue;
}
if (id == "BT_HAVE")
{
other_peer_bt_have_version = supported_version;
continue;
}
if (id == "BT_INTERESTED")
{
other_peer_interested_version = supported_version;
continue;
}
if (id == "BT_KEEP_ALIVE")
{
other_peer_keep_alive_version = supported_version;
continue;
}
if (id == "BT_PIECE")
{
other_peer_piece_version = supported_version;
continue;
}
if (id == "BT_UNCHOKE")
{
other_peer_unchoke_version = supported_version;
continue;
}
if (id == "BT_UNINTERESTED")
{
other_peer_uninterested_version = supported_version;
continue;
}
if (id == "BT_REQUEST")
{
other_peer_request_version = supported_version;
continue;
}
if (id == "AZ_PEER_EXCHANGE")
{
other_peer_pex_version = supported_version;
continue;
}
if (id == "AZ_REQUEST_HINT")
{
other_peer_az_request_hint_version = supported_version;
continue;
}
if (id == "AZ_HAVE")
{
other_peer_az_have_version = supported_version;
continue;
}
if (id == "AZ_BAD_PIECE")
{
other_peer_az_bad_piece_version = supported_version;
continue;
}
if (id == "BT_DHT_PORT")
ml_dht_enabled = true;
}
supported_messages = (Message[])(Message[])messages.toArray(new Message[messages.size()]);
if (outgoing_piece_message_handler != null)
outgoing_piece_message_handle
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -