当前位置: 首页>>代码示例>>Java>>正文


Java SelectionKey.attach方法代码示例

本文整理汇总了Java中java.nio.channels.SelectionKey.attach方法的典型用法代码示例。如果您正苦于以下问题:Java SelectionKey.attach方法的具体用法?Java SelectionKey.attach怎么用?Java SelectionKey.attach使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.nio.channels.SelectionKey的用法示例。


在下文中一共展示了SelectionKey.attach方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: closeCurrentConnection

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private void closeCurrentConnection(SelectionKey key, Throwable e) {
  if (key != null) {
    Connection c = (Connection)key.attachment();
    if (c != null) {
      if (LOG.isDebugEnabled()) {
        LOG.debug(getName() + ": disconnecting client " + c.getHostAddress() +
            (e != null ? " on error " + e.getMessage() : ""));
      }
      closeConnection(c);
      key.attach(null);
    }
  }
}
 
开发者ID:fengchen8086,项目名称:ditb,代码行数:14,代码来源:RpcServer.java

示例2: doAccept

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private void doAccept(SelectionKey sk){
    ServerSocketChannel server=(ServerSocketChannel)sk.channel();
    SocketChannel clientChannel;
    try {
        //获取客户端的channel
        clientChannel = server.accept();
        clientChannel.configureBlocking(false);

        //register the channel for reading
        SelectionKey clientKey=clientChannel.register(selector,SelectionKey.OP_READ);
        //Allocate an EchoClient instance and attach it to this selection key.
        EchoClient echoClient=new EchoClient();
        clientKey.attach(echoClient);

        InetAddress clientAddress=clientChannel.socket().getInetAddress();
        System.out.println("Accepted connetion from "+clientAddress.getHostAddress()+".");
    }catch (Exception e){
        System.out.println("Failed to accept new client");
        e.printStackTrace();
    }
}
 
开发者ID:sean417,项目名称:LearningOfThinkInJava,代码行数:22,代码来源:MultiThreadNIOEchoServer.java

示例3: openConnection

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
/**
 * out node try to connect
 *
 * @param node
 */
public void openConnection(Node node) {

    SocketChannel channel = null;
    try {
        channel = SocketChannel.open();
        channel.configureBlocking(false);
        channel.socket().setReuseAddress(true);
        InetSocketAddress socketAddress = new InetSocketAddress(node.getIp(), node.getPort());
        channel.connect(socketAddress);
        PendingConnect data = new PendingConnect(channel, node);
        SelectionKey key = channel.register(selector, SelectionKey.OP_CONNECT);
        key.attach(data);
        selector.wakeup();
    } catch (IOException e) {
        e.printStackTrace();
        if (channel != null) {
            try {
                channel.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        node.destroy();
    }
}
 
开发者ID:nuls-io,项目名称:nuls,代码行数:31,代码来源:ConnectionManager.java

示例4: unregisterChannel

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
/**
 * Unregister a channel. The connections streams are not drained before finishing.
 * @param c
 */
void unregisterChannel (Connection c) {
    VoltPort port = (VoltPort)c;
    assert(c != null);
    SelectionKey selectionKey = port.getKey();

    acquireRegistrationLock();
    try {
        synchronized (m_ports) {
            if (!m_ports.contains(port)) {
                return;
            }
        }
        port.unregistering();
        selectionKey.cancel();
        selectionKey.attach(null);
        synchronized (m_ports) {
            m_ports.remove(port);
        }
    } finally {
        releaseRegistrationLock();
    }
    port.unregistered();
}
 
开发者ID:s-store,项目名称:s-store,代码行数:28,代码来源:VoltNetwork.java

示例5: acceptConnection

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private final void acceptConnection(SelectionKey key, MMOConnection<T> con)
{
	final ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
	SocketChannel sc;
	
	try
	{
		while ((sc = ssc.accept()) != null)
		{
			if ((_acceptFilter == null) || _acceptFilter.accept(sc))
			{
				sc.configureBlocking(false);
				final SelectionKey clientKey = sc.register(_selector, SelectionKey.OP_READ);
				con = new MMOConnection<>(this, sc.socket(), clientKey, TCP_NODELAY);
				con.setClient(_clientFactory.create(con));
				clientKey.attach(con);
			}
			else
			{
				sc.socket().close();
			}
		}
	}
	catch (IOException e)
	{
		e.printStackTrace();
	}
}
 
开发者ID:rubenswagner,项目名称:L2J-Global,代码行数:29,代码来源:SelectorThread.java

示例6: 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();
}
 
开发者ID:sunmingshuai,项目名称:apache-tomcat-7.0.73-with-comment,代码行数:40,代码来源:NioBlockingSelector.java

示例7: clearDeferredRegistrations

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private void clearDeferredRegistrations() {
    synchronized (deferredRegistrations) {
        int deferredListSize = deferredRegistrations.size();
        if (orb.transportDebugFlag) {
            dprint(".clearDeferredRegistrations:deferred list size == " + deferredListSize);
        }
        for (int i = 0; i < deferredListSize; i++) {
            EventHandler eventHandler =
                (EventHandler)deferredRegistrations.get(i);
            if (orb.transportDebugFlag) {
                dprint(".clearDeferredRegistrations: " + eventHandler);
            }
            SelectableChannel channel = eventHandler.getChannel();
            SelectionKey selectionKey = null;

            try {
                if (orb.transportDebugFlag) {
                    dprint(".clearDeferredRegistrations:close channel == "
                            + channel);
                    dprint(".clearDeferredRegistrations:close channel class == "
                            + channel.getClass().getName());
                }
                channel.close();
                selectionKey = eventHandler.getSelectionKey();
                if (selectionKey != null) {
                    selectionKey.cancel();
                    selectionKey.attach(null);
                }
            } catch (IOException ioEx) {
                if (orb.transportDebugFlag) {
                    dprint(".clearDeferredRegistrations: ", ioEx);
                }
            }
        }
        deferredRegistrations.clear();
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:38,代码来源:SelectorImpl.java

示例8: register

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
@Override
public void register(TCPClientSocket clientSocket, boolean writable) throws IOException {

    int interestOps = SelectionKey.OP_READ;
    if (writable) {
        interestOps |= SelectionKey.OP_WRITE;
    }

    SelectionKey key = clientSocket.getChannel().register(selector, interestOps);
    key.attach(clientSocket);
}
 
开发者ID:cpppwner,项目名称:NoRiskNoFun,代码行数:12,代码来源:SocketSelectorImpl.java

示例9: 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);
    }
}
 
