me.bigteddy98.mcproxy.protocol.handlers.ServerSideHandler.java Source code

Java tutorial

Introduction

Here is the source code for me.bigteddy98.mcproxy.protocol.handlers.ServerSideHandler.java

Source

/* 
 * MCProxy
 * Copyright (C) 2014 Sander Gielisse
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package me.bigteddy98.mcproxy.protocol.handlers;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

import java.util.List;

import me.bigteddy98.mcproxy.ProxyLogger;
import me.bigteddy98.mcproxy.protocol.NetworkManager;
import me.bigteddy98.mcproxy.protocol.packet.Packet;

public class ServerSideHandler extends ChannelHandlerAdapter {

    private final Channel inboundChannel;
    private final NetworkManager networkManager;

    public ServerSideHandler(NetworkManager networkManager, Channel inboundChannel) {
        this.networkManager = networkManager;
        this.inboundChannel = inboundChannel;
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        networkManager.serversidePipeline = ctx.pipeline();
        ctx.read();
        ctx.write(Unpooled.EMPTY_BUFFER);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        this.networkManager.disconnect();
        super.channelInactive(ctx);
    }

    @Override
    public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf bufferOriginal = (ByteBuf) msg;
        ByteBuf bufferClone = Unpooled.copiedBuffer(bufferOriginal);
        final List<Packet> packets = this.networkManager.handleClientBoundPackets((ByteBuf) msg, bufferClone);
        bufferClone.release();

        inboundChannel.writeAndFlush(msg).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                for (Packet packet : packets) {
                    packet.onSend(networkManager);
                }
                if (future.isSuccess()) {
                    ctx.channel().read();
                } else {
                    future.channel().close();
                }
            }
        });
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ProxyLogger.exception(cause);
        super.exceptionCaught(ctx, cause);
    }
}