package Netty; import java.net.InetSocketAddress; import java.util.Vector; import BaoWen.Dell_55AA01; import BaoWen.Dell_Baowen; import BaoWen.Urt_7000_port; import Frame.Tool; import Method.BaoWenShow; import Method.GetNowTime; import Method.SaveFIleInTxt; import PbuliClass.Systems; import io.netty.buffer.ByteBuf; import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelPipeline; import io.netty.handler.timeout.IdleStateEvent; import io.netty.util.ReferenceCountUtil; import io.netty.util.ResourceLeakDetector; import monitorGas.ReadGas; import rtkanchor.Dell_jizhan; import rtkanchor.Jizhan; import rtkanchor.Rtkac_manage; import rtkanchor.SendMessage; /* ˵Ã÷ 1. ÎÒÃÇ×Ô¶¨ÒåÒ»¸öHandler ÐèÒª¼Ì³ÐNETTY ¹æ¶¨ºÃµÄij¸öHandlerAdapter(¹æ·¶) 2. ÕâʱÎÒÃÇ×Ô¶¨ÒåÒ»¸öHandler , ²ÅÄܳÆÎªÒ»¸öhandler */ public class NettyServerHandler extends ChannelInboundHandlerAdapter { //ËùÓÐÔÚÏß»ùÕ¾µÄ¼¯ºÏ static Vector tagidonlie = new Vector(); String tport = Systems.getTcpreceiveport();//ϵͳÉèÖô¦TCP½ÓÊÕ¶Ë¿Ú; StringBuffer message = new StringBuffer(); static String ip = null; Tool tool = new Tool(); //¶ÁÈ¡Êý¾Ýʵ¼Ê(ÕâÀïÎÒÃÇ¿ÉÒÔ¶ÁÈ¡¿Í»§¶Ë·¢Ë͵ÄÏûÏ¢) /* 1. ChannelHandlerContext ctx:ÉÏÏÂÎĶÔÏó, º¬ÓÐ ¹ÜµÀpipeline , ͨµÀchannel, µØÖ· 2. Object msg: ¾ÍÊǿͻ§¶Ë·¢Ë͵ÄÊý¾Ý ĬÈÏObject */ @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { Channel channel = ctx.channel(); @SuppressWarnings("unused") ChannelPipeline pipeline = ctx.pipeline(); //±¾ÖÊÊÇÒ»¸öË«ÏòÁ´½Ó, ³öÕ¾ÈëÕ¾ ByteBuf buf = (ByteBuf) msg;//½« msg ת³ÉÒ»¸ö ByteBuf // String message1=buf.toString(CharsetUtil.UTF_8);//ASCLL¸ñʽ±¨ÎÄ // System.out.println(message1); byte[] bytes = new byte[buf.readableBytes()]; buf.getBytes(buf.readerIndex(), bytes); buf.readBytes(bytes); InetSocketAddress insocket = (InetSocketAddress) channel.remoteAddress(); ip = insocket.getAddress().getHostAddress(); String datas = Bytes2HexString(bytes); String message1=new String(bytes, 0, bytes.length); Dell_55AA01.dell_gui(); if (message1.startsWith("55BB,HeightModify")) { Tool.HeightModify(message1); }else if (datas.startsWith("010314")){ ReadGas.dell_gas2_data_new(ip,datas); } SaveFIleInTxt.save_baowen(datas); String port = String.valueOf(insocket.getPort());//É豸µÄ¶Ë¿Ú InetSocketAddress insocket2 = (InetSocketAddress) channel.localAddress(); String port1 = String.valueOf(insocket2.getPort());//·þÎñÆ÷µÄ¶Ë¿Ú String message = "TCPÀ´×Ô" + port1 + "," + ip + ":" + port + "," + datas; String trim = message1.trim(); String s = trim.toUpperCase(); //55AA¿ªÍ·Êý¾Ý if (datas.toUpperCase().startsWith("55AA")) { Dell_Baowen.intsert(ip.toString(), datas.toUpperCase(), GetNowTime.timestamp2(), insocket.getPort()); }else { Urt_7000_port.dellmessage(s, s, ip, insocket.getPort(), GetNowTime.timestamp2()); BaoWenShow.ok("2", "ԭʼÊý¾Ý", s, ip, "´¦Àíԭʼ±¨ÎÄ"); BaoWenShow.tcpshow(message, message); } if (port1.equals("tport")) { } else if (!Rtkac_manage.isPeizhijizhan()) {//µ±»ù×¼Õ¾²»ÔÚÅäÖÃģʽµÄʱºò Jizhan jz = Dell_jizhan.get_2port_jizhan(port1);//ͨ¹ýÊý¾ÝÉÏÀ´µÄ¶Ë¿ÚÕÒµ½»ùÕ¾ if (jz != null) { SendMessage.sendmessage_to_urt(jz, datas);//½«²î·ÖÊý¾Ý·¢×߸øµÄÒÆ¶¯ÖÕ¶Ë jz.setAddtime(GetNowTime.now()); jz.setBaoliu2(port); jz.setBaoliu1(ip); jz.setState("ÔÚÏß"); int num = jz.getNum() + 1; if (num == 65535) { num = 0; } jz.setNum(num); } } ReferenceCountUtil.release(buf); ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.ADVANCED); } //Êý¾Ý¶ÁÈ¡Íê±Ï @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { } //´¦ÀíÒì³£, Ò»°ãÊÇÐèÒª¹Ø±ÕͨµÀ @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.close(); } @SuppressWarnings("unused") @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof IdleStateEvent) { //½« evt ÏòÏÂתÐÍ IdleStateEvent IdleStateEvent event = (IdleStateEvent) evt; String eventType = null; switch (event.state()) { case READER_IDLE: eventType = "¶Á¿ÕÏÐ"; break; case WRITER_IDLE: eventType = "д¿ÕÏÐ"; break; case ALL_IDLE: eventType = "¶Áд¿ÕÏÐ"; break; } InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress(); StringBuffer ip = new StringBuffer(insocket.getAddress().getHostAddress()); //Èç¹û·¢Éú¿ÕÏУ¬ÎÒÃǹرÕͨµÀ // ctx.channel().close(); } } public static String getIp() { return ip; } /** * ½«×Ö½ÚÊý×éתΪ16½øÖÆ×Ö·û´® */ public static String Bytes2HexString(byte[] b) { String ret = ""; for (int i = 0; i < b.length; i++) { String hex = Integer.toHexString(b[i] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } ret += hex.toUpperCase(); } return ret; } public String convertByteBufToString(ByteBuf buf) { String str; if (buf.hasArray()) { // ´¦Àí¶Ñ»º³åÇø str = new String(buf.array(), buf.arrayOffset() + buf.readerIndex(), buf.readableBytes()); } else { // ´¦ÀíÖ±½Ó»º³åÇøÒÔ¼°¸´ºÏ»º³åÇø byte[] bytes = new byte[buf.readableBytes()]; buf.getBytes(buf.readerIndex(), bytes); str = new String(bytes, 0, buf.readableBytes()); } return str; } }