org.jboss.netty.channel.FixedReceiveBufferSizePredictorFactory Java Examples

The following examples show how to use org.jboss.netty.channel.FixedReceiveBufferSizePredictorFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UdpServer.java    From feeyo-hlsserver with Apache License 2.0 6 votes vote down vote up
public void startup(int port) {
	
	ChannelFactory channelFactory = new NioDatagramChannelFactory(Executors.newCachedThreadPool());
	
	bootstrap = new ConnectionlessBootstrap( channelFactory );
	bootstrap.setOption("reuseAddress", false);
	bootstrap.setOption("child.reuseAddress", false);		
	bootstrap.setOption("readBufferSize", 1024 * 1024 * 15); //15M
	bootstrap.setOption("writeBufferSize", 1024 * 20);		
	bootstrap.setOption("receiveBufferSizePredictor", new FixedReceiveBufferSizePredictor(1024 * 3));
	bootstrap.setOption("receiveBufferSizePredictorFactory", new FixedReceiveBufferSizePredictorFactory(1024 * 3));
	bootstrap.setPipelineFactory( new ChannelPipelineFactory() {
		@Override
		public ChannelPipeline getPipeline() throws Exception {
			ChannelPipeline pipeline = Channels.pipeline();
			pipeline.addLast("handler", new UdpServerChannelHandler());
			return pipeline;
		}			
	});		
	datagramChannel = (DatagramChannel) bootstrap.bind( new InetSocketAddress( port ) );
}
 
Example #2
Source File: Controller.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes the netty client channel connection.
 */
private void initConnection() {
    if (peerBootstrap != null) {
        return;
    }
    peerBootstrap = createPeerBootStrap();

    peerBootstrap.setOption("reuseAddress", true);
    peerBootstrap.setOption("tcpNoDelay", true);
    peerBootstrap.setOption("keepAlive", true);
    peerBootstrap.setOption("receiveBufferSize", Controller.BUFFER_SIZE);
    peerBootstrap.setOption("receiveBufferSizePredictorFactory",
                            new FixedReceiveBufferSizePredictorFactory(
                                    Controller.BUFFER_SIZE));
    peerBootstrap.setOption("receiveBufferSizePredictor",
                            new AdaptiveReceiveBufferSizePredictor(64, 4096, 65536));
    peerBootstrap.setOption("child.keepAlive", true);
    peerBootstrap.setOption("child.tcpNoDelay", true);
    peerBootstrap.setOption("child.sendBufferSize", Controller.BUFFER_SIZE);
    peerBootstrap.setOption("child.receiveBufferSize", Controller.BUFFER_SIZE);
    peerBootstrap.setOption("child.receiveBufferSizePredictorFactory",
                            new FixedReceiveBufferSizePredictorFactory(
                                    Controller.BUFFER_SIZE));
    peerBootstrap.setOption("child.reuseAddress", true);

    ospfChannelHandler = new OspfInterfaceChannelHandler(this, processes);
    ChannelPipelineFactory pfact = new OspfPipelineFactory(ospfChannelHandler);
    peerBootstrap.setPipelineFactory(pfact);
}
 
Example #3
Source File: Controller.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes the netty client channel connection.
 */
private void initConnection() {
    if (peerBootstrap != null) {
        return;
    }
    peerBootstrap = createPeerBootStrap();

    peerBootstrap.setOption("reuseAddress", true);
    peerBootstrap.setOption("tcpNoDelay", true);
    peerBootstrap.setOption("keepAlive", true);
    peerBootstrap.setOption("receiveBufferSize", Controller.BUFFER_SIZE);
    peerBootstrap.setOption("receiveBufferSizePredictorFactory",
            new FixedReceiveBufferSizePredictorFactory(
                    Controller.BUFFER_SIZE));
    peerBootstrap.setOption("receiveBufferSizePredictor",
            new AdaptiveReceiveBufferSizePredictor(64, 1024, 65536));
    peerBootstrap.setOption("child.keepAlive", true);
    peerBootstrap.setOption("child.tcpNoDelay", true);
    peerBootstrap.setOption("child.sendBufferSize", Controller.BUFFER_SIZE);
    peerBootstrap.setOption("child.receiveBufferSize", Controller.BUFFER_SIZE);
    peerBootstrap.setOption("child.receiveBufferSizePredictorFactory",
            new FixedReceiveBufferSizePredictorFactory(
                    Controller.BUFFER_SIZE));
    peerBootstrap.setOption("child.reuseAddress", true);

    isisChannelHandler = new IsisChannelHandler(this, processes);
    ChannelPipelineFactory pfact = new IsisPipelineFactory(isisChannelHandler);
    peerBootstrap.setPipelineFactory(pfact);
}
 
