| New file | 
 |  |  | 
 |  |  | package com.dy.common.mw.channel.tcp; | 
 |  |  |  | 
 |  |  | import org.apache.mina.filter.codec.ProtocolCodecFactory; | 
 |  |  | import org.apache.mina.filter.codec.ProtocolEncoder; | 
 |  |  | import org.apache.mina.filter.codec.ProtocolDecoder; | 
 |  |  | import org.apache.mina.core.session.IoSession; | 
 |  |  |  | 
 |  |  | public class DataCodecFactory implements ProtocolCodecFactory { | 
 |  |  |  | 
 |  |  |    private TcpUnitAdapter adapter ; | 
 |  |  |     private boolean stop = false ;//为true时,停止TCP服务,并把已经TCP连接断连接 | 
 |  |  |  | 
 |  |  |     public DataCodecFactory(TcpUnitAdapter adapter) { | 
 |  |  |        this.adapter = adapter ; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public void stop(){ | 
 |  |  |         stop = true ; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public void recover(){ | 
 |  |  |         this.stop = false ; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     public ProtocolEncoder getEncoder(IoSession ioSession) { | 
 |  |  |         if(stop){ | 
 |  |  |             ioSession.closeNow() ; | 
 |  |  |         } | 
 |  |  |         return new DataEncoder(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public ProtocolDecoder getDecoder(IoSession ioSession) { | 
 |  |  |         if(stop){ | 
 |  |  |             ioSession.closeNow() ; | 
 |  |  |         } | 
 |  |  |         return new DataDecoder(adapter.newPrefixedDataAvailableHandle()); | 
 |  |  |     } | 
 |  |  | } |