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

📄 gameclient.java

📁 一个MMORPG手机游戏的服务器端程序源代码
💻 JAVA
字号:
package zsw_mmorpg.client;import zsw_mmorpg.common.*;import java.nio.*;import java.nio.channels.*;import java.net.*;import java.io.*;import org.apache.log4j.*;/** * GameClient.java * * * @author <a href="mailto:shiwei@raymobile.com">朱世伟</a> * @version 1.0 *//**对应SERVER的客户端    ,调试用的*/public abstract class GameClient extends Thread{    protected static Logger log = Logger.getLogger("GameClient");    protected InetAddress serverAddress;    protected SocketChannel channel;    protected EventQueue inQueue;    protected EventQueue outQueue;    protected NIOEventReader netReader;    protected ByteBuffer writeBuffer;    protected String playerId;    protected String opponentId;    protected boolean inGame = false;    protected boolean running = true;    public void init(String args[]) {	inQueue = new EventQueue("GameClient-in");	outQueue = new EventQueue("GameClient-out");	writeBuffer = ByteBuffer.allocate(Globals.MAX_EVENT_SIZE );    writeBuffer.order(ByteOrder.LITTLE_ENDIAN) ;    try {	    serverAddress = InetAddress.getByName(args[0]);	}	catch (UnknownHostException uhe) {	    log.error("unknown host: " + args[0]);	    System.exit(1);	}	this.playerId = args[1];	// connect to the server	if (!connect()) 	    System.exit(1);		// start our net reader	netReader = new NIOEventReader(this, channel, inQueue);	netReader.start();	    }    public void run() {	login();	// wait for LOGIN_ACK (hack)	threadSleep(200L);	// main loop	while(running) {	    processIncomingEvents();	    writeOutgoingEvents();	    threadSleep(50);	}    }    public abstract String getGameName();    public abstract GameEvent createGameEvent();    public abstract GameEvent createLoginEvent();    public abstract GameEvent createDisconnectEvent(String reason);    protected abstract void processIncomingEvents();    private void writeOutgoingEvents() {	GameEvent outEvent;	while (outQueue.size() > 0) {	    try {		outEvent = outQueue.deQueue();		writeEvent(outEvent);	    }	    catch (InterruptedException ie) {}	}	    }    protected boolean connect() {	log.info("connect()");	try {	    // open the socket channel	    channel = SocketChannel.open(new InetSocketAddress(serverAddress, Globals.PORT));	    channel.configureBlocking(false);	    channel.socket().setTcpNoDelay(true);	    return true;	}	catch (ConnectException ce) {	    log.error("Connect Exception: " + ce.getMessage());	    return false;	}	catch (Exception e) {	    log.error("Exception while connecting", e);	    return false;	}    }    protected void login() {	GameEvent e = createLoginEvent();	e.setGameName(getGameName());	e.setPlayerId(playerId);    e.setMessage(playerId +" 1 1");    writeEvent(e);    }    protected void shutdown() {	running = false;	netReader.shutdown();	//	consoleReader.shutdown();	try {	    channel.close();	}	catch (IOException ioe) {	    log.error("exception while closing channel", ioe);	}    }    protected void writeEvent(GameEvent ge) {	// set the gamename and player id	ge.setGameName(getGameName());	ge.setPlayerId(playerId);	NIOUtils.prepBuffer(ge, writeBuffer);	NIOUtils.channelWrite(channel, writeBuffer);    }       private void threadSleep(long time) {	try { 	    Thread.sleep(time); 	} 	catch(InterruptedException e) {}    }    public void stdOut(String str) {	if ((str != null) && !str.equals(""))	    System.out.println("\n" + str);	if (inGame)	    System.out.print( playerId + " vs. " + opponentId + " > " );	else	    System.out.print( playerId + " > " );    }    public void stdErr(String str) {	System.err.println("\n" + str);    }       public void setOpponent(String opp) {	opponentId = opp;    }}    

⌨️ 快捷键说明

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