⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nioeventreader.java

📁 一个MMORPG手机游戏的服务器端程序源代码
💻 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 *//**对应SERVER的客户端    ,调试用的*/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();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -