use of java.nio.channels.Selector in project Mycat-Server by MyCATApache.
the class NIOConnector method run.
@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);
}
}
}
use of java.nio.channels.Selector in project Aeron by real-logic.
the class HackSelectReceiveSendUdpPong method run.
private void run() throws IOException {
final InetSocketAddress sendAddress = new InetSocketAddress("localhost", Common.PONG_PORT);
final ByteBuffer buffer = ByteBuffer.allocateDirect(Configuration.MTU_LENGTH_DEFAULT);
final DatagramChannel receiveChannel = DatagramChannel.open();
Common.init(receiveChannel);
receiveChannel.bind(new InetSocketAddress("localhost", Common.PING_PORT));
final DatagramChannel sendChannel = DatagramChannel.open();
Common.init(sendChannel);
final Selector selector = Selector.open();
final NioSelectedKeySet keySet = Common.keySet(selector);
final ToIntFunction<SelectionKey> handler = (key) -> {
try {
buffer.clear();
receiveChannel.receive(buffer);
final long receivedSequenceNumber = buffer.getLong(0);
final long receivedTimestamp = buffer.getLong(SIZE_OF_LONG);
buffer.clear();
buffer.putLong(receivedSequenceNumber);
buffer.putLong(receivedTimestamp);
buffer.flip();
sendChannel.send(buffer, sendAddress);
} catch (final IOException ex) {
ex.printStackTrace();
}
return 1;
};
receiveChannel.register(selector, OP_READ, null);
final AtomicBoolean running = new AtomicBoolean(true);
SigInt.register(() -> running.set(false));
while (true) {
while (selector.selectNow() == 0) {
if (!running.get()) {
return;
}
}
keySet.forEach(handler);
}
}
use of java.nio.channels.Selector in project Aeron by real-logic.
the class SelectReceiveSendUdpPong method run.
private void run() throws IOException {
final InetSocketAddress sendAddress = new InetSocketAddress("localhost", Common.PONG_PORT);
final ByteBuffer buffer = ByteBuffer.allocateDirect(Configuration.MTU_LENGTH_DEFAULT);
final DatagramChannel receiveChannel = DatagramChannel.open();
Common.init(receiveChannel);
receiveChannel.bind(new InetSocketAddress("localhost", Common.PING_PORT));
final DatagramChannel sendChannel = DatagramChannel.open();
Common.init(sendChannel);
final Selector selector = Selector.open();
final IntSupplier handler = () -> {
try {
buffer.clear();
receiveChannel.receive(buffer);
final long receivedSequenceNumber = buffer.getLong(0);
final long receivedTimestamp = buffer.getLong(SIZE_OF_LONG);
buffer.clear();
buffer.putLong(receivedSequenceNumber);
buffer.putLong(receivedTimestamp);
buffer.flip();
sendChannel.send(buffer, sendAddress);
} catch (final IOException ex) {
ex.printStackTrace();
}
return 1;
};
receiveChannel.register(selector, OP_READ, handler);
final AtomicBoolean running = new AtomicBoolean(true);
SigInt.register(() -> running.set(false));
while (true) {
while (selector.selectNow() == 0) {
if (!running.get()) {
return;
}
}
final Set<SelectionKey> selectedKeys = selector.selectedKeys();
final Iterator<SelectionKey> iter = selectedKeys.iterator();
while (iter.hasNext()) {
final SelectionKey key = iter.next();
if (key.isReadable()) {
((IntSupplier) key.attachment()).getAsInt();
}
iter.remove();
}
}
}
use of java.nio.channels.Selector in project Aeron by real-logic.
the class SendHackSelectReceiveUdpPing method run.
private void run() throws IOException {
receiveChannel = DatagramChannel.open();
Common.init(receiveChannel);
receiveChannel.bind(new InetSocketAddress("localhost", Common.PONG_PORT));
final DatagramChannel sendChannel = DatagramChannel.open();
Common.init(sendChannel);
final Selector selector = Selector.open();
receiveChannel.register(selector, OP_READ, this);
final NioSelectedKeySet keySet = Common.keySet(selector);
final AtomicBoolean running = new AtomicBoolean(true);
SigInt.register(() -> running.set(false));
while (running.get()) {
measureRoundTrip(HISTOGRAM, SEND_ADDRESS, buffer, sendChannel, selector, keySet, running);
HISTOGRAM.reset();
System.gc();
LockSupport.parkNanos(1000 * 1000 * 1000);
}
}
use of java.nio.channels.Selector in project jeromq by zeromq.
the class Proxy method proxy.
public static boolean proxy(SocketBase frontend, SocketBase backend, SocketBase capture) {
// The algorithm below assumes ratio of requests and replies processed
// under full load to be 1:1.
// TODO: The current implementation drops messages when
// any of the pipes becomes full.
boolean success = true;
int rc;
long more;
Msg msg;
PollItem[] items = new PollItem[2];
items[0] = new PollItem(frontend, ZMQ.ZMQ_POLLIN);
items[1] = new PollItem(backend, ZMQ.ZMQ_POLLIN);
Selector selector;
try {
selector = Selector.open();
} catch (IOException e) {
throw new ZError.IOException(e);
}
try {
while (!Thread.currentThread().isInterrupted()) {
// Wait while there are either requests or replies to process.
rc = ZMQ.poll(selector, items, -1);
if (rc < 0) {
return false;
}
// Process a request.
if (items[0].isReadable()) {
while (true) {
msg = frontend.recv(0);
if (msg == null) {
return false;
}
more = frontend.getSocketOpt(ZMQ.ZMQ_RCVMORE);
if (more < 0) {
return false;
}
// Copy message to capture socket if any
if (capture != null) {
Msg ctrl = new Msg(msg);
success = capture.send(ctrl, more > 0 ? ZMQ.ZMQ_SNDMORE : 0);
if (!success) {
return false;
}
}
success = backend.send(msg, more > 0 ? ZMQ.ZMQ_SNDMORE : 0);
if (!success) {
return false;
}
if (more == 0) {
break;
}
}
}
// Process a reply.
if (items[1].isReadable()) {
while (true) {
msg = backend.recv(0);
if (msg == null) {
return false;
}
more = backend.getSocketOpt(ZMQ.ZMQ_RCVMORE);
if (more < 0) {
return false;
}
// Copy message to capture socket if any
if (capture != null) {
Msg ctrl = new Msg(msg);
success = capture.send(ctrl, more > 0 ? ZMQ.ZMQ_SNDMORE : 0);
if (!success) {
return false;
}
}
success = frontend.send(msg, more > 0 ? ZMQ.ZMQ_SNDMORE : 0);
if (!success) {
return false;
}
if (more == 0) {
break;
}
}
}
}
} finally {
try {
selector.close();
} catch (Exception e) {
}
}
return true;
}
Aggregations