Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UdpCodecHandler.encode数据时必须写入UdpAttachment吗 #88

Open
LucaLq opened this issue Jan 10, 2024 · 1 comment
Open

UdpCodecHandler.encode数据时必须写入UdpAttachment吗 #88

LucaLq opened this issue Jan 10, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@LucaLq
Copy link
Contributor

LucaLq commented Jan 10, 2024

有个疑问:encode会写入UdpAttachment,但是接收方decode数据包时又不关心发送方写入的UdpAttachment,所以encode那里是不是去掉UdpAttachment?

NetContext.getPacketService().write(byteBuf, out.getPacket(), out.getAttachment());

public class UdpCodecHandler extends MessageToMessageCodec<DatagramPacket, EncodedPacketInfo> {
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, DatagramPacket datagramPacket, List<Object> list) {
        ...
        var packetInfo = NetContext.getPacketService().read(sliceByteBuf);
        var sender = datagramPacket.sender();
        packetInfo.setAttachment(UdpAttachment.valueOf(sender.getHostString(), sender.getPort()));
        list.add(packetInfo);
    }

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, EncodedPacketInfo out, List<Object> list) {
        var byteBuf = channelHandlerContext.alloc().ioBuffer();
        var udpAttachment = (UdpAttachment) out.getAttachment();
        NetContext.getPacketService().write(byteBuf, out.getPacket(), out.getAttachment());
        list.add(new DatagramPacket(byteBuf, new InetSocketAddress(udpAttachment.getHost(), udpAttachment.getPort())));
    }
}
@godotg
Copy link
Contributor

godotg commented Jan 10, 2024

是可以的,欢迎提pr优化

@godotg godotg added the bug Something isn't working label Jan 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants