use of com.biglybt.core.networkmanager.impl.ProtocolDecoder in project BiglyBT by BiglySoftware.
the class UDPConnectionManager method accept.
protected void accept(final int local_port, final InetSocketAddress remote_address, final UDPConnection connection) {
final UDPTransportHelper helper = new UDPTransportHelper(this, remote_address, connection);
try {
connection.setTransport(helper);
TransportCryptoManager.getSingleton().manageCrypto(helper, null, true, null, new TransportCryptoManager.HandshakeListener() {
@Override
public void handshakeSuccess(ProtocolDecoder decoder, ByteBuffer remaining_initial_data) {
TransportHelperFilter filter = decoder.getFilter();
ConnectionEndpoint co_ep = new ConnectionEndpoint(remote_address);
ProtocolEndpointUDP pe_udp = (ProtocolEndpointUDP) ProtocolEndpointFactory.createEndpoint(ProtocolEndpoint.PROTOCOL_UDP, co_ep, remote_address);
UDPTransport transport = new UDPTransport(pe_udp, filter);
helper.setTransport(transport);
incoming_manager.addConnection(local_port, filter, transport);
}
@Override
public void handshakeFailure(Throwable failure_msg) {
if (Logger.isEnabled()) {
Logger.log(new LogEvent(LOGID, "incoming crypto handshake failure: " + Debug.getNestedExceptionMessage(failure_msg)));
}
connection.close("handshake failure: " + Debug.getNestedExceptionMessage(failure_msg));
}
@Override
public void gotSecret(byte[] session_secret) {
helper.getConnection().setSecret(session_secret);
}
@Override
public int getMaximumPlainHeaderLength() {
return (incoming_manager.getMaxMinMatchBufferSize());
}
@Override
public int matchPlainHeader(ByteBuffer buffer) {
Object[] match_data = incoming_manager.checkForMatch(helper, local_port, buffer, true);
if (match_data == null) {
return (TransportCryptoManager.HandshakeListener.MATCH_NONE);
} else {
return (TransportCryptoManager.HandshakeListener.MATCH_CRYPTO_NO_AUTO_FALLBACK);
}
}
});
} catch (Throwable e) {
Debug.printStackTrace(e);
helper.close(Debug.getNestedExceptionMessage(e));
}
}
use of com.biglybt.core.networkmanager.impl.ProtocolDecoder in project BiglyBT by BiglySoftware.
the class UDPConnectionManager method connectOutbound.
public void connectOutbound(final UDPTransport udp_transport, final InetSocketAddress address, byte[][] shared_secrets, ByteBuffer initial_data, final ConnectListener listener) {
UDPTransportHelper helper = null;
try {
if (address.isUnresolved()) {
listener.connectFailure(new UnknownHostException(address.getHostName()));
return;
}
int time = listener.connectAttemptStarted(-1);
if (time != -1) {
Debug.out("UDP connect time override not supported");
}
helper = new UDPTransportHelper(this, address, udp_transport);
final UDPTransportHelper f_helper = helper;
synchronized (this) {
outbound_connection_count++;
if (outbound_connection_count >= max_outbound_connections) {
if (!max_conn_exceeded_logged) {
max_conn_exceeded_logged = true;
Debug.out("UDPConnectionManager: max outbound connection limit reached (" + max_outbound_connections + ")");
}
}
}
try {
TransportCryptoManager.getSingleton().manageCrypto(helper, shared_secrets, false, initial_data, new TransportCryptoManager.HandshakeListener() {
@Override
public void handshakeSuccess(ProtocolDecoder decoder, ByteBuffer remaining_initial_data) {
synchronized (UDPConnectionManager.this) {
if (outbound_connection_count > 0) {
outbound_connection_count--;
}
}
TransportHelperFilter filter = decoder.getFilter();
try {
udp_transport.setFilter(filter);
if (udp_transport.isClosed()) {
udp_transport.close("Already closed");
listener.connectFailure(new Exception("Connection already closed"));
} else {
if (Logger.isEnabled()) {
Logger.log(new LogEvent(LOGID, "Outgoing UDP stream to " + address + " established, type = " + filter.getName(false)));
}
udp_transport.connectedOutbound();
listener.connectSuccess(udp_transport, remaining_initial_data);
}
} catch (Throwable e) {
Debug.printStackTrace(e);
udp_transport.close(Debug.getNestedExceptionMessageAndStack(e));
listener.connectFailure(e);
}
}
@Override
public void handshakeFailure(Throwable failure_msg) {
synchronized (UDPConnectionManager.this) {
if (outbound_connection_count > 0) {
outbound_connection_count--;
}
}
f_helper.close(Debug.getNestedExceptionMessageAndStack(failure_msg));
listener.connectFailure(failure_msg);
}
@Override
public void gotSecret(byte[] session_secret) {
f_helper.getConnection().setSecret(session_secret);
}
@Override
public int getMaximumPlainHeaderLength() {
// this is outgoing
throw (new RuntimeException());
}
@Override
public int matchPlainHeader(ByteBuffer buffer) {
// this is outgoing
throw (new RuntimeException());
}
});
} catch (Throwable e) {
synchronized (this) {
if (outbound_connection_count > 0) {
outbound_connection_count--;
}
}
throw (e);
}
} catch (Throwable e) {
Debug.printStackTrace(e);
if (helper != null) {
helper.close(Debug.getNestedExceptionMessage(e));
}
listener.connectFailure(e);
}
}
Aggregations