List of usage examples for io.netty.channel ChannelOption SO_SNDBUF
ChannelOption SO_SNDBUF
To view the source code for io.netty.channel ChannelOption SO_SNDBUF.
Click Source Link
From source file:reactor.ipc.netty.options.ClientOptions.java
License:Open Source License
static void defaultClientOptions(Bootstrap bootstrap) { bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 30000).option(ChannelOption.AUTO_READ, false) .option(ChannelOption.SO_RCVBUF, 1024 * 1024).option(ChannelOption.SO_SNDBUF, 1024 * 1024); }
From source file:reactor.ipc.netty.options.ServerOptions.java
License:Open Source License
static void defaultServerOptions(ServerBootstrap bootstrap) { bootstrap.localAddress(LOCALHOST_AUTO_PORT).option(ChannelOption.SO_REUSEADDR, true) .option(ChannelOption.SO_BACKLOG, 1000) .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(ChannelOption.SO_RCVBUF, 1024 * 1024).childOption(ChannelOption.SO_SNDBUF, 1024 * 1024) .childOption(ChannelOption.AUTO_READ, false).childOption(ChannelOption.SO_KEEPALIVE, true) .childOption(ChannelOption.SO_LINGER, 0).childOption(ChannelOption.TCP_NODELAY, true) .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 30000); }
From source file:reactor.tcp.netty.NettyTcpClient.java
License:Open Source License
/** * Creates a new NettyTcpClient that will use the given {@code env} for configuration and the given * {@code reactor} to send events. The number of IO threads used by the client is configured by * the environment's {@code reactor.tcp.ioThreadCount} property. In its absence the number of * IO threads will be equal to the {@link Environment#PROCESSORS number of available processors}. * </p>//from w w w . j a v a2s .c o m * The client will connect to the given {@code connectAddress}, configuring its socket using the * given {@code opts}. The given {@code codec} will be used for encoding and decoding of data. * * @param env The configuration environment * @param reactor The reactor used to send events * @param connectAddress The address the client will connect to * @param opts The configuration options for the client's socket * @param codec The codec used to encode and decode data */ public NettyTcpClient(@Nonnull Environment env, @Nonnull Reactor reactor, @Nonnull InetSocketAddress connectAddress, ClientSocketOptions opts, @Nullable Codec<Buffer, IN, OUT> codec) { super(env, reactor, connectAddress, codec); this.eventsReactor = reactor; this.options = opts; int ioThreadCount = env.getProperty("reactor.tcp.ioThreadCount", Integer.class, Environment.PROCESSORS); ioGroup = new NioEventLoopGroup(ioThreadCount, new NamedDaemonThreadFactory("reactor-tcp-io")); this.bootstrap = new Bootstrap().group(ioGroup).channel(NioSocketChannel.class) .option(ChannelOption.SO_RCVBUF, options.rcvbuf()).option(ChannelOption.SO_SNDBUF, options.sndbuf()) .option(ChannelOption.SO_KEEPALIVE, options.keepAlive()) .option(ChannelOption.SO_LINGER, options.linger()) .option(ChannelOption.TCP_NODELAY, options.tcpNoDelay()).remoteAddress(connectAddress) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(final SocketChannel ch) throws Exception { ch.config().setConnectTimeoutMillis(options.timeout()); ch.pipeline().addLast(createChannelHandlers(ch)); ch.closeFuture().addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { close(ch); } }); } }); }
From source file:sailfish.remoting.channel.AbstractConfigurableExchangeChannelGroup.java
License:Apache License
private Bootstrap newBootstrap() { Bootstrap boot = new Bootstrap(); boot.channel(NettyPlatformIndependent.channelClass()); boot.option(ChannelOption.TCP_NODELAY, true); // replace by heart beat boot.option(ChannelOption.SO_KEEPALIVE, false); // default is pooled direct // ByteBuf(io.netty.util.internal.PlatformDependent.DIRECT_BUFFER_PREFERRED) boot.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); // 32kb(for massive long connections, See // http://www.infoq.com/cn/articles/netty-million-level-push-service-design-points) // 64kb(RocketMq remoting default value) boot.option(ChannelOption.SO_SNDBUF, 32 * 1024); boot.option(ChannelOption.SO_RCVBUF, 32 * 1024); // temporary settings, need more tests boot.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(8 * 1024, 32 * 1024)); //default is true, reduce thread context switching boot.option(ChannelOption.SINGLE_EVENTEXECUTOR_PER_GROUP, true); return boot;/*w w w .ja va 2 s.c o m*/ }
From source file:sailfish.remoting.DefaultServer.java
License:Apache License
private ServerBootstrap newServerBootstrap() { ServerBootstrap serverBoot = new ServerBootstrap(); serverBoot.channel(NettyPlatformIndependent.serverChannelClass()); // connections wait for accept serverBoot.option(ChannelOption.SO_BACKLOG, 1024); serverBoot.option(ChannelOption.SO_REUSEADDR, true); // replace by heart beat serverBoot.childOption(ChannelOption.SO_KEEPALIVE, false); serverBoot.childOption(ChannelOption.TCP_NODELAY, true); serverBoot.childOption(ChannelOption.SO_SNDBUF, 32 * 1024); serverBoot.childOption(ChannelOption.SO_RCVBUF, 32 * 1024); // temporary settings, need more tests serverBoot.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(8 * 1024, 32 * 1024)); serverBoot.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); //default is true, reduce thread context switching serverBoot.childOption(ChannelOption.SINGLE_EVENTEXECUTOR_PER_GROUP, true); return serverBoot; }
From source file:sas.systems.imflux.session.rtp.AbstractRtpSession.java
License:Apache License
/** * {@inheritDoc}/*w w w .j ava 2 s. co m*/ */ @Override public synchronized boolean init() { if (this.running.get()) { return true; } Class<? extends Channel> channelType; if (useNio) { // create data channel bootstrap // EventLoopGroup bossGroup = new NioEventLoopGroup(5, Executors.defaultThreadFactory()); // if we want to use others than the defaults this.workerGroup = new NioEventLoopGroup(); channelType = NioDatagramChannel.class; } else { this.workerGroup = new OioEventLoopGroup(); channelType = OioDatagramChannel.class; } Bootstrap dataBootstrap = new Bootstrap(); dataBootstrap.group(this.workerGroup).option(ChannelOption.SO_SNDBUF, this.sendBufferSize) .option(ChannelOption.SO_RCVBUF, this.receiveBufferSize) // option not set: "receiveBufferSizePredictorFactory", new FixedReceiveBufferSizePredictorFactory(this.receiveBufferSize) .channel(channelType) // use an UDP channel implementation => forces us to use AddressedEnvelope .handler(new ChannelInitializer<Channel>() { // is used to initialize the ChannelPipeline @Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("decoder", UdpDataPacketDecoder.getInstance()); pipeline.addLast("encoder", UdpDataPacketEncoder.getInstance()); pipeline.addLast("handler", new UdpDataHandler(AbstractRtpSession.this)); } }); // create control channel bootstrap Bootstrap controlBootstrap = new Bootstrap(); controlBootstrap.group(this.workerGroup).option(ChannelOption.SO_SNDBUF, this.sendBufferSize) .option(ChannelOption.SO_RCVBUF, this.receiveBufferSize) // option not set: "receiveBufferSizePredictorFactory", new FixedReceiveBufferSizePredictorFactory(this.receiveBufferSize) .channel(channelType) // use an UDP channel implementation => forces us to use AddressedEnvelope .handler(new ChannelInitializer<Channel>() { // is used to initialize the ChannelPipeline @Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("decoder", UdpControlPacketDecoder.getInstance()); pipeline.addLast("encoder", UdpControlPacketEncoder.getInstance()); pipeline.addLast("handler", new UdpControlHandler(AbstractRtpSession.this)); } }); // create data channel SocketAddress dataAddress = this.localParticipant.getDataDestination(); try { ChannelFuture future = dataBootstrap.bind(dataAddress); this.dataChannel = future.sync().channel(); // wait for future to complete and retrieve channel } catch (Exception e) { LOG.error("Failed to bind data channel for session with id " + this.id, e); shutdownEventLoopGroup(); return false; } // create control channel SocketAddress controlAddress = this.localParticipant.getControlDestination(); try { ChannelFuture future = controlBootstrap.bind(controlAddress); this.controlChannel = future.sync().channel(); // wait for future to complete and retrieve channel } catch (Exception e) { LOG.error("Failed to bind control channel for session with id " + this.id, e); this.dataChannel.close(); shutdownEventLoopGroup(); return false; } LOG.debug("Data & Control channels bound for RtpSession with id {}.", this.id); // Send first RTCP packet. this.joinSession(this.localParticipant.getSsrc()); this.running.set(true); // Add the participant database cleaner. this.timer.newTimeout(new TimerTask() { @Override public void run(Timeout timeout) throws Exception { if (!running.get()) { return; } participantDatabase.cleanup(); timer.newTimeout(this, participantDatabaseCleanup, TimeUnit.SECONDS); } }, this.participantDatabaseCleanup, TimeUnit.SECONDS); // Add the periodic RTCP report generator. if (this.automatedRtcpHandling) { this.timer.newTimeout(this, this.updatePeriodicRtcpSendInterval(), TimeUnit.SECONDS); } if (this.internalTimer) { this.timer.start(); } return true; }
From source file:sas.systems.imflux.session.rtsp.SimpleRtspSession.java
License:Apache License
/** * {@inheritDoc}// w w w . ja v a 2 s. c o m */ @Override public synchronized boolean init() { if (this.running.get()) { return true; } // create bootstrap Class<? extends ServerChannel> channelType; if (useNio) { this.workerGroup = new NioEventLoopGroup(); this.bossGroup = new NioEventLoopGroup(); channelType = NioServerSocketChannel.class; } else { this.workerGroup = new OioEventLoopGroup(); this.bossGroup = new OioEventLoopGroup(); channelType = OioServerSocketChannel.class; } bootstrap = new ServerBootstrap(); bootstrap.group(this.bossGroup, this.workerGroup).option(ChannelOption.SO_SNDBUF, this.sendBufferSize) .option(ChannelOption.SO_RCVBUF, this.receiveBufferSize).channel(channelType) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<Channel>() { // is used to initialize the ChannelPipeline @Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("encoder", new RtspEncoder()); pipeline.addLast("decoder", new RtspDecoder()); pipeline.addLast("aggregator", new HttpObjectAggregator(64 * 1024)); pipeline.addLast("handler", new RtspHandler(SimpleRtspSession.this)); } }); // create channel try { ChannelFuture future = bootstrap.bind(this.localAddress); this.channel = future.sync().channel(); // wait for future to complete and retrieve channel } catch (Exception e) { LOG.error("Failed to bind RTSP channel for session with id " + this.id, e); this.workerGroup.shutdownGracefully(); this.bossGroup.shutdownGracefully(); this.workerGroup.terminationFuture().syncUninterruptibly(); this.bossGroup.terminationFuture().syncUninterruptibly(); return false; } LOG.debug("RTSP channel bound for RtspSession with id {}.", this.id); this.running.set(true); return true; }
From source file:se.sics.gvod.net.NettyNetwork.java
License:Open Source License
/** * Start listening as a server at the given address.. * * @param addr the address to listen at//from w w w . ja v a2 s. c o m * @param port the port number to listen at * @param bindAllNetworkIfs whether to bind on all network interfaces * @return true if listening was started * @throws ChannelException in case binding failed */ private boolean bindUdpPort(final InetAddress addr, final int port, final boolean bindAllNetworkIfs) { if (udpPortsToSockets.containsKey(port)) { return true; } EventLoopGroup group = new NioEventLoopGroup(); Bootstrap bootstrap = new Bootstrap(); bootstrap.group(group).channel(NioDatagramChannel.class) .handler(new NettyMsgHandler(component, Transport.UDP, msgDecoderClass)); // Allow packets as large as up to 1600 bytes (default is 768). // You could increase or decrease this value to avoid truncated packets // or to improve memory footprint respectively. // // Please also note that a large UDP packet might be truncated or // dropped by your router no matter how you configured this option. // In UDP, a packet is truncated or dropped if it is larger than a // certain size, depending on router configuration. IPv4 routers // truncate and IPv6 routers drop a large packet. That's why it is // safe to send small packets in UDP. bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(1500)); bootstrap.option(ChannelOption.SO_RCVBUF, RECV_BUFFER_SIZE); bootstrap.option(ChannelOption.SO_SNDBUF, SEND_BUFFER_SIZE); // bootstrap.setOption("trafficClass", trafficClass); // bootstrap.setOption("soTimeout", soTimeout); // bootstrap.setOption("broadcast", broadcast); bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, CONNECT_TIMEOUT_MS); bootstrap.option(ChannelOption.SO_REUSEADDR, true); try { DatagramChannel c; if (bindAllNetworkIfs) { c = (DatagramChannel) bootstrap.bind( // new InetSocketAddress(port) port).sync().channel(); } else { c = (DatagramChannel) bootstrap.bind(new InetSocketAddress(addr, port)).sync().channel(); } addLocalSocket(new InetSocketAddress(addr, port), c); logger.debug("Successfully bound to ip:port {}:{}", addr, port); } catch (InterruptedException e) { logger.warn("Problem when trying to bind to {}:{}", addr.getHostAddress(), port); trigger(new Fault(e.getCause()), control); return false; } udpSocketsToBootstraps.put(new InetSocketAddress(addr, port), bootstrap); return true; }
From source file:se.sics.kompics.network.netty.NettyNetwork.java
License:Open Source License
private boolean bindUdpPort(final InetAddress addr, final int port) { EventLoopGroup group = new NioEventLoopGroup(); bootstrapUDP = new Bootstrap(); bootstrapUDP.group(group).channel(NioDatagramChannel.class) .handler(new DatagramHandler(this, Transport.UDP)); bootstrapUDP.option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(1500, 1500, RECV_BUFFER_SIZE)); bootstrapUDP.option(ChannelOption.SO_RCVBUF, RECV_BUFFER_SIZE); bootstrapUDP.option(ChannelOption.SO_SNDBUF, SEND_BUFFER_SIZE); // bootstrap.setOption("trafficClass", trafficClass); // bootstrap.setOption("soTimeout", soTimeout); // bootstrap.setOption("broadcast", broadcast); bootstrapUDP.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, CONNECT_TIMEOUT_MS); bootstrapUDP.option(ChannelOption.SO_REUSEADDR, true); try {// w w w .j a v a 2s . c o m InetSocketAddress iAddr = new InetSocketAddress(addr, port); DatagramChannel c = (DatagramChannel) bootstrapUDP.bind(iAddr).sync().channel(); addLocalSocket(iAddr, c); LOG.info("Successfully bound to ip:port {}:{}", addr, port); } catch (InterruptedException e) { LOG.error("Problem when trying to bind to {}:{}", addr.getHostAddress(), port); return false; } return true; }
From source file:storage.netty.HTTPUploadClientAsync.java
License:Apache License
public void putBlob(String filepath) throws Exception { String resourceUrl = "/mycontainer/" + randomString(5); String putBlobUrl = base_url + resourceUrl; URI uriSimple = new URI(putBlobUrl); String scheme = uriSimple.getScheme() == null ? "http" : uriSimple.getScheme(); String host = uriSimple.getHost() == null ? "127.0.0.1" : uriSimple.getHost(); int port = uriSimple.getPort(); if (port == -1) { if ("http".equalsIgnoreCase(scheme)) { port = 80;/*w w w .j a va 2s .c o m*/ } else if ("https".equalsIgnoreCase(scheme)) { port = 443; } } if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) { System.err.println("Only HTTP(S) is supported."); return; } final boolean ssl = "https".equalsIgnoreCase(scheme); final SslContext sslCtx; if (ssl) { sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build(); } else { sslCtx = null; } File path = new File(filepath); if (!path.canRead()) { throw new FileNotFoundException(filepath); } // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); // setup the factory: here using a mixed memory/disk based on size threshold HttpDataFactory factory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE); // Disk if MINSIZE exceed DiskFileUpload.deleteOnExitTemporaryFile = true; // should delete file on exit (in normal exit) DiskFileUpload.baseDirectory = null; // system temp directory DiskAttribute.deleteOnExitTemporaryFile = true; // should delete file on exit (in normal exit) DiskAttribute.baseDirectory = null; // system temp directory try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new HttpUploadClientInitializer(sslCtx)); b.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 10 * 64 * 1024); b.option(ChannelOption.SO_SNDBUF, 1048576); b.option(ChannelOption.SO_RCVBUF, 1048576); b.option(ChannelOption.TCP_NODELAY, true); b.option(ChannelOption.SO_REUSEADDR, true); b.option(ChannelOption.AUTO_CLOSE, true); //Iterate over files Collection<ChannelFuture> futures = new ArrayList<ChannelFuture>(); for (final File file : path.listFiles()) { String blobname = file.getName(); System.out.println(blobname); HttpRequest request = formpost(host, port, resourceUrl + blobname, file, factory); ChannelFuture cf = b.connect(host, port); futures.add(cf); cf.channel().attr(HTTPREQUEST).set(request); cf.channel().attr(FILETOUPLOAD).set(file); cf.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) throws Exception { // check to see if we succeeded if (future.isSuccess()) { System.out.println("connected for " + blobname); } else { System.out.println(future.cause().getMessage()); } } }); } for (ChannelFuture cf : futures) { cf.channel().closeFuture().awaitUninterruptibly(); } } finally { // Shut down executor threads to exit. group.shutdownGracefully(); // Really clean all temporary files if they still exist factory.cleanAllHttpDatas(); } }