Java源码示例:org.apache.mina.transport.socket.SocketSessionConfig

示例1
public static void run_startup_configurations() {
    autoPaoDian = Integer.parseInt(ServerProperties.getProperty("autoPaoDian", "1"));
    port = Short.parseShort(ServerProperties.getProperty("cashshop.port", String.valueOf(DEFAULT_PORT)));
    ip = ServerProperties.getProperty("world.host", ServerConstants.IP) + ":" + port;

    IoBuffer.setUseDirectBuffer(false);
    IoBuffer.setAllocator(new SimpleBufferAllocator());

    acceptor = new NioSocketAcceptor();
    acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MapleCodecFactory()));
    acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 30);
    players = new PlayerStorage(MapleServerHandler.CASH_SHOP_SERVER);
    try {
        acceptor.setHandler(new MapleServerHandler(MapleServerHandler.CASH_SHOP_SERVER));
        acceptor.bind(new InetSocketAddress(port));
        ((SocketSessionConfig) acceptor.getSessionConfig()).setTcpNoDelay(true);

        FileoutputUtil.log("完成!");
        FileoutputUtil.log("商城伺服器正在监听" + port + "端口\r\n");
    } catch (IOException e) {
        FileoutputUtil.log("失败!");
        System.err.println("无法绑定" + port + "端口");
        throw new RuntimeException("绑定端口失败.", e);
    }
}
 
示例2
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));
}
 
示例3
public static void main(String[] args) throws IOException {
    acceptor = new NioSocketAcceptor();
    acceptor.getFilterChain().addLast("protocol", new ProtocolCodecFilter(new WebSocketCodecFactory()));
    // close sessions when the acceptor is stopped
    acceptor.setCloseOnDeactivation(true);
    acceptor.setHandler(new WebSocketHandler());
    SocketSessionConfig sessionConf = acceptor.getSessionConfig();
    sessionConf.setReuseAddress(true);
    acceptor.setReuseAddress(true);
    // loop through the addresses and bind
    Set<InetSocketAddress> socketAddresses = new HashSet<InetSocketAddress>();
    socketAddresses.add(new InetSocketAddress("0.0.0.0", 8888));
    //socketAddresses.add(new InetSocketAddress("localhost", 8888));
    log.debug("Binding to {}", socketAddresses.toString());
    acceptor.bind(socketAddresses);
    System.out.println("WS server started listening");
    listening = true;
    while (true) {
        try {
            Thread.sleep(2000L);
        } catch (InterruptedException e) {
            System.out.println("WS server stopped listening");
        }
    }
}
 
示例4
@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());
	}
}
 
示例5
/**
 * 设置连接配置
 *
 * @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
}
 
示例6
private SocketSessionConfig getSessionConfig() {
	SocketSessionConfig config = new DefaultSocketSessionConfig();
	config.setKeepAlive(true);
	config.setReuseAddress(true);

	return config;
}
 
示例7
private SocketSessionConfig getSessionConfig() {
	SocketSessionConfig config = new DefaultSocketSessionConfig();
	config.setKeepAlive(true);
	config.setReuseAddress(true);

	return config;
}
 
示例8
/**
 * Create the connector
 * 
 * @throws LdapException If the connector can't be created
 */
private void createConnector() throws LdapException
{
    // Use only one thread inside the connector
    connector = new NioSocketConnector( 1 );
    
    if ( socketSessionConfig != null )
    {
        ( ( SocketSessionConfig ) connector.getSessionConfig() ).setAll( socketSessionConfig );
    }
    else
    {
        ( ( SocketSessionConfig ) connector.getSessionConfig() ).setReuseAddress( true );
    }

    // Add the codec to the chain
    connector.getFilterChain().addLast( "ldapCodec", ldapProtocolFilter );

    // If we use SSL, we have to add the SslFilter to the chain
    if ( config.isUseSsl() )
    {
        addSslFilter();
    }

    // Inject the protocolHandler
    connector.setHandler( this );
}
 
示例9
public static void run_startup_configurations() {
    userLimit = ServerProperties.getProperty("userlimit", 140);
    serverName = ServerProperties.getProperty("serverName", "MapleStory");
    flag = ServerProperties.getProperty("flag", (byte) 3);
    adminOnly = Boolean.parseBoolean(ServerProperties.getProperty("admin", "false"));
    maxCharacters = ServerProperties.getProperty("maxCharacters", 30);
    autoReg = Boolean.parseBoolean(ServerProperties.getProperty("autoReg", "false"));
    checkMacs = Boolean.parseBoolean(ServerProperties.getProperty("checkMacs", "false"));
    port = Short.parseShort(ServerProperties.getProperty("world.port", String.valueOf(DEFAULT_PORT)));

    IoBuffer.setUseDirectBuffer(false);
    IoBuffer.setAllocator(new SimpleBufferAllocator());

    acceptor = new NioSocketAcceptor();
    acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MapleCodecFactory()));
    acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 30);

    try {
        acceptor.setHandler(new MapleServerHandler(MapleServerHandler.LOGIN_SERVER));
        acceptor.bind(new InetSocketAddress(port));
        ((SocketSessionConfig) acceptor.getSessionConfig()).setTcpNoDelay(true);

        FileoutputUtil.log("\"登入\"伺服器正在监听" + port + "端口\r\n");
    } catch (IOException e) {
        System.err.println("无法绑定" + port + "端口: " + e);
    }
}
 
示例10
@Override
public void sessionCreated(IoSession session) throws Exception {
    SocketSessionConfig cfg = (SocketSessionConfig) session.getConfig();
    cfg.setReceiveBufferSize(2 * 1024 * 1024);
    cfg.setReadBufferSize(2 * 1024 * 1024);
    cfg.setKeepAlive(true);
    cfg.setSoLinger(0);
}
 
示例11
public WSClient(String host, int port) {
    this.host = host;
    this.port = port;
    connector = new NioSocketConnector();
    connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new WebSocketCodecFactory()));
    connector.setHandler(this);
    SocketSessionConfig sessionConf = connector.getSessionConfig();
    sessionConf.setReuseAddress(true);
    connector.setConnectTimeout(3);
}
 
示例12
public WSClient(String host, int port, int cookieLength) {
    this.cookie = RandomStringUtils.randomAscii(cookieLength);
    log.debug("Cookie length: {}", cookie.length());
    this.host = host;
    this.port = port;
    connector = new NioSocketConnector();
    connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new WebSocketCodecFactory()));
    connector.setHandler(this);
    SocketSessionConfig sessionConf = connector.getSessionConfig();
    sessionConf.setReuseAddress(true);
    connector.setConnectTimeout(3);
}
 
示例13
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;
}
 
示例14
/** {@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);
            }
        }
    }
}
 
示例15
/**
 * @return the socketSessionConfig
 */
public SocketSessionConfig getSocketSessionConfig()
{
    return socketSessionConfig;
}
 
示例16
/**
 * @param socketSessionConfig the socketSessionConfig to set
 */
public void setSocketSessionConfig( SocketSessionConfig socketSessionConfig )
{
    this.socketSessionConfig = socketSessionConfig;
}
 
示例17
/**
 * {@inheritDoc}
 */
@Override
public SocketSessionConfig getSessionConfig() {
    return (SocketSessionConfig) super.getSessionConfig();
}
 
示例18
/**
 * {@inheritDoc}
 */
@Override
public SocketSessionConfig getSessionConfig() {
    return (SocketSessionConfig) super.getSessionConfig();
}
 
示例19
/**
 * {@inheritDoc}
 */
public SocketSessionConfig getConfig() {
    return (SocketSessionConfig) config;
}
 
示例20
public Channel(final int world, final int channel, long startTime) {
    this.world = world;
    this.channel = channel;
    
    this.ongoingStartTime = startTime + 10000;  // rude approach to a world's last channel boot time, placeholder for the 1st wedding reservation ever
    this.mapManager = new MapleMapManager(null, world, channel);
    try {
        port = 7575 + this.channel - 1;
        port += (world * 100);
        ip = YamlConfig.config.server.HOST + ":" + port;
        IoBuffer.setUseDirectBuffer(false);
        IoBuffer.setAllocator(new SimpleBufferAllocator());
        acceptor = new NioSocketAcceptor();
        acceptor.setHandler(new MapleServerHandler(world, channel));
        acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 30);
        acceptor.getFilterChain().addLast("codec", (IoFilter) new ProtocolCodecFilter(new MapleCodecFactory()));
        acceptor.bind(new InetSocketAddress(port));
        ((SocketSessionConfig) acceptor.getSessionConfig()).setTcpNoDelay(true);
        for (MapleExpeditionType exped : MapleExpeditionType.values()) {
        	expedType.add(exped);
        }
        
        if (Server.getInstance().isOnline()) {  // postpone event loading to improve boot time... thanks Riizade, daronhudson for noticing slow startup times
            eventSM = new EventScriptManager(this, getEvents());
            eventSM.init();
        } else {
            String[] ev = {"0_EXAMPLE"};
            eventSM = new EventScriptManager(this, ev);
        }
        
        dojoStage = new int[20];
        dojoFinishTime = new long[20];
        dojoTask = new ScheduledFuture<?>[20];
        for(int i = 0; i < 20; i++) {
            dojoStage[i] = 0;
            dojoFinishTime[i] = 0;
            dojoTask[i] = null;
        }
        
        services = new ServicesManager(ChannelServices.OVERALL);
        
        System.out.println("    Channel " + getId() + ": Listening on port " + port);
    } catch (Exception e) {
        e.printStackTrace();
    }
}