use of com.hazelcast.internal.networking.SocketChannelWrapper in project hazelcast by hazelcast.
the class SocketReaderInitializerImpl method init.
@Override
public void init(TcpIpConnection connection, SocketReader reader) throws IOException {
TcpIpConnectionManager connectionManager = connection.getConnectionManager();
IOService ioService = connectionManager.getIoService();
ByteBuffer protocolBuffer = reader.getProtocolBuffer();
SocketChannelWrapper socketChannel = reader.getSocketChannel();
int readBytes = socketChannel.read(protocolBuffer);
if (readBytes == -1) {
throw new EOFException("Could not read protocol type!");
}
if (readBytes == 0 && connectionManager.isSSLEnabled()) {
// when using SSL, we can read 0 bytes since data read from socket can be handshake frames.
return;
}
if (protocolBuffer.hasRemaining()) {
// we have not yet received all protocol bytes
return;
}
ReadHandler readHandler;
String protocol = bytesToString(protocolBuffer.array());
SocketWriter socketWriter = connection.getSocketWriter();
if (CLUSTER.equals(protocol)) {
initInputBuffer(connection, reader, ioService.getSocketReceiveBufferSize());
connection.setType(MEMBER);
socketWriter.setProtocol(CLUSTER);
readHandler = ioService.createReadHandler(connection);
} else if (CLIENT_BINARY_NEW.equals(protocol)) {
initInputBuffer(connection, reader, ioService.getSocketClientReceiveBufferSize());
socketWriter.setProtocol(CLIENT_BINARY_NEW);
readHandler = new ClientReadHandler(reader.getNormalFramesReadCounter(), connection, ioService);
} else {
ByteBuffer inputBuffer = initInputBuffer(connection, reader, ioService.getSocketReceiveBufferSize());
socketWriter.setProtocol(TEXT);
inputBuffer.put(protocolBuffer.array());
readHandler = new TextReadHandler(connection);
connectionManager.incrementTextConnections();
}
if (readHandler == null) {
throw new IOException("Could not initialize ReadHandler!");
}
reader.initReadHandler(readHandler);
}
use of com.hazelcast.internal.networking.SocketChannelWrapper in project hazelcast by hazelcast.
the class InitConnectionTask method tryToConnect.
private void tryToConnect(InetSocketAddress socketAddress, int timeout) throws Exception {
SocketChannel socketChannel = SocketChannel.open();
connectionManager.initSocket(socketChannel.socket());
if (ioService.isSocketBind()) {
bindSocket(socketChannel);
}
Level level = silent ? Level.FINEST : Level.INFO;
if (logger.isLoggable(level)) {
logger.log(level, "Connecting to " + socketAddress + ", timeout: " + timeout + ", bind-any: " + ioService.isSocketBindAny());
}
try {
socketChannel.configureBlocking(true);
connectSocketChannel(socketAddress, timeout, socketChannel);
if (logger.isFinestEnabled()) {
logger.finest("Successfully connected to: " + address + " using socket " + socketChannel.socket());
}
SocketChannelWrapper socketChannelWrapper = connectionManager.wrapSocketChannel(socketChannel, true);
connectionManager.interceptSocket(socketChannel.socket(), false);
socketChannelWrapper.configureBlocking(false);
TcpIpConnection connection = connectionManager.newConnection(socketChannelWrapper, address);
connection.getSocketWriter().setProtocol(Protocols.CLUSTER);
connectionManager.sendBindRequest(connection, address, true);
} catch (Exception e) {
closeSocket(socketChannel);
logger.log(level, "Could not connect to: " + socketAddress + ". Reason: " + e.getClass().getSimpleName() + "[" + e.getMessage() + "]");
throw e;
}
}
Aggregations