Example usage for io.netty.channel ChannelHandlerContext channel

List of usage examples for io.netty.channel ChannelHandlerContext channel

Introduction

In this page you can find the example usage for io.netty.channel ChannelHandlerContext channel.

Prototype

Channel channel();

Source Link

Document

Return the Channel which is bound to the ChannelHandlerContext .

Usage

From source file:buildcraft.builders.network.PacketHandlerBuilders.java

License:Minecraft Mod Public

@Override
public void decodeInto(ChannelHandlerContext ctx, ByteBuf data, BuildCraftPacket packet) {
    super.decodeInto(ctx, data, packet);

    try {/*w  ww  .j  av a2s .co m*/
        INetHandler netHandler = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get();
        EntityPlayer player = CoreProxy.proxy.getPlayerFromNetHandler(netHandler);

        int packetID = packet.getID();

        switch (packetID) {
        case PacketIds.ARCHITECT_NAME:
            onArchitectName(player, (PacketUpdate) packet);
            break;
        case PacketIds.LIBRARY_ACTION:
            onLibraryAction(player, (PacketLibraryAction) packet);
            break;
        case PacketIds.LIBRARY_SELECT:
            onLibrarySelect(player, (PacketLibraryAction) packet);
            break;
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:buildcraft.core.lib.network.base.PacketHandler.java

License:Minecraft Mod Public

@Override
protected void channelRead0(ChannelHandlerContext ctx, Packet packet) {
    Side side = ctx.channel().attr(NetworkRegistry.CHANNEL_SOURCE).get();
    if (packet.dimensionId == INVALID_DIM_ID)
        BCLog.logger.warn("Found a packet with an invalid dimension id! [read]");
    if (!packet.hasDoneByteStuff)
        throw new IllegalArgumentException("Tried to use a packet that hasn't read yet! THIS IS VERY BAD!");
    if (side != null) {
        getQueue(side, packet.dimensionId).add(packet);
        livePacketCount++;//from  ww w. j  ava2  s  .  c  om
    } else {
        BCLog.logger.error("Found a message without a side! THIS IS VERY BAD, MAJOR ERRORS COULD OCCOUR!");
    }
}

From source file:buildcraft.core.lib.network.ChannelHandler.java

License:Minecraft Mod Public

@Override
protected void encode(ChannelHandlerContext ctx, Packet msg, List<Object> out) throws Exception {
    ByteBuf buffer = Unpooled.buffer();//from  w w w.j a va2s . c  om
    Class<? extends Packet> clazz = msg.getClass();
    byte discriminator = types.get(clazz);
    buffer.writeByte(discriminator);
    msg.writeData(buffer);
    FMLProxyPacket proxy = new FMLProxyPacket(buffer.copy(),
            ctx.channel().attr(NetworkRegistry.FML_CHANNEL).get());
    WeakReference<FMLProxyPacket> ref = ctx.attr(INBOUNDPACKETTRACKER).get().get();
    FMLProxyPacket old = ref == null ? null : ref.get();
    if (old != null) {
        proxy.setDispatcher(old.getDispatcher());
    }
    out.add(proxy);
}

From source file:buildcraft.core.lib.network.PacketHandler.java

License:Minecraft Mod Public

@Override
protected void channelRead0(ChannelHandlerContext ctx, Packet packet) {
    try {// w  w w . j  ava  2s  .c  o  m
        INetHandler netHandler = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get();
        EntityPlayer player = CoreProxy.proxy.getPlayerFromNetHandler(netHandler);

        int packetID = packet.getID();

        switch (packetID) {
        case PacketIds.TILE_UPDATE: {
            onTileUpdate(player, (PacketTileUpdate) packet);
            break;
        }

        case PacketIds.ENTITY_UPDATE: {
            onEntityUpdate(player, (PacketEntityUpdate) packet);
            break;
        }

        case PacketIds.COMMAND: {
            ((PacketCommand) packet).handle(player);
            break;
        }

        case PacketIds.STATE_UPDATE: {
            PacketTileState pkt = (PacketTileState) packet;
            World world = player.worldObj;

            TileEntity tile = world.getTileEntity(pkt.posX, pkt.posY, pkt.posZ);

            if (tile instanceof ISyncedTile) {
                pkt.applyStates((ISyncedTile) tile);
            }

            break;
        }

        case PacketIds.GUI_RETURN: {
            // action will have happened already at read time
            break;
        }

        case PacketIds.GUI_WIDGET: {
            // action will have happened already at read time
            break;
        }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:buildcraft.core.network.PacketHandler.java

License:Minecraft Mod Public

@Override
protected void channelRead0(ChannelHandlerContext ctx, BuildCraftPacket packet) {
    try {/*from  w w w.  j  a  va2  s . co m*/
        INetHandler netHandler = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get();
        EntityPlayer player = CoreProxy.proxy.getPlayerFromNetHandler(netHandler);

        int packetID = packet.getID();

        switch (packetID) {
        case PacketIds.TILE_UPDATE: {
            onTileUpdate(player, (PacketTileUpdate) packet);
            break;
        }

        case PacketIds.STATE_UPDATE: {
            PacketTileState pkt = (PacketTileState) packet;
            World world = player.worldObj;

            TileEntity tile = world.getTileEntity(pkt.posX, pkt.posY, pkt.posZ);

            if (tile instanceof ISyncedTile) {
                pkt.applyStates((ISyncedTile) tile);
            }

            break;
        }

        case PacketIds.GUI_RETURN: {
            // action will have happened already at read time
            break;
        }

        case PacketIds.GUI_WIDGET: {
            // action will have happened already at read time
            break;
        }

        case PacketIds.RPC_TILE: {
            ((PacketRPCTile) packet).call(player);

            break;
        }

        case PacketIds.RPC_PIPE: {
            // TODO: RPC pipes are not used right now. Ressurect this
            // code if needed later.
            /*
             * PacketRPCPipe rpc = new PacketRPCPipe(); rpc.sender = player;
             *
             * int dimId = data.readShort(); World world = null;
             *
             * if (!rpc.sender.worldObj.isRemote) { // if this is a server,
             * then get the world
             *
             * world = DimensionManager.getProvider(dimId).worldObj; } else
             * if (rpc.sender.worldObj.provider.dimensionId == dimId) { //
             * if the player is on this world, then synchronize things
             *
             * world = rpc.sender.worldObj; }
             *
             * if (world != null) { int x = data.readInt(); int y =
             * data.readInt(); int z = data.readInt();
             *
             * TileEntity tile = world.getTileEntity(x, y, z);
             *
             * if (tile instanceof TileGenericPipe) { rpc.setPipe
             * (((TileGenericPipe) tile).pipe); rpc.readData(data); } }
             */
            break;

        }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:buildcraft.factory.network.PacketHandlerFactory.java

License:Minecraft Mod Public

@Override
protected void channelRead0(ChannelHandlerContext ctx, BuildCraftPacket packet) {
    try {// w  w w. j  a va 2s .c  om
        INetHandler netHandler = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get();
        EntityPlayer player = CoreProxy.proxy.getPlayerFromNetHandler(netHandler);

        int packetID = packet.getID();

        switch (packetID) {

        case PacketIds.REFINERY_FILTER_SET:
            onRefinerySelect(player, (PacketUpdate) packet);
            break;

        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:buildcraft.silicon.network.PacketHandlerSilicon.java

License:Minecraft Mod Public

@Override
protected void channelRead0(ChannelHandlerContext ctx, BuildCraftPacket packet) {
    try {//from   ww w. j a v  a2s  .co  m
        INetHandler netHandler = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get();

        EntityPlayer player = CoreProxy.proxy.getPlayerFromNetHandler(netHandler);

        int packetID = packet.getID();

        switch (packetID) {
        case PacketIds.SELECTION_ASSEMBLY_SEND:
            onSelectionUpdate(player, (PacketNBT) packet);
            break;
        case PacketIds.SELECTION_ASSEMBLY:
            onAssemblySelect(player, (PacketNBT) packet);
            break;
        case PacketIds.SELECTION_ASSEMBLY_GET:
            onAssemblyGetSelection(player, (PacketCoordinates) packet);
            break;
        case PacketIds.ADVANCED_WORKBENCH_SETSLOT:
            onAdvancedWorkbenchSet(player, (PacketSlotChange) packet);
            break;

        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:buildcraft.transport.network.PacketHandlerTransport.java

License:Minecraft Mod Public

/**
 * TODO: A lot of this is based on the player to retrieve the world.
 * Passing a dimension id would be more appropriate. More generally, it
 * seems like a lot of these packets could be replaced with tile-based
 * RPCs./*  w  w  w .  ja  v a 2s . co m*/
 */
@Override
protected void channelRead0(ChannelHandlerContext ctx, BuildCraftPacket packet) {
    try {
        INetHandler netHandler = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get();
        EntityPlayer player = CoreProxy.proxy.getPlayerFromNetHandler(netHandler);

        int packetID = packet.getID();

        switch (packetID) {
        case PacketIds.PIPE_POWER:
            onPacketPower(player, (PacketPowerUpdate) packet);
            break;
        case PacketIds.PIPE_LIQUID:
            // action will have happened already at read time
            break;
        case PacketIds.PIPE_TRAVELER: {
            onPipeTravelerUpdate(player, (PacketPipeTransportTraveler) packet);
            break;
        }
        case PacketIds.GATE_ACTIONS:
            onGateActions(player, (PacketUpdate) packet);
            break;
        case PacketIds.GATE_TRIGGERS:
            onGateTriggers(player, (PacketUpdate) packet);
            break;
        case PacketIds.GATE_SELECTION:
            onGateSelection(player, (PacketUpdate) packet);
            break;
        case PacketIds.PIPE_ITEMSTACK: {
            // action will have happened already at read time
            break;
        }
        case PacketIds.PIPE_GATE_EXPANSION_MAP: {
            // action will have happened already at read time
            break;
        }

        /**
         * SERVER SIDE *
         */
        case PacketIds.DIAMOND_PIPE_SELECT: {
            onDiamondPipeSelect(player, (PacketSlotChange) packet);
            break;
        }

        case PacketIds.EMERALD_PIPE_SELECT: {
            onEmeraldPipeSelect(player, (PacketSlotChange) packet);
            break;
        }

        case PacketIds.GATE_REQUEST_INIT:
            onGateInitRequest(player, (PacketCoordinates) packet);
            break;

        case PacketIds.GATE_REQUEST_SELECTION:
            onGateSelectionRequest(player, (PacketCoordinates) packet);
            break;

        case PacketIds.GATE_SELECTION_CHANGE:
            onGateSelectionChange(player, (PacketUpdate) packet);
            break;

        case PacketIds.PIPE_ITEMSTACK_REQUEST: {
            ((PacketPipeTransportItemStackRequest) packet).sendDataToPlayer(player);
            break;
        }
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:c5db.client.FutureBasedMessageHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, Response msg) throws Exception {
    switch (msg.getCommand()) {
    case MULTI:/*  w  w  w .j a  va  2s .  co m*/
        futures.get(msg.getCommandId()).set(msg);
        break;
    case MUTATE:
        futures.get(msg.getCommandId()).set(msg);
        break;
    case SCAN:
        final long scannerId = msg.getScan().getScannerId();
        ClientScanner clientScanner;

        if (CLIENT_SCANNER_MANAGER.hasScanner(scannerId)) {
            clientScanner = CLIENT_SCANNER_MANAGER.get(scannerId).get();
        } else {
            clientScanner = CLIENT_SCANNER_MANAGER.createAndGet(ctx.channel(), scannerId, msg.getCommandId());
            scannerFutures.get(msg.getCommandId()).set(scannerId);
        }

        clientScanner.add(msg.getScan());

        if (!msg.getScan().getMoreResults()) {
            clientScanner.close();
        }
        break;
    default:
        futures.get(msg.getCommandId()).set(msg);
        break;
    }
}

From source file:c5db.control.SimpleControlClient.java

License:Apache License

public ListenableFuture<CommandReply> sendRequest(CommandRpcRequest<?> request,
        InetSocketAddress remoteAddress) {
    SettableFuture<CommandReply> replyMessageFuture = SettableFuture.create();
    ChannelFuture connectFuture = client.connect(remoteAddress);
    connectFuture.addListener(new ChannelFutureListener() {
        @Override//from w ww .ja  v  a  2s.com
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                future.channel().pipeline().addLast(new SimpleChannelInboundHandler<CommandReply>() {
                    @Override
                    protected void channelRead0(ChannelHandlerContext ctx, CommandReply msg) throws Exception {
                        replyMessageFuture.set(msg);
                        ctx.channel().close();
                    }
                });

                // connected is fine, flush message:
                future.channel().writeAndFlush(request);
            } else {
                replyMessageFuture.setException(future.cause());
                future.channel().close();
            }
        }
    });

    return replyMessageFuture;
}