/** Stop the server. */ public void stop() { try { this_mon.enter(); if (server_channel != null) { try { server_channel.close(); server_channel = null; } catch (Throwable t) { Debug.out(t); } } } finally { this_mon.exit(); } }
protected void accept_loop() { while (isRunning()) { try { SocketChannel client_channel = server_channel.accept(); last_accept_time = SystemTime.getCurrentTime(); client_channel.configureBlocking(false); listener.newConnectionAccepted(server_channel, client_channel); } catch (AsynchronousCloseException e) { /* is thrown when stop() is called */ } catch (Throwable t) { Debug.out(t); try { Thread.sleep(500); } catch (Exception e) { e.printStackTrace(); } } } }
/** Start the server and begin accepting incoming connections. */ 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()) { 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)); } last_accept_time = SystemTime.getCurrentTime(); // init to now } } finally { this_mon.exit(); } }