Example #4
Source File: RtpServer.java    From feeyo-hlsserver with Apache License 2.0 4 votes vote down vote up
public void startup(int port) {

		this.dataBootstrap = new ConnectionlessBootstrap(factory);
		dataBootstrap.setOption("receiveBufferSizePredictor", new FixedReceiveBufferSizePredictor(2048));
		dataBootstrap.setOption("receiveBufferSizePredictorFactory", new FixedReceiveBufferSizePredictorFactory(2048));

		this.dataBootstrap.getPipeline().addLast("handler", new SimpleChannelUpstreamHandler() {
			@Override
			public void messageReceived(ChannelHandlerContext ctx, final MessageEvent e) throws Exception {

				ChannelBuffer buffer = (ChannelBuffer) e.getMessage();

				if (buffer.readableBytes() < 12) {
					throw new IllegalArgumentException("A RTP packet must be at least 12 octets long");
				}

				byte b = buffer.readByte();
				byte version = (byte) (b & 0xc0);
				boolean padding = (b & 0x20) > 0; // mask 0010 0000
				boolean extension = (b & 0x10) > 0; // mask 0001 0000
				int contributingSourcesCount = b & 0x0f; // mask 0000 1111

				// Marker, Payload Type
				b = buffer.readByte();
				boolean marker = (b & 0x80) > 0; // mask 0000 0001
				int payloadType = (b & 0x7f); // mask 0111 1111

				int sequenceNumber = buffer.readUnsignedShort();
				long timestamp = buffer.readUnsignedInt();
				long ssrc = buffer.readUnsignedInt();

				// Read CCRC's
				if (contributingSourcesCount > 0) {
					for (int i = 0; i < contributingSourcesCount; i++) {
						long contributingSource = buffer.readUnsignedInt();
					}
				}

				// Read extension headers & data
				if (extension) {
					short extensionHeaderData = buffer.readShort();
					byte[] extensionData = new byte[buffer.readUnsignedShort() * 4];
					buffer.readBytes(extensionData);
				}

				if (!padding) {
					// No padding used, assume remaining data is the packet
					byte[] remainingBytes = new byte[buffer.readableBytes()];
					buffer.readBytes(remainingBytes);

					// remainingBytes == data
				} else {
					// Padding bit was set, so last byte contains the number of
					// padding octets that should be discarded.
					short lastByte = buffer.getUnsignedByte(buffer.readerIndex() + buffer.readableBytes() - 1);
					byte[] dataBytes = new byte[buffer.readableBytes() - lastByte];
					buffer.readBytes(dataBytes);

					// dataBytes == data

					// Discard rest of buffer.
					buffer.skipBytes(buffer.readableBytes());
				}

				// 应答
				ChannelBuffer replyBuffer = ChannelBuffers.copiedBuffer(REPLY_BYTES);
				e.getChannel().write(replyBuffer, e.getRemoteAddress());

			}
		});
		this.dataChannel = (DatagramChannel) this.dataBootstrap.bind(new InetSocketAddress(port));

	}
 
