Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
增加了一个echoserver的例子
- Loading branch information
Showing
24 changed files
with
1,279 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/bin | ||
/target | ||
.settings/ | ||
.project | ||
.classpath |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.github.XGameEnginee</groupId> | ||
<artifactId>XGameEnginee</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>XGameEnginee</name> | ||
<packaging>jar</packaging> | ||
<description>游戏通讯和逻辑框架,通讯层基于Netty</description> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.googlecode.sli4j</groupId> | ||
<artifactId>sli4j-slf4j</artifactId> | ||
<version>2.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>log4j</groupId> | ||
<artifactId>log4j</artifactId> | ||
<version>1.2.16</version> | ||
<type>bundle</type> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.googlecode.sli4j</groupId> | ||
<artifactId>sli4j-slf4j-log4j</artifactId> | ||
<version>2.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.netty</groupId> | ||
<artifactId>netty</artifactId> | ||
<version>3.4.0.Final</version> | ||
</dependency> | ||
</dependencies> | ||
<url>https://github.com/cuixin/XGameEnginee</url> | ||
<organization> | ||
<name>The XGameEnginee Project</name> | ||
<url>https://github.com/cuixin/XGameEnginee</url> | ||
</organization> | ||
<scm> | ||
<url>https://github.com/cuixin/XGameEnginee</url> | ||
<connection>https://github.com/cuixin/XGameEnginee.git</connection> | ||
</scm> | ||
</project> |
68 changes: 68 additions & 0 deletions
68
src/main/com/github/xgameenginee/buffer/GameBufferFactory.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.github.xgameenginee.buffer; | ||
|
||
|
||
import org.jboss.netty.buffer.ChannelBuffer; | ||
import org.jboss.netty.buffer.ChannelBuffers; | ||
|
||
public final class GameBufferFactory { | ||
|
||
private static int readHeader; | ||
|
||
private static boolean hasReadSize; | ||
|
||
private static int writeHeader; | ||
|
||
private static boolean hasWriteSize; | ||
|
||
private GameBufferFactory(int readSize, boolean hasReader, int writeSize, boolean hasWriter) { | ||
if (!(readSize == 2 || readSize == 4)) { | ||
throw new IllegalArgumentException("readSize must be 2 or 4"); | ||
} | ||
|
||
if (!(readSize == 2 || readSize == 4)) { | ||
throw new IllegalArgumentException("writeSize must be 2 or 4"); | ||
} | ||
readHeader = readSize; | ||
hasReadSize = hasReader; | ||
writeHeader = writeSize; | ||
hasWriteSize = hasWriter; | ||
} | ||
|
||
public static int getReadHeaderSize() { | ||
return readHeader; | ||
} | ||
|
||
public static boolean isHeaderSize() { | ||
return hasReadSize; | ||
} | ||
|
||
public static int getWriteHeaderSize() { | ||
return writeHeader; | ||
} | ||
|
||
public static boolean isWriterSize() { | ||
return hasWriteSize; | ||
} | ||
|
||
private static int getWriterSize() { | ||
return hasWriteSize ? 2 : 0; | ||
} | ||
|
||
public static final ChannelBuffer getBuffer(final int capacity) { | ||
final ChannelBuffer buffer; | ||
buffer = ChannelBuffers.buffer(writeHeader + capacity); | ||
if (writeHeader == 2) | ||
buffer.writeShort(capacity + getWriterSize()); | ||
else | ||
buffer.writeInt(capacity + getWriterSize()); | ||
return buffer; | ||
} | ||
|
||
private static GameBufferFactory instance; | ||
|
||
public synchronized static final GameBufferFactory setupGameBuffer(int readSize, boolean hasReader, int writeSize, boolean hasWriter) { | ||
if (instance == null) | ||
instance = new GameBufferFactory(readSize, hasReader, writeSize, hasWriter); | ||
return instance; | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
src/main/com/github/xgameenginee/buffer/GameDownBuffer.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.github.xgameenginee.buffer; | ||
import java.io.UnsupportedEncodingException; | ||
import java.nio.ByteBuffer; | ||
|
||
import org.jboss.netty.buffer.ChannelBuffer; | ||
import org.jboss.netty.buffer.ChannelBuffers; | ||
|
||
/** | ||
* 游戏下行消息 | ||
* @author Steven | ||
* | ||
*/ | ||
public class GameDownBuffer { | ||
private ChannelBuffer buffer; | ||
|
||
private GameDownBuffer(ChannelBuffer buffer) { | ||
this.buffer = buffer; | ||
} | ||
|
||
public void putShort(short value) { | ||
buffer.writeShort(value); | ||
} | ||
|
||
public void putInt(int value) { | ||
buffer.writeInt(value); | ||
} | ||
|
||
public void put(byte value) { | ||
buffer.writeByte(value); | ||
} | ||
|
||
public void put(byte[] src) { | ||
buffer.writeBytes(src); | ||
} | ||
|
||
public void put(byte[] src, int srcIndex, int length) { | ||
buffer.writeBytes(src, srcIndex, length); | ||
} | ||
|
||
public void putLong(long value) { | ||
buffer.writeLong(value); | ||
} | ||
|
||
public void putUTFString(String src) { | ||
if (src == null) | ||
src = ""; | ||
byte[] strData; | ||
try { | ||
strData = src.getBytes("UTF-8"); | ||
} catch (UnsupportedEncodingException e1) { | ||
strData = src.getBytes(); | ||
e1.printStackTrace(); | ||
} | ||
buffer.writeShort(strData.length); | ||
buffer.writeBytes(strData); | ||
} | ||
|
||
public void putString(String src, String encode) throws UnsupportedEncodingException { | ||
if (src == null) | ||
src = ""; | ||
byte[] strData = src.getBytes(encode); | ||
buffer.writeShort(strData.length); | ||
buffer.writeBytes(strData); | ||
} | ||
|
||
public int remaining() { | ||
return buffer.writableBytes(); | ||
} | ||
|
||
public boolean hasRemain() { | ||
return buffer.writableBytes() > 0; | ||
} | ||
|
||
public static final GameDownBuffer allocat(int capacity) { | ||
return new GameDownBuffer(GameBufferFactory.getBuffer(capacity)); | ||
} | ||
|
||
public static final GameDownBuffer wrappedBuffer(ByteBuffer buffer) { | ||
return new GameDownBuffer(ChannelBuffers.wrappedBuffer(buffer)); | ||
} | ||
|
||
public static final GameDownBuffer wrappedBuffer(byte[] buffer) { | ||
return new GameDownBuffer(ChannelBuffers.wrappedBuffer(buffer)); | ||
} | ||
|
||
public static final GameDownBuffer allocat(short gameType, int capacity) { | ||
GameDownBuffer gb = new GameDownBuffer(GameBufferFactory.getBuffer(capacity + 2)); | ||
gb.putShort(gameType); | ||
return gb; | ||
} | ||
|
||
public ChannelBuffer getChannelBuffer() { | ||
return this.buffer; | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.github.xgameenginee.buffer; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
|
||
import org.jboss.netty.buffer.ChannelBuffer; | ||
|
||
import com.github.xgameenginee.core.Connection; | ||
|
||
/** | ||
* 游戏上行消息 | ||
* @author Steven | ||
* | ||
*/ | ||
public class GameUpBuffer { | ||
|
||
private ChannelBuffer buffer; | ||
|
||
private Connection connection; | ||
|
||
public Connection getConnection() { | ||
return connection; | ||
} | ||
|
||
public GameUpBuffer(ChannelBuffer buffer, Connection connection) { | ||
this.connection = connection; | ||
this.buffer = buffer; | ||
} | ||
|
||
public short getShort() { | ||
return buffer.readShort(); | ||
} | ||
|
||
public int getInt() { | ||
return buffer.readInt(); | ||
} | ||
|
||
public byte get() { | ||
return buffer.readByte(); | ||
} | ||
|
||
public void get(byte[] dst) { | ||
buffer.readBytes(dst); | ||
} | ||
|
||
public void get(byte[] dst, int dstIndex, int length) { | ||
buffer.readBytes(dst, dstIndex, length); | ||
} | ||
|
||
public long getLong() { | ||
return buffer.readLong(); | ||
} | ||
|
||
public String getUTFString() { | ||
int len = buffer.readShort(); | ||
if (len <= 0) | ||
throw new IllegalArgumentException("string length cannot less than zero"); | ||
byte[] strData = new byte[len]; | ||
String result = null; | ||
try { | ||
result = new String(strData, "UTF-8"); | ||
} catch (UnsupportedEncodingException e) { | ||
result = new String(strData); | ||
e.printStackTrace(); | ||
} | ||
return result; | ||
} | ||
|
||
public String getString(String encode) throws UnsupportedEncodingException { | ||
int len = buffer.readShort(); | ||
if (len <= 0) | ||
throw new IllegalArgumentException("string length cannot less than zero"); | ||
byte[] strData = new byte[len]; | ||
String result = new String(strData, encode); | ||
return result; | ||
} | ||
|
||
public int remaining() { | ||
return buffer.readableBytes(); | ||
} | ||
|
||
public boolean hasRemain() { | ||
return buffer.readableBytes() > 0; | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.github.xgameenginee.channel; | ||
|
||
import org.jboss.netty.channel.group.DefaultChannelGroup; | ||
|
||
import com.github.xgameenginee.buffer.GameDownBuffer; | ||
import com.github.xgameenginee.core.Connection; | ||
|
||
public class GameChannel { | ||
private DefaultChannelGroup channel; | ||
|
||
public GameChannel(String name) { | ||
channel = new DefaultChannelGroup(name); | ||
} | ||
|
||
public void addConnection(Connection c) { | ||
channel.add(c.getChannelHandlerContext().getChannel()); | ||
} | ||
|
||
public void removeConnection(Connection c) { | ||
channel.remove(c.getChannelHandlerContext().getChannel()); | ||
} | ||
|
||
public void broaddcast(Connection except, GameDownBuffer buffer) { | ||
channel.remove(except.getChannelHandlerContext().getChannel()); | ||
channel.write(buffer); | ||
channel.add(except.getChannelHandlerContext().getChannel()); | ||
} | ||
|
||
public void broadcast(GameDownBuffer buffer) { | ||
channel.write(buffer); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/com/github/xgameenginee/channel/GameChannelFactory.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.github.xgameenginee.channel; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class GameChannelFactory { | ||
|
||
private Map<String, GameChannel> channelMap = new HashMap<String, GameChannel>(); | ||
|
||
private static final GameChannelFactory instance = new GameChannelFactory(); | ||
|
||
private GameChannel all = new GameChannel("All"); | ||
|
||
private GameChannelFactory() { | ||
} | ||
|
||
public static final GameChannelFactory getInstance() { | ||
return instance; | ||
} | ||
|
||
public GameChannel getAllChannel() { | ||
return all; | ||
} | ||
|
||
public GameChannel getChannel(String name) { | ||
synchronized(channelMap) { | ||
return channelMap.get(name); | ||
} | ||
} | ||
|
||
public GameChannel newChannel(String name) { | ||
synchronized (channelMap) { | ||
GameChannel channel = channelMap.get(name); | ||
if (channel != null) | ||
return channel; | ||
else { | ||
channel = new GameChannel(name); | ||
channelMap.put(name, channel); | ||
return channel; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.