Search in sources :

Example 41 with SelectionKey

use of java.nio.channels.SelectionKey in project Openfire by igniterealtime.

the class ConferenceReceiver method initLoneReceiverChannel.

private void initLoneReceiverChannel(int loneReceiverPort) {
    if (this.loneReceiverPort != loneReceiverPort && loneReceiverChannel != null) {
        close();
    }
    this.loneReceiverPort = loneReceiverPort;
    try {
        selector = Selector.open();
    } catch (IOException e) {
        Logger.println("Conference receiver failed to open selector " + e.getMessage());
        return;
    }
    if (loneReceiverPort == 0) {
        return;
    }
    Logger.println("Init lone channel using port " + loneReceiverPort);
    try {
        loneReceiverChannel = DatagramChannel.open();
        if (Logger.logLevel >= Logger.LOG_INFO) {
            Logger.println("Opened lone receiver channel " + loneReceiverChannel);
        }
    } catch (IOException e) {
        Logger.println("Conference receiver failed to open DatagramChannel " + " " + e.getMessage());
        return;
    }
    try {
        loneReceiverChannel.configureBlocking(false);
    } catch (IOException e) {
        Logger.println("Conference receiver failed to configureBlocking to false " + e.getMessage());
        return;
    }
    DatagramSocket socket = loneReceiverChannel.socket();
    try {
        socket.setReceiveBufferSize(RtpSocket.MAX_RECEIVE_BUFFER);
    } catch (SocketException e) {
        Logger.println("ConferenceReceiver failed to set receive buffer size " + e.getMessage());
        return;
    }
    try {
        socket.setSoTimeout(0);
    } catch (SocketException e) {
        Logger.println("ConferenceReceiver failed to set timeout " + e.getMessage());
        return;
    }
    InetSocketAddress bridgeAddress = Bridge.getLocalBridgeAddress();
    InetSocketAddress isa = new InetSocketAddress(bridgeAddress.getAddress(), loneReceiverPort);
    try {
        socket.bind(isa);
    } catch (IOException e) {
        Logger.println("Conference receiver unable to bind to " + loneReceiverPort + " " + e.getMessage());
        return;
    }
    try {
        SelectionKey selectionKey = loneReceiverChannel.register(selector, SelectionKey.OP_READ);
    } catch (Exception e) {
        Logger.println("Conference receiver unable to register:  " + e.getMessage());
        return;
    }
    memberCount++;
    Logger.println("Lone Channel uses port " + loneReceiverPort);
}
Also used : SocketException(java.net.SocketException) SelectionKey(java.nio.channels.SelectionKey) DatagramSocket(java.net.DatagramSocket) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) IOException(java.io.IOException) SocketException(java.net.SocketException)

Example 42 with SelectionKey

use of java.nio.channels.SelectionKey in project hs4j by killme2008.

the class AbstractNioSession method flush0.

protected final void flush0() {
    SelectionKey tmpKey = null;
    Selector writeSelector = null;
    int attempts = 0;
    try {
        while (true) {
            if (writeSelector == null) {
                writeSelector = SelectorFactory.getSelector();
                if (writeSelector == null) {
                    return;
                }
                tmpKey = selectableChannel.register(writeSelector, SelectionKey.OP_WRITE);
            }
            if (writeSelector.select(1000) == 0) {
                attempts++;
                if (attempts > 2) {
                    return;
                }
            } else {
                break;
            }
        }
        onWrite(selectableChannel.keyFor(writeSelector));
    } catch (ClosedChannelException cce) {
        onException(cce);
        log.error("Flush error", cce);
        close();
    } catch (IOException ioe) {
        onException(ioe);
        log.error("Flush error", ioe);
        close();
    } finally {
        if (tmpKey != null) {
            // Cancel the key.
            tmpKey.cancel();
            tmpKey = null;
        }
        if (writeSelector != null) {
            try {
                writeSelector.selectNow();
            } catch (IOException e) {
                log.error("Temp selector selectNow error", e);
            }
            // return selector
            SelectorFactory.returnSelector(writeSelector);
        }
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) Selector(java.nio.channels.Selector)

Example 43 with SelectionKey

use of java.nio.channels.SelectionKey in project Mycat-Server by MyCATApache.

the class NIOAcceptor method run.

@Override
public void run() {
    final Selector tSelector = this.selector;
    for (; ; ) {
        ++acceptCount;
        try {
            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);
        }
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) IOException(java.io.IOException) Selector(java.nio.channels.Selector)

Example 44 with SelectionKey

use of java.nio.channels.SelectionKey in project Mycat-Server by MyCATApache.

the class NIOSocketWR method disableWrite.

private void disableWrite() {
    try {
        SelectionKey key = this.processKey;
        key.interestOps(key.interestOps() & OP_NOT_WRITE);
    } catch (Exception e) {
        AbstractConnection.LOGGER.warn("can't disable write " + e + " con " + con);
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) IOException(java.io.IOException)

Example 45 with SelectionKey

use of java.nio.channels.SelectionKey in project Mycat-Server by MyCATApache.

the class NIOSocketWR method enableRead.

public void enableRead() {
    boolean needWakeup = false;
    try {
        SelectionKey key = this.processKey;
        key.interestOps(key.interestOps() | SelectionKey.OP_READ);
        needWakeup = true;
    } catch (Exception e) {
        AbstractConnection.LOGGER.warn("enable read fail " + e);
    }
    if (needWakeup) {
        processKey.selector().wakeup();
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) IOException(java.io.IOException)

Aggregations

SelectionKey (java.nio.channels.SelectionKey)202 IOException (java.io.IOException)93 SocketChannel (java.nio.channels.SocketChannel)59 Selector (java.nio.channels.Selector)44 ServerSocketChannel (java.nio.channels.ServerSocketChannel)41 ClosedChannelException (java.nio.channels.ClosedChannelException)30 InetSocketAddress (java.net.InetSocketAddress)27 CancelledKeyException (java.nio.channels.CancelledKeyException)25 ByteBuffer (java.nio.ByteBuffer)24 ClosedSelectorException (java.nio.channels.ClosedSelectorException)17 SelectableChannel (java.nio.channels.SelectableChannel)13 Test (org.junit.Test)13 Iterator (java.util.Iterator)9 Socket (java.net.Socket)7 ArrayList (java.util.ArrayList)7 SocketTimeoutException (java.net.SocketTimeoutException)6 AbstractSelectionKey (java.nio.channels.spi.AbstractSelectionKey)6 EOFException (java.io.EOFException)5 DatagramChannel (java.nio.channels.DatagramChannel)5 HashSet (java.util.HashSet)5