List of usage examples for io.netty.buffer Unpooled copiedBuffer
public static ByteBuf copiedBuffer(ByteBuffer... buffers)
From source file:com.thomas.netty4.SimpleEchoServerHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) { ByteBuf buf = ((ByteBuf) msg);//from w ww.ja v a 2 s .c o m //ctx.writeAndFlush(Unpooled.copiedBuffer(buf)); //System.out.println("exchange : " + Unpooled.copiedBuffer(bbuf)); String message = new String(Unpooled.copiedBuffer(buf).array(), CharsetUtil.UTF_8); System.out.println("exchange:" + message); ctx.writeAndFlush(Unpooled.copiedBuffer(buf)); if (message.startsWith("producer")) { Iterator<Entry<String, Channel>> citerator = channels.entrySet().iterator(); while (citerator.hasNext()) { Entry<String, Channel> hpch = citerator.next(); String hp = hpch.getKey(); //Channel channel = channels.get(hp); Channel channel = hpch.getValue(); if (channel == null) { channels.remove(hp); System.out.println("remove damn null channel : " + hp); System.out.println("connection leave, " + channels.size() + " connections counted"); } else if (!channel.isActive()) { channels.remove(hp); System.out.println("remove damn dead channel : " + hp); System.out.println("connection leave, " + channels.size() + " connections counted"); } else { // int random = RandomUtils.nextInt(0, 3); // if(random == 0) { // channel.writeAndFlush(Unpooled.copiedBuffer(buf)); //soon run out of memory // channel.write(Unpooled.copiedBuffer(buf)); // } channel.writeAndFlush(Unpooled.copiedBuffer(buf)); //soon run out of memory // channel.write(Unpooled.copiedBuffer(buf)); } } } }
From source file:com.thomas.netty4.websocket.client.GriffinWebSocketClientHandler.java
License:Apache License
@Override public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { Channel ch = ctx.channel();// w ww. j a v a2s .c o m if (!handshaker.isHandshakeComplete()) { handshaker.finishHandshake(ch, (FullHttpResponse) msg); System.out.println("WebSocket Client connected!"); handshakeFuture.setSuccess(); return; } if (msg instanceof FullHttpResponse) { FullHttpResponse response = (FullHttpResponse) msg; throw new IllegalStateException("Unexpected FullHttpResponse (getStatus=" + response.getStatus() + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')'); } WebSocketFrame frame = (WebSocketFrame) msg; if (frame instanceof TextWebSocketFrame) { TextWebSocketFrame textFrame = (TextWebSocketFrame) frame; MessagePack msgp = new MessagePack(); try { byte[] array = frame.content().array(); statistic(array); Value val = msgp.read(array); System.out.println(val); } catch (IOException ioe) { ioe.printStackTrace(); } System.out.println("WebSocket Client received message: " + textFrame.text()); } else if (frame instanceof BinaryWebSocketFrame) { BinaryWebSocketFrame binFrame = (BinaryWebSocketFrame) frame; MessagePack msgp = new MessagePack(); try { ByteBuf buf = Unpooled.copiedBuffer(binFrame.content()); // ByteBuf buf = binFrame.content(); byte[] array = buf.array(); statistic(array); Value val = msgp.read(array); System.out.println("WebSocket Client received message: " + val); // System.out.println(val); } catch (IOException ioe) { ioe.printStackTrace(); } } else if (frame instanceof PongWebSocketFrame) { System.out.println("WebSocket Client received pong"); } else if (frame instanceof CloseWebSocketFrame) { System.out.println("WebSocket Client received closing"); ch.close(); } }
From source file:com.whirvis.jraknet.Packet.java
License:Open Source License
/** * Creates packet from an existing {@link DatagramPacket}. * /*from w w w .java2 s. c o m*/ * @param datagram * the {@link DatagramPacket} to read from. */ public Packet(DatagramPacket datagram) { this(Unpooled.copiedBuffer(datagram.content())); }
From source file:com.whirvis.jraknet.Packet.java
License:Open Source License
/** * Creates a packet from an existing <code>byte[]</code> * // w ww .j a va 2 s . c om * @param data * the <code>byte[]</code> to read from. */ public Packet(byte[] data) { this(Unpooled.copiedBuffer(data)); }
From source file:com.whirvis.jraknet.Packet.java
License:Open Source License
/** * Creates a packet from an existing packet's buffer. * //from w ww . j a va2s.com * @param packet * the packet whose buffer to copy and then read from and write * to. */ public Packet(Packet packet) { this(Unpooled.copiedBuffer(packet.copy())); }
From source file:com.whirvis.jraknet.Packet.java
License:Open Source License
/** * Updates the buffer./*from w w w .j a v a2 s. c o m*/ * * @param data * the <code>byte[]</code> to create the new buffer from. * @return the packet. * @throws NullPointerException * if the <code>data</code> is <code>null</code>. */ public final Packet setBuffer(byte[] data) throws NullPointerException { if (data == null) { throw new NullPointerException("Data cannot be null"); } return this.setBuffer(Unpooled.copiedBuffer(data)); }
From source file:com.whirvis.jraknet.Packet.java
License:Open Source License
/** * Flips the packet./* w w w . j a va2 s . co m*/ * * @return the packet. */ public Packet flip() { byte[] data = buffer.array(); this.buffer = Unpooled.copiedBuffer(data); return this; }
From source file:com.whirvis.jraknet.RakNetPacket.java
License:Open Source License
/** * Creates a RakNet packet.//from w w w.j a va 2s .co m * * @param data * the byte array to read to read from. The byte array must have * at least one byte to be read from for the ID. * @throws IllegalArgumentException * if the length of the <code>data</code> is less than * <code>1</code>. * @see #RakNetPacket(ByteBuf) */ public RakNetPacket(byte[] data) throws IllegalArgumentException { this(Unpooled.copiedBuffer(data)); }
From source file:com.whirvis.jraknet.RakNetPacket.java
License:Open Source License
/** * Updates the buffer.// w ww . j ava 2 s. c o m * * @param data * the <code>byte[]</code> to create the new buffer from. * @param updateId * <code>true</code> if the ID should be updated, * <code>false</code> otherwise. * @return the packet. * @throws NullPointerException * if the <code>data</code> is <code>null</code>. * @throws IndexOutOfBoundsException * if <code>updateId</code> is <code>true</code> and the new * buffer has less than <code>1</code> readable * <code>byte</code>. * @see #setBuffer(byte[]) */ public final RakNetPacket setBuffer(byte[] data, boolean updateId) throws NullPointerException, IndexOutOfBoundsException { return this.setBuffer(Unpooled.copiedBuffer(data), updateId); }
From source file:com.whizzosoftware.foscam.camera.protocol.OrderDecoderTest.java
License:Open Source License
@Test public void testValidDatagram() throws Exception { byte[] b = new byte[] { 'M', 'O', '_', 'I', // camera operate protocol 1, 0, // operation code 0, // reserve 0, 0, 0, 0, 0, 0, 0, 0, // reserve 0x26, 0, 0, 0, // text length 0, 0, 0, 0, // reserve 'c', 'a', 'm', 'e', 'r', 'a', '1', ' ', ' ', ' ', ' ', ' ', ' ', // camera ID 'M', 'y', ' ', 'C', 'a', 'm', 'e', 'r', 'a', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // camera name (byte) 192, (byte) 168, (byte) 0, (byte) 150 }; List<Object> orders = new ArrayList<>(); OrderDecoder decoder = new OrderDecoder(); decoder.decode(null, Unpooled.copiedBuffer(b), orders); assertEquals(1, orders.size());/*from w w w . j av a 2 s . com*/ assertTrue(orders.get(0) instanceof SearchResponse); }