Java tutorial
/* * __ ______ ______ __ __ __ __ ______ ______ * /\ \ /\ == \ /\ __ \ /\ \/ / /\ "-.\ \ /\ ___\ /\__ _\ * _\_\ \ \ \ __< \ \ __ \ \ \ _"-. \ \ \-. \ \ \ __\ \/_/\ \/ * /\_____\ \ \_\ \_\ \ \_\ \_\ \ \_\ \_\ \ \_\\"\_\ \ \_____\ \ \_\ * \/_____/ \/_/ /_/ \/_/\/_/ \/_/\/_/ \/_/ \/_/ \/_____/ \/_/ * * the MIT License (MIT) * * Copyright (c) 2016-2019 Trent "Whirvis" Summerlin * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * the above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package com.whirvis.jraknet.client; import java.net.InetSocketAddress; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import com.whirvis.jraknet.RakNetPacket; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.socket.DatagramPacket; /** * Used by the {@link RakNetClient} with the sole purpose of sending received * packets to the client so they can be handled. Any errors that occurs will * also be sent to the client to be dealt with. * * @author Trent "Whirvis" Summerlin * @since JRakNet v1.0.0 */ public final class RakNetClientHandler extends ChannelInboundHandlerAdapter { private final Logger log; private final RakNetClient client; private InetSocketAddress causeAddress; /** * Creates a RakNet client Netty handler. * * @param client * the client to send received packets to. */ protected RakNetClientHandler(RakNetClient client) { this.log = LogManager.getLogger(RakNetClientHandler.class.getSimpleName() + "-" + Long.toHexString(client.getGloballyUniqueId()).toUpperCase()); this.client = client; } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { if (msg instanceof DatagramPacket) { // Get packet and sender data DatagramPacket datagram = (DatagramPacket) msg; InetSocketAddress sender = datagram.sender(); RakNetPacket packet = new RakNetPacket(datagram); // If an exception happens it's because of this address this.causeAddress = sender; // Handle the packet and release the buffer client.handleMessage(sender, packet); datagram.content().readerIndex(0); // Reset position log.debug("Sent packet to client and reset datagram buffer read position"); client.callEvent(listener -> listener.handleNettyMessage(client, sender, datagram.content())); datagram.content().release(); // No longer needed log.debug("Sent datagram buffer to client and released it"); // No exceptions occurred, release the suspect this.causeAddress = null; } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { client.handleHandlerException(causeAddress, cause); } }