use of com.hazelcast.internal.networking.SocketChannelWrapper in project hazelcast by hazelcast.
the class ClientConnectionManagerImpl method createSocketConnection.
protected ClientConnection createSocketConnection(final Address address) throws IOException {
if (!alive) {
throw new HazelcastException("ConnectionManager is not active!");
}
SocketChannel socketChannel = null;
try {
socketChannel = SocketChannel.open();
Socket socket = socketChannel.socket();
socket.setKeepAlive(socketOptions.isKeepAlive());
socket.setTcpNoDelay(socketOptions.isTcpNoDelay());
socket.setReuseAddress(socketOptions.isReuseAddress());
if (socketOptions.getLingerSeconds() > 0) {
socket.setSoLinger(true, socketOptions.getLingerSeconds());
}
int bufferSize = getBufferSize();
socket.setSendBufferSize(bufferSize);
socket.setReceiveBufferSize(bufferSize);
InetSocketAddress inetSocketAddress = address.getInetSocketAddress();
socketChannel.socket().connect(inetSocketAddress, connectionTimeout);
SocketChannelWrapper socketChannelWrapper = socketChannelWrapperFactory.wrapSocketChannel(socketChannel, true);
final ClientConnection clientConnection = new ClientConnection(client, ioThreadingModel, connectionIdGen.incrementAndGet(), socketChannelWrapper);
socketChannel.configureBlocking(true);
if (socketInterceptor != null) {
socketInterceptor.onConnect(socket);
}
socketChannel.configureBlocking(ioThreadingModel.isBlocking());
socket.setSoTimeout(0);
clientConnection.start();
return clientConnection;
} catch (Exception e) {
if (socketChannel != null) {
socketChannel.close();
}
throw ExceptionUtil.rethrow(e, IOException.class);
}
}
use of com.hazelcast.internal.networking.SocketChannelWrapper in project hazelcast by hazelcast.
the class SocketAcceptorThread method acceptSocket.
private void acceptSocket() {
SocketChannelWrapper socketChannelWrapper = null;
try {
final SocketChannel socketChannel = serverSocketChannel.accept();
if (socketChannel != null) {
socketChannelWrapper = connectionManager.wrapSocketChannel(socketChannel, false);
}
} catch (Exception e) {
exceptionCount.inc();
if (e instanceof ClosedChannelException && !connectionManager.isLive()) {
// ClosedChannelException
// or AsynchronousCloseException
// or ClosedByInterruptException
logger.finest("Terminating socket acceptor thread...", e);
} else {
logger.severe("Unexpected error while accepting connection! " + e.getClass().getName() + ": " + e.getMessage());
try {
serverSocketChannel.close();
} catch (Exception ex) {
logger.finest("Closing server socket failed", ex);
}
ioService.onFatalError(e);
}
}
if (socketChannelWrapper != null) {
final SocketChannelWrapper socketChannel = socketChannelWrapper;
logger.info("Accepting socket connection from " + socketChannel.socket().getRemoteSocketAddress());
if (connectionManager.isSocketInterceptorEnabled()) {
configureAndAssignSocket(socketChannel);
} else {
ioService.executeAsync(new Runnable() {
@Override
public void run() {
configureAndAssignSocket(socketChannel);
}
});
}
}
}
use of com.hazelcast.internal.networking.SocketChannelWrapper in project hazelcast by hazelcast.
the class TcpIpConnectionManager method stop.
@Override
public synchronized void stop() {
if (!live) {
return;
}
live = false;
logger.finest("Stopping ConnectionManager");
shutdownAcceptorThread();
for (SocketChannelWrapper socketChannel : acceptedSockets) {
closeResource(socketChannel);
}
for (Connection conn : connectionsMap.values()) {
destroySilently(conn, "TcpIpConnectionManager is stopping");
}
for (TcpIpConnection conn : activeConnections) {
destroySilently(conn, "TcpIpConnectionManager is stopping");
}
ioThreadingModel.shutdown();
acceptedSockets.clear();
connectionsInProgress.clear();
connectionsMap.clear();
monitors.clear();
activeConnections.clear();
}
use of com.hazelcast.internal.networking.SocketChannelWrapper in project hazelcast by hazelcast.
the class TcpIpConnectionManager method wrapSocketChannel.
SocketChannelWrapper wrapSocketChannel(SocketChannel socketChannel, boolean client) throws Exception {
SocketChannelWrapper wrapper = socketChannelWrapperFactory.wrapSocketChannel(socketChannel, client);
acceptedSockets.add(wrapper);
return wrapper;
}
use of com.hazelcast.internal.networking.SocketChannelWrapper in project hazelcast by hazelcast.
the class DefaultSocketChannelWrapperFactoryTest method wrapSocketChannel.
@Test
public void wrapSocketChannel() throws Exception {
SocketChannel socketChannel = mock(SocketChannel.class);
SocketChannelWrapper wrapper = factory.wrapSocketChannel(socketChannel, false);
assertInstanceOf(DefaultSocketChannelWrapper.class, wrapper);
}
Aggregations