📄 serversocketchannelsession.java
字号:
/****************************************************************************
* Package : com.ecSolutions.ecAppServer.server.session.nio
* File : ServerSocketChannelSession.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.InetSocketAddress;
import java.net.ServerSocket;
import java.net.SocketAddress;
import java.nio.channels.SelectableChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import com.ecSolutions.ecAppServer.server.Future;
import com.ecSolutions.ecAppServer.server.Packet;
import com.ecSolutions.ecAppServer.server.SessionType;
import com.ecSolutions.ecAppServer.server.session.nio.reactor.Reactor;
import com.ecSolutions.ecAppServer.server.session.nio.reactor.ReactorHandler;
import com.ecSolutions.ecAppServer.server.util.ChannelUtils;
import com.ecSolutions.ecAppServer.server.util.Configuration;
/**
* Server socket channel session. Application can override buildSession method
* to build custom session.
*
* @author Steven Chen
* @version $Id: ServerSocketChannelSession.java,v 1.2 2007/07/26 03:48:59 stevenchen Exp $
*/
public class ServerSocketChannelSession extends AbstractChannelSession {
private ServerSocketChannel channel;
public SessionType getSessionType() {
return SessionType.UNKNOWN;
}
public SocketAddress getRemoteAddress() {
return null;
}
public SocketAddress getLocalAddress() {
if (isStarted())
return channel.socket().getLocalSocketAddress();
return super.getLocalAddress();
}
/**
* Set the port the server socket session listen to.
*
* @param port
* listen port
* @throws IllegalStateException
*/
public void setLocalPort(int port) {
if (isStarted())
throw new IllegalStateException(
"can't set local port after session started");
setLocalAddress(new InetSocketAddress(port));
}
/**
* Set the server socket channel which the session will used.
*
* @param ssc
* server socket channel
* @throws IllegalStateException
*/
public void setChannel(ServerSocketChannel ssc) {
if (isStarted())
throw new IllegalStateException(
"can't set server socket channel after session started");
this.channel = ssc;
}
/**
* Get the server socket channel associted with the session.
*
* @return server socket channel
*/
public ServerSocketChannel getChannel() {
return channel;
}
/**
* Get the server socket associted with the session.
*
* @return server socket
*/
public ServerSocket getSocket() {
ServerSocketChannel ssc = channel;
if (ssc == null)
return null;
return ssc.socket();
}
public Future send(Packet packet, int priority) {
throw new IllegalStateException(
"can't send packet to server socket session");
}
protected ReactorHandler getReactorHandler() {
return new ChannelReactorHandler() {
public SelectableChannel[] getChannels() {
return new SelectableChannel[] { channel };
}
public void onAcceptable() {
SocketChannel sc = null;
try {
while ((sc = channel.accept()) != null) {
buildSession(sc);
}
// accept next
getReactor().interest(this, Reactor.OP_ACCEPT);
} catch (IOException e) {
ChannelUtils.close(sc);
dispatchException(e);
close();
}
}
};
}
/**
* Build accepted session. Application can choose to start a session or
* close the channel. The default action is close the socket channel.
*
* @param sc
* the socket channel that server socket session accepted
*/
protected void buildSession(SocketChannel sc) {
ChannelUtils.close(sc);
}
protected void doStart() throws IOException {
if (channel != null)
return;
try {
channel = ServerSocketChannel.open();
SocketAddress localAddr = getLocalAddress();
ServerSocket socket = channel.socket();
socket.setReuseAddress(Configuration.isReuseAcceptorAddress());
int recvBufferSize = Configuration.getRecvBufferSize();
if (recvBufferSize > 0)
socket.setReceiveBufferSize(recvBufferSize);
socket.bind(localAddr == null ? new InetSocketAddress(0)
: localAddr, Configuration.getAcceptorBacklog());
} catch (IOException e) {
doClose();
throw e;
}
}
protected void doClose() {
ChannelUtils.close(channel);
channel = null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -