List of usage examples for io.netty.channel ChannelHandlerContext alloc
ByteBufAllocator alloc();
From source file:org.lanternpowered.server.network.query.QueryHandler.java
License:MIT License
private void handleBasicStats(ChannelHandlerContext ctx, DatagramPacket packet, int sessionId) { LanternServer server = this.queryServer.getGame().getServer(); // TODO: Find out how to support the size and max size properties final QueryServerEvent.Basic event = SpongeEventFactory.createQueryServerEventBasic( Cause.source(ctx.channel().remoteAddress()).build(), (InetSocketAddress) ctx.channel().localAddress(), "SMP", this.getWorldName(), server.getMotd().toPlain(), server.getMaxPlayers(), Integer.MAX_VALUE, server.getOnlinePlayers().size(), 0); Sponge.getEventManager().post(event); final InetSocketAddress address = event.getAddress(); ByteBuf buf = ctx.alloc().buffer(); buf.writeByte(ACTION_STATS);//from w ww.j a v a2s. c om buf.writeInt(sessionId); writeString(buf, event.getMotd()); writeString(buf, event.getGameType()); writeString(buf, event.getMap()); writeString(buf, String.valueOf(event.getPlayerCount())); writeString(buf, String.valueOf(event.getMaxPlayerCount())); buf.writeShortLE(address.getPort()); writeString(buf, address.getHostString()); ctx.write(new DatagramPacket(buf, packet.sender())); }
From source file:org.lanternpowered.server.network.query.QueryHandler.java
License:MIT License
private void handleFullStats(ChannelHandlerContext ctx, DatagramPacket packet, int sessionId) { final LanternGame game = this.queryServer.getGame(); final LanternServer server = game.getServer(); final Platform platform = game.getPlatform(); final PluginContainer api = platform.getContainer(Platform.Component.API); final PluginContainer impl = platform.getContainer(Platform.Component.IMPLEMENTATION); final PluginContainer mc = platform.getContainer(Platform.Component.GAME); final StringBuilder plugins = new StringBuilder().append(impl.getName()).append(" ") .append(impl.getVersion()).append(" on ").append(api.getName()).append(" ") .append(api.getVersion());//from ww w .j ava 2 s. c o m if (this.showPlugins) { final List<PluginContainer> containers = new ArrayList<>(game.getPluginManager().getPlugins()); containers.remove(api); containers.remove(impl); containers.remove(mc); char delim = ':'; for (PluginContainer plugin : containers) { plugins.append(delim).append(' ').append(plugin.getName()); delim = ';'; } } final List<String> playerNames = server.getOnlinePlayers().stream().map(CommandSource::getName) .collect(Collectors.toList()); final Cause cause = Cause.source(ctx.channel().remoteAddress()).build(); final QueryServerEvent.Full event = SpongeEventFactory.createQueryServerEventFull(cause, (InetSocketAddress) ctx.channel().localAddress(), new HashMap<>(), "MINECRAFT", "SMP", getWorldName(), server.getMotd().toPlain(), playerNames, plugins.toString(), mc.getVersion().orElse("unknown"), server.getMaxPlayers(), Integer.MAX_VALUE, playerNames.size(), 0); final InetSocketAddress address = event.getAddress(); final Map<String, Object> data = new LinkedHashMap<>(); data.put("hostname", event.getMotd()); data.put("gametype", event.getGameType()); data.put("game_id", event.getGameId()); data.put("version", event.getVersion()); data.put("plugins", event.getPlugins()); data.put("map", event.getMap()); data.put("numplayers", event.getPlayerCount()); data.put("maxplayers", event.getMaxPlayerCount()); data.put("hostport", address.getPort()); data.put("hostip", address.getHostString()); event.getCustomValuesMap().entrySet().stream().filter(entry -> !data.containsKey(entry.getKey())) .forEach(entry -> data.put(entry.getKey(), entry.getValue())); final ByteBuf buf = ctx.alloc().buffer(); buf.writeByte(ACTION_STATS); buf.writeInt(sessionId); // constant: splitnum\x00\x80\x00 buf.writeBytes(new byte[] { 0x73, 0x70, 0x6C, 0x69, 0x74, 0x6E, 0x75, 0x6D, 0x00, (byte) 0x80, 0x00 }); for (Entry<String, Object> e : data.entrySet()) { writeString(buf, e.getKey()); writeString(buf, String.valueOf(e.getValue())); } buf.writeByte(0); // constant: \x01player_\x00\x00 buf.writeBytes(new byte[] { 0x01, 0x70, 0x6C, 0x61, 0x79, 0x65, 0x72, 0x5F, 0x00, 0x00 }); for (Player player : game.getServer().getOnlinePlayers()) { writeString(buf, player.getName()); } buf.writeByte(0); ctx.write(new DatagramPacket(buf, packet.sender())); }
From source file:org.lanternpowered.server.network.rcon.RconFramingHandler.java
License:MIT License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() < 4) { return;/* ww w . jav a 2s .c o m*/ } in.markReaderIndex(); int length = in.readIntLE(); if (in.readableBytes() < length) { in.resetReaderIndex(); return; } final ByteBuf buf = ctx.alloc().buffer(length); in.readBytes(buf, length); out.add(buf); }
From source file:org.lanternpowered.server.network.rcon.RconHandler.java
License:MIT License
private static void sendResponse(ChannelHandlerContext ctx, int requestId, int type, String payload) { final ByteBuf buf = ctx.alloc().buffer(); buf.writeIntLE(requestId);//from w w w . j a va 2 s . c o m buf.writeIntLE(type); buf.writeBytes(payload.getBytes(StandardCharsets.UTF_8)); buf.writeByte(0); buf.writeByte(0); ctx.write(buf); }
From source file:org.lightmare.remote.rcp.decoders.RcpEncoder.java
License:Open Source License
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws IOException { RcpWrapper wrapper = ObjectUtils.cast(msg, RcpWrapper.class); boolean valid = wrapper.isValid(); int validNum = BooleanNumber.getValue(valid); Object value = wrapper.getValue(); byte[] valueBt = NativeSerializer.serialize(value); int valueSize = valueBt.length; int protSize = (RpcUtils.INT_SIZE + RpcUtils.BYTE_SIZE + valueSize); ByteBuf buffer = ctx.alloc().buffer(protSize); buffer.writeInt(valueSize);/*from w ww .j av a 2s.c o m*/ buffer.writeByte(validNum); buffer.writeBytes(valueBt); ctx.write(buffer); }
From source file:org.lightmare.remote.rpc.decoders.RpcEncoder.java
License:Open Source License
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws IOException { RpcWrapper wrapper = ObjectUtils.cast(msg, RpcWrapper.class); String beanName = wrapper.getBeanName(); String methodName = wrapper.getMethodName(); Class<?>[] paramTypes = wrapper.getParamTypes(); Class<?> interfaceClass = wrapper.getInterfaceClass(); Object[] params = wrapper.getParams(); byte[] beanNameBt = beanName.getBytes("UTF8"); byte[] beanMethodBt = NativeSerializer.serialize(methodName); byte[] paramTypesBt = NativeSerializer.serialize(paramTypes); byte[] interfaceClassBt = NativeSerializer.serialize(interfaceClass); byte[] paramBt = NativeSerializer.serialize(params); int paramsSize = RpcUtils.PROTOCOL_SIZE + beanNameBt.length + beanMethodBt.length + paramTypesBt.length + interfaceClassBt.length + paramBt.length; ByteBuf buffer = ctx.alloc().buffer(paramsSize); buffer.writeInt(beanNameBt.length);/* w w w .j ava 2 s.co m*/ buffer.writeInt(beanMethodBt.length); buffer.writeInt(paramTypesBt.length); buffer.writeInt(interfaceClassBt.length); buffer.writeInt(paramBt.length); buffer.writeBytes(beanNameBt); buffer.writeBytes(beanMethodBt); buffer.writeBytes(paramTypesBt); buffer.writeBytes(interfaceClassBt); buffer.writeBytes(paramBt); ctx.write(buffer); }
From source file:org.mashupbots.socko.netty.HttpChunkedFile.java
License:Apache License
@Override public HttpContent readChunk(ChannelHandlerContext ctx) throws Exception { long offset = this.offset; if (offset >= endOffset) { if (sentLastChunk) { return null; } else {/*from w w w .java 2 s. c o m*/ // Send last chunk for this file sentLastChunk = true; return new DefaultLastHttpContent(); } } int chunkSize = (int) Math.min(this.chunkSize, endOffset - offset); // Check if the buffer is backed by an byte array. If so we can optimize it a bit an safe a copy ByteBuf buf = ctx.alloc().heapBuffer(chunkSize); boolean release = true; try { file.readFully(buf.array(), buf.arrayOffset(), chunkSize); buf.writerIndex(chunkSize); this.offset = offset + chunkSize; release = false; return new DefaultHttpContent(buf); } finally { if (release) { buf.release(); } } }
From source file:org.neo4j.bolt.transport.SocketTransportHandler.java
License:Open Source License
private void chooseProtocolVersion(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception { switch (protocolChooser.handleVersionHandshakeChunk(buffer, ctx.channel())) { case PROTOCOL_CHOSEN: protocol = protocolChooser.chosenProtocol(); ctx.writeAndFlush(ctx.alloc().buffer(4).writeInt(protocol.version())); // If there is more data pending, the client optimistically sent this in its initial payload. It really // shouldn't be doing that since it can't know which versions we support, but here we are anyway. // Emulate a second call to channelRead, the remaining data in the buffer will be forwarded to the newly // selected protocol. if (buffer.readableBytes() > 0) { channelRead(ctx, buffer);//from w w w. ja v a 2 s. c o m } else { buffer.release(); } return; case NO_APPLICABLE_PROTOCOL: buffer.release(); ctx.writeAndFlush(wrappedBuffer(new byte[] { 0, 0, 0, 0 })).sync().channel().close(); return; case INVALID_HANDSHAKE: buffer.release(); ctx.close(); return; case PARTIAL_HANDSHAKE: } }
From source file:org.neo4j.bolt.transport.TransportSelectionHandler.java
License:Open Source License
private void enableSsl(ChannelHandlerContext ctx) { ChannelPipeline p = ctx.pipeline();/*from w w w .ja v a2s .c om*/ p.addLast(sslCtx.newHandler(ctx.alloc())); p.addLast(new TransportSelectionHandler(null, true, logging, protocolVersions)); p.remove(this); }
From source file:org.neo4j.bolt.v1.transport.socket.SocketTransportHandlerTest.java
License:Open Source License
@Test public void shouldCloseSessionOnChannelClose() throws Throwable { // Given/*ww w . ja v a 2s. c om*/ Session session = mock(Session.class); Channel ch = mock(Channel.class); ChannelHandlerContext ctx = mock(ChannelHandlerContext.class); when(ctx.channel()).thenReturn(ch); when(ch.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT); when(ctx.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT); SocketTransportHandler handler = new SocketTransportHandler(protocolChooser(session), NullLogProvider.getInstance()); // And Given a session has been established handler.channelRead(ctx, handshake()); // When handler.channelInactive(ctx); // Then verify(session).close(); }