public TRTrackerServerUDP(String _name, int _port, boolean _start_up_ready) { super(_name, _start_up_ready); port = _port; thread_pool = new ThreadPool("TrackerServer:UDP:" + port, THREAD_POOL_SIZE); try { InetAddress bind_ip = NetworkAdmin.getSingleton().getSingleHomedServiceBindAddress(); InetSocketAddress address; DatagramSocket socket; if (bind_ip == null) { address = new InetSocketAddress(InetAddress.getByName("127.0.0.1"), port); socket = new DatagramSocket(port); } else { current_bind_ip = bind_ip; address = new InetSocketAddress(bind_ip, port); socket = new DatagramSocket(address); } socket.setReuseAddress(true); dg_socket = socket; final InetSocketAddress f_address = address; Thread recv_thread = new AEThread("TRTrackerServerUDP:recv.loop") { public void runSupport() { recvLoop(dg_socket, f_address); } }; recv_thread.setDaemon(true); recv_thread.start(); Logger.log(new LogEvent(LOGID, "TRTrackerServerUDP: recv established on port " + port)); } catch (Throwable e) { Logger.log( new LogEvent( LOGID, "TRTrackerServerUDP: " + "DatagramSocket bind failed on port " + port, e)); } }
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; } } } } }