本文整理汇总了Java中java.nio.channels.CancelledKeyException类的典型用法代码示例。如果您正苦于以下问题:Java CancelledKeyException类的具体用法?Java CancelledKeyException怎么用?Java CancelledKeyException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CancelledKeyException类属于java.nio.channels包,在下文中一共展示了CancelledKeyException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doAsyncWrite
import java.nio.channels.CancelledKeyException; //导入依赖的package包/类
private void doAsyncWrite(SelectionKey key) throws IOException {
Call call = (Call)key.attachment();
if (call == null) {
return;
}
if (key.channel() != call.connection.channel) {
throw new IOException("doAsyncWrite: bad channel");
}
synchronized(call.connection.responseQueue) {
if (processResponse(call.connection.responseQueue, false)) {
try {
key.interestOps(0);
} catch (CancelledKeyException e) {
/* The Listener/reader might have closed the socket.
* We don't explicitly cancel the key, so not sure if this will
* ever fire.
* This warning could be removed.
*/
LOG.warn("Exception while changing ops : " + e);
}
}
}
}
示例2: processDebuggerActivity
import java.nio.channels.CancelledKeyException; //导入依赖的package包/类
private void processDebuggerActivity(SelectionKey key) {
Debugger dbg = (Debugger)key.attachment();
try {
if (key.isAcceptable()) {
try {
acceptNewDebugger(dbg, null);
} catch (IOException ioe) {
Log.w("ddms", "debugger accept() failed");
ioe.printStackTrace();
}
} else if (key.isReadable()) {
processDebuggerData(key);
} else {
Log.d("ddm-debugger", "key in unknown state");
}
} catch (CancelledKeyException cke) {
// key has been cancelled we can ignore that.
}
}
示例3: sendPacket
import java.nio.channels.CancelledKeyException; //导入依赖的package包/类
public final void sendPacket(final SendablePacket<T> sp)
{
sp._client = _client;
if (_pendingClose)
return;
synchronized (getSendQueue())
{
_sendQueue.addLast(sp);
}
if (!_sendQueue.isEmpty())
{
try
{
_selectionKey.interestOps(_selectionKey.interestOps() | SelectionKey.OP_WRITE);
}
catch (CancelledKeyException e)
{
// ignore
}
}
}
示例4: sendPacket
import java.nio.channels.CancelledKeyException; //导入依赖的package包/类
public final void sendPacket(SendablePacket<T> sp)
{
sp._client = _client;
if (_pendingClose)
{
return;
}
synchronized (getSendQueue())
{
_sendQueue.addLast(sp);
}
if (!_sendQueue.isEmpty())
{
try
{
_selectionKey.interestOps(_selectionKey.interestOps() | SelectionKey.OP_WRITE);
}
catch (CancelledKeyException e)
{
// ignore
}
}
}
示例5: registerSelector
import java.nio.channels.CancelledKeyException; //导入依赖的package包/类
public static SelectionKey registerSelector(final Selector selector,
final SocketChannel channel, final int op)
throws CancelledKeyException, ClosedChannelException {
// Register the selector at the channel, so that it will be notified
// on the socket's events
synchronized (RegisterGate) {
// Wakeup the currently blocking reader/writer thread; we have
// locked the RegisterGate to prevent the awakened thread to block again
selector.wakeup();
// Lock the selector to prevent the waiting worker threads going
// into selector.select() which would block the selector.
synchronized (selector) {
return channel.register(selector, op, null);
}
}
}
示例6: testValidSelectionKey
import java.nio.channels.CancelledKeyException; //导入依赖的package包/类
@Test(timeout = 30000)
public void testValidSelectionKey() throws Exception {
final ZooKeeper zk = createZKClient(hostPort, 3000);
try {
Iterable<ServerCnxn> connections = serverFactory.getConnections();
for (ServerCnxn serverCnxn : connections) {
MockNIOServerCnxn mock = new MockNIOServerCnxn((NIOServerCnxn) serverCnxn);
// Cancel key
((NIOServerCnxn) serverCnxn).sock.keyFor(((NIOServerCnxnFactory) serverFactory).selector).cancel();;
mock.mockSendBuffer(ByteBuffer.allocate(8));
}
} catch (CancelledKeyException e) {
LOG.error("Exception while sending bytes!", e);
Assert.fail(e.toString());
} finally {
zk.close();
}
}
示例7: doAsyncWrite
import java.nio.channels.CancelledKeyException; //导入依赖的package包/类
private void doAsyncWrite(SelectionKey key) throws IOException {
Connection connection = (Connection) key.attachment();
if (connection == null) {
throw new IOException("doAsyncWrite: no connection");
}
if (key.channel() != connection.channel) {
throw new IOException("doAsyncWrite: bad channel");
}
if (processAllResponses(connection)) {
try {
// We wrote everything, so we don't need to be told when the socket is ready for
// write anymore.
key.interestOps(0);
} catch (CancelledKeyException e) {
/* The Listener/reader might have closed the socket.
* We don't explicitly cancel the key, so not sure if this will
* ever fire.
* This warning could be removed.
*/
LOG.warn("Exception while changing ops : " + e);
}
}
}
示例8: disable
import java.nio.channels.CancelledKeyException; //导入依赖的package包/类
private void disable()
{
if (m_enabled)
{
try
{
m_key.interestOps(0); // pass zero which means that we are not interested in being
// notified of anything for this channel.
}
catch (CancelledKeyException eat) // If we finished writing and didn't get an exception, then
{ // we probably don't need to throw this exception (if they try to write
// again, we will then throw an exception).
}
m_enabled = false;
}
}
示例9: send
import java.nio.channels.CancelledKeyException; //导入依赖的package包/类
/**
* Send a message. Whether it's over TCP or UDP is determined by the message flag.
*
* @param message The message to send.
* @throws IOException When a writing error occurs.
*/
public void send(Message message) throws IOException {
if (!isConnected) throw new IOException("Not connected yet. Use connect() first.");
try {
if (message.isReliable()) {
messageQueue.add(message);
if (!isConnector) {
tcp.socketChannel.keyFor(tcp.selector).interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE);
} else {
tcpChannel.keyFor(tcp.selector).interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE);
}
} else {
udp.sendObject(message);
}
} catch (CancelledKeyException e) {
// Client was disconnected.
}
}
示例10: processKeyChangeRequests
import java.nio.channels.CancelledKeyException; //导入依赖的package包/类
private void processKeyChangeRequests() {
KeyChangeRequest keyChangeRequest;
if (AsyncGlobals.debug && debug) {
Log.fine("processKeyChangeRequests");
}
do {
keyChangeRequest = keyChangeRequests.poll();
if (keyChangeRequest != null) {
try {
processKeyChangeRequest(keyChangeRequest);
} catch (CancelledKeyException cke) {
Log.warning("Ignoring CancelledKeyException");
}
}
} while (keyChangeRequest != null);
}
示例11: interestOps
import java.nio.channels.CancelledKeyException; //导入依赖的package包/类
public SelectionKey interestOps(int ops)
{
if (cancelled)
throw new CancelledKeyException();
if ((ops & ~(channel.validOps())) != 0)
throw new IllegalArgumentException("unsupported channel ops");
try
{
selector.epoll_modify(this, ops);
interestOps = ops;
}
catch (IOException ioe)
{
throw new IllegalArgumentException(ioe);
}
return this;
}
示例12: run
import java.nio.channels.CancelledKeyException; //导入依赖的package包/类
@Override
public void run() {
if (selfCancel) {
throw new CancelledKeyException();
}
if (cancelled) {
terminated = true;
disposeTimer();
}
queue.offer(NEXT);
if (enter()) {
drainLoop();
}
}
示例13: run
import java.nio.channels.CancelledKeyException; //导入依赖的package包/类
@Override
public void run() {
if (selfCancel) {
throw new CancelledKeyException();
}
if (cancelled) {
terminated = true;
dispose();
}
queue.offer(NEXT);
if (enter()) {
drainLoop();
}
}
示例14: doRegister
import java.nio.channels.CancelledKeyException; //导入依赖的package包/类
@Override
protected void doRegister() throws Exception {
boolean selected = false;
for (;;) {
try {
selectionKey = javaChannel().register(eventLoop().selector, 0, this);
return;
} catch (CancelledKeyException e) {
if (!selected) {
// Force the Selector to select now as the "canceled" SelectionKey may still be
// cached and not removed because no Select.select(..) operation was called yet.
eventLoop().selectNow();
selected = true;
} else {
// We forced a select operation on the selector before but the SelectionKey is still cached
// for whatever reason. JDK bug ?
throw e;
}
}
}
}
示例15: setInterestOp
import java.nio.channels.CancelledKeyException; //导入依赖的package包/类
/**
* Set one or more SelectionKey bits in the select mask.
*
* May be called from outside Worker thread
* May recursively lock selectorLock_
*
* @param op - OP_READ | OP_WRITE: may be 0 just to force wakeup
*/
private void setInterestOp(int op) {
synchronized (selectorLock_) {
final SelectionKey key = selectionKey_;
if (key != null && key.isValid()) {
try {
key.interestOps(key.interestOps() | op);
/*
* We could check that we have actually changed the mask before
* invoking a wakeup. The usage pattern, however, is such that
* such a check would almost always be true.
*/
final Selector selector = key.selector();
// Check "isOpen" to avoid Android crash see
// https://code.google.com/p/android/issues/detail?id=80785
if (selector.isOpen()) {
selector.wakeup();
}
} catch (CancelledKeyException e) {
// channel has been closed
}
}
}
}