本文整理汇总了Java中java.nio.channels.SelectionKey.interestOps方法的典型用法代码示例。如果您正苦于以下问题:Java SelectionKey.interestOps方法的具体用法?Java SelectionKey.interestOps怎么用?Java SelectionKey.interestOps使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.SelectionKey
的用法示例。
在下文中一共展示了SelectionKey.interestOps方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: finishConnection
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private final void finishConnection(SelectionKey key, MMOConnection<T> con)
{
try
{
((SocketChannel) key.channel()).finishConnect();
}
catch (IOException e)
{
con.getClient().onForcedDisconnection();
closeConnectionImpl(key, con);
}
// key might have been invalidated on finishConnect()
if (key.isValid())
{
key.interestOps(key.interestOps() | SelectionKey.OP_READ);
key.interestOps(key.interestOps() & ~SelectionKey.OP_CONNECT);
}
}
示例2: Client
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
public Client() throws IOException {
// 同样的,注册闹钟.
this.selector = Selector.open();
// 连接远程server
socketChannel = SocketChannel.open();
// 如果快速的建立了连接,返回true.如果没有建立,则返回false,并在连接后出发Connect事件.
Boolean isConnected = socketChannel.connect(new InetSocketAddress("localhost", 3562));
socketChannel.configureBlocking(false);
SelectionKey key = socketChannel.register(selector, SelectionKey.OP_READ);
if (isConnected) {
this.sendFirstMsg();
} else {
// 如果连接还在尝试中,则注册connect事件的监听. connect成功以后会出发connect事件.
key.interestOps(SelectionKey.OP_CONNECT);
}
}
示例3: SSLSocketChannel2
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
public SSLSocketChannel2( SocketChannel channel , SSLEngine sslEngine , ExecutorService exec , SelectionKey key ) throws IOException {
if( channel == null || sslEngine == null || exec == null )
throw new IllegalArgumentException( "parameter must not be null" );
this.socketChannel = channel;
this.sslEngine = sslEngine;
this.exec = exec;
readEngineResult = writeEngineResult = new SSLEngineResult( Status.BUFFER_UNDERFLOW, sslEngine.getHandshakeStatus(), 0, 0 ); // init to prevent NPEs
tasks = new ArrayList<Future<?>>( 3 );
if( key != null ) {
key.interestOps( key.interestOps() | SelectionKey.OP_WRITE );
this.selectionKey = key;
}
createBuffers( sslEngine.getSession() );
// kick off handshake
socketChannel.write( wrap( emptybuffer ) );// initializes res
processHandshake();
}
示例4: setInterestedInWrite
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
@Override
protected void setInterestedInWrite(NioSession session, boolean isInterested) throws Exception {
SelectionKey key = session.getSelectionKey();
if (key == null) {
return;
}
int newInterestOps = key.interestOps();
if (isInterested) {
newInterestOps |= SelectionKey.OP_WRITE;
//newInterestOps &= ~SelectionKey.OP_READ;
} else {
newInterestOps &= ~SelectionKey.OP_WRITE;
//newInterestOps |= SelectionKey.OP_READ;
}
key.interestOps(newInterestOps);
}
示例5: NIOServerCnxn
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
public NIOServerCnxn(ZooKeeperServer zk, SocketChannel sock,
SelectionKey sk, NIOServerCnxnFactory factory) throws IOException {
this.zkServer = zk;
this.sock = sock;
this.sk = sk;
this.factory = factory;
if (this.factory.login != null) {
this.zooKeeperSaslServer = new ZooKeeperSaslServer(factory.login);
}
if (zk != null) {
outstandingLimit = zk.getGlobalOutstandingLimit();
}
sock.socket().setTcpNoDelay(true);
/* set socket linger to false, so that socket close does not
* block */
sock.socket().setSoLinger(false, -1);
InetAddress addr = ((InetSocketAddress) sock.socket()
.getRemoteSocketAddress()).getAddress();
authInfo.add(new Id("ip", addr.getHostAddress()));
sk.interestOps(SelectionKey.OP_READ);
}
示例6: invokeCallbacks
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
/** Set the selected interest set on the port and run it. */
protected void invokeCallbacks() {
final Set<SelectionKey> selectedKeys = m_selector.selectedKeys();
ArrayList<Runnable> generatedTasks = null;
for(SelectionKey key : selectedKeys) {
final VoltPort port = (VoltPort) key.attachment();
if (port == null) {
continue;
}
try {
port.lockForHandlingWork();
key.interestOps(0);
final Runnable runner = getPortCallRunnable(port);
if (m_useExecutorService) {
if (generatedTasks == null) generatedTasks = new ArrayList<Runnable>();
generatedTasks.add(runner);
} else {
runner.run();
}
}
catch (CancelledKeyException e) {
e.printStackTrace();
// no need to do anything here until
// shutdown makes more sense
}
}
if (generatedTasks != null && !generatedTasks.isEmpty()) {
synchronized (m_tasks) {
m_tasks.addAll(generatedTasks);
if (m_tasks.size() > 1) {
m_tasks.notifyAll();
} else {
m_tasks.notify();
}
}
}
selectedKeys.clear();
}
示例7: setKey
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
void setKey (SelectionKey key) {
m_selectionKey = key;
m_channel = (SocketChannel)key.channel();
m_readStream = new NIOReadStream();
m_writeStream = new NIOWriteStream(
this,
m_handler.offBackPressure(),
m_handler.onBackPressure(),
m_handler.writestreamMonitor());
m_interestOps = key.interestOps();
}
示例8: writeAndFlush
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
public Futures.WriteFuture writeAndFlush(NioChannel channel, Object msg) {
SelectionKey key = channel.socketChannel().keyFor(selectorLoop.selector());
if (key != null && key.isValid()) {
key.interestOps(SelectionKey.OP_WRITE);
}
return write(channel, msg, true);
}
示例9: connect
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
final public void connect(SelectionKey key) throws IOException
{
if(clientSocket)
{
if(!((SocketChannel)key.channel()).finishConnect())
{
throw new IOException("Error connecting the channel.");
}
key.interestOps(key.interestOps()&~SelectionKey.OP_CONNECT);
connected(key);
}else
{
throw new IOException("Connect is not allowed on non-client channel.");
}
}
示例10: enableWrite
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private void enableWrite(boolean wakeup) {
boolean needWakeup = false;
try {
SelectionKey key = this.processKey;
key.interestOps(key.interestOps() | SelectionKey.OP_WRITE);
needWakeup = true;
} catch (Exception e) {
AbstractConnection.LOGGER.warn("can't enable write " + e);
}
if (needWakeup && wakeup) {
processKey.selector().wakeup();
}
}
示例11: write
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private void write(SelectionKey key) throws IOException {
SocketChannel channel = (SocketChannel) key.channel();
synchronized (this.sendQueues) {
List<RCONPacket> queue = this.sendQueues.get(channel);
ByteBuffer buffer = queue.get(0).toBuffer();
try {
channel.write(buffer);
queue.remove(0);
} catch (IOException exception) {
key.cancel();
channel.close();
if (this.rconSessions.contains(channel)) {
this.rconSessions.remove(channel);
}
if (this.sendQueues.containsKey(channel)) {
this.sendQueues.remove(channel);
}
return;
}
if (queue.isEmpty()) {
this.sendQueues.remove(channel);
}
key.interestOps(SelectionKey.OP_READ);
}
}
示例12: registerForRead
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
protected void registerForRead(final SelectionKey key, ObjectReader reader) {
if ( log.isTraceEnabled() )
log.trace("Adding key for read event:"+key);
reader.finish();
//register our OP_READ interest
Runnable r = new Runnable() {
@Override
public void run() {
try {
if (key.isValid()) {
// cycle the selector so this key is active again
key.selector().wakeup();
// resume interest in OP_READ, OP_WRITE
int resumeOps = key.interestOps() | SelectionKey.OP_READ;
key.interestOps(resumeOps);
if ( log.isTraceEnabled() )
log.trace("Registering key for read:"+key);
}
} catch (CancelledKeyException ckx ) {
NioReceiver.cancelledKey(key);
if ( log.isTraceEnabled() )
log.trace("CKX Cancelling key:"+key);
} catch (Exception x) {
log.error("Error registering key for read:"+key,x);
}
}
};
receiver.addEvent(r);
}
示例13: remove
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
public void remove(final KeyAttachment key, final int ops) {
Runnable r = new Runnable() {
@Override
public void run() {
if (key == null)
return;
NioChannel nch = key.getChannel();
if (nch == null)
return;
SocketChannel ch = nch.getIOChannel();
if (ch == null)
return;
SelectionKey sk = ch.keyFor(selector);
try {
if (sk == null) {
if (SelectionKey.OP_WRITE == (ops & SelectionKey.OP_WRITE))
countDown(key.getWriteLatch());
if (SelectionKey.OP_READ == (ops & SelectionKey.OP_READ))
countDown(key.getReadLatch());
} else {
if (sk.isValid()) {
sk.interestOps(sk.interestOps() & (~ops));
if (SelectionKey.OP_WRITE == (ops & SelectionKey.OP_WRITE))
countDown(key.getWriteLatch());
if (SelectionKey.OP_READ == (ops & SelectionKey.OP_READ))
countDown(key.getReadLatch());
if (sk.interestOps() == 0) {
sk.cancel();
sk.attach(null);
}
} else {
sk.cancel();
sk.attach(null);
}
}
} catch (CancelledKeyException cx) {
if (sk != null) {
sk.cancel();
sk.attach(null);
}
}
}
};
events.offer(r);
wakeup();
}
示例14: disableRead
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
public void disableRead() {
SelectionKey key = this.processKey;
key.interestOps(key.interestOps() & OP_NOT_READ);
}
示例15: process
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
/**
* State machine to send data
* @param key SelectionKey
* @return boolean
* @throws IOException
*/
public boolean process(SelectionKey key, boolean waitForAck) throws IOException {
int ops = key.readyOps();
key.interestOps(key.interestOps() & ~ops);
//in case disconnect has been called
if ((!isConnected()) && (!connecting)) throw new IOException("Sender has been disconnected, can't selection key.");
if ( !key.isValid() ) throw new IOException("Key is not valid, it must have been cancelled.");
if ( key.isConnectable() ) {
if ( socketChannel.finishConnect() ) {
completeConnect();
if ( current != null ) key.interestOps(key.interestOps() | SelectionKey.OP_WRITE);
return false;
} else {
//wait for the connection to finish
key.interestOps(key.interestOps() | SelectionKey.OP_CONNECT);
return false;
}//end if
} else if ( key.isWritable() ) {
boolean writecomplete = write(key);
if ( writecomplete ) {
//we are completed, should we read an ack?
if ( waitForAck ) {
//register to read the ack
key.interestOps(key.interestOps() | SelectionKey.OP_READ);
} else {
//if not, we are ready, setMessage will reregister us for another write interest
//do a health check, we have no way of verify a disconnected
//socket since we don't register for OP_READ on waitForAck=false
read(key);//this causes overhead
setRequestCount(getRequestCount()+1);
return true;
}
} else {
//we are not complete, lets write some more
key.interestOps(key.interestOps()|SelectionKey.OP_WRITE);
}//end if
} else if ( key.isReadable() ) {
boolean readcomplete = read(key);
if ( readcomplete ) {
setRequestCount(getRequestCount()+1);
return true;
} else {
key.interestOps(key.interestOps() | SelectionKey.OP_READ);
}//end if
} else {
//unknown state, should never happen
log.warn("Data is in unknown state. readyOps="+ops);
throw new IOException("Data is in unknown state. readyOps="+ops);
}//end if
return false;
}