2021-07-09

Netty 框架学习 —— UDP 广播


UDP 广播

面向连接的传输(如 TCP)管理两个网络端点之间的连接的建立,在连接的生命周期的有序和可靠的消息传输,以及最后,连接的有序终止。相比之下,类似 UDP 的无连接协议中则没有持久化连接的概念,此外,UDP 也没有 TCP 的纠错机制。但 UDP 的性能比 TCP 要好很多,适合那些能够处理或者忍受消息丢失的应用程序

目前为止,我们所有的例子都是采用一种叫作单播的传输模式,定义为发送消息给一个由唯一地址所标识的单一的网络目的地。面向连接的协议和无连接协议都支持这种模式

UDP 提供了向多个接收者发送消息的额外传输模式:

  • 多播:传输到一个预定义的主机组
  • 广播:传输到网络(子网)上的所有主机

本章示例将通过发送能够被同一个网络中的所有主机接收的消息来演示 UDP 广播的使用


UDP 示例应用程序

我们的程序将打开一个文件,随后通过 UDP 把每一行作为一个消息广播到一个指定的端口。而接收方只需简单地在指定端口上启动一个监听程序,便可以创建一个事件监视器来接受消息。本次示例以日志文件处理程序为例

1. 消息 POJO:LogEvent

在这个应用程序中,我们将会把消息作为事件处理,并且由于该数据来自于日志文件,所以我们将它称为 LogEvent

public class LogEvent { public static final byte SEPARATOR = (byte) ':'; private final InetSocketAddress source; private final String logfile; private final String msg; private final long received; public LogEvent(String logfile, String msg) {  this(null, logfile, msg, -1); } public LogEvent(InetSocketAddress source, String logfile, String msg, long received) {  this.source = source;  this.logfile = logfile;  this.msg = msg;  this.received = received; } public InetSocketAddress getSource() {  return source; } public String getLogfile() {  return logfile; } public String getMsg() {  return msg; } public long getReceived() {  return received; }}

2. 编写广播者

Netty 的 DatagramPacket 是一个简单的消息容器,DatagramChannel 实现和远程节点的通信,要将 LogEvent 消息转换为 DatagramPacket,我们需要一个编码器

下述是编码器的代码实现

public class LogEventEncoder extends MessageToMessageEncoder<LogEvent> { private final InetSocketAddress remoteAddress; public LogEventEncoder(InetSocketAddress remoteAddress) {  this.remoteAddress = remoteAddress; } @Override protected void encode(ChannelHandlerContext ctx, LogEvent logEvent, List<Object> out) throws Exception {  byte[] file = logEvent.getLogfile().getBytes(StandardCharsets.UTF_8);  byte[] msg = logEvent.getMsg().getBytes(StandardCharsets.UTF_8);  ByteBuf buf = ctx.alloc().buffer(file.length + msg.length + 1);  buf.writeBytes(file);  buf.writeByte(LogEvent.SEPARATOR);  buf.writeBytes(msg);  out.add(new DatagramPacket(buf, remoteAddress)); }}

接下来准备引导该服务器,包括设置 ChannelOption,以及在 ChannelPipeline 中安装所需的 ChannelHandler,这部分通过主类 LogEventBroadcaster 完成

public class LogEventBroadcaster { private final EventLoopGroup group; private final Bootstrap bootstrap; private final File file; public LogEventBroadcaster(InetSocketAddress address, File file) {  group = new NioEventLoopGroup();  bootstrap = new Bootstrap();  bootstrap.group(group).channel(NioDatagramChannel.class)    .option(ChannelOption.SO_BROADCAST, true)    .handler(new LogEventEncoder(address));  this.file = file; } public void run() throws Exception {  // 绑定 Channel  Channel ch = bootstrap.bind(0).sync().channel();  long pointer = 0;  for (; ; ) {   long len = file.length();   if (len < pointer) {    // 将文件指针指向文件的最后一个字节    pointer = len;   } else if (len > pointer) {    RandomAccessFile raf = new RandomAccessFile(file, "r");    // 设置当前文件指针    raf.seek(pointer);    String line;    while ((line = raf.readLine()) != null) {     ch.writeAndFlush(new LogEvent(null, line, file.getAbsolutePath(), -1));    }    pointer = raf.getFilePointer();    raf.close();   }   try {    Thread.sleep(1000);   } catch (InterruptedException e) {    Thread.interrupted();    break;   }  } } public void stop() {  group.shutdownGracefully(); } public static void main(String[] args) throws Exception {  if (args.length != 2) {   throw new InterruptedException();  }  LogEventBroadcaster broadcaster = new LogEventBroadcaster(new InetSocketAddress("255.255.255.255",    Integer.parseInt(args[0])), new File(args[1]));  try {   broadcaster.run();  }  finally {   broadcaster.stop();  } }}

3. 编写监视器

编写一个称为 LogEventMonitor 的消费者程序,它的作用包括:

