[fix] 优化tcp、udp架构 test

This commit is contained in:
wujiawei
2024-12-17 15:14:09 +08:00
parent f9188037a0
commit fac28c4fc8
11 changed files with 70 additions and 64 deletions

View File

@ -2,6 +2,7 @@ package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.filter;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;
@ -12,7 +13,7 @@ import org.framework.lazy.cloud.network.heartbeat.common.decoder.NettyProxyMsgDe
import org.framework.lazy.cloud.network.heartbeat.common.encoder.NettyProxyMsgEncoder;
import org.framework.lazy.cloud.network.heartbeat.common.filter.DebugChannelInitializer;
public class NettyUdpClientFilter extends DebugChannelInitializer<SocketChannel> {
public class NettyUdpClientFilter extends DebugChannelInitializer<NioDatagramChannel> {
private final ChannelTypeAdapter channelTypeAdapter;
@ -24,7 +25,7 @@ public class NettyUdpClientFilter extends DebugChannelInitializer<SocketChannel>
}
@Override
protected void initChannel0(SocketChannel ch) throws Exception {
protected void initChannel0(NioDatagramChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
/* * 解码和编码,应和服务端一致 * */

View File

@ -2,10 +2,13 @@ package org.framework.lazy.cloud.network.heartbeat.client.netty.udp.socket;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.DatagramPacket;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.internal.SocketUtils;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.client.netty.NettyClientSocket;
@ -86,6 +89,7 @@ public class NettyUdpClientSocket implements NettyClientSocket {
.option(ChannelOption.SO_SNDBUF, 1024 * 1024)
.option(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.SO_BROADCAST, true)
// .childOption(ChannelOption.UDP_NODELAY, false)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 秒
// .childOption(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认AdaptiveRecvByteBufAllocator.DEFAULT
@ -100,7 +104,7 @@ public class NettyUdpClientSocket implements NettyClientSocket {
future.addListener((ChannelFutureListener) futureListener -> {
if (futureListener.isSuccess()) {
log.info("clientId:{},connect to server IP:{},server port :{} isSuccess ", clientId, inetHost, inetPort);
log.info("clientId:{},connect to server IP:{},server port :{} udp isSuccess ", clientId, inetHost, inetPort);
// 告诉服务端这条连接是client的连接
NettyProxyMsg nettyMsg = new NettyProxyMsg();
nettyMsg.setType(UdpMessageType.UDP_REPORT_CLIENT_CONNECT_SUCCESS);