Java Code Examples for org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder#addLast()
The following examples show how to use
org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder#addLast() .
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: MinaTcpClient.java From game-server with MIT License | 6 votes |
/** * 初始化tcp连接 * @param clientProtocolHandler */ private void init(IoHandler clientProtocolHandler) { connector = new NioSocketConnector(); DefaultIoFilterChainBuilder chain = connector.getFilterChain(); chain.addLast("codec", codecFilter); if(filters != null){ filters.forEach((key, filter)->{ if("ssl".equalsIgnoreCase(key) || "tls".equalsIgnoreCase(key)){ //ssl过滤器必须添加到首部 chain.addFirst(key, filter); }else{ chain.addLast(key, filter); } }); } connector.setHandler(clientProtocolHandler); connector.setConnectTimeoutMillis(60000L); connector.setConnectTimeoutCheckInterval(10000); }
Example 2
Source File: CrossServer.java From jforgame with Apache License 2.0 | 6 votes |
/** * start Mina serversocket * @throws Exception */ @Override public void start() throws Exception { final int serverPort = ServerConfig.getInstance().getCrossPort(); IoBuffer.setUseDirectBuffer(false); IoBuffer.setAllocator(new SimpleBufferAllocator()); acceptor = new NioSocketAcceptor(pool); acceptor.setReuseAddress(true); acceptor.getSessionConfig().setAll(getSessionConfig()); logger.info("cross server start at port:{},正在监听服务器点对点的连接...", serverPort); DefaultIoFilterChainBuilder filterChain = acceptor.getFilterChain(); filterChain.addLast("codec", new ProtocolCodecFilter(SerializerHelper.getInstance().getCodecFactory())); //指定业务逻辑处理器 acceptor.setHandler(new Game2GameIoHandler(BaseCrossMessageDispatcher.getInstance())); //设置端口号 acceptor.setDefaultLocalAddress(new InetSocketAddress(serverPort)); //启动监听 acceptor.bind(); }
Example 3
Source File: MinaSocketServer.java From jforgame with Apache License 2.0 | 6 votes |
/** * start Mina serversocket * @throws Exception */ @Override public void start() throws Exception { int serverPort = ServerConfig.getInstance().getServerPort(); IoBuffer.setUseDirectBuffer(false); IoBuffer.setAllocator(new SimpleBufferAllocator()); acceptor = new NioSocketAcceptor(pool); acceptor.setReuseAddress(true); acceptor.getSessionConfig().setAll(getSessionConfig()); logger.info("mina socket server start at port:{},正在监听客户端的连接...", serverPort); DefaultIoFilterChainBuilder filterChain = acceptor.getFilterChain(); filterChain.addLast("codec", new ProtocolCodecFilter(SerializerHelper.getInstance().getCodecFactory())); filterChain.addLast("moduleEntrance", new ModuleEntranceFilter()); filterChain.addLast("msgTrace", new MessageTraceFilter()); filterChain.addLast("flood", new FloodFilter()); //指定业务逻辑处理器 acceptor.setHandler(new ServerSocketIoHandler(new MessageDispatcher())); //设置端口号 acceptor.setDefaultLocalAddress(new InetSocketAddress(serverPort)); //启动监听 acceptor.bind(); }
Example 4
Source File: LdapsInitializer.java From MyVirtualDirectory with Apache License 2.0 | 6 votes |
public static IoFilterChainBuilder init( LdapServer server ) throws LdapException { SSLContext sslCtx; try { sslCtx = server.getSSLContext(); } catch ( Exception e ) { throw new LdapException( I18n.err( I18n.ERR_683 ), e ); } DefaultIoFilterChainBuilder chain = new DefaultIoFilterChainBuilder(); SslFilter sslFilter = new SslFilter( sslCtx ); List<String> cipherSuites = server.getEnabledCipherSuites(); if( ( cipherSuites != null ) && !cipherSuites.isEmpty() ) { sslFilter.setEnabledCipherSuites( cipherSuites.toArray( new String[cipherSuites.size()] ) ); } sslFilter.setWantClientAuth( true ); chain.addLast( "sslFilter", sslFilter ); return chain; }
Example 5
Source File: NetManager.java From GameServer with Apache License 2.0 | 6 votes |
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 6
Source File: HttpServer.java From game-server with MIT License | 5 votes |
@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 7
Source File: UdpServer.java From game-server with MIT License | 5 votes |
/** {@inheritDoc} */ @Override public void run() { synchronized (this) { if (!isRunning) { DefaultIoFilterChainBuilder chain = acceptor.getFilterChain(); if (factory == null) { factory = new DefaultProtocolCodecFactory(); } factory.getDecoder().setMaxReadSize(minaServerConfig.getMaxReadSize()); factory.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)->chain.addLast(key, filter)); } DatagramSessionConfig dc = acceptor.getSessionConfig(); dc.setReuseAddress(minaServerConfig.isReuseAddress()); dc.setReceiveBufferSize(minaServerConfig.getReceiveBufferSize()); dc.setSendBufferSize(minaServerConfig.getSendBufferSize()); dc.setIdleTime(IdleStatus.READER_IDLE, minaServerConfig.getReaderIdleTime()); dc.setIdleTime(IdleStatus.WRITER_IDLE, minaServerConfig.getWriterIdleTime()); dc.setBroadcast(true); dc.setCloseOnPortUnreachable(true); acceptor.setHandler(ioHandler); try { acceptor.bind(new InetSocketAddress(minaServerConfig.getPort())); LOGGER.warn("已开始监听UDP端口:{}", minaServerConfig.getPort()); } catch (IOException e) { LOGGER.warn("监听UDP端口:{}已被占用", minaServerConfig.getPort()); LOGGER.error("UDP, 服务异常", e); } } } }
Example 8
Source File: AbstractMINAService.java From sailfish-core with Apache License 2.0 | 4 votes |
protected void initFilterChain(DefaultIoFilterChainBuilder filterChain) throws Exception { CodecFactory codecFactory = new CodecFactory(serviceContext, messageFactory, dictionary, getCodecClass(), getCodecSettings()); filterChain.addLast("codec", new ProtocolCodecFilter(codecFactory)); }
Example 9
Source File: TcpServer.java From game-server with MIT License | 4 votes |
/** {@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); } } } }
Example 10
Source File: LdapsInitializer.java From MyVirtualDirectory with Apache License 2.0 | 4 votes |
/** * Initialize the LDAPS server. * * @param ldapServer The LDAP server instance * @param transport The TCP transport that contains the SSL configuration * @return A IoFilter chain * @throws LdapException If we had a pb */ public static IoFilterChainBuilder init( LdapServer ldapServer, TcpTransport transport ) throws LdapException { SSLContext sslCtx; try { sslCtx = ldapServer.getSSLContext(); //TODO see if this is correct // Initialize the SSLContext to work with our key managers. //sslCtx = SSLContext.getInstance( "TLS" ); //sslCtx.init( ldapServer.getKeyManagerFactory().getKeyManagers(), new TrustManager[] // { new NoVerificationTrustManager() }, new SecureRandom() ); } catch ( Exception e ) { throw new LdapException( I18n.err( I18n.ERR_683 ), e ); } DefaultIoFilterChainBuilder chain = new DefaultIoFilterChainBuilder(); SslFilter sslFilter = new SslFilter( sslCtx ); // The ciphers List<String> cipherSuites = transport.getCipherSuite(); if ( ( cipherSuites != null ) && !cipherSuites.isEmpty() ) { sslFilter.setEnabledCipherSuites( cipherSuites.toArray( new String[cipherSuites.size()] ) ); } // The protocols List<String> enabledProtocols = transport.getEnabledProtocols(); if ( ( enabledProtocols != null ) && !enabledProtocols.isEmpty() ) { sslFilter.setEnabledProtocols( enabledProtocols.toArray( new String[enabledProtocols.size()] ) ); } else { // Be sure we disable SSLV3 sslFilter.setEnabledProtocols( new String[] { "SSLv2Hello", "TLSv1", "TLSv1.1", "TLSv1.2" } ); } // The remaining SSL parameters sslFilter.setNeedClientAuth( transport.isNeedClientAuth() ); sslFilter.setWantClientAuth( transport.isWantClientAuth() ); chain.addLast( "sslFilter", sslFilter ); return chain; }