Example usage for io.netty.buffer Unpooled buffer

List of usage examples for io.netty.buffer Unpooled buffer

Introduction

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

Prototype

public static ByteBuf buffer(int initialCapacity) 

Source Link

Document

Creates a new big-endian Java heap buffer with the specified capacity , which expands its capacity boundlessly on demand.

Usage

From source file:com.whizzosoftware.hobson.lifx.api.codec.LIFXFrameEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext channelHandlerContext, Message m, List<Object> list)
        throws Exception {
    ByteBuf buf = null;/* w  w  w  . ja  va  2  s  .c  om*/

    switch (m.getType()) {
    case DeviceGetPower.TYPE:
    case DeviceGetService.TYPE:
    case LightGet.TYPE:
        buf = Unpooled.buffer(m.getLength());
        writeMessageHeader(buf, m.getHeader());
        break;
    case DeviceSetPower.TYPE:
        buf = Unpooled.buffer(m.getLength());
        writeDeviceSetPower(buf, (DeviceSetPower) m);
        break;
    case LightSetColor.TYPE:
        buf = Unpooled.buffer(m.getLength());
        writeLightSetColor(buf, (LightSetColor) m);
        break;
    case LightSetPower.TYPE:
        buf = Unpooled.buffer(m.getLength());
        writeLightSetPower(buf, (LightSetPower) m);
        break;
    default:
        logger.error("Unsupported message passed to encoder: {}", m);
    }

    if (buf != null) {
        list.add(new DatagramPacket(buf, m.getRecipient()));
    } else {
        logger.error("Unknown message passed to encoder: {}", m);
    }
}

From source file:com.witjit.game.client.Client.java

License:Apache License

public static void main(String[] args) throws Exception {

    EventLoopGroup group = new NioEventLoopGroup();
    try {/*w w  w .  j  a v  a2 s.  co  m*/
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new ClientInitializer());

        // Start the connection attempt.
        Channel ch = b.connect(HOST, PORT).sync().channel();

        // Read commands from the stdin.
        ChannelFuture lastWriteFuture = null;
        //            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        //            for (;;) {
        //                String line = in.readLine();
        //                if (line == null) {
        //                    break;
        //                }
        //
        //                // Sends the received line to the server.
        //                lastWriteFuture = ch.writeAndFlush(line + "\r\n");
        //
        //                // If user typed the 'bye' command, wait until the server closes
        //                // the connection.
        //                if ("bye".equals(line.toLowerCase())) {
        //                    ch.closeFuture().sync();
        //                    break;
        //                }
        //            }

        ByteBuf outb = Unpooled.buffer(200);
        outb.writeShort(127);
        outb.writeShort(10);
        outb.writeInt(10001);
        outb.writeByte(40);
        outb.writeByte(50);
        // Sends the received line to the server.
        lastWriteFuture = ch.writeAndFlush(outb);
        // Wait until all messages are flushed before closing the channel.
        if (lastWriteFuture != null) {
            lastWriteFuture.sync();
        }

        // Wait until the connection is closed.
        ch.closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.witjit.game.client.EchoClient.java

License:Apache License

private static void sendData(Channel ch) throws InterruptedException {
    ByteBuf outb = Unpooled.buffer(200);
    outb.writeShort(127);//from   w w w  .j  a  va 2 s.c  om
    outb.writeShort(10);
    outb.writeInt(10001);
    outb.writeByte(40);
    outb.writeByte(50);
    // Sends the received line to the server.
    ChannelFuture writeFuture = ch.writeAndFlush(outb);
    // Wait until all messages are flushed before closing the channel.
    if (writeFuture != null) {
        writeFuture.sync();
    }

}

From source file:com.witjit.game.server.communication.bbmessage.MessageInHandler.java

@Override
protected ByteBuf readMessage(ByteBuf mb) {
    ByteBuf buffer = Unpooled.buffer(128);
    mb.readBytes(buffer);
    return buffer;
}

From source file:com.witjit.game.server.communication.netty.handler.AbstractMessageOutHandler.java

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (!(msg instanceof Message)) {
        //TODO: handle error message.
        return;//from w  w  w . java  2s  . co  m
    }
    Message<M> message = (Message<M>) msg;
    ByteBuf buf = Unpooled.buffer(256);
    writeEndpointId(buf, message.getReceiver());
    writeEndpointId(buf, message.getSender());
    writeMessage(buf, message.getContent());
    ctx.write(buf, promise);
}

From source file:com.witjit.game.server.communication.netty.handler.NodeChannelInitializer.java

protected void sendNodeInformation(Channel ch) throws Exception {
    ByteBuf buffer = Unpooled.buffer(4);
    buffer.writeShort(localNodeId);
    ch.writeAndFlush(buffer);
}

From source file:com.xxx.demo.netty.echo.EchoClientHandler.java

License:Apache License

/**
 * Creates a client-side handler./*from   w w  w  .ja  v  a2s .  co  m*/
 */
public EchoClientHandler() {
    firstMessage = Unpooled.buffer(EchoClient.SIZE);
    //        for (int i = 0; i < firstMessage.capacity(); i ++) {
    //            firstMessage.writeByte((byte) i);
    //        }
    firstMessage.writeBytes("123".getBytes());
}

From source file:com.xxx.util.io.client.netty.EchoClient.java

License:Apache License

public byte[] sendAndRec(byte[] bytes) {
    ByteBuf firstMessage = Unpooled.buffer(EchoClient.SIZE);
    firstMessage.writeBytes(bytes);/*from   ww  w.  j  a v  a 2  s.c  om*/
    channel.writeAndFlush(firstMessage);

    return null;
}

From source file:com.xxx.util.io.client.netty.EchoClientHandler.java

License:Apache License

/**
 * Creates a client-side handler.//  w  ww. ja v a 2s  .co m
 */
public EchoClientHandler() {
    firstMessage = Unpooled.buffer(EchoClient.SIZE);
    //        for (int i = 0; i < firstMessage.capacity(); i ++) {
    //            firstMessage.writeByte((byte) i);
    //        }
    firstMessage.writeBytes("00000003123".getBytes());
}

From source file:com.xxx.util.io.client.netty.EchoClientHandler.java

License:Apache License

public EchoClientHandler(int headSize, boolean isLongConnect) {
    firstMessage = Unpooled.buffer(EchoClient.SIZE);
    firstMessage.writeBytes("00000003123".getBytes());
    this.headSize = headSize;
    this.isLongConnect = isLongConnect;
}