use of com.biglybt.core.logging.LogAlert in project BiglyBT by BiglySoftware.
the class SelectorGuard method verifySelectorIntegrity.
/**
* Checks whether selector is still OK, and not spinning.
*/
public void verifySelectorIntegrity(int num_keys_ready, long time_threshold) {
if (num_keys_ready > 0) {
// non-zero select, so OK
ignores++;
if (ignores > MAX_IGNORES) {
// allow MAX_IGNORES / SELECTOR_SPIN_THRESHOLD to be successful select ops and still trigger a spin alert
ignores = 0;
consecutiveZeroSelects = 0;
}
return;
}
if (marked)
marked = false;
else
Debug.out("Error: You must run markPreSelectTime() before calling isSelectorOK");
select_op_time = SystemTime.getCurrentTime() - beforeSelectTime;
if (select_op_time > time_threshold || select_op_time < 0) {
// zero-select, but over the time threshold, so OK
consecutiveZeroSelects = 0;
return;
}
// if we've gotten here, then we have a potential selector anomalie
consecutiveZeroSelects++;
if (consecutiveZeroSelects % 20 == 0 && Constants.isWindows) {
// getting triggered with 20 +_ sometimes 40 due to general high CPU usage
if (consecutiveZeroSelects > 40) {
Debug.out("consecutiveZeroSelects=" + consecutiveZeroSelects);
}
}
if (consecutiveZeroSelects > SELECTOR_SPIN_THRESHOLD) {
if (Constants.isWindows && (Constants.JAVA_VERSION.startsWith("1.4") || Constants.JAVA_VERSION.startsWith("1.5"))) {
// Should be fixed in later 1.5, but play it safe and assume 1.6 or newer only.
if (!listener.safeModeSelectEnabled()) {
String msg = "Likely faulty socket selector detected: reverting to safe-mode socket selection. [JRE " + Constants.JAVA_VERSION + "]\n";
msg += "Please see " + Constants.AZUREUS_WIKI + "LikelyFaultySocketSelector for help.";
Debug.out(msg);
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_WARNING, msg));
consecutiveZeroSelects = 0;
listener.spinDetected();
return;
}
} else {
// under linux, it seems that selector spin is somewhat common, but normal??? behavior, so just sleep a bit
consecutiveZeroSelects = 0;
try {
Thread.sleep(50);
} catch (Throwable t) {
t.printStackTrace();
}
return;
}
}
if (consecutiveZeroSelects > SELECTOR_FAILURE_THRESHOLD) {
// should only happen under Windows + JRE 1.4
String msg = "Likely network disconnect/reconnect: Repairing socket channel selector. [JRE " + Constants.JAVA_VERSION + "]\n";
msg += "Please see " + Constants.AZUREUS_WIKI + "LikelyNetworkDisconnectReconnect for help.";
Debug.out(msg);
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_WARNING, msg));
consecutiveZeroSelects = 0;
listener.failureDetected();
return;
}
// not yet over the count threshold
}
use of com.biglybt.core.logging.LogAlert in project BiglyBT by BiglySoftware.
the class TCPConnectionManager method addNewRequest.
private void addNewRequest(final ConnectionRequest request) {
request.setConnectTimeout(request.listener.connectAttemptStarted(request.getConnectTimeout()));
boolean ipv6problem = false;
boolean bind_failed = false;
try {
request.channel = SocketChannel.open();
InetAddress bindIP = null;
try {
// advanced socket options
if (rcv_size > 0) {
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "Setting socket receive buffer size" + " for outgoing connection [" + request.address + "] to: " + rcv_size));
request.channel.socket().setReceiveBufferSize(rcv_size);
}
if (snd_size > 0) {
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "Setting socket send buffer size " + "for outgoing connection [" + request.address + "] to: " + snd_size));
request.channel.socket().setSendBufferSize(snd_size);
}
if (ip_tos.length() > 0) {
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "Setting socket TOS field " + "for outgoing connection [" + request.address + "] to: " + ip_tos));
request.channel.socket().setTrafficClass(Integer.decode(ip_tos).intValue());
}
if (local_bind_port > 0) {
request.channel.socket().setReuseAddress(true);
}
try {
bindIP = NetworkAdmin.getSingleton().getMultiHomedOutgoingRoundRobinBindAddress(request.address.getAddress());
if (bindIP != null) {
if (bindIP.isAnyLocalAddress() || !AEProxyFactory.isPluginProxy(request.address)) {
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "Binding outgoing connection [" + request.address + "] to local IP address: " + bindIP + ":" + local_bind_port));
request.channel.socket().bind(new InetSocketAddress(bindIP, local_bind_port));
}
} else if (local_bind_port > 0) {
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "Binding outgoing connection [" + request.address + "] to local port #: " + local_bind_port));
request.channel.socket().bind(new InetSocketAddress(local_bind_port));
}
} catch (SocketException e) {
bind_failed = true;
String msg = e.getMessage().toLowerCase();
if ((msg.contains("address family not supported by protocol family") || msg.contains("protocol family unavailable")) && !NetworkAdmin.getSingleton().hasIPV6Potential(true)) {
ipv6problem = true;
}
throw e;
}
} catch (Throwable t) {
if (bind_failed && NetworkAdmin.getSingleton().mustBind()) {
throw (t);
} else if (!ipv6problem) {
// dont pass the exception outwards, so we will continue processing connection without advanced options set
String msg = "Error while processing advanced socket options (rcv=" + rcv_size + ", snd=" + snd_size + ", tos=" + ip_tos + ", port=" + local_bind_port + ", bind=" + bindIP + ")";
// Debug.out( msg, t );
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, msg, t));
} else {
throw (t);
}
}
request.channel.configureBlocking(false);
request.connect_start_time = SystemTime.getMonotonousTime();
if (request.channel.connect(request.address)) {
// already connected
finishConnect(request);
} else {
try {
new_canceled_mon.enter();
pending_attempts.put(request, null);
} finally {
new_canceled_mon.exit();
}
connect_selector.register(request.channel, new VirtualChannelSelector.VirtualSelectorListener() {
@Override
public boolean selectSuccess(VirtualChannelSelector selector, SocketChannel sc, Object attachment) {
try {
new_canceled_mon.enter();
pending_attempts.remove(request);
} finally {
new_canceled_mon.exit();
}
finishConnect(request);
return true;
}
@Override
public void selectFailure(VirtualChannelSelector selector, SocketChannel sc, Object attachment, Throwable msg) {
try {
new_canceled_mon.enter();
pending_attempts.remove(request);
} finally {
new_canceled_mon.exit();
}
closeConnection(request.channel);
request.listener.connectFailure(msg);
}
}, null);
}
} catch (Throwable t) {
String full = request.address.toString();
String hostname = request.address.getHostName();
int port = request.address.getPort();
boolean unresolved = request.address.isUnresolved();
InetAddress inet_address = request.address.getAddress();
String full_sub = inet_address == null ? request.address.toString() : inet_address.toString();
String host_address = inet_address == null ? request.address.toString() : inet_address.getHostAddress();
String msg = "ConnectDisconnectManager::address exception: full=" + full + ", hostname=" + hostname + ", port=" + port + ", unresolved=" + unresolved + ", full_sub=" + full_sub + ", host_address=" + host_address;
if (request.channel != null) {
String channel = request.channel.toString();
Socket socket = request.channel.socket();
String socket_string = socket.toString();
InetAddress local_address = socket.getLocalAddress();
String local_address_string = local_address == null ? "<null>" : local_address.toString();
int local_port = socket.getLocalPort();
SocketAddress ra = socket.getRemoteSocketAddress();
String remote_address;
if (ra != null)
remote_address = ra.toString();
else
remote_address = "<null>";
int remote_port = socket.getPort();
msg += "\n channel=" + channel + ", socket=" + socket_string + ", local_address=" + local_address_string + ", local_port=" + local_port + ", remote_address=" + remote_address + ", remote_port=" + remote_port;
} else {
msg += "\n channel=<null>";
}
if (ipv6problem || t instanceof UnresolvedAddressException || t instanceof NoRouteToHostException) {
Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING, msg));
} else {
Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR, msg, t));
}
if (request.channel != null) {
closeConnection(request.channel);
}
request.listener.connectFailure(t);
}
}
use of com.biglybt.core.logging.LogAlert in project BiglyBT by BiglySoftware.
the class VirtualBlockingServerChannelSelector method start.
/**
* Start the server and begin accepting incoming connections.
*/
@Override
public void start() {
try {
this_mon.enter();
if (!isRunning()) {
try {
server_channel = ServerSocketChannel.open();
server_channel.socket().setReuseAddress(true);
if (receive_buffer_size > 0)
server_channel.socket().setReceiveBufferSize(receive_buffer_size);
server_channel.socket().bind(bind_address, 1024);
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "TCP incoming server socket " + bind_address));
AEThread accept_thread = new AEThread("VServerSelector:port" + bind_address.getPort()) {
@Override
public void runSupport() {
accept_loop();
}
};
accept_thread.setDaemon(true);
accept_thread.start();
} catch (Throwable t) {
Debug.out(t);
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, "ERROR, unable to bind TCP incoming server socket to " + bind_address.getPort(), t));
}
// init to now
last_accept_time = SystemTime.getCurrentTime();
}
} finally {
this_mon.exit();
}
}
use of com.biglybt.core.logging.LogAlert in project BiglyBT by BiglySoftware.
the class VirtualNonBlockingServerChannelSelector method start.
/**
* Start the server and begin accepting incoming connections.
*/
@Override
public void start() {
try {
this_mon.enter();
if (!isRunning()) {
for (int i = start_port; i < start_port + num_ports; i++) {
try {
final ServerSocketChannel server_channel = ServerSocketChannel.open();
server_channels.add(server_channel);
server_channel.socket().setReuseAddress(true);
if (receive_buffer_size > 0)
server_channel.socket().setReceiveBufferSize(receive_buffer_size);
server_channel.socket().bind(new InetSocketAddress(bind_address, i), 1024);
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "TCP incoming server socket " + bind_address));
server_channel.configureBlocking(false);
VirtualAcceptSelector.getSingleton().register(server_channel, new VirtualAcceptSelector.AcceptListener() {
@Override
public void newConnectionAccepted(SocketChannel channel) {
last_accept_time = SystemTime.getCurrentTime();
listener.newConnectionAccepted(server_channel, channel);
}
});
} catch (Throwable t) {
Debug.out(t);
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, "ERROR, unable to bind TCP incoming server socket to " + i, t));
}
}
// init to now
last_accept_time = SystemTime.getCurrentTime();
}
} finally {
this_mon.exit();
}
}
use of com.biglybt.core.logging.LogAlert in project BiglyBT by BiglySoftware.
the class TRTrackerServerUDP method recvLoop.
protected void recvLoop(DatagramSocket socket, InetSocketAddress address) {
long successful_accepts = 0;
long failed_accepts = 0;
while (!closed) {
try {
byte[] buf = new byte[PRUDPPacket.MAX_PACKET_SIZE];
DatagramPacket packet = new DatagramPacket(buf, buf.length, address);
socket.receive(packet);
successful_accepts++;
failed_accepts = 0;
String ip = packet.getAddress().getHostAddress();
if (!ip_filter.isInRange(ip, "Tracker", null)) {
thread_pool.run(new TRTrackerServerProcessorUDP(this, socket, packet));
}
} catch (Throwable e) {
if (!closed) {
failed_accepts++;
Logger.log(new LogEvent(LOGID, "TRTrackerServer: receive failed on port " + port, e));
if ((failed_accepts > 100 && successful_accepts == 0) || failed_accepts > 1000) {
// looks like its not going to work...
// some kind of socket problem
Logger.logTextResource(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_ERROR, "Network.alert.acceptfail"), new String[] { "" + port, "UDP" });
break;
}
}
}
}
}
Aggregations