?? nioeventreader.java
字號:
package zsw_mmorpg.client;import zsw_mmorpg.common.*;import java.nio.*;import java.nio.channels.*;import java.util.*;import java.io.*;import org.apache.log4j.*;/** * NIOEventReader.java * * Reads GameEvents from the server. * * @author <a href="mailto:shiwei@raymobile.com">朱世偉</a> * @version 1.0 *//**對應(yīng)SERVER的客戶端 ,調(diào)試用的*/public class NIOEventReader extends Thread { private Logger log = Logger.getLogger(NIOEventReader.class); GameClient gameClient; private SocketChannel channel; private EventQueue queue; private Selector selector; private boolean running; public NIOEventReader(GameClient gc, SocketChannel channel, EventQueue queue) { super("NIOEventReader"); this.gameClient = gc; this.queue = queue; this.channel = channel; } public void run() { try { selector = Selector.open(); channel.register(selector, SelectionKey.OP_READ, new Attachment()); } catch (ClosedChannelException cce) { log.error("closedchannelexception while registering channel with selector", cce); return; } catch (IOException ioe) { log.error("ioexception while registering channel with selector", ioe); return; } running = true; while (running) { try { selector.select(); Set readyKeys = selector.selectedKeys(); Iterator i = readyKeys.iterator(); while (i.hasNext()) { SelectionKey key = (SelectionKey) i.next(); i.remove(); SocketChannel channel = (SocketChannel) key.channel(); Attachment attachment = (Attachment) key.attachment(); try { long nbytes = channel.read(attachment.readBuff); if (nbytes == -1) { channel.close(); GameEvent event = gameClient.createDisconnectEvent("end-of-stream"); queue.enQueue(event); } try { if (attachment.readBuff.position() >= attachment.HEADER_SIZE) { attachment.readBuff.flip(); while(attachment.eventReady()) { GameEvent event = getEvent(attachment); queue.enQueue(event); attachment.reset(); } attachment.readBuff.compact(); } } catch (IllegalArgumentException e) { log.error("illegalargument while parsing incoming event", e); } } catch (IOException ioe) { log.warn("IOException during read(), closing channel:" + channel.socket().getInetAddress()); channel.close(); } } } catch (IOException ioe2) { log.warn("error during select(): " + ioe2.getMessage()); } catch (Exception e) { log.error("exception during select()", e); } } } private GameEvent getEvent(Attachment attachment) { GameEvent event = null; ByteBuffer bb = ByteBuffer.wrap(attachment.payload); bb.order(ByteOrder.LITTLE_ENDIAN); event = gameClient.createGameEvent(); event.read(bb); return event; } public void shutdown() { running = false; // force the selector to unblock selector.wakeup(); }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -