Java Code Examples for org.apache.mina.transport.socket.SocketSessionConfig#setSendBufferSize()

The following examples show how to use org.apache.mina.transport.socket.SocketSessionConfig#setSendBufferSize() . 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: NetManager.java    From GameServer with Apache License 2.0 6 votes vote down vote up
public  void startListner(IoHandler iohandler,int listenPort) throws Exception{
	acceptor = new NioSocketAcceptor();
	acceptor.setBacklog(100);
	acceptor.setReuseAddress(true);
	acceptor.setHandler(iohandler);
	
       DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
       IoFilter protocol = new ProtocolCodecFilter(new GameProtocolcodecFactory());
       chain.addLast("codec", protocol);
	threadpool = new OrderedThreadPoolExecutor(500);
	threadpool.setThreadFactory(new ServerThreadFactory("OrderedThreadPool"));
	chain.addLast("threadPool", new ExecutorFilter(threadpool));
	
	int recsize = 5120;
	int sendsize = 40480;                                                                                         
	int timeout = 10;
	SocketSessionConfig sc = acceptor.getSessionConfig();
	sc.setReuseAddress(true);// 设置每一个非主监听连接的端口可以重用
	sc.setReceiveBufferSize(recsize);// 设置输入缓冲区的大小
	sc.setSendBufferSize(sendsize);// 设置输出缓冲区的大小
	sc.setTcpNoDelay(true);// flush函数的调用 设置为非延迟发送,为true则不组装成大包发送,收到东西马上发出   
	sc.setSoLinger(0);
	sc.setIdleTime(IdleStatus.READER_IDLE, timeout);
	acceptor.bind(new InetSocketAddress(listenPort));
}
 
Example 2
Source File: HttpServer.java    From game-server with MIT License 5 votes vote down vote up
@Override
public void run() {
	DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
	chain.addLast("codec", new HttpServerCodecImpl());

	// // 线程队列池
	OrderedThreadPoolExecutor threadpool = new OrderedThreadPoolExecutor(minaServerConfig.getOrderedThreadPoolExecutorSize());
	chain.addLast("threadPool", new ExecutorFilter(threadpool));

	acceptor.setReuseAddress(minaServerConfig.isReuseAddress()); // 允许地址重用

	SocketSessionConfig sc = acceptor.getSessionConfig();
	sc.setReuseAddress(minaServerConfig.isReuseAddress());
	sc.setReceiveBufferSize(minaServerConfig.getMaxReadSize());
	sc.setSendBufferSize(minaServerConfig.getSendBufferSize());
	sc.setTcpNoDelay(minaServerConfig.isTcpNoDelay());
	sc.setSoLinger(minaServerConfig.getSoLinger());
	sc.setIdleTime(IdleStatus.READER_IDLE, minaServerConfig.getReaderIdleTime());
	sc.setIdleTime(IdleStatus.WRITER_IDLE, minaServerConfig.getWriterIdleTime());

	acceptor.setHandler(ioHandler);

	try {
		acceptor.bind(new InetSocketAddress(minaServerConfig.getHttpPort()));
		LOG.warn("已开始监听HTTP端口:{}", minaServerConfig.getHttpPort());
	} catch (IOException e) {
		SysUtil.exit(getClass(), e, "监听HTTP端口:{}已被占用", minaServerConfig.getHttpPort());
	}
}
 
Example 3
Source File: MinaTcpClient.java    From game-server with MIT License 5 votes vote down vote up
/**
 * 设置连接配置
 *
 * @param minaClientConfig a {@link com.jzy.game.engine.mina.config.MinaClientConfig} object.
 */
public void setMinaClientConfig(MinaClientConfig minaClientConfig) {
    if (minaClientConfig == null) {
        return;
    }
    this.minaClientConfig = minaClientConfig;
    SocketSessionConfig sc = connector.getSessionConfig();
    maxConnectCount = minaClientConfig.getMaxConnectCount();
    sc.setReceiveBufferSize(minaClientConfig.getReceiveBufferSize()); // 524288
    sc.setSendBufferSize(minaClientConfig.getSendBufferSize()); // 1048576
    sc.setMaxReadBufferSize(minaClientConfig.getMaxReadSize()); // 1048576
    factory.getDecoder().setMaxReadSize(minaClientConfig.getMaxReadSize());
    sc.setSoLinger(minaClientConfig.getSoLinger()); // 0
}
 
Example 4
Source File: MINAConnectionAcceptor.java    From Openfire with Apache License 2.0 5 votes vote down vote up
private static NioSocketAcceptor buildSocketAcceptor()
{
    // Create SocketAcceptor with correct number of processors
    final int processorCount = JiveGlobals.getIntProperty( "xmpp.processor.count", Runtime.getRuntime().availableProcessors() );

    final NioSocketAcceptor socketAcceptor = new NioSocketAcceptor( processorCount );

    // Set that it will be possible to bind a socket if there is a connection in the timeout state.
    socketAcceptor.setReuseAddress( true );

    // Set the listen backlog (queue) length. Default is 50.
    socketAcceptor.setBacklog( JiveGlobals.getIntProperty( "xmpp.socket.backlog", 50 ) );

    // Set default (low level) settings for new socket connections
    final SocketSessionConfig socketSessionConfig = socketAcceptor.getSessionConfig();

    //socketSessionConfig.setKeepAlive();
    final int receiveBuffer = JiveGlobals.getIntProperty( "xmpp.socket.buffer.receive", -1 );
    if ( receiveBuffer > 0 )
    {
        socketSessionConfig.setReceiveBufferSize( receiveBuffer );
    }

    final int sendBuffer = JiveGlobals.getIntProperty( "xmpp.socket.buffer.send", -1 );
    if ( sendBuffer > 0 )
    {
        socketSessionConfig.setSendBufferSize( sendBuffer );
    }

    final int linger = JiveGlobals.getIntProperty( "xmpp.socket.linger", -1 );
    if ( linger > 0 )
    {
        socketSessionConfig.setSoLinger( linger );
    }

    socketSessionConfig.setTcpNoDelay( JiveGlobals.getBooleanProperty( "xmpp.socket.tcp-nodelay", socketSessionConfig.isTcpNoDelay() ) );

    return socketAcceptor;
}
 
Example 5
Source File: TcpServer.java    From game-server with MIT License 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public void run() {
    synchronized (this) {
        if (!isRunning) {
            isRunning = true;
            DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
            if (factory == null) {
                factory = new DefaultProtocolCodecFactory();
            }

            if (factory instanceof DefaultProtocolCodecFactory) {
                ProtocolCodecFactoryImpl defaultFactory = (ProtocolCodecFactoryImpl) factory;
                defaultFactory.getDecoder().setMaxReadSize(minaServerConfig.getMaxReadSize());
                defaultFactory.getEncoder()
                              .setMaxScheduledWriteMessages(minaServerConfig.getMaxScheduledWriteMessages());
            }

            chain.addLast("codec", new ProtocolCodecFilter(factory));
            threadpool = new OrderedThreadPoolExecutor(minaServerConfig.getOrderedThreadPoolExecutorSize());
            chain.addLast("threadPool", new ExecutorFilter(threadpool));
            if (filters != null) {
                filters.forEach((key, filter) -> {
                    if ("ssl".equalsIgnoreCase(key) || "tls".equalsIgnoreCase(key)) { // ssl过滤器必须添加到首部
                        chain.addFirst(key, filter);
                    } else {
                        chain.addLast(key, filter);
                    }
                });
            }

            acceptor.setReuseAddress(minaServerConfig.isReuseAddress()); // 允许地址重用

            SocketSessionConfig sc = acceptor.getSessionConfig();
            sc.setReuseAddress(minaServerConfig.isReuseAddress());
            sc.setReceiveBufferSize(minaServerConfig.getReceiveBufferSize());
            sc.setSendBufferSize(minaServerConfig.getSendBufferSize());
            sc.setTcpNoDelay(minaServerConfig.isTcpNoDelay());
            sc.setSoLinger(minaServerConfig.getSoLinger());
            sc.setIdleTime(IdleStatus.READER_IDLE, minaServerConfig.getReaderIdleTime());
            sc.setIdleTime(IdleStatus.WRITER_IDLE, minaServerConfig.getWriterIdleTime());

            acceptor.setHandler(ioHandler);

            try {
                acceptor.bind(new InetSocketAddress(minaServerConfig.getPort()));
                log.warn("已开始监听TCP端口:{}", minaServerConfig.getPort());
            } catch (IOException e) {
                log.warn("监听TCP端口:{}已被占用", minaServerConfig.getPort());
                log.error("TCP 服务异常", e);
            }
        }
    }
}