开发者ID:actiontech,项目名称:dble,代码行数:12,代码来源:NIOSocketWR.java

示例10: 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());
    }
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:9,代码来源:NioBlockingSelector.java

示例11: addInNode

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private void addInNode(SelectionKey key) {
    SocketChannel socketChannel = null;
    Node node = null;
    try {
        socketChannel = serverSocketChannel.accept();
        InetSocketAddress socketAddress = (InetSocketAddress) socketChannel.getRemoteAddress();
        if (!allowConnection(socketAddress)) {
            socketChannel.close();
            return;
        }
        socketChannel.configureBlocking(false);
        SelectionKey newKey = socketChannel.register(selector, SelectionKey.OP_READ);

        node = new Node(network, Node.IN, socketAddress);
        nodesManager.addNodeToGroup(NetworkConstant.NETWORK_NODE_IN_GROUP, node);
        ConnectionHandler handler = new ConnectionHandler(node, socketChannel, newKey);
        node.setWriteTarget(handler);
        newKey.attach(handler);
        node.connectionOpened();
    } catch (Exception e) {
        if (socketChannel != null) {
            Log.warn("in node Failed to connect" + node.getIp());
            try {
                socketChannel.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        if (node != null) {
            node.destroy();
        }
    }
}
 
开发者ID:nuls-io,项目名称:nuls,代码行数:34,代码来源:ConnectionManager.java

示例12: closeConnectionImpl

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private final void closeConnectionImpl(SelectionKey key, MMOConnection<T> con)
{
	try
	{
		// notify connection
		con.getClient().onDisconnection();
	}
	finally
	{
		try
		{
			// close socket and the SocketChannel
			con.close();
		}
		catch (IOException e)
		{
			// ignore, we are closing anyway
		}
		finally
		{
			con.releaseBuffers();
			// clear attachment
			key.attach(null);
			// cancel key
			key.cancel();
		}
	}
}
 
开发者ID:rubenswagner,项目名称:L2J-Global,代码行数:29,代码来源:SelectorThread.java

示例13: clearSelectionKey

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private void clearSelectionKey(SelectionKey key) {
    if (key.isValid()) {
        key.attach(null);
        key.cancel();
    }
}
 
开发者ID:actiontech,项目名称:dble,代码行数:7,代码来源:NIOConnector.java

示例14: clearSelectionKey

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
private void clearSelectionKey(SelectionKey key) {
	if (key.isValid()) {
		key.attach(null);
		key.cancel();
	}
}
 
开发者ID:variflight,项目名称:feeyo-redisproxy,代码行数:7,代码来源:NIOConnector.java

示例15: processKey

import java.nio.channels.SelectionKey; //导入方法依赖的package包/类
protected boolean processKey(SelectionKey sk, KeyAttachment attachment) {
    boolean result = true;
    try {
        if ( close ) {
            cancelledKey(sk, SocketStatus.STOP, attachment.comet);
        } else if ( sk.isValid() && attachment != null ) {
            attachment.access();//make sure we don't time out valid sockets
            sk.attach(attachment);//cant remember why this is here
            NioChannel channel = attachment.getChannel();
            if (sk.isReadable() || sk.isWritable() ) {
                if ( attachment.getSendfileData() != null ) {
                    processSendfile(sk,attachment, false);
                } else {
                    if ( isWorkerAvailable() ) {
                        unreg(sk, attachment, sk.readyOps());
                        boolean closeSocket = false;
                        // Read goes before write
                        if (sk.isReadable()) {
                            if (!processSocket(channel, SocketStatus.OPEN_READ, true)) {
                                closeSocket = true;
                            }
                        }
                        if (!closeSocket && sk.isWritable()) {
                            if (!processSocket(channel, SocketStatus.OPEN_WRITE, true)) {
                                closeSocket = true;
                            }
                        }
                        if (closeSocket) {
                            cancelledKey(sk,SocketStatus.DISCONNECT,false);
                        }
                    } else {
                        result = false;
                    }
                }
            }
        } else {
            //invalid key
            cancelledKey(sk, SocketStatus.ERROR,false);
        }
    } catch ( CancelledKeyException ckx ) {
        cancelledKey(sk, SocketStatus.ERROR,false);
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        log.error("",t);
    }
    return result;
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:48,代码来源:NioEndpoint.java


注:本文中的java.nio.channels.SelectionKey.attach方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。