use of java.nio.channels.ByteChannel in project spring-boot by spring-projects.
the class HttpTunnelServer method getServerThread.
/**
* Returns the active server thread, creating and starting it if necessary.
* @return the {@code ServerThread} (never {@code null})
* @throws IOException in case of I/O errors
*/
protected ServerThread getServerThread() throws IOException {
synchronized (this) {
if (this.serverThread == null) {
ByteChannel channel = this.serverConnection.open(this.longPollTimeout);
this.serverThread = new ServerThread(channel);
this.serverThread.start();
}
return this.serverThread;
}
}
use of java.nio.channels.ByteChannel in project spring-boot by spring-projects.
the class SocketTargetServerConnectionTests method timeout.
@Test
public void timeout() throws Exception {
this.server.delay(1000);
this.server.start();
ByteChannel channel = this.connection.open(10);
long startTime = System.currentTimeMillis();
try {
channel.read(ByteBuffer.allocate(5));
fail("No socket timeout thrown");
} catch (SocketTimeoutException ex) {
// Expected
long runTime = System.currentTimeMillis() - startTime;
assertThat(runTime).isGreaterThanOrEqualTo(10L);
assertThat(runTime).isLessThan(10000L);
}
}
use of java.nio.channels.ByteChannel in project spring-boot by spring-projects.
the class SocketTargetServerConnectionTests method writeData.
@Test
public void writeData() throws Exception {
this.server.expect("hello".getBytes());
this.server.start();
ByteChannel channel = this.connection.open(DEFAULT_TIMEOUT);
ByteBuffer buffer = ByteBuffer.wrap("hello".getBytes());
channel.write(buffer);
this.server.closeAndVerify();
}
use of java.nio.channels.ByteChannel in project h2o-3 by h2oai.
the class H2ONode method getTCPSocket.
ByteChannel getTCPSocket() throws IOException {
// Under lock, claim an existing open socket if possible
synchronized (this) {
// Limit myself to the number of open sockets from node-to-node
while (_socksAvail == 0) try {
wait(1000);
} catch (InterruptedException ignored) {
}
// Claim an open socket
ByteChannel sock = _socks[--_socksAvail];
if (sock != null) {
// Return existing socket!
if (sock.isOpen())
return sock;
// Else it's an already-closed socket, lower open TCP count
assert TCPS.get() > 0;
TCPS.decrementAndGet();
}
}
// Must make a fresh socket
SocketChannel sock2 = SocketChannel.open();
sock2.socket().setReuseAddress(true);
sock2.socket().setSendBufferSize(AutoBuffer.BBP_BIG._size);
boolean res = sock2.connect(_key);
assert res && !sock2.isConnectionPending() && sock2.isBlocking() && sock2.isConnected() && sock2.isOpen();
ByteBuffer bb = ByteBuffer.allocate(4).order(ByteOrder.nativeOrder());
bb.put((byte) 2);
bb.putChar((char) H2O.H2O_PORT);
bb.put((byte) 0xef);
bb.flip();
ByteChannel wrappedSocket = _socketFactory.clientChannel(sock2, _key.getHostName(), _key.getPort());
while (bb.hasRemaining()) {
wrappedSocket.write(bb);
}
// Cluster-wide counting
TCPS.incrementAndGet();
return wrappedSocket;
}
use of java.nio.channels.ByteChannel in project h2o-3 by h2oai.
the class H2ONode method openChan.
/**
* Returns a new connection of type {@code tcpType}, the type can be either
* TCPReceiverThread.TCP_SMALL, TCPReceiverThread.TCP_BIG or
* TCPReceiverThread.TCP_EXTERNAL.
*
* If socket channel factory is set, the communication will considered to be secured - this depends on the
* configuration of the {@link SocketChannelFactory}. In case of the factory is null, the communication won't be secured.
* @return new socket channel
*/
public static ByteChannel openChan(byte tcpType, SocketChannelFactory socketFactory, InetAddress originAddr, int originPort) throws IOException {
// Must make a fresh socket
SocketChannel sock = SocketChannel.open();
sock.socket().setReuseAddress(true);
sock.socket().setSendBufferSize(AutoBuffer.BBP_BIG._size);
InetSocketAddress isa = new InetSocketAddress(originAddr, originPort);
// Can toss IOEx, esp if other node is still booting up
boolean res = sock.connect(isa);
assert res : "Should be already connected, but connection is in non-blocking mode and the connection operation is in progress!";
sock.configureBlocking(true);
assert !sock.isConnectionPending() && sock.isBlocking() && sock.isConnected() && sock.isOpen();
sock.socket().setTcpNoDelay(true);
ByteBuffer bb = ByteBuffer.allocate(4).order(ByteOrder.nativeOrder());
bb.put(tcpType).putChar((char) H2O.H2O_PORT).put((byte) 0xef).flip();
ByteChannel wrappedSocket = socketFactory.clientChannel(sock, isa.getHostName(), isa.getPort());
while (bb.hasRemaining()) {
// Write out magic startup sequence
wrappedSocket.write(bb);
}
return wrappedSocket;
}
Aggregations