UdpServerSocketChannel icon indicating copy to clipboard operation
UdpServerSocketChannel copied to clipboard

SimpleChannelInboundHandler<DatagramPacket> added in the pipeline does not work

Open arunp1990 opened this issue 3 years ago • 4 comments

There is a requirement in my project to obtain Host Address of the sender from the UDP Header for processing the request. As per my understanding, I need to add a handler using which I will get DatagramPacket. So, I modified the usage example given in https://github.com/Shevchik/UdpServerSocketChannel by replacing: private static class Echo extends SimpleChannelInboundHandler<ByteBuf> with public static class Echo extends SimpleChannelInboundHandler<DatagramPacket>.

The modified example is attached.

But, protected void channelRead0(ChannelHandlerContext channelHandlerContext, DatagramPacket datagramPacket) throws Exception is not invoked at all. It may be because the library (UdpServerSocketChannel) already has a similar handler https://github.com/Shevchik/UdpServerSocketChannel/blob/a181129af82014fd7a1546f3d5480c7b5b01714b/src/udpserversocketchannel/channel/UdpServerChannel.java#L61 : protected class ReadRouteChannelHandler extends SimpleChannelInboundHandler<DatagramPacket> {

Can you please let me know if the above solution which I am trying can be made to work? If not, please let me know any alternate solution which I can try.

Thanks!

ExampleUdpServer.txt

arunp1990 avatar May 02 '21 09:05 arunp1990

No. Thats literally a part of the readme about channel per remote address and using channel remote address for that. If you dont plan on making a stateful udp connection, you should just use the normal netty datagram channel.

Shevchik avatar May 02 '21 13:05 Shevchik

Thanks for the quick reply!

I would like to explain the scenario which I am trying to solve. The UDP server which uses your library is behind a load balancer. So, the channel remote address is the ip address of the Load Balancer. But, I have to obtain the IP address of the client which sent the request.

I noticed that p.sender() is used for computation in https://github.com/Shevchik/UdpServerSocketChannel/blob/a181129af82014fd7a1546f3d5480c7b5b01714b/src/udpserversocketchannel/channel/UdpServerChannel.java#L77 But, for removal , channel's remoteAddress is used. https://github.com/Shevchik/UdpServerSocketChannel/blob/a181129af82014fd7a1546f3d5480c7b5b01714b/src/udpserversocketchannel/channel/UdpServerChannel.java#L108

If the UDP server is behind a Load Balancer, p.sender() and userChannel.remoteAddress() will be different as per my understanding. p.sender() will be the IP address of the client which sent the request. userChannel.remoteAddress() will be the IP address of the Load balancer. Please correct me if I am wrong.

arunp1990 avatar May 02 '21 17:05 arunp1990

The user channel remote address is literally the datagram sender address. So they will always the the same. That's literally the whole point of this library, every new sender address allocates a new user channel.

The load balancer you use proxies the remote address using it's own protocol if it ever does.

Shevchik avatar May 02 '21 18:05 Shevchik

Ok Thanks a lot! Let me test the use-case with Load balancer and get back if needed.

arunp1990 avatar May 03 '21 04:05 arunp1990