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);
}
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);
}
}
}
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);
}
}
}
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);
}
}
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();
}
}
Aggregations