📄 zfserverhandler.java
字号:
package com.frontMachine.server;
import java.io.IOException;
import org.apache.log4j.Logger;
import org.apache.mina.common.ByteBuffer;
import org.apache.mina.common.IdleStatus;
import org.apache.mina.common.IoFilter;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;
import org.apache.mina.common.IoSessionConfig;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.ProtocolDecoderException;
import org.apache.mina.transport.socket.nio.SocketSessionConfig;
import com.frontMachine.client.ToQFSocketManager;
import com.frontMachine.codec.ZFCodecFactory;
import com.frontMachine.setting.RemoteHost;
import com.frontMachine.setting.ServerSetting;
import com.frontMachine.util.ErrMsg;
import com.frontMachine.util.ExceptionLoger;
import com.frontMachine.util.MACMan;
import com.frontMachine.util.StrUtil;
/**
* @转发服务事件驱动类
* @author LRT
* @version 1.0
*/
public class ZFServerHandler extends IoHandlerAdapter
{
private static Logger loger = Logger.getLogger(ZFServerHandler.class);
private static IoFilter CODEC_FILTER = new ProtocolCodecFilter(new ZFCodecFactory());
private static RemoteHost qfHost=(RemoteHost)ServerSetting.getInstance().getRemoteHostMap().get("QFSYS");
public ZFServerHandler()
{
}
public void sessionCreated(IoSession session) throws Exception
{
session.getFilterChain().addLast("codec", CODEC_FILTER);
IoSessionConfig cfg = session.getConfig();
if (cfg instanceof SocketSessionConfig)
{
((SocketSessionConfig) cfg).setReceiveBufferSize(ServerSetting.getInstance().getReceiveBufferSize());
((SocketSessionConfig) cfg).setKeepAlive(true);
//((SocketSessionConfig) cfg).setSoLinger(true, 0);
((SocketSessionConfig) cfg).setTcpNoDelay(true);
}
}
public void messageReceived(IoSession session, Object message) throws Exception
{
String strIn=message.toString();
loger.info("["+session.getRemoteAddress()+"]"+strIn.substring(4));
byte[] rtnMsg = ToQFSocketManager.send(qfHost,strIn);
loger.info("["+session.getRemoteAddress()+"]"+new String(rtnMsg));
session.write(ByteBuffer.wrap(rtnMsg));
//loger.info("return Msg:"+rtnMsg);
session.close();
}
public void messageSent(IoSession session, Object message) throws Exception
{
}
public void sessionOpened(IoSession session) throws Exception
{
session.setIdleTime( IdleStatus.BOTH_IDLE, ServerSetting.getInstance().getIdleTime() );
}
public void exceptionCaught(IoSession session, Throwable exp) throws Exception
{
//ExceptionLoger.ExLog("S-------------------------ZFServerHandler::ExceptionCaught--------------------------");
if(exp instanceof IOException)
{
ExceptionLoger.ExLog("Client "+session.getRemoteAddress().toString()+"-----disconnect!!!!");
ExceptionLoger.ExLog(exp);
}
else if(exp instanceof ProtocolDecoderException)
{
String RtnStr=new MACMan().countTXMAC(ErrMsg.PACKAGE_ERROR);
session.write(ByteBuffer.wrap(StrUtil.packageXML(RtnStr)));
loger.info("To Client "+session.getRemoteAddress().toString()+":: "+RtnStr);
}else
{
ExceptionLoger.ExLog(exp);
}
session.close();
}
public void sessionIdle( IoSession session, IdleStatus status )
{
loger.info("["+session.getRemoteAddress()+"] caught session idle");
session.close();
}
public void sessionClosed(IoSession session) throws Exception {
super.sessionClosed(session);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -