Example usage for io.netty.buffer ByteBuf readByte

List of usage examples for io.netty.buffer ByteBuf readByte

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf readByte.

Prototype

public abstract byte readByte();

Source Link

Document

Gets a byte at the current readerIndex and increases the readerIndex by 1 in this buffer.

Usage

From source file:controlspy3.SpyAsServer.java

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    System.out.println("\nrecibio algo Servidor");
    ByteBuf in = (ByteBuf) msg;
    ArrayList in2 = new ArrayList();
    String messageServer = "";
    try {/* w w  w.  ja  v a2  s .c o m*/
        while (in.isReadable()) { // (1)
            byte rxByte = in.readByte();
            in2.add(rxByte);
        }
        ux1 = new UnixTime1(in2);
        System.out.println("A");
        SpyAsServer.channelSend();

        messageServer = "";
        for (int i = 0; i < in2.size(); i++) {
            byte rxByte = (byte) in2.get(i);
            messageServer += String.valueOf(Character.toChars(rxByte));
        }

    } catch (Exception e) {
        System.out.println("Entro al ERROR RARO");
    } finally {
        in.release(); // (2)
    }
}

From source file:cyril.server.io.AuthHandler.java

License:Open Source License

@Override
public void inboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    if (!timeState.compareAndSet(0, DATA_MASK)) {
        // Other thread is reading (?!) or timeout has been reached in the
        // past.//from   www .  j av  a2s.co m
        return;
    }

    boolean goOn = true;
    while (goOn && in.readable() && ((timeState.get() & KILL_MASK) == 0)) {
        // The killmask is set when something goes wrong
        // That why we test for it with every cycle

        PaketPosition s = paketState;
        switch (s) {
        case START:
            goOn = handleIncomingStart(ctx, in);
            break;

        case CREATE_PENDING:
            // Client tried to send something while *we* were sending the
            // login data.
            // Client is trying to troll / spam us.
            readerAborts(ctx);
            goOn = false;
            break;

        case MUST_LOGIN:
            if (in.readByte() == '!') {
                paketState = PaketPosition.LOGIN;
            } else {
                readerAborts(ctx);
                goOn = false;
            }
            break;

        case LOGIN:
            // Awaiting the first two bytes
            if (in.readableBytes() < 2) {
                goOn = false;
            } else {
                int usernameLength = in.readShort();
                goOn = auth.isValidUsernameLength(wrapped, usernameLength);
                if (goOn) {
                    username = new byte[usernameLength];
                    index = 0;
                }
            }
            break;

        case LOGIN_USER:
            goOn = handleIncomingUsername(ctx, in);
            break;

        case LOGIN_TOKEN_LENGTH:
            goOn = handleIncomingTokenLength(ctx, in);
            break;

        case LOGIN_TOKEN:
            goOn = handleIncomingToken(ctx, in);
            break;

        case DECLINED_WAIT:
            goOn = handleIncomingDeclined(ctx, in);
            break;

        case AUTHENTICATED:
        default:
            throw new InternalError("Dafuq: Illegal / Unknown" + " PaketPosition " + s);
        }
    }

    if (paketState == PaketPosition.AUTHENTICATED) {
        timeState.set(0);
        return;
    }

    int oldState;
    do {
        oldState = timeState.get();
    } while (!timeState.compareAndSet(oldState, oldState & ~DATA_MASK));
    if ((oldState | KILL_MASK) != 0) {
        // Don't care about anything when KILL_MASK is set.
        readerAborts(ctx, true);
        return;
    }
}

From source file:cyril.server.io.AuthHandler.java

License:Open Source License

/**
 * Handles the first incoming byte and returns whether further processing
 * may happen./*from w ww .ja va 2s  .c om*/
 * 
 * @param ctx
 *            The current context
 * @param in
 *            The buffer to read from (guaranteed to have at least one
 *            readable byte)
 * @return whether further processing may happen
 */
private boolean handleIncomingStart(ChannelHandlerContext ctx, ByteBuf in) {
    byte b = in.readByte();
    switch (b) {
    case '?':
        // Requesting a new account
        Login l = auth.createLogin(wrapped);
        if (l == null) {
            // Not allowed, at least not currently
            readerAborts(ctx, false);
        }
        paketState = PaketPosition.CREATE_PENDING;

        // Trigger transmission of token
        new LoginTransmission(ctx, l).start();

        return false;

    case '!':
        paketState = PaketPosition.LOGIN;
        return true;

    default: // Illegal paket at this stage
        readerAborts(ctx, false);
        return false;
    }

    // This should be dead code:
    // System.out.println();
}

From source file:cyril.server.io.AuthHandler.java

License:Open Source License

/**
 * Handles and compares the incoming token. Automatically switches to
 * "discard mode" to be more efficient without revealing the fact that a
 * wrong byte has been encountered.<br />
 * If the transmission of the token is complete (and both tokens match), the
 * login process is complete. The next handler is automatically called, and
 * this handler is dislodged from the channel.
 * //from   w ww  . j  a va2s .com
 * @param ctx
 *            The current context
 * @param in
 *            The buffer to read from (guaranteed to have at least one
 *            readable byte)
 * @return whether further processing may happen
 */
private boolean handleIncomingToken(ChannelHandlerContext ctx, ByteBuf in) {
    // This *does* work with 0 length tokens, but only if the next packet
    // starts right away.
    // This doesn't pose a security threat -- if you ever hand out 0 byte
    // tokens, there's no security anymore that could be threatened.

    int checkable = Math.min(login.token.length - index, in.readableBytes());
    for (; checkable > 0; checkable--) {
        if (login.token[index] != in.readByte()) {
            paketState = PaketPosition.DECLINED_WAIT;
            break;
        }
        index++;
    }

    if (index >= login.token.length) {
        // Token is correct!
        // Dispensing product
        killTask.cancel(false);
        paketState = PaketPosition.AUTHENTICATED;
        listener.stateChanged(ctx.channel(), ConnectionState.GOOD_AUTH);
        service.handleNewSession(ctx.channel(), login);
        ctx.pipeline().remove(this);
    }

    return false;
}

From source file:dan200.qcraft.shared.QCraftPacket.java

License:Open Source License

@Override
public void fromBytes(ByteBuf buffer) {
    packetType = buffer.readByte();
    byte nString = buffer.readByte();
    byte nInt = buffer.readByte();
    int nByte = buffer.readInt();
    if (nString == 0) {
        dataString = null;//from  ww w.  j  av  a  2 s. com
    } else {
        dataString = new String[nString];
        for (int k = 0; k < nString; k++) {
            if (buffer.readBoolean()) {
                int len = buffer.readInt();
                byte[] b = new byte[len];
                buffer.readBytes(b);
                try {
                    dataString[k] = new String(b, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    dataString[k] = null;
                }
            }
        }
    }
    if (nInt == 0) {
        dataInt = null;
    } else {
        dataInt = new int[nInt];
        for (int k = 0; k < nInt; k++) {
            dataInt[k] = buffer.readInt();
        }
    }
    if (nByte == 0) {
        dataByte = null;
    } else {
        dataByte = new byte[nByte][];
        for (int k = 0; k < nByte; k++) {
            int length = buffer.readInt();
            if (length > 0) {
                dataByte[k] = new byte[length];
                buffer.getBytes(buffer.readerIndex(), dataByte[k]);
            }
        }
    }
}

From source file:de.jackwhite20.cascade.shared.pipeline.handler.PacketDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> out) throws Exception {

    int length = byteBuf.readInt();

    // Allow packets with no data
    if (length > 0) {
        byte id = byteBuf.readByte();

        try {//from ww  w.  j  a  va  2s . c  o m
            Packet packet = protocol.create(id);
            packet.read(byteBuf);

            out.add(packet);
        } catch (IllegalStateException e) {
            ctx.fireExceptionCaught(e);
        }
    }
}

From source file:de.jackwhite20.comix.network.Protocol.java

License:Open Source License

public static int readVarInt(ByteBuf input, int maxBytes) {
    int out = 0;/*from   ww w  . jav a2 s .  c  o  m*/
    int bytes = 0;
    byte in;
    while (true) {
        in = input.readByte();

        out |= (in & 0x7F) << (bytes++ * 7);

        if (bytes > maxBytes) {
            throw new RuntimeException("VarInt too big");
        }

        if ((in & 0x80) != 0x80) {
            break;
        }
    }

    return out;
}

From source file:de.jackwhite20.hftp.shared.packet.DeleteFileResultPacket.java

License:Open Source License

@Override
public void read(ByteBuf byteBuf) throws Exception {

    result = Result.values()[byteBuf.readByte()];
}

From source file:de.sanandrew.mods.turretmod.network.PacketInitAssemblyCrafting.java

License:Creative Commons License

@Override
public void fromBytes(ByteBuf buf) {
    this.pos = new BlockPos(buf.readInt(), buf.readInt(), buf.readInt());
    this.crfUUID = ByteBufUtils.readUTF8String(buf);
    this.count = buf.readByte();
}

From source file:de.sanandrew.mods.turretmod.network.PacketOpenGui.java

License:Creative Commons License

@Override
public void fromBytes(ByteBuf buf) {
    this.guiId = buf.readByte();
    this.x = buf.readInt();
    this.y = buf.readInt();
    this.z = buf.readInt();
}