|  |  |  | 
|---|
|  |  |  | private static boolean started = false ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private TcpUnitAdapter adapter ; | 
|---|
|  |  |  | private TcpIoHandler tcpIoHandler ; | 
|---|
|  |  |  | private DataCodecFactory dataCodecFactory ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private TcpUnit(){} ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | //得到网络 通信数据过滤器链 | 
|---|
|  |  |  | DefaultIoFilterChainBuilder chain = acceptor.getFilterChain() ; | 
|---|
|  |  |  | //编解码过滤器 | 
|---|
|  |  |  | chain.addLast("protocol", new ProtocolCodecFilter(new DataCodecFactory(this.adapter))); | 
|---|
|  |  |  | dataCodecFactory = new DataCodecFactory(this.adapter) ; | 
|---|
|  |  |  | chain.addLast("protocol", new ProtocolCodecFilter(dataCodecFactory)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /* | 
|---|
|  |  |  | * 一般ExecutorFilter 都要放在ProtocolCodecFilter 过滤器的后面, | 
|---|
|  |  |  | 
|---|
|  |  |  | 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 { | 
|---|
|  |  |  | 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(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|