package com.xy.net.mina.protocol;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.AttributeKey;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolDecoderException;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
import org.apache.mina.filter.codec.RecoverableProtocolDecoderException;
import org.apache.mina.filter.codec.textline.LineDelimiter;
public class MinaDecoder implements ProtocolDecoder{
private final AttributeKey CONTEXT = new AttributeKey(getClass(), "context");
private final Charset charset;
/** The default maximum Line length. Default to 1024. */
private int maxLineLength = 1024;
/** The default maximum buffer length. Default to 128 chars. */
private int bufferLength = 128;
/**
* Creates a new instance with the spcified <tt>charset</tt>
* and {@link LineDelimiter#AUTO} delimiter.
*/
public MinaDecoder(Charset charset) {
if (charset == null) {
throw new IllegalArgumentException("charset parameter shuld not be null");
}
this.charset = charset;
}
/**
* Returns the allowed maximum size of the line to be decoded.
* If the size of the line to be decoded exceeds this value, the
* decoder will throw a {@link BufferDataException}. The default
* value is <tt>1024</tt> (1KB).
*/
public int getMaxLineLength() {
return maxLineLength;
}
/**
* Sets the allowed maximum size of the line to be decoded.
* If the size of the line to be decoded exceeds this value, the
* decoder will throw a {@link BufferDataException}. The default
* value is <tt>1024</tt> (1KB).
*/
public void setMaxLineLength(int maxLineLength) {
if (maxLineLength <= 0) {
throw new IllegalArgumentException("maxLineLength ("
+ maxLineLength + ") should be a positive value");
}
this.maxLineLength = maxLineLength;
}
/**
* Sets the default buffer size. This buffer is used in the Context
* to store the decoded line.
*
* @param bufferLength The default bufer size
*/
public void setBufferLength(int bufferLength) {
if ( bufferLength <= 0) {
throw new IllegalArgumentException("bufferLength ("
+ maxLineLength + ") should be a positive value");
}
this.bufferLength = bufferLength;
}
/**
* Returns the allowed buffer size used to store the decoded line
* in the Context instance.
*/
public int getBufferLength() {
return bufferLength;
}
/**
* {@inheritDoc}
*/
public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
Context ctx = getContext(session);
decodeAuto(ctx, session, in, out);
}
/**
* Return the context for this session
*/
private Context getContext(IoSession session) {
Context ctx;
ctx = (Context) session.getAttribute(CONTEXT);
if (ctx == null) {
ctx = new Context(bufferLength);
session.setAttribute(CONTEXT, ctx);
}
return ctx;
}
/**
* {@inheritDoc}
*/
public void finishDecode(IoSession session, ProtocolDecoderOutput out)
throws Exception {
// Do nothing
}
/**
* {@inheritDoc}
*/
public void dispose(IoSession session) throws Exception {
Context ctx = (Context) session.getAttribute(CONTEXT);
if (ctx != null) {
session.removeAttribute(CONTEXT);
}
}
/**
* Decode a line using the default delimiter on the current system
*/
private void decodeAuto(Context ctx, IoSession session, IoBuffer in, ProtocolDecoderOutput out)
throws CharacterCodingException, ProtocolDecoderException {
int matchCount = ctx.getMatchCount();
// Try to find a match
int oldPos = in.position();
int oldLimit = in.limit();
System.out.print("input string: ");
while (in.hasRemaining()) {
byte b = in.get();
boolean matched = false;
switch (b) {
case '\r':
// Might be Mac, but we don't auto-detect Mac EOL
// to avoid confusion.
matchCount++;
//matched = true;
break;
/* case '\n':
// UNIX
matchCount++;
matched = true;
break;*/
case '\0':
// UNIX
matchCount++;
matched = true;
break;
default:
System.out.print((char)b);
matchCount = 0;
}
if (matched) {
// Found a match.
System.out.println();
int pos = in.position();
in.limit(pos);
in.position(oldPos);
ctx.append(in);
in.limit(oldLimit);
in.position(pos);
if (ctx.getOverflowPosition() == 0) {
IoBuffer buf = ctx.getBuffer();
buf.flip();
buf.limit(buf.limit() - matchCount);
try {
writeText(session, buf.getString(ctx.getDecoder()), out);
} finally {
buf.clear();
}
} else {
int overflowPosition = ctx.getOverflowPosition();
ctx.reset();
throw new RecoverableProtocolDecoderException(
"Line is too long: " + overflowPosition);
}
oldPos = pos;
matchCount = 0;
}
}
// Put remainder to buf.
in.position(oldPos);
ctx.append(in);
ctx.setMatchCount(matchCount);
}
/**
* By default, this method propagates the decoded line of text to
* {@code ProtocolDecoderOutput#write(Object)}. You may override this method to modify
* the default behavior.
*
* @param session the {@code IoSession} the received data.
* @param text the decoded text
* @param out the upstream {@code ProtocolDecoderOutput}.
*/
protected void writeText(IoSession session, String text, ProtocolDecoderOutput out) {
out.write(text);
}
/**
* A Context used during the decoding of a lin. It stores the decoder,
* the temporary buffer containing the decoded line, and other status flags.
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
* @version $Rev: 936203 $, $Date: 2010-04-21 10:49:20 +0200 (mer. 21 avril 2010) $
*/
private class Context {
/** The decoder */
private final CharsetDecoder decoder;
/** The temporary buffer containing the decoded line */
private final IoBuffer buf;
/** The number of lines found so far */
private int matchCount = 0;
/** A counter to signal that the line is too long */
private int overflowPosition = 0;
/** Create
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
说明:该服务器框架试用于需要频繁修改数据库内容的游戏,没有使用数据库缓存,jdbc直接修改数据库内容。 申明: 我是个新手,有出错的地方莫怪哈,把源码发布出来更多的是想大伙帮忙改进下框架,改进的代码和新版本希望能够发到我的邮箱:xiamiy01@gmail.com,我会把新的修改和新版本发布出来,为大家服务。 文件夹简介: common:文件夹主要放通用文件, common.struct:自定义结构体 common.struct.constants:自定义常量 db:内存数据库(主要是一些不变的,需要频繁获取的数据) db.dao:顾名思义啦,Database Access Object db.jdbc:jdbc连接池 game:开始游戏 game.message:消息管理者,将客户端的消息传到具体的处理器处理 game.message.handler:消息处理者(功能实现的地方,上接service,下连DAO) net.mina.protocol:自定义通信协议 net.mina.service:网络层(MINA框架,这个不多说,你懂的)
资源推荐
资源详情
资源评论
收起资源包目录
MinaServer.rar (31个子文件)
MinaServer
resource
messageType.ini 63B
system.ini 117B
src
com
xy
common
struct
constants
MessageType.java 2KB
RoleAttribute.java 1KB
Tools.java 3KB
db
dao
EquipmentDAO.java 2KB
NullSQLHandleException.java 267B
RoleDAO.java 1KB
jdbc
ConnectionPool.java 7KB
ConnectionPoolManager.java 897B
DBManager.java 272B
RoleAttributeDB.java 697B
net
mina
service
MinaSessionHandler.java 3KB
MinaNetworkManager.java 2KB
protocol
MinaEncoder.java 3KB
MinaCodecFactory.java 857B
MinaDecoder.java 9KB
game
message
MessageManager.java 2KB
handler
RoleHandler.java 768B
AbstractHandler.java 505B
StartGame.java 2KB
jars_external
mina-core-2.0.0-M3.jar 517KB
slf4j-simple-1.4.3.jar 7KB
json
ezmorph-1.0.2.jar 76KB
commons-collections-3.2.jar 558KB
commons-beanutils-1.7.0.jar 184KB
commons-lang-2.1.jar 203KB
json-lib-2.4-jdk15.jar 155KB
commons-logging-1.1.3.jar 61KB
slf4j-api-1.4.3.jar 15KB
mysql-connector-java-5.1.25-bin.jar 829KB
共 31 条
- 1
Xiamiy
- 粉丝: 5
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页