Search in sources :

Example 6 with SocketChannelWrapper

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);
}
Also used : IOService(com.hazelcast.nio.IOService) SocketChannelWrapper(com.hazelcast.internal.networking.SocketChannelWrapper) TextReadHandler(com.hazelcast.nio.ascii.TextReadHandler) ReadHandler(com.hazelcast.internal.networking.ReadHandler) EOFException(java.io.EOFException) StringUtil.bytesToString(com.hazelcast.util.StringUtil.bytesToString) IOException(java.io.IOException) SocketWriter(com.hazelcast.internal.networking.SocketWriter) TextReadHandler(com.hazelcast.nio.ascii.TextReadHandler) ByteBuffer(java.nio.ByteBuffer) IOUtil.newByteBuffer(com.hazelcast.nio.IOUtil.newByteBuffer)

Example 7 with SocketChannelWrapper

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;
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) SocketChannelWrapper(com.hazelcast.internal.networking.SocketChannelWrapper) Level(java.util.logging.Level) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) SocketException(java.net.SocketException)

Aggregations

SocketChannelWrapper (com.hazelcast.internal.networking.SocketChannelWrapper)7 IOException (java.io.IOException)4 SocketChannel (java.nio.channels.SocketChannel)4 AuthenticationException (com.hazelcast.client.AuthenticationException)1 HazelcastException (com.hazelcast.core.HazelcastException)1 ReadHandler (com.hazelcast.internal.networking.ReadHandler)1 SocketWriter (com.hazelcast.internal.networking.SocketWriter)1 Connection (com.hazelcast.nio.Connection)1 IOService (com.hazelcast.nio.IOService)1 IOUtil.newByteBuffer (com.hazelcast.nio.IOUtil.newByteBuffer)1 TextReadHandler (com.hazelcast.nio.ascii.TextReadHandler)1 QuickTest (com.hazelcast.test.annotation.QuickTest)1 StringUtil.bytesToString (com.hazelcast.util.StringUtil.bytesToString)1 EOFException (java.io.EOFException)1 InetSocketAddress (java.net.InetSocketAddress)1 Socket (java.net.Socket)1 SocketException (java.net.SocketException)1 UnknownHostException (java.net.UnknownHostException)1 ByteBuffer (java.nio.ByteBuffer)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1