Created
October 3, 2012 18:21
-
-
Save arianvp/3828767 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.net.InetSocketAddress; | |
import java.nio.ByteBuffer; | |
import java.nio.channels.SelectionKey; | |
import java.nio.channels.Selector; | |
import java.nio.channels.ServerSocketChannel; | |
import java.nio.channels.SocketChannel; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.ScheduledExecutorService; | |
import java.util.concurrent.TimeUnit; | |
public class Server implements Runnable { | |
private Selector selector; | |
private ServerSocketChannel serverSocketChannel; | |
private ScheduledExecutorService scheduledExecutorService; | |
private InetSocketAddress inetSocketAddress; | |
public Server() throws IOException { | |
selector = Selector.open(); | |
serverSocketChannel = ServerSocketChannel.open(); | |
scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); | |
inetSocketAddress = new InetSocketAddress("localhost", 43594); | |
} | |
@Override | |
public void run() { | |
try { | |
serverSocketChannel.configureBlocking(false); | |
serverSocketChannel.socket().bind(inetSocketAddress); | |
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); | |
scheduledExecutorService.scheduleAtFixedRate(new Runnable() { | |
private final ByteBuffer receivedData = ByteBuffer | |
.allocateDirect(512); | |
@Override | |
public void run() { | |
try { | |
selector.selectNow(); | |
for (SelectionKey selectionKey : selector | |
.selectedKeys()) { | |
if (selectionKey.isAcceptable()) { | |
onAccept(); | |
} | |
if (selectionKey.isReadable()) { | |
try { | |
SocketChannel channel = (SocketChannel) selectionKey | |
.channel(); | |
if ((channel).read(receivedData) == -1) { | |
((Player)selectionKey.attachment()).disconnect(); | |
} | |
receivedData.flip(); | |
if (receivedData.hasRemaining()) { | |
((Client) selectionKey.attachment()) | |
.onData(StreamBuffer.newInBuffer(receivedData)); | |
} | |
receivedData.clear(); | |
} catch (IOException ioe) { | |
((Player)selectionKey.attachment()).disconnect(); | |
} | |
} | |
} | |
} catch (Exception e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
World.getWorld().update(); | |
} | |
private void onAccept() throws IOException { | |
SocketChannel socketChannel; | |
do { | |
socketChannel = serverSocketChannel.accept(); | |
if(socketChannel == null) { | |
break; | |
} | |
socketChannel.configureBlocking(false); | |
SelectionKey selectionKey = socketChannel.register( | |
selector, SelectionKey.OP_READ); | |
Client client = new Player(selectionKey); | |
World.getWorld().submitPlayer((Player)client); | |
selectionKey.attach(client); | |
} while (socketChannel != null); | |
} | |
}, 0, 600, TimeUnit.MILLISECONDS); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
public static void main(String[] args) { | |
try { | |
new Server().run(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment