All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.nadron.client.handlers.netty.UDPUpstreamHandler Maven / Gradle / Ivy

Go to download

This is a client library for Nadron server https://github.com/menacher/java-game-server/tree/netty4/nadron. Java clients can use this program to connect and interact with nadron server.

There is a newer version: 0.5
Show newest version
package io.nadron.client.handlers.netty;

import io.nadron.client.NettyUDPClient;
import io.nadron.client.app.Session;
import io.nadron.client.event.Event;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;


/**
 * This upstream handler handles ALL UDP events. It will lookup the
 * appropriate session from {@link NettyUDPClient#CLIENTS} map and then transmit
 * the event to that {@link Session}. Note If this class cannot find the
 * appropriate session to transmit this event to, then the event is
 * silently discarded.
 * 
 * @author Abraham Menacherry.
 * 
 */
@Sharable
public class UDPUpstreamHandler extends SimpleChannelInboundHandler
{
	private final MessageBufferEventDecoder decoder;
	
	public UDPUpstreamHandler()
	{
		super();
		decoder = new MessageBufferEventDecoder();
	}

	@Override
	public void channelRead0(ChannelHandlerContext ctx,
			DatagramPacket packet) throws Exception
	{
		Session session = NettyUDPClient.CLIENTS.get(ctx.channel().localAddress());
		if (null != session)
		{
			Event event = (Event)decoder.decode(null, packet.content());
			// Pass the event on to the session
			session.onEvent(event);
		}
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy