本文整理汇总了Java中org.apache.mina.core.service.IoHandler类的典型用法代码示例。如果您正苦于以下问题:Java IoHandler类的具体用法?Java IoHandler怎么用?Java IoHandler使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
IoHandler类属于org.apache.mina.core.service包,在下文中一共展示了IoHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: startListner
import org.apache.mina.core.service.IoHandler; //导入依赖的package包/类
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));
}
示例2: createReceiver
import org.apache.mina.core.service.IoHandler; //导入依赖的package包/类
private static void createReceiver(IoHandler serverHandler, InetSocketAddress address) {
try {
NioSocketAcceptor acceptor = new NioSocketAcceptor();
acceptor.setReuseAddress(true);
acceptor.setHandler(serverHandler);
acceptor.bind(address);
logger.info("Receiver started on port {}", address.getPort());
receivers.put(address, acceptor);
} catch (IOException ex) {
logger.error(ex.getMessage(), ex);
fail("Failed to start listener on port " + address.getPort());
}
}
示例3: connect
import org.apache.mina.core.service.IoHandler; //导入依赖的package包/类
/**
* <p>Connect to a remote socket. If org.opennms.netmgt.provision.maxConcurrentConnections
* is set, this may block until a connection slot is available.</p>
*
* <p>You must dispose both the {@link ConnectionFactoryNewConnectorImpl} and {@link ConnectFuture} when done
* by calling {@link #dispose(ConnectionFactoryNewConnectorImpl, ConnectFuture)}.</p>
*
* @param remoteAddress
* Destination address
* @param init
* Initialiser for the IoSession
* @return
* ConnectFuture from a Mina connect call
*/
@Override
public ConnectFuture connect(SocketAddress remoteAddress, IoSessionInitializer<? extends ConnectFuture> init, IoHandler handler) {
SocketConnector connector = getSocketConnector(getTimeout(), handler);
InetSocketAddress localAddress = null;
synchronized (m_portMutex) {
if (m_port.get() == null) {
// Fetch a new ephemeral port
localAddress = new InetSocketAddress(0);
m_port.set(localAddress.getPort());
} else {
localAddress = new InetSocketAddress(m_port.get());
}
}
final ConnectFuture cf = connector.connect(remoteAddress, localAddress, init);
cf.addListener(portSwitcher(connector, remoteAddress, init, handler));
cf.addListener(connectorDisposer(connector));
return cf;
}
示例4: connect0
import org.apache.mina.core.service.IoHandler; //导入依赖的package包/类
/**
* Connects to the specified <code>address</code>. If communication starts
* successfully, events are fired to the connector's <code>handler</code>.
*
* @param remoteAddress the remote address to connect to
* @param localAddress the local address
* @param sessionInitializer the session initializer
* @return {@link ConnectFuture} that will tell the result of the connection attempt
*/
@SuppressWarnings("unchecked")
@Override
protected ConnectFuture connect0(final SocketAddress remoteAddress, final SocketAddress localAddress,
final IoSessionInitializer<? extends ConnectFuture> sessionInitializer) {
if (!proxyIoSession.isReconnectionNeeded()) {
// First connection
IoHandler handler = getHandler();
if (!(handler instanceof AbstractProxyIoHandler)) {
throw new IllegalArgumentException("IoHandler must be an instance of AbstractProxyIoHandler");
}
connector.setHandler(handler);
future = new DefaultConnectFuture();
}
ConnectFuture conFuture = connector.connect(proxyIoSession.getProxyAddress(), new ProxyIoSessionInitializer(
sessionInitializer, proxyIoSession));
// If proxy does not use reconnection like socks the connector's
// future is returned. If we're in the middle of a reconnection
// then we send back the connector's future which is only used
// internally while <code>future</code> will be used to notify
// the user of the connection state.
if (proxyIoSession.getRequest() instanceof SocksProxyRequest || proxyIoSession.isReconnectionNeeded()) {
return conFuture;
}
return future;
}
示例5: VmPipeSession
import org.apache.mina.core.service.IoHandler; //导入依赖的package包/类
VmPipeSession(IoService service, IoServiceListenerSupport serviceListeners, VmPipeAddress localAddress,
IoHandler handler, VmPipe remoteEntry) {
super(service);
config = new DefaultVmPipeSessionConfig();
this.serviceListeners = serviceListeners;
lock = new ReentrantLock();
this.localAddress = localAddress;
remoteAddress = serviceAddress = remoteEntry.getAddress();
filterChain = new VmPipeFilterChain(this);
receivedMessageQueue = new LinkedBlockingQueue<Object>();
remoteSession = new VmPipeSession(this, remoteEntry);
}
示例6: setHandler
import org.apache.mina.core.service.IoHandler; //导入依赖的package包/类
/**
* Sets the {@link IoHandler} which handles this session.
*/
public void setHandler(IoHandler handler) {
if (handler == null) {
throw new IllegalArgumentException("handler");
}
this.handler = handler;
}
示例7: createIoHandler
import org.apache.mina.core.service.IoHandler; //导入依赖的package包/类
private static IoHandler createIoHandler() {
StateMachine sm = StateMachineFactory.getInstance(
IoHandlerTransition.class).create(ServerHandler.NOT_CONNECTED,
new ServerHandler());
return new StateMachineProxyBuilder().setStateContextLookup(
new IoSessionStateContextLookup(new StateContextFactory() {
public StateContext create() {
return new ServerHandler.TetrisServerContext();
}
})).create(IoHandler.class, sm);
}
示例8: GameClient
import org.apache.mina.core.service.IoHandler; //导入依赖的package包/类
public GameClient(String remoteHost, int remotePort, IoHandler handler) {
this.remoteHost = remoteHost;
this.remotePort = remotePort;
if ( handler == null ) {
this.handler = this;
} else {
this.handler = handler;
}
}
示例9: SimpleClient
import org.apache.mina.core.service.IoHandler; //导入依赖的package包/类
/**
* Create a new Client
* @param filter
* @param handler
* @param remoteHost
* @param remotePort
*/
public SimpleClient(IoFilter filter, IoHandler handler,
String remoteHost, int remotePort) {
this.ioFilter = filter;
this.ioHandler = handler;
this.remoteHost = remoteHost;
this.remotePort = remotePort;
this.connectKey = StringUtil.concat(remoteHost, Constant.COLON, remotePort);
this.clientNo = SimpleClient.totalClientNo++;
}
示例10: ReloadProtocolCodecFilter
import org.apache.mina.core.service.IoHandler; //导入依赖的package包/类
public ReloadProtocolCodecFilter(String ioFilterClassName, String ioHandlerClassName){
try {
this.ioFilterClassName = ioFilterClassName;
this.ioHandlerClassName = ioHandlerClassName;
this.classLoader = ReloadClassLoader.currentClassLoader();
Class ioFilterClass = this.classLoader.loadClass(ioFilterClassName);
Class ioHandlerClass = this.classLoader.loadClass(ioHandlerClassName);
this.ioFilter = (IoFilter)ioFilterClass.newInstance();
this.ioHandler = (IoHandler)ioHandlerClass.newInstance();
} catch (Throwable e) {
log.error("Failed to create ReloadProtocolCodecFilter", e);
}
}
示例11: reload
import org.apache.mina.core.service.IoHandler; //导入依赖的package包/类
/**
* Reload the internal instances.
*/
public void reload() {
try {
this.classLoader = ReloadClassLoader.newClassloader(this.classLoader.getClasspathURLs());
Class ioFilterClass = this.classLoader.loadClass(ioFilterClassName);
Class ioHandlerClass = this.classLoader.loadClass(ioHandlerClassName);
this.ioFilter = (IoFilter)ioFilterClass.newInstance();
this.ioHandler = (IoHandler)ioHandlerClass.newInstance();
} catch (Throwable e) {
log.error("Reload error", e);
}
}
示例12: getSocketConnector
import org.apache.mina.core.service.IoHandler; //导入依赖的package包/类
private static final NioSocketConnector getSocketConnector(long timeout, IoHandler handler) {
// Create a new NioSocketConnector
//NioSocketConnector connector = new NioSocketConnector(m_executor, m_processor);
// To address bug NMS-6412, I'm changing this to use the default constructor so that the
// Executor pool and IoProcessor pools are created and destroyed for every connector.
// This slows things down but might be an acceptable tradeoff for more reliable detection.
//
// @see http://issues.opennms.org/browse/NMS-6412
//
NioSocketConnector connector = new NioSocketConnector(Runtime.getRuntime().availableProcessors());
// Enable SO_REUSEADDR on the socket so that TIMED_WAIT connections that are bound on the
// same port do not block new outgoing connections. If the connections are blocked, then
// the following exception will be thrown under heavy load:
//
// Caused by: java.net.SocketException: Invalid argument
// at sun.nio.ch.Net.connect(Native Method)
// at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:500)
// at org.apache.mina.transport.socket.nio.NioSocketConnector.connect(NioSocketConnector.java:188)
// ...
//
// @see http://issues.opennms.org/browse/NMS-5469
//
connector.getSessionConfig().setReuseAddress(true);
// Setting SO_LINGER will prevent TIME_WAIT sockets altogether because TCP connections will be
// forcefully terminated with RST packets. However, this doesn't seem to be necessary to maintain
// performance of the outgoing connections. As long as SO_REUSEADDR is set, the operating system
// and Java appear to recycle the ports effectively although some will remain in TIME_WAIT state.
//
// @see http://issues.opennms.org/browse/NMS-5469
//
//connector.getSessionConfig().setSoLinger(0);
connector.setHandler(handler);
connector.setConnectTimeoutMillis(timeout);
return connector;
}
示例13: PeerlessClientSupport
import org.apache.mina.core.service.IoHandler; //导入依赖的package包/类
public PeerlessClientSupport(String name, String pass, String id, IoHandler handler) {
if (name == null) {
throw new IllegalArgumentException("Name can not be null");
}
this.name = name;
this.password = pass;
this.handler = handler;
this.id = id;
}
示例14: startReceivers
import org.apache.mina.core.service.IoHandler; //导入依赖的package包/类
/**
* Starts one or more listeners. There will be one unique listener for each port.
* Each listener will be assigned a handler defined by the createServerHandler() method.
* If the handler is shared then it should be made thread-safe.
*/
protected static void startReceivers(IoHandler serverHandler, InetSocketAddress... addresses) {
for (InetSocketAddress address : addresses) {
NioSocketAcceptor acceptor = receivers.get(address);
if (acceptor == null) {
createReceiver(serverHandler, address);
}
}
}
示例15: getSocketConnector
import org.apache.mina.core.service.IoHandler; //导入依赖的package包/类
private static final NioSocketConnector getSocketConnector(long timeout, IoHandler handler) {
// Create a new NioSocketConnector
NioSocketConnector connector = new NioSocketConnector(m_executor, m_processor);
// Enable SO_REUSEADDR on the socket so that TIMED_WAIT connections that are bound on the
// same port do not block new outgoing connections. If the connections are blocked, then
// the following exception will be thrown under heavy load:
//
// Caused by: java.net.SocketException: Invalid argument
// at sun.nio.ch.Net.connect(Native Method)
// at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:500)
// at org.apache.mina.transport.socket.nio.NioSocketConnector.connect(NioSocketConnector.java:188)
// ...
//
// @see http://issues.opennms.org/browse/NMS-5469
//
connector.getSessionConfig().setReuseAddress(true);
// Setting SO_LINGER will prevent TIME_WAIT sockets altogether because TCP connections will be
// forcefully terminated with RST packets. However, this doesn't seem to be necessary to maintain
// performance of the outgoing connections. As long as SO_REUSEADDR is set, the operating system
// and Java appear to recycle the ports effectively although some will remain in TIME_WAIT state.
//
// @see http://issues.opennms.org/browse/NMS-5469
//
//connector.getSessionConfig().setSoLinger(0);
connector.setHandler(handler);
connector.setConnectTimeoutMillis(timeout);
return connector;
}