本文整理汇总了Java中java.nio.channels.SelectionKey.cancel方法的典型用法代码示例。如果您正苦于以下问题:Java SelectionKey.cancel方法的具体用法?Java SelectionKey.cancel怎么用?Java SelectionKey.cancel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.SelectionKey
的用法示例。
在下文中一共展示了SelectionKey.cancel方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: run
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
@Override
public void run() {
final Selector selector = this.selector;
for (;;) {
++acceptCount;
try {
selector.select( 1000L );
Set<SelectionKey> keys = selector.selectedKeys();
try {
for (SelectionKey key : keys) {
if (key.isValid() && key.isAcceptable()) {
accept();
} else {
key.cancel();
}
}
} finally {
keys.clear();
}
} catch (Throwable e) {
LOGGER.warn(getName(), e);
}
}
}
示例2: run
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
@Override
public void run() {
final Selector tSelector = this.selector;
//轮询发现新连接请求
for (;;) {
++acceptCount;
try {
// 阻塞的,当超时,有注册的响应事件,或者被执行wakeup方法时继续
tSelector.select(1000L);
Set<SelectionKey> keys = tSelector.selectedKeys();
try {
for (SelectionKey key : keys) {
if (key.isValid() && key.isAcceptable()) {
accept();
} else {
key.cancel();
}
}
} finally {
keys.clear();
}
} catch (Exception e) {
LOGGER.warn(getName(), e);
}
}
}
示例3: receive
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
/**
* 接受Server的相应数据,存放在receiveBuffer中。
* 若receiveBuffer已经满一行数据,则打印该数据,并把此数据从receiveBuffer中删除
* 若打印的字符串为bye,则关闭
* @param key
* @throws IOException
*/
private void receive(SelectionKey key) throws IOException {
SocketChannel socketChannel = (SocketChannel) key.channel();
socketChannel.read(receiveBuffer);
receiveBuffer.flip();
String receiveData = decode(receiveBuffer);
if(receiveData.contains("\n")){
String outputData = receiveData.substring(0,receiveData.indexOf("\n") + 1);
//若服务器响应echo:exit(exit由此client发出)则关闭client
if("echo:exit\r\n".equals(outputData)){
key.cancel();
socketChannel.close();
Agent.OUT.println("Agent已关闭成功");
closeClient();
}
ByteBuffer temp = encode(outputData);
receiveBuffer.position(temp.limit());
receiveBuffer.compact();//删除已经打印的数据
}
}
示例4: closeMaster
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private void closeMaster() {
if (null != this.socketChannel) {
try {
SelectionKey sk = this.socketChannel.keyFor(this.selector);
if (sk != null) {
sk.cancel();
}
this.socketChannel.close();
this.socketChannel = null;
} catch (IOException e) {
log.warn("closeMaster exception. ", e);
}
this.lastWriteTimestamp = 0;
this.dispatchPostion = 0;
this.byteBufferBackup.position(0);
this.byteBufferBackup.limit(READ_MAX_BUFFER_SIZE);
this.byteBufferRead.position(0);
this.byteBufferRead.limit(READ_MAX_BUFFER_SIZE);
}
}
示例5: close
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
@Override
protected void close(DatagramChannel handle) throws Exception {
SelectionKey key = handle.keyFor(selector);
if (key != null) {
key.cancel();
}
handle.disconnect();
handle.close();
}
示例6: waitForWriteBufferToDrain
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private void waitForWriteBufferToDrain() throws IOException {
if (selector == null) {
selector = Selector.open();
}
SelectionKey key = socket.register(selector, SelectionKey.OP_WRITE);
// block until ready for write operations
selector.select();
// cancel OP_WRITE selection
key.cancel();
// complete cancelling key
selector.selectNow();
}
示例7: 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);
}
}
示例8: clearSelectionKey
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private void clearSelectionKey() {
try {
SelectionKey key = this.processKey;
if (key != null && key.isValid()) {
key.attach(null);
key.cancel();
}
} catch (Exception e) {
AbstractConnection.LOGGER.info("clear selector keys err:" + e);
}
}
示例9: read
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private void read(SelectionKey k) {
try {
SocketChannel channel = (SocketChannel) k.channel();
ByteBuffer data = ByteBuffer.allocate(100000);
data.clear();
channel.read(data);
ByteArrayInputStream bais = new ByteArrayInputStream(data.array());
ObjectInputStream ois = new ObjectInputStream(bais);
Object obj = ois.readObject();
MeviusClient client = handler.getClientHandler()
.getClient(channel.socket().getInetAddress().getHostAddress());
if (obj instanceof PublicKey) {
handler.getClientHandler().setPublicKey(client, ((PublicKey) obj));
channel.write(convert(keypair.getPublic()));
return;
}
if (!(obj instanceof MeviusPacket))
return;
MeviusPacket packet = (MeviusPacket) obj;
handler.callEvent(MeviusHandler.getPacketEventInstance(packet, client, PacketEventType.RECEIVE));
} catch (IOException | ClassNotFoundException e) {
if (e.getClass().equals(StreamCorruptedException.class)) {
k.cancel();
MeviusClient mc = handler.getClientHandler()
.getClient(((SocketChannel) k.channel()).socket().getInetAddress().getHostAddress());
try {
mc.disconnect();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return;
}
k.cancel();
e.printStackTrace();
}
}
示例10: unregisterChannel
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private void unregisterChannel(SelectableChannel channel) {
SelectionKey key = channel.keyFor(selector);
if (key != null) {
// not registered with this selector => null returned
key.cancel();
}
}
示例11: cancel
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
public void cancel(SelectionKey sk, KeyAttachment key, int ops){
if (sk!=null) {
sk.cancel();
sk.attach(null);
if (SelectionKey.OP_WRITE==(ops&SelectionKey.OP_WRITE)) countDown(key.getWriteLatch());
if (SelectionKey.OP_READ==(ops&SelectionKey.OP_READ))countDown(key.getReadLatch());
}
}
示例12: run
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
@Override
public void run() {
final Selector tSelector = this.selector;
for (;;) {
++connectCount;
try {
// 查看有无连接就绪 阻塞的
tSelector.select(1000L);
connect(tSelector);
Set<SelectionKey> keys = tSelector.selectedKeys();
try {
for (SelectionKey key : keys) {
Object att = key.attachment();
if (att != null && key.isValid() && key.isConnectable()) {
finishConnect(key, att);
} else {
key.cancel();
}
}
} finally {
keys.clear();
}
} catch (Exception e) {
LOGGER.warn(name, e);
}
}
}
示例13: run
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
@Override
public void run() {
try {
while (!stop) {
SelectionKey selectionKey = null;
selector.select(1000);
//获取就绪的key
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectedKeys.iterator();
//遍历就绪的key
while (iterator.hasNext()) {
selectionKey = iterator.next();
iterator.remove();
//处理每一个就绪的key
try{
handleInput(selectionKey);
}catch(IOException ioe){
if (null != selectionKey) {
selectionKey.cancel();
if (null != selectionKey.channel()) {
selectionKey.channel().close();
}
}
}
}
}
//循环结束, 停止服务
if(null != selector){
selector.close();
}
}catch (Exception e) {
e.printStackTrace();
}
}
示例14: read
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private void read(SelectionKey key) throws IOException {
SocketChannel channel = (SocketChannel) key.channel();
ByteBuffer buffer = ByteBuffer.allocate(4096);
buffer.order(ByteOrder.LITTLE_ENDIAN);
int bytesRead;
try {
bytesRead = channel.read(buffer);
} 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 (bytesRead == -1) {
key.cancel();
channel.close();
if (this.rconSessions.contains(channel)) {
this.rconSessions.remove(channel);
}
if (this.sendQueues.containsKey(channel)) {
this.sendQueues.remove(channel);
}
return;
}
buffer.flip();
this.handle(channel, new RCONPacket(buffer));
}
示例15: disconnect
import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private void disconnect(SelectionKey sk) {
sk.cancel();
}