  • 接收由 LogEventBroadcaster 广播的 UDP DatagramPacket
  • 解码为 LogEvent 消息
  • 处理 LogEvent 消息

和之前一样,解码器 LogEventDecoder 负责将传入的 DatagramPacket 解码为 LogEvent 消息

public class LogEventDecoder extends MessageToMessageDecoder<DatagramPacket> { @Override protected void decode(ChannelHandlerContext ctx, DatagramPacket datagramPacket, List<Object> out) throws Exception {  ByteBuf data = datagramPacket.content();  int idx = data.indexOf(0, data.readableBytes(), LogEvent.SEPARATOR);  String filename = data.slice(0, idx).toString(CharsetUtil.UTF_8);  String logMsg = data.slice(idx + 1, data.readableBytes()).toString(CharsetUtil.UTF_8);  LogEvent event = new LogEvent(datagramPacket.sender(), logMsg, filename, System.currentTimeMillis());  out.add(event); }}

创建一个处理 LogEvent 的 ChannelHandler

public class LogEventHandler extends SimpleChannelInboundHandler<LogEvent> { @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {  cause.printStackTrace();  ctx.close(); } @Override protected void messageReceived(ChannelHandlerContext ct......

原文转载:http://www.shaoqun.com/a/859328.html

跨境电商:https://www.ikjzd.com/

敦煌网站:https://www.ikjzd.com/w/189

友家速递:https://www.ikjzd.com/w/1341

cb体系:https://www.ikjzd.com/w/2063


UDP广播面向连接的传输(如TCP)管理两个网络端点之间的连接的建立,在连接的生命周期的有序和可靠的消息传输,以及最后,连接的有序终止。相比之下,类似UDP的无连接协议中则没有持久化连接的概念,此外,UDP也没有TCP的纠错机制。但UDP的性能比TCP要好很多,适合那些能够处理或者忍受消息丢失的应用程序目前为止,我们所有的例子都是采用一种叫作单播的传输模式,定义为发送消息给一个由唯一地址所标识的单
跨境通:https://www.ikjzd.com/w/1329
澳大利亚,一个被卖家们低估的跨境蓝海市场:https://www.ikjzd.com/articles/126855
一周Wish政策回顾:WishPost部分渠道价格调整,新增登记交易平台辖区:https://www.ikjzd.com/articles/126923
作为亚马逊卖家,哪些技能是必备的呢?:https://www.ikjzd.com/articles/126846
把竞争对手从自己亚马逊Listing页面挤掉,备战2020节日季你不知道的3个广告技巧!:https://www.ikjzd.com/articles/126921
二个人一上一下进出 把女朋友做到下不了床:http://lady.shaoqun.com/a/248082.html
情感故事:回忆我当坐台小姐的那些年(11/15):http://lady.shaoqun.com/a/116014.html
他把舌头伸进两腿之间 舌尖逗弄她颤抖的小核:http://www.30bags.com/m/a/249891.html
女海王阵型!不告诉男朋友就勾搭富二代!数百万粉丝,Tik Tok女人,网络名人,做了很多轮:http://lady.shaoqun.com/a/410933.html
清迈的一个天桥小舒林成同志,被居民举报:http://lady.shaoqun.com/a/410934.html
女人为什么选择"暧昧"?倾听中年妇女的心声:http://lady.shaoqun.com/a/410935.html
独立站高昂流量成废品,如何操作才能起死回生?(上):https://www.ikjzd.com/articles/146487

No comments:

Post a Comment