|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.dy.common.mw.UnitAdapterInterface; | 
|---|
|  |  |  | import com.dy.common.mw.UnitInterface; | 
|---|
|  |  |  | import com.dy.common.mw.UnitStartedCallbackInterface; | 
|---|
|  |  |  | import com.dy.common.mw.UnitCallbackInterface; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @SuppressWarnings("unused") | 
|---|
|  |  |  | public class TcpUnit implements UnitInterface { | 
|---|
|  |  |  | 
|---|
|  |  |  | private static boolean started = false ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private TcpUnitAdapter adapter ; | 
|---|
|  |  |  | private TcpIoHandler tcpIoHandler ; | 
|---|
|  |  |  | private DataCodecFactory dataCodecFactory ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private TcpUnit(){} ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 启动模块 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void start(UnitStartedCallbackInterface callback) throws Exception { | 
|---|
|  |  |  | public void start(UnitCallbackInterface callback) throws Exception { | 
|---|
|  |  |  | if(!started){ | 
|---|
|  |  |  | started = true ; | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //得到网络 通信数据过滤器链 | 
|---|
|  |  |  | DefaultIoFilterChainBuilder chain = acceptor.getFilterChain() ; | 
|---|
|  |  |  | //编解码过滤器 | 
|---|
|  |  |  | chain.addLast("protocol", new ProtocolCodecFilter(new DataCodecFactory(this.adapter))); | 
|---|
|  |  |  | //生成编解码过滤器工厂类 | 
|---|
|  |  |  | dataCodecFactory = new DataCodecFactory(this.adapter) ; | 
|---|
|  |  |  | //设置“protocol”,加入编解码过滤器,过滤器在IoProcessor线程中执行 | 
|---|
|  |  |  | chain.addLast("protocol", new ProtocolCodecFilter(dataCodecFactory)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /* | 
|---|
|  |  |  | * 一般ExecutorFilter 都要放在ProtocolCodecFilter 过滤器的后面, | 
|---|
|  |  |  | * 也就是不要让编解码运行在独立的线程上,而是要运行在IoProcessor 所在的线程, | 
|---|
|  |  |  | * 因为编解码处理的数据都是由IoProcessor 读取和发送的,没必要开启新的线程, | 
|---|
|  |  |  | * 否则性能反而会下降。一般使用ExecutorFilter 的典型场景是将业务逻辑(譬如:耗时的数据库操作) | 
|---|
|  |  |  | * 放在单独的线程中运行,也就是说与IO 处理无关的操作可以考虑使用ExecutorFilter 来异步执行。 | 
|---|
|  |  |  | * 一般ExecutorFilter 都要放在ProtocolCodecFilter过滤器的后面, | 
|---|
|  |  |  | * 也就是让编解码运行在IoProcessor所在的线程,因为编解码处理的数据都是 | 
|---|
|  |  |  | * 由IoProcessor读取和发送的,没必要开启新的线程,否则性能反而会下降。 | 
|---|
|  |  |  | * ExecutorFilter过程器会启动一个线程池,处理后续代码逻辑。 | 
|---|
|  |  |  | * 一般使用ExecutorFilter的典型场景是将业务逻辑(譬如:耗时的数据库操作) | 
|---|
|  |  |  | * 放在单独的线程中运行,也就是说与IO处理无关的操作可以考虑使用ExecutorFilter来异步执行。 | 
|---|
|  |  |  | * 本处用法,使ExecutorFilter线程池中的线程处理IOHandler(TcpIoHandler)操作 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | chain.addLast("exceutor", new ExecutorFilter()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //业务逻辑处理器,负责处理网络会话及输入输出数据 | 
|---|
|  |  |  | acceptor.setHandler(new TcpIoHandler(this.adapter)); | 
|---|
|  |  |  | tcpIoHandler = new TcpIoHandler(this.adapter) ; | 
|---|
|  |  |  | acceptor.setHandler(tcpIoHandler) ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | boolean isException = false ; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 停止模块运行,将不再接入TCP网络连接,并把已经tcp连接的全部断连接 | 
|---|
|  |  |  | * @param callback | 
|---|
|  |  |  | * @throws Exception | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void stop(UnitStartedCallbackInterface callback) throws Exception { | 
|---|
|  |  |  | public void stop(UnitCallbackInterface callback) throws Exception { | 
|---|
|  |  |  | this.tcpIoHandler.stop(); | 
|---|
|  |  |  | this.dataCodecFactory.stop(); | 
|---|
|  |  |  | this.adapter.newUnitStopCallback().callback(); | 
|---|
|  |  |  | callback.call(null); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 解除停止,恢复TCP服务运行 | 
|---|
|  |  |  | * @throws Exception | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void recover() throws Exception { | 
|---|
|  |  |  | this.tcpIoHandler.recover(); | 
|---|
|  |  |  | this.dataCodecFactory.recover(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|