Example #5
Source File: NettyTransport.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Inject
public NettyTransport(Settings settings, ThreadPool threadPool, NetworkService networkService, BigArrays bigArrays, Version version, NamedWriteableRegistry namedWriteableRegistry) {
    super(settings);
    this.threadPool = threadPool;
    this.networkService = networkService;
    this.bigArrays = bigArrays;
    this.version = version;

    if (settings.getAsBoolean("netty.epollBugWorkaround", false)) {
        System.setProperty("org.jboss.netty.epollBugWorkaround", "true");
    }

    this.workerCount = settings.getAsInt(WORKER_COUNT, EsExecutors.boundedNumberOfProcessors(settings) * 2);
    this.blockingClient = settings.getAsBoolean("transport.netty.transport.tcp.blocking_client", settings.getAsBoolean(TCP_BLOCKING_CLIENT, settings.getAsBoolean(TCP_BLOCKING, false)));
    this.connectTimeout = this.settings.getAsTime("transport.netty.connect_timeout", settings.getAsTime("transport.tcp.connect_timeout", settings.getAsTime(TCP_CONNECT_TIMEOUT, TCP_DEFAULT_CONNECT_TIMEOUT)));
    this.maxCumulationBufferCapacity = this.settings.getAsBytesSize("transport.netty.max_cumulation_buffer_capacity", null);
    this.maxCompositeBufferComponents = this.settings.getAsInt("transport.netty.max_composite_buffer_components", -1);
    this.compress = settings.getAsBoolean(TransportSettings.TRANSPORT_TCP_COMPRESS, false);

    this.connectionsPerNodeRecovery = this.settings.getAsInt("transport.netty.connections_per_node.recovery", settings.getAsInt(CONNECTIONS_PER_NODE_RECOVERY, 2));
    this.connectionsPerNodeBulk = this.settings.getAsInt("transport.netty.connections_per_node.bulk", settings.getAsInt(CONNECTIONS_PER_NODE_BULK, 3));
    this.connectionsPerNodeReg = this.settings.getAsInt("transport.netty.connections_per_node.reg", settings.getAsInt(CONNECTIONS_PER_NODE_REG, 6));
    this.connectionsPerNodeState = this.settings.getAsInt("transport.netty.connections_per_node.high", settings.getAsInt(CONNECTIONS_PER_NODE_STATE, 1));
    this.connectionsPerNodePing = this.settings.getAsInt("transport.netty.connections_per_node.ping", settings.getAsInt(CONNECTIONS_PER_NODE_PING, 1));

    // we want to have at least 1 for reg/state/ping
    if (this.connectionsPerNodeReg == 0) {
        throw new IllegalArgumentException("can't set [connection_per_node.reg] to 0");
    }
    if (this.connectionsPerNodePing == 0) {
        throw new IllegalArgumentException("can't set [connection_per_node.ping] to 0");
    }
    if (this.connectionsPerNodeState == 0) {
        throw new IllegalArgumentException("can't set [connection_per_node.state] to 0");
    }

    long defaultReceiverPredictor = 512 * 1024;
    if (JvmInfo.jvmInfo().getMem().getDirectMemoryMax().bytes() > 0) {
        // we can guess a better default...
        long l = (long) ((0.3 * JvmInfo.jvmInfo().getMem().getDirectMemoryMax().bytes()) / workerCount);
        defaultReceiverPredictor = Math.min(defaultReceiverPredictor, Math.max(l, 64 * 1024));
    }

    // See AdaptiveReceiveBufferSizePredictor#DEFAULT_XXX for default values in netty..., we can use higher ones for us, even fixed one
    this.receivePredictorMin = this.settings.getAsBytesSize("transport.netty.receive_predictor_min", this.settings.getAsBytesSize("transport.netty.receive_predictor_size", new ByteSizeValue(defaultReceiverPredictor)));
    this.receivePredictorMax = this.settings.getAsBytesSize("transport.netty.receive_predictor_max", this.settings.getAsBytesSize("transport.netty.receive_predictor_size", new ByteSizeValue(defaultReceiverPredictor)));
    if (receivePredictorMax.bytes() == receivePredictorMin.bytes()) {
        receiveBufferSizePredictorFactory = new FixedReceiveBufferSizePredictorFactory((int) receivePredictorMax.bytes());
    } else {
        receiveBufferSizePredictorFactory = new AdaptiveReceiveBufferSizePredictorFactory((int) receivePredictorMin.bytes(), (int) receivePredictorMin.bytes(), (int) receivePredictorMax.bytes());
    }

    this.scheduledPing = new ScheduledPing();
    this.pingSchedule = settings.getAsTime(PING_SCHEDULE, DEFAULT_PING_SCHEDULE);
    if (pingSchedule.millis() > 0) {
        threadPool.schedule(pingSchedule, ThreadPool.Names.GENERIC, scheduledPing);
    }
    this.namedWriteableRegistry = namedWriteableRegistry;
}
 
