Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void listen() throws IOException {
- long loopTime;
- long timer = System.currentTimeMillis();
- Log.log("Starting to listen for connections.", MessageState.ENGINE);
- while(state == ServerState.RUNNING) {
- loopTime = System.currentTimeMillis();
- if(System.currentTimeMillis() - timer > 10000) {
- Log.log("*************************************\nCurrently there are " + connectionCount + " connections!\n*************************************", MessageState.ENGINE);
- timer = System.currentTimeMillis();
- }
- selector.select(500);
- Iterator selectedKeys = selector.selectedKeys().iterator();
- while(selectedKeys.hasNext()) {
- SelectionKey key = (SelectionKey) selectedKeys.next();
- selectedKeys.remove();
- if(!key.isValid()) {
- continue;
- }
- if(key.isAcceptable()) {
- accept(key);
- connectionCount++;
- } else if(key.isReadable()) {
- read(key);
- }
- }
- loopTime = System.currentTimeMillis();
- long engineUsage = (loopTime - timer < 1000) ? (loopTime - timer) / 10 : 100;
- Log.log("Engine Usage: " + engineUsage + "%", MessageState.ENGINE);
- }
- serverSocket.close();
- Log.log("Server ended...", MessageState.ENGINE);
- }
- public void accept(SelectionKey key) throws IOException {
- ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();
- SocketChannel socketChannel = serverSocketChannel.accept();
- socketChannel.configureBlocking(false);
- socketChannel.register(selector, SelectionKey.OP_READ);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement