org.apache.mina.transport.socket.SocketSessionConfig Java Examples

The following examples show how to use org.apache.mina.transport.socket.SocketSessionConfig. 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: WebSocketServerTest.java    From red5-websocket with Apache License 2.0 6 votes vote down vote up
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");
        }
    }
}
 
Example #2
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 #3
Source File: CashShopServer.java    From mapleLemon with GNU General Public License v2.0 6 votes vote down vote up
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);
    }
}
 
Example #4
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 #5
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 #6
Source File: WebSocketServerTest.java    From red5-websocket with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #7
Source File: WebSocketServerTest.java    From red5-websocket with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #8
Source File: MapleServerHandler.java    From mapleLemon with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #9
Source File: LoginServer.java    From mapleLemon with GNU General Public License v2.0 5 votes vote down vote up
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);
    }
}
 
Example #10
Source File: LdapNetworkConnection.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * 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 );
}
 
Example #11
Source File: MinaSocketServer.java    From jforgame with Apache License 2.0 5 votes vote down vote up
private SocketSessionConfig getSessionConfig() {
	SocketSessionConfig config = new DefaultSocketSessionConfig();
	config.setKeepAlive(true);
	config.setReuseAddress(true);

	return config;
}
 
Example #12
Source File: CrossServer.java    From jforgame with Apache License 2.0 5 votes vote down vote up
private SocketSessionConfig getSessionConfig() {
	SocketSessionConfig config = new DefaultSocketSessionConfig();
	config.setKeepAlive(true);
	config.setReuseAddress(true);

	return config;
}
 
Example #13
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 #14
Source File: NioSocketAcceptor.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public SocketSessionConfig getSessionConfig() {
    return (SocketSessionConfig) super.getSessionConfig();
}
 
Example #15
Source File: NioSocketSession.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public SocketSessionConfig getConfig() {
    return (SocketSessionConfig) config;
}
 
Example #16
Source File: NioSocketConnector.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public SocketSessionConfig getSessionConfig() {
    return (SocketSessionConfig) super.getSessionConfig();
}
 
Example #17
Source File: LdapNetworkConnection.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * @param socketSessionConfig the socketSessionConfig to set
 */
public void setSocketSessionConfig( SocketSessionConfig socketSessionConfig )
{
    this.socketSessionConfig = socketSessionConfig;
}
 
Example #18
Source File: LdapNetworkConnection.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * @return the socketSessionConfig
 */
public SocketSessionConfig getSocketSessionConfig()
{
    return socketSessionConfig;
}
 
Example #19
Source File: Channel.java    From HeavenMS with GNU Affero General Public License v3.0 4 votes vote down vote up
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();
    }
}
 
Example #20
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);
            }
        }
    }
}