码迷,mamicode.com
首页 > 其他好文 > 详细

MINA的使用

时间:2015-05-26 15:45:45      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

Apache Mina Server 是一个网络通信应用框架,也就是说,它主要是对基于TCP/IP、UDP/IP协议栈的通信框架,Mina 可以帮助我们快速开发高性能、高扩展性的网络通信应用,Mina 提供了事件驱动、异步(Mina 的异步IO 默认使用的是JAVA NIO 作为底层支持)操作的编程模型。

 

Server 

IoAcceptor acceptor=new NioSocketAcceptor();
acceptor.getSessionConfig().setReadBufferSize(2048);
acceptor.getSessionConfig.setIdleTime(IdleStatus.BOTH_IDLE,10);
acceptor.bind(new InetSocketAddress(9123));

CLIENT 端 发起连接
IoConnector connector = new NioSocketConnector();
ConnectFuture connectFuture;
try {
  this.connector.getSessionConfig().setReadBufferSize(1024);
this.connector.setHandler(this.innerHandler);
this.connector.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE,
8);
// InetSocketAddress address = new InetSocketAddress(
// tempIpAddress.getIp(), tempIpAddress.getPort());
InetSocketAddress address = new InetSocketAddress(testIp, testPort);

connector.getFilterChain().addLast("codec",
new ProtocolCodecFilter(new FrameCoderFactory()));

connector.setConnectTimeoutMillis(8000);
connectFuture = connector.connect(address);

   
connectFuture.addListener(new ConnectListener());

} catch (Exception e) {
connector.dispose();
connector = null;
}

 

IoFutureListener

new IoFutureListener<ConnectFuture>() {
@Override
public void operationComplete(ConnectFuture future) {
if (future.isConnected()) {
IoSession session = future.getSession();

}else{

 //若在指定时间内没连接成功

}
}
}

 FrameDecoder

 public class FrameDecoder extends CumulativeProtocolDecoder {

private static byte _Header = (byte)0xcb;

private static byte _Tailer = (byte)0xfb;

@Override
protected synchronized boolean doDecode(IoSession session, IoBuffer in,
ProtocolDecoderOutput out) throws Exception {
// TODO Auto-generated method stub

if(in.remaining() > 0){

long lframeLen = 0;
int iframeLen = 0;
byte[] frameData = null;

while(in.remaining() > 0){
in.mark();
if(in.get() == _Header){

if(in.remaining() > 4){

lframeLen = in.getUnsignedInt();

if(lframeLen < 5 || lframeLen > Integer.MAX_VALUE){
in.reset();
in.get();
return true;
}

iframeLen = (int)lframeLen;

if(in.remaining() >= (iframeLen - 5)){

frameData = new byte[iframeLen];
in.reset();
in.mark();
in.get(frameData, 0, iframeLen);

if(frameData[iframeLen - 1] != _Tailer){
in.reset();
in.get();
return true;
}

com.gq.protocols.uniITP.frames.AbstractFrame baseFrame = com.gq.protocols.uniITP.FrameHelper.BuildFrame(IoBuffer.wrap(frameData), 0, frameData.length);

if(baseFrame != null){
out.write(baseFrame);
}

if(in.remaining() > 0){
return true;
}else{
return false;
}

}else{

in.reset();
return false;
}

}else{
in.reset();
return false;
}
}
}

}

return false;
}

}

FrameEncoder

 public class FrameEncoder extends ProtocolEncoderAdapter {

@Override
public void encode(IoSession session, Object frame, ProtocolEncoderOutput out)
throws Exception {
// TODO Auto-generated method stub

if(frame instanceof IoBuffer){

out.write(frame);

}else{

com.gq.protocols.uniITP.frames.AbstractFrame baseFrame = (com.gq.protocols.uniITP.frames.AbstractFrame)frame;

IoBuffer buffer = baseFrame.ToBianryData();

out.write(buffer);
}
}

}



 

MINA的使用

标签:

原文地址:http://www.cnblogs.com/outMCat/p/4530517.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!