package cn.mb.cloud.gateway.util.echo; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPromise; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.timeout.IdleState; import io.netty.handler.timeout.IdleStateEvent; import io.netty.util.ReferenceCountUtil; import java.net.InetSocketAddress; import java.util.HashMap; public class DiscardServerHandler extends SimpleChannelInboundHandler { private NettyServerController nettyServerController = new NettyServerController(); public static boolean isdebug = true; @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress(); if(isdebug) { // System.err.println(insocket.getAddress() + ": 收到客户端数据......."); } try { // 调用service nettyServerController.JudgeOperation(ctx, msg); } catch (Exception e) { e.printStackTrace(); } finally { ReferenceCountUtil.release(msg); } } @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress(); if(isdebug) { // System.err.println(insocket.getAddress() + ": 收到客户端数据......."); } try { // 调用service nettyServerController.JudgeOperation(ctx, msg); } catch (Exception e) { e.printStackTrace(); } finally { ReferenceCountUtil.release(msg); } } // @Override // protected void messageReceived(ChannelHandlerContext channelHandlerContext, String s) throws Exception { // // } // @Override // protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception { // // } // @Override // protected void messageReceived(ChannelHandlerContext channelHandlerContext, String s) throws Exception { // // } /** 在连接被建立并且准备进行通信时被调用 */ public void channelActive(final ChannelHandlerContext ctx) throws Exception { InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress(); if(isdebug) { // System.err.println(insocket.getAddress() + ": Connect successful......"); } } /** 心跳 */ @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof IdleStateEvent) { IdleStateEvent event = (IdleStateEvent) evt; if (event.state().equals(IdleState.READER_IDLE)) { // } else if (event.state().equals(IdleState.WRITER_IDLE)) { // } else if (event.state().equals(IdleState.ALL_IDLE)) { String msg = NettyMsg.setMsg(Method.ok, new HashMap()); if(ctx != null && ctx.channel().isActive()) { ctx.writeAndFlush(Unpooled.copiedBuffer((msg).getBytes())); // System.err.println(msg); } } } //super.userEventTriggered(ctx, evt); } /** 连接处于不活跃时调用(连接关闭) **/ public void channelInactive(ChannelHandlerContext ctx) throws Exception { if(isdebug) { InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress(); // System.err.println(insocket.getAddress() + ": Disconnect connection......"); } NettyChannelMap.remove(ctx); // System.err.println("清除通道" + ctx); // super.channelInactive(ctx); } public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ctx.flush(); } /** 处理方法是当出现Throwable对象才会被调用 **/ public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause) throws Exception { ctx.close(); } public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { if(isdebug) { InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress(); // System.err.println("close......." + insocket.getAddress()); } ctx.close(promise); } public void read(ChannelHandlerContext ctx) throws Exception { ctx.read(); } public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { ctx.write(msg, promise); } // @Override // protected void channelRead(ChannelHandlerContext ctx, String msg) throws Exception { // InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress(); // if(isdebug) { // System.out.println(insocket.getAddress() + ": 收到客户端数据......."); // } // try { // // 调用service // nettyServerController.JudgeOperation(ctx, msg); // } catch (Exception e) { // e.printStackTrace(); // } finally { // ReferenceCountUtil.release(msg); // } // } }