Guest User

Untitled

a guest
Jul 19th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. package cn.isccn.familycloudtv.network;
  2.  
  3. import com.google.gson.Gson;
  4.  
  5.  
  6. import java.net.InetSocketAddress;
  7.  
  8. import cn.isccn.familycloudtv.bean.Packet;
  9. import cn.isccn.familycloudtv.utils.Utils;
  10. import io.netty.buffer.ByteBuf;
  11. import io.netty.buffer.Unpooled;
  12. import io.netty.channel.ChannelHandlerContext;
  13. import io.netty.channel.SimpleChannelInboundHandler;
  14. import io.netty.channel.socket.DatagramPacket;
  15. import io.netty.util.CharsetUtil;
  16. import io.netty.util.concurrent.Future;
  17. import io.netty.util.concurrent.GenericFutureListener;
  18.  
  19. import static cn.isccn.familycloudtv.utils.SocketUtils.parsePacket;
  20.  
  21. /**
  22. * Create by hongxin.wen on 2018/7/10
  23. * <p>
  24. * 修改记录:
  25. * DES: 2018/7/6 V1.0 基于Netty框架的UDP通信客户端引导 by hongxin.wen
  26. */
  27. public class UDPClientHandler extends SimpleChannelInboundHandler<DatagramPacket> {
  28.  
  29. @Override
  30. public void channelActive(ChannelHandlerContext ctx) throws Exception {
  31. Utils.printLog("client channel is ready!");
  32. super.channelActive(ctx);
  33. }
  34.  
  35.  
  36. public static void sendMsg(final String msg, final InetSocketAddress inetSocketAddress) {
  37. Utils.printLog("udp client send msg!");
  38. if (msg == null) {
  39. Utils.printLog("send msg is null");
  40. throw new NullPointerException("msg is null");
  41. }
  42. senderInternal(datagramPacket(msg, inetSocketAddress));
  43.  
  44. }
  45.  
  46. private static void senderInternal(DatagramPacket datagramPacket) {
  47. if (UDPClient.channel != null) {
  48. UDPClient.channel.writeAndFlush(datagramPacket).addListener(new GenericFutureListener<Future<? super Void>>() {
  49. @Override
  50. public void operationComplete(Future<? super Void> future) {
  51. Utils.printLog(" send msg success");
  52. }
  53. });
  54. }
  55. }
  56.  
  57. private static DatagramPacket datagramPacket(String msg, InetSocketAddress inetSocketAddress) {
  58. ByteBuf buf = Unpooled.copiedBuffer(msg, CharsetUtil.UTF_8);
  59. return new DatagramPacket(buf, inetSocketAddress);
  60. }
  61.  
  62.  
  63. @Override
  64. protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) {
  65. final ByteBuf buf = msg.content();
  66. byte[] res = new byte[buf.readableBytes()];
  67. buf.readBytes(res);
  68. String result = new String(res, CharsetUtil.UTF_8);
  69. Gson gson = new Gson();
  70. Packet packet = gson.fromJson(result, Packet.class);
  71. parsePacket(packet);
  72. Utils.printLog("server response result = " + result);
  73. }
  74.  
  75. @Override
  76. public void channelInactive(ChannelHandlerContext ctx) throws Exception {
  77. Utils.printLog("conn close!");
  78. super.channelInactive(ctx);
  79. }
  80.  
  81. }
Add Comment
Please, Sign In to add comment