Example #6
Source File: RaopAudioHandler.java    From Android-Airplay-Server with MIT License 4 votes vote down vote up
/**
 * Creates an UDP socket and handler pipeline for RTP channels
 * 
 * @param local local end-point address
 * @param remote remote end-point address
 * @param channelType channel type. Determines which handlers are put into the pipeline
 * @return open data-gram channel
 */
private Channel createRtpChannel(final SocketAddress local, final SocketAddress remote, final RaopRtpChannelType channelType) {
	/* Create bootstrap helper for a data-gram socket using NIO */
	//final ConnectionlessBootstrap bootstrap = new ConnectionlessBootstrap(new NioDatagramChannelFactory(rtpExecutorService));
	final ConnectionlessBootstrap bootstrap = new ConnectionlessBootstrap(new OioDatagramChannelFactory(rtpExecutorService));
	
	
	
	/* Set the buffer size predictor to 1500 bytes to ensure that
	 * received packets will fit into the buffer. Packets are
	 * truncated if they are larger than that!
	 */
	bootstrap.setOption("receiveBufferSizePredictorFactory", new FixedReceiveBufferSizePredictorFactory(1500));
	
	/* Set the socket's receive buffer size. We set it to 1MB */
	bootstrap.setOption("receiveBufferSize", 1024 * 1024);
	
	/* Set pipeline factory for the RTP channel */
	bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
		@Override
		public ChannelPipeline getPipeline() throws Exception {
			final ChannelPipeline pipeline = Channels.pipeline();

			final AirPlayServer airPlayServer = AirPlayServer.getIstance();
			
			pipeline.addLast("executionHandler", airPlayServer.getChannelExecutionHandler());
			pipeline.addLast("exceptionLogger", exceptionLoggingHandler);
			pipeline.addLast("decoder", decodeHandler);
			pipeline.addLast("encoder", encodeHandler);
			
			/* We pretend that all communication takes place on the audio channel,
			 * and simply re-route packets from and to the control and timing channels
			 */
			if ( ! channelType.equals(RaopRtpChannelType.Audio)) {
				pipeline.addLast("inputToAudioRouter", inputToAudioRouterDownstreamHandler);
				
				/* Must come *after* the router, otherwise incoming packets are logged twice */
				pipeline.addLast("packetLogger", packetLoggingHandler);
			}
			else {
				/* Must come *before* the router, otherwise outgoing packets are logged twice */
				pipeline.addLast("packetLogger", packetLoggingHandler);
				pipeline.addLast("audioToOutputRouter", audioToOutputRouterUpstreamHandler);
				pipeline.addLast("timing", timingHandler);
				pipeline.addLast("resendRequester", resendRequestHandler);
				
				if (decryptionHandler != null){
					pipeline.addLast("decrypt", decryptionHandler);
				}
				
				if (audioDecodeHandler != null){
					pipeline.addLast("audioDecode", audioDecodeHandler);
				}
				
				pipeline.addLast("enqueue", audioEnqueueHandler);
			}

			return pipeline;
		}
	});

	Channel channel = null;
	boolean didThrow = true;
	try {
		/* Bind to local address */
		channel = bootstrap.bind(local);
		
		/* Add to group of RTP channels beloging to this RTSP connection */
		rtpChannels.add(channel);

		/* Connect to remote address if one was provided */
		if (remote != null){
			channel.connect(remote);
		}
		
		didThrow = false;
		return channel;
	}
	finally {
		if (didThrow && (channel != null)){
			channel.close();
		}
	}
}