コード例 #1
0
ファイル: Util.java プロジェクト: dorkbox/barchart-udt
 // When finished, invoker must ensure that selector is empty
 // by cancelling any related keys and explicitly releasing
 // the selector by invoking releaseTemporarySelector()
 static Selector getTemporarySelector(SelectableChannel sc) throws IOException {
   SoftReference ref = (SoftReference) localSelector.get();
   SelectorWrapper selWrapper = null;
   Selector sel = null;
   if (ref == null
       || ((selWrapper = (SelectorWrapper) ref.get()) == null)
       || ((sel = selWrapper.get()) == null)
       || (sel.provider() != sc.provider())) {
     sel = sc.provider().openSelector();
     localSelector.set(new SoftReference(new SelectorWrapper(sel)));
   } else {
     localSelectorWrapper.set(selWrapper);
   }
   return sel;
 }
コード例 #2
0
  public void bind(Selector selector, InetSocketAddress localPort) throws IOException {
    close();
    readBuffer.clear();
    writeBuffer.clear();
    try {
      datagramChannel = selector.provider().openDatagramChannel();
      datagramChannel.socket().bind(localPort);
      datagramChannel.configureBlocking(false);
      selectionKey = datagramChannel.register(selector, SelectionKey.OP_READ);

      lastCommunicationTime = System.currentTimeMillis();
    } catch (IOException ex) {
      close();
      throw ex;
    }
  }
コード例 #3
0
 public void testOpenSelector() throws IOException {
   Selector selector = SystemUtils.openSelector();
   assertNotNull(selector);
   assertTrue(selector.isOpen());
   if (SystemUtils.isLinuxPlatform()) {
     final String pollClassName = selector.provider().getClass().getCanonicalName();
     assertTrue(
         pollClassName.equals("sun.nio.ch.EPollSelectorProvider")
             || pollClassName.equals("sun.nio.ch.PollSelectorProvider"));
   }
   Selector selector2 = SystemUtils.openSelector();
   ;
   assertNotSame(selector, selector2);
   selector.close();
   selector2.close();
 }
コード例 #4
0
  public void connect(Selector selector, InetSocketAddress remoteAddress) throws IOException {
    close();
    readBuffer.clear();
    writeBuffer.clear();
    try {
      datagramChannel = selector.provider().openDatagramChannel();
      datagramChannel.socket().bind(null);
      datagramChannel.socket().connect(remoteAddress);
      datagramChannel.configureBlocking(false);

      selectionKey = datagramChannel.register(selector, SelectionKey.OP_READ);

      lastCommunicationTime = System.currentTimeMillis();

      connectedAddress = remoteAddress;
    } catch (IOException ex) {
      close();
      IOException ioEx = new IOException("Unable to connect to: " + remoteAddress);
      ioEx.initCause(ex);
      throw ioEx;
    }
  }
コード例 #5
0
  public void connect(Selector selector, SocketAddress remoteAddress, int timeout)
      throws IOException {
    close();
    writeBuffer.clear();
    readBuffer.clear();
    readBuffer.flip();
    currentObjectLength = 0;
    try {
      SocketChannel socketChannel = selector.provider().openSocketChannel();
      Socket socket = socketChannel.socket();
      socket.setTcpNoDelay(true);
      // socket.setTrafficClass(IPTOS_LOWDELAY);
      socket.connect(remoteAddress, timeout); // Connect using blocking mode for simplicity.
      socketChannel.configureBlocking(false);
      this.socketChannel = socketChannel;

      selectionKey = socketChannel.register(selector, SelectionKey.OP_READ);
      selectionKey.attach(this);

      if (DEBUG) {
        debug(
            "kryonet",
            "Port "
                + socketChannel.socket().getLocalPort()
                + "/TCP connected to: "
                + socketChannel.socket().getRemoteSocketAddress());
      }

      lastReadTime = lastWriteTime = System.currentTimeMillis();
    } catch (IOException ex) {
      close();
      IOException ioEx = new IOException("Unable to connect to: " + remoteAddress);
      ioEx.initCause(ex);
      throw ioEx;
    }
  }