📄 responder.java
字号:
/**
* Copyright (C) 2003 Manfred Andres
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Created on 22.10.2003
*/
package freecs.core;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.channels.spi.SelectorProvider;
import java.util.Iterator;
import java.util.Set;
import freecs.Server;
import freecs.interfaces.IContainer;
import freecs.util.ObjectBuffer;
/**
* @author Manfred Andres
*
* freecs.core
*/
public class Responder extends Thread {
public static final Responder res = new Responder();
private Selector sel;
private Responder() {
try {
sel = SelectorProvider.provider().openSelector();
} catch (Exception e) {
Server.debug ("Unable to start Listener!", e, Server.MSG_ERROR, Server.LVL_HALT);
}
}
public static void startResponder() {
if (!res.isAlive())
res.start();
res.setPriority(MAX_PRIORITY);
}
public void addToWrite(SocketChannel sc, ConnectionBuffer cb) {
if (!cb.isValid())
return;
SelectionKey sk = sc.keyFor(res.sel);
if (sk == null) try {
sk = sc.register(sel, SelectionKey.OP_WRITE, cb);
} catch (ClosedChannelException cce) {
Server.debug ("Responder.addToWrite: Channel closed", cce, Server.MSG_TRAFFIC, Server.LVL_VERY_VERBOSE);
dropKey(sk);
}
}
public void run() {
while (Server.srv.isRunning() || CentralSelector.cSel.isAlive()) try {
try {
sel.selectNow();
} catch (IOException ioe) {
Server.debug("Responder.run: ", ioe, Server.MSG_ERROR, Server.LVL_VERBOSE);
} catch (Exception e) {
Server.debug("Responder.run: ", e, Server.MSG_ERROR, Server.LVL_MAJOR);
}
Set ks = sel.selectedKeys();
if (ks!=null && !ks.isEmpty()) {
for (Iterator i = ks.iterator(); i.hasNext(); ) {
SelectionKey ck = (SelectionKey) i.next();
i.remove();
ConnectionBuffer cb = (ConnectionBuffer) ck.attachment();
if (!cb.isValid()) {
dropKey (ck);
continue;
}
synchronized (cb) {
SocketChannel sc = (SocketChannel) ck.channel();
ObjectBuffer ob = cb.getWriteQueue();
if (ob.isEmpty()) {
if (cb.hasToBeClosed()) {
Server.log ("Responder.run: closing connection (keep-alive-timout)", Server.MSG_TRAFFIC, Server.LVL_VERY_VERBOSE);
dropKey(ck);
}
cb.notifyAll();
continue;
}
IContainer ic = (IContainer) ob.get();
if (writeContent (sc, ic.getByteBuffer())) {
ob.pop();
if (!sc.isOpen() || ic.closeSocket())
dropKey(ck);
} else if (!sc.isOpen()) {
dropKey(ck);
}
cb.updateCloseWhen();
cb.notifyAll();
}
}
}
try {
Thread.sleep(33);
} catch (InterruptedException ie) { }
} catch (Exception e) {
Server.debug ("Responder.run: ", e, Server.MSG_ERROR, Server.LVL_MAJOR);
}
}
private void dropKey (SelectionKey sk) {
if (sk == null)
return;
SocketChannel sc = (SocketChannel) sk.channel();
sk.cancel();
CentralSelector.dropChannel(sc);
}
public void dropChannel (SocketChannel sc) {
SelectionKey sk = sc.keyFor(sel);
if (sk != null)
sk.cancel();
}
/**
* writes out the content. if there is nothing more to write in this bytebuffer or
* the channel isn't open anymore, true will be returned, false otherwise
* @param sc
* @param bb
* @return
* @throws IOException
*/
private boolean writeContent (SocketChannel sc, ByteBuffer bb) {
int written = 0;
try {
written = sc.write (bb);
} catch (IOException ioe) {
// is thrown if the connection was closed from remote host...
// do nothing than...
Server.log("CentralSelector.writeContent: remote host has closed connection", Server.MSG_TRAFFIC, Server.LVL_VERY_VERBOSE);
return true;
}
if (written < 0) {
return true;
}
if (bb.remaining () > 0)
return false;
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -