📄 nonblockingsessionacceptor.java
字号:
/****************************************************************************
* Package : com.ecSolutions.ecAppServer.server.session.nio
* File : DatagramChannelSession.java
* Create Date : 2007-7-20
* Author : Steven Chen
*
* Copyright(C) 2006 ecSolutions(shanghai) Co.,Limited.All Rights Reserved.
*
***************************************************************************/
package com.ecSolutions.ecAppServer.server.session.nio;
import java.io.IOException;
import java.net.SocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import com.ecSolutions.ecAppServer.server.Session;
import com.ecSolutions.ecAppServer.server.SessionHandlerAdapter;
import com.ecSolutions.ecAppServer.server.SessionType;
import com.ecSolutions.ecAppServer.server.session.AbstractSessionAcceptor;
import com.ecSolutions.ecAppServer.server.util.ChannelUtils;
import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
/**
* Non-blocking session acceptor.
*
* @author Steven Chen
* @version $Id: NonBlockingSessionAcceptor.java,v 1.2 2007/07/26 03:48:59 stevenchen Exp $
*/
public class NonBlockingSessionAcceptor extends AbstractSessionAcceptor {
private final AtomicInteger counter = new AtomicInteger();
private final ServerSocketChannelSession session = new ServerSocketChannelSession() {
protected void buildSession(SocketChannel sc) {
counter.incrementAndGet();
try {
setSocketOptions(sc.socket());
SocketChannelSession session = new SocketChannelSession();
session.setChannel(sc);
sessionAccepted(session);
} catch (Throwable e) {
exceptionCaught(e);
}
}
};
public NonBlockingSessionAcceptor() {
session.setSessionHandler(new SessionHandlerAdapter() {
public void exceptionCaught(Session session, Throwable cause) {
NonBlockingSessionAcceptor.this.exceptionCaught(cause);
}
});
}
public SessionType getSessionType() {
return SessionType.TCP;
}
public boolean isStarted() {
return session.isStarted();
}
public SocketAddress getListenAddress() {
if (isStarted())
return session.getSocket().getLocalSocketAddress();
return super.getListenAddress();
}
public int getListenPort() {
if (isStarted())
return session.getSocket().getLocalPort();
return super.getListenPort();
}
public synchronized void start() {
if (getAcceptorHandler() == null)
throw new IllegalStateException("acceptor handler is null");
if (isStarted())
return;
ServerSocketChannel channel = null;
try {
channel = ServerSocketChannel.open();
setServerSocketOptions(channel.socket());
counter.set(0);
session.setChannel(channel);
session.start().complete();
} catch (IOException e) {
ChannelUtils.close(channel);
exceptionCaught(e);
}
}
public int getAcceptedCount() {
return counter.get();
}
public synchronized void close() {
session.close().complete();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -