List of usage examples for io.netty.buffer PooledByteBufAllocator DEFAULT
PooledByteBufAllocator DEFAULT
To view the source code for io.netty.buffer PooledByteBufAllocator DEFAULT.
Click Source Link
From source file:me.ferrybig.p2pnetwork.Peer.java
public boolean routePacket(Address to, Packet packet, byte ttl) { MultiConnection router = routingTable.tryRoute(to, this::getByAddress); if (router != null) { if (!(packet instanceof RelayPacket) && !router.getDirectNode().equals(to)) { Packet unwrapped = packet;//from w ww.j a v a2 s.com ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer(); try { buf.writeInt(PacketMap.getPacketId(unwrapped)); unwrapped.write(buf); packet = new RelayPacket(buf.retain(), address, to, ttl); } finally { unwrapped.release(); buf.release(); } } router.sendPacket(packet).addListener(ErrorLoggingFuture.SINGLETON); return true; } return false; }
From source file:net.hasor.rsf.rpc.net.Connector.java
License:Apache License
/** * ??// w ww .j av a 2s. com * @param listenLoopGroup ? */ public void startListener(NioEventLoopGroup listenLoopGroup) { // ServerBootstrap boot = new ServerBootstrap(); boot.group(listenLoopGroup, this.workLoopGroup); boot.channel(NioServerSocketChannel.class); boot.childHandler(new ChannelInitializer<SocketChannel>() { public void initChannel(SocketChannel ch) throws Exception { ChannelHandler[] handlerArrays = channelHandler(); ArrayList<ChannelHandler> handlers = new ArrayList<ChannelHandler>(); handlers.addAll(Arrays.asList(handlerArrays)); // ?? handlers.add(Connector.this); // ?RequestInfo?ResponseInfoRSF // ch.pipeline().addLast(handlers.toArray(new ChannelHandler[handlers.size()])); } }); boot.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); boot.childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture future = configBoot(boot).bind(this.bindAddress.toSocketAddress()); // final BasicFuture<RsfChannel> result = new BasicFuture<RsfChannel>(); future.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { future.channel().close(); result.failed(future.cause()); } else { Channel channel = future.channel(); result.completed(new RsfChannel(protocol, bindAddress, channel, LinkType.Listener)); } } }); try { this.localListener = result.get(); logger.info("rsf Server started at {}", this.bindAddress); } catch (Exception e) { logger.error("rsf start listener error: " + e.getMessage(), e); throw new RsfException(ProtocolStatus.NetworkError, this.bindAddress.toString() + " -> " + e.getMessage()); } // }
From source file:net.hasor.rsf.rpc.net.Connector.java
License:Apache License
private <T extends AbstractBootstrap<?, ?>> T configBoot(T boot) { boot.option(ChannelOption.SO_KEEPALIVE, true); // boot.option(ChannelOption.SO_BACKLOG, 128); // boot.option(ChannelOption.SO_BACKLOG, 1024); // boot.option(ChannelOption.SO_RCVBUF, 1024 * 256); // boot.option(ChannelOption.SO_SNDBUF, 1024 * 256); boot.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); return boot;/*from www.j a v a 2 s. c om*/ }
From source file:net.qing.sms.simulator.NettySmsSimulatorServer.java
License:Apache License
protected void applyConnectionOptions(ServerBootstrap bootstrap) { SocketConfig config = configuration.getSocketConfig(); bootstrap.childOption(ChannelOption.TCP_NODELAY, config.isTcpNoDelay()); if (config.getTcpSendBufferSize() != -1) { bootstrap.childOption(ChannelOption.SO_SNDBUF, config.getTcpSendBufferSize()); }/*from w w w . ja v a2 s.c om*/ if (config.getTcpReceiveBufferSize() != -1) { bootstrap.childOption(ChannelOption.SO_RCVBUF, config.getTcpReceiveBufferSize()); } // bootstrap.option(ChannelOption.ALLOCATOR, // PooledByteBufAllocator.DEFAULT); bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); // bootstrap.childOption(ChannelOption.ALLOCATOR, // PooledByteBufAllocator.DEFAULT); bootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); bootstrap.childOption(ChannelOption.SO_KEEPALIVE, config.isTcpKeepAlive()); bootstrap.option(ChannelOption.SO_LINGER, config.getSoLinger()); bootstrap.option(ChannelOption.SO_REUSEADDR, config.isReuseAddress()); bootstrap.option(ChannelOption.SO_BACKLOG, config.getAcceptBackLog()); }
From source file:nettyClient4.clientImpl.java
License:Apache License
private ByteBuf getWriteBuffer(int arg1, int arg2, ByteBuf buffer, Object... paras) { if (buffer == null) { buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(10); }/*w ww .j a v a2 s. com*/ buffer.writeShort(Short.MIN_VALUE);//?2 buffer.writeByte(arg1); if (arg2 != 0) buffer.writeByte(arg2); for (Object para : paras) { if (para instanceof Byte) { buffer.writeByte((Byte) para); // ?1 } else if ((para instanceof String)) { buffer.writeBytes(((String) para).getBytes()); } else if (para instanceof Integer) { buffer.writeInt((Integer) para); //?4 } else if (para instanceof Short) { buffer.writeShort((Short) para); //?2 } } /**?2setShort*/ buffer.setShort(0, buffer.writerIndex() - 0x2); return buffer; }
From source file:okhttp3.benchmarks.NettyHttpClient.java
License:Apache License
@Override public void prepare(final Benchmark benchmark) { this.concurrencyLevel = benchmark.concurrencyLevel; this.targetBacklog = benchmark.targetBacklog; ChannelInitializer<SocketChannel> channelInitializer = new ChannelInitializer<SocketChannel>() { @Override//w w w .j av a 2s . c om public void initChannel(SocketChannel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); if (benchmark.tls) { SslClient sslClient = SslClient.localhost(); SSLEngine engine = sslClient.sslContext.createSSLEngine(); engine.setUseClientMode(true); pipeline.addLast("ssl", new SslHandler(engine)); } pipeline.addLast("codec", new HttpClientCodec()); pipeline.addLast("inflater", new HttpContentDecompressor()); pipeline.addLast("handler", new HttpChannel(channel)); } }; bootstrap = new Bootstrap(); bootstrap.group(new NioEventLoopGroup(concurrencyLevel)) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT).channel(NioSocketChannel.class) .handler(channelInitializer); }
From source file:org.apache.activemq.artemis.core.protocol.proton.TestConversions.java
License:Apache License
private ProtonJMessage reEncodeMsg(Object obj) { ProtonJMessage objOut = (ProtonJMessage) obj; ByteBuf nettyBuffer = PooledByteBufAllocator.DEFAULT.heapBuffer(1024); objOut.encode(new NettyWritable(nettyBuffer)); return objOut; }
From source file:org.apache.activemq.artemis.core.protocol.proton.TestConversions.java
License:Apache License
private EncodedMessage encodeMessage(MessageImpl message) { ByteBuf buf = PooledByteBufAllocator.DEFAULT.heapBuffer(1024 * 1024); message.encode(new NettyWritable(buf)); byte[] bytesConvert = new byte[buf.writerIndex()]; buf.readBytes(bytesConvert);/* ww w. j a v a2 s .co m*/ return new EncodedMessage(0, bytesConvert, 0, bytesConvert.length); }
From source file:org.apache.activemq.artemis.core.replication.ReplicationManager.java
License:Apache License
/** * Sends large files in reasonably sized chunks to the backup during replication synchronization. * * @param content journal type or {@code null} for large-messages and pages * @param pageStore page store name for pages, or {@code null} otherwise * @param id journal file id or (large) message id * @param file/*from w ww . j av a 2 s . co m*/ * @param maxBytesToSend maximum number of bytes to read and send from the file * @throws Exception */ private void sendLargeFile(AbstractJournalStorageManager.JournalContent content, SimpleString pageStore, final long id, SequentialFile file, long maxBytesToSend) throws Exception { if (!enabled) return; if (!file.isOpen()) { file.open(); } int size = 32 * 1024; int flowControlSize = 10; int packetsSent = 0; FlushAction action = new FlushAction(); try { try (FileInputStream fis = new FileInputStream(file.getJavaFile()); FileChannel channel = fis.getChannel()) { // We can afford having a single buffer here for this entire loop // because sendReplicatePacket will encode the packet as a NettyBuffer // through ActiveMQBuffer class leaving this buffer free to be reused on the next copy while (true) { final ByteBuf buffer = PooledByteBufAllocator.DEFAULT.directBuffer(size, size); buffer.clear(); ByteBuffer byteBuffer = buffer.writerIndex(size).readerIndex(0).nioBuffer(); final int bytesRead = channel.read(byteBuffer); int toSend = bytesRead; if (bytesRead > 0) { if (bytesRead >= maxBytesToSend) { toSend = (int) maxBytesToSend; maxBytesToSend = 0; } else { maxBytesToSend = maxBytesToSend - bytesRead; } } logger.debug("sending " + buffer.writerIndex() + " bytes on file " + file.getFileName()); // sending -1 or 0 bytes will close the file at the backup // We cannot simply send everything of a file through the executor, // otherwise we would run out of memory. // so we don't use the executor here sendReplicatePacket(new ReplicationSyncFileMessage(content, pageStore, id, toSend, buffer), true); packetsSent++; if (packetsSent % flowControlSize == 0) { flushReplicationStream(action); } if (bytesRead == -1 || bytesRead == 0 || maxBytesToSend == 0) break; } } flushReplicationStream(action); } finally { if (file.isOpen()) file.close(); } }
From source file:org.apache.activemq.artemis.protocol.amqp.broker.AMQPMessage.java
License:Apache License
private void encodeProtonMessage() { int estimated = Math.max(1500, data != null ? data.capacity() + 1000 : 0); ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(estimated); try {/* w w w . j av a 2s .c om*/ getProtonMessage().encode(new NettyWritable(buffer)); byte[] bytes = new byte[buffer.writerIndex()]; buffer.readBytes(bytes); this.data = ReadableBuffer.ByteBufferReader.wrap(ByteBuffer.wrap(bytes)); } finally { buffer.release(); } }