List of usage examples for io.netty.buffer Unpooled buffer
public static ByteBuf buffer()
From source file:at.yawk.accordion.distributed.InternalProtocol.java
License:Mozilla Public License
/** * Encode a packet to be read by other nodes. *///from w ww . j ava 2 s. c o m static ByteBuf encodePacket(byte[] typeBytes, long id, ByteBuf payload, Compressor compressor) { ByteBuf body = Unpooled.buffer(); // channel writeByteArray(body, typeBytes); // payload body.writeBytes(payload); ByteBuf compressedBody = compressor.encode(body); ByteBuf full = Unpooled.buffer(); // id header full.writeLong(id); // body full.writeBytes(compressedBody); return full; }
From source file:at.yawk.accordion.simulation.Simulation.java
License:Mozilla Public License
public static void main(String[] args) throws UnknownHostException, InterruptedException { Logger.getRootLogger().addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender.SYSTEM_OUT)); Logger.getRootLogger().setLevel(Level.DEBUG); Simulation simulation = new Simulation(); simulation.populate();/* w ww.ja v a 2s . c o m*/ TimeUnit.SECONDS.sleep(1); simulation.nodes.get(simulation.tiers[0][0]).getConnectionManager().getChannel("test").subscribe(msg -> { int len = msg.readInt(); byte[] blob = new byte[len]; msg.readBytes(blob); Log.getDefaultLogger().info("Received: " + new String(blob)); }); TimeUnit.SECONDS.sleep(1); ByteBuf msg = Unpooled.buffer(); byte[] text = "ping".getBytes(); msg.writeInt(text.length); msg.writeBytes(text); simulation.nodes.get(simulation.tiers[0][1]).getConnectionManager().getChannel("test").publish(msg); }
From source file:at.yawk.dbus.protocol.codec.MessageHeaderCodecTest.java
@Test public void testEncodeDecode() throws Exception { MessageHeader inHeader = new MessageHeader(); // defaults// ww w. j a va 2s . c om inHeader.setByteOrder(ByteOrder.BIG_ENDIAN); inHeader.setMajorProtocolVersion((byte) 1); inHeader.setSerial(1); inHeader.setMessageType(MessageType.METHOD_CALL); inHeader.setMessageBodyLength(5); inHeader.addHeader(HeaderField.PATH, ObjectPathObject.create("/org/freedesktop/UPower/devices/DisplayDevice")); inHeader.addHeader(HeaderField.DESTINATION, BasicObject.createString("org.freedesktop.UPower")); inHeader.addHeader(HeaderField.MEMBER, BasicObject.createString("org.freedesktop.DBus.Properties.Get")); inHeader.addHeader(HeaderField.SIGNATURE, SignatureObject.create(Arrays.asList(BasicType.UINT16, BasicType.UINT32))); MessageHeaderCodec codec = new MessageHeaderCodec(); ByteBuf buffer = Unpooled.buffer(); codec.encode(null, inHeader, buffer); List<Object> out = new ArrayList<>(); codec.decode(null, buffer, out); Assert.assertEquals(out.size(), 1); Assert.assertEquals(out.get(0), inHeader); }
From source file:at.yawk.dbus.protocol.object.DbusObjectTest.java
private static void testSerialize(DbusObject object) { for (int readerOffset = 0; readerOffset < 10; readerOffset++) { AlignableByteBuf buf = new AlignableByteBuf(Unpooled.buffer(), readerOffset, 8); object.serialize(buf);// ww w.j av a2 s . c o m DbusObject des = object.getType().deserialize(buf); assertEquals(des, object, "offset=" + readerOffset); assertEquals(buf.readableBytes(), 0, "offset=" + readerOffset); } }
From source file:be.guntherdw.minecraft.snowballsclient.LiteModSnowBalls.java
License:Open Source License
public C17PacketCustomPayload getRegisterPacket() { PacketBuffer pb = new PacketBuffer(Unpooled.buffer()); pb.writeByte((byte) 26); C17PacketCustomPayload registerPacket = new C17PacketCustomPayload(snowballsPluginMessageChannel, pb); return registerPacket; }
From source file:blazingcache.network.jvm.JVMChannel.java
License:Apache License
private Message cloneMessage(Message message) { ByteBuf buf = Unpooled.buffer(); DodoMessageUtils.encodeMessage(buf, message); return DodoMessageUtils.decodeMessage(buf); }
From source file:books.netty.protocol.netty.codec.TestCodeC.java
License:Apache License
public ByteBuf encode(NettyMessage msg) throws Exception { ByteBuf sendBuf = Unpooled.buffer(); sendBuf.writeInt((msg.getHeader().getCrcCode())); sendBuf.writeInt((msg.getHeader().getLength())); sendBuf.writeLong((msg.getHeader().getSessionID())); sendBuf.writeByte((msg.getHeader().getType())); sendBuf.writeByte((msg.getHeader().getPriority())); sendBuf.writeInt((msg.getHeader().getAttachment().size())); String key = null;/*from ww w . j a va 2s. c o m*/ byte[] keyArray = null; Object value = null; for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) { key = param.getKey(); keyArray = key.getBytes("UTF-8"); sendBuf.writeInt(keyArray.length); sendBuf.writeBytes(keyArray); value = param.getValue(); marshallingEncoder.encode(value, sendBuf); } key = null; keyArray = null; value = null; if (msg.getBody() != null) { marshallingEncoder.encode(msg.getBody(), sendBuf); } else sendBuf.writeInt(0); sendBuf.setInt(4, sendBuf.readableBytes()); return sendBuf; }
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(); Class<? extends Packet> clazz = msg.getClass(); byte discriminator = types.get(clazz); buffer.writeByte(discriminator);/*from w w w . j av a 2s . c om*/ 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.PacketTileState.java
License:Minecraft Mod Public
@Override public void writeData(ByteBuf data) { super.writeData(data); ByteBuf tmpState = Unpooled.buffer(); tmpState.writeByte(stateList.size()); for (StateWithId stateWithId : stateList) { tmpState.writeByte(stateWithId.stateId); stateWithId.state.writeData(tmpState); }/* w ww.j av a 2 s . c om*/ data.writeShort((short) tmpState.readableBytes()); data.writeBytes(tmpState.readBytes(tmpState.readableBytes())); }
From source file:buildcraft.core.lib.network.PacketTileState.java
License:Minecraft Mod Public
@Override public void readData(ByteBuf data) { super.readData(data); state = Unpooled.buffer(); int length = data.readUnsignedShort(); state.writeBytes(data.readBytes(length)); }