Search in sources :

Example 1 with IOService

use of com.hazelcast.nio.IOService in project hazelcast by hazelcast.

the class SocketWriterInitializerImpl method initHandler.

private void initHandler(TcpIpConnection connection, SocketWriter writer, String protocol) {
    WriteHandler handler;
    if (CLUSTER.equals(protocol)) {
        IOService ioService = connection.getConnectionManager().getIoService();
        handler = ioService.createWriteHandler(connection);
    } else if (CLIENT_BINARY_NEW.equals(protocol)) {
        handler = new ClientWriteHandler();
    } else {
        handler = new TextWriteHandler(connection);
    }
    writer.initWriteHandler(handler);
}
Also used : IOService(com.hazelcast.nio.IOService) TextWriteHandler(com.hazelcast.nio.ascii.TextWriteHandler) WriteHandler(com.hazelcast.internal.networking.WriteHandler) TextWriteHandler(com.hazelcast.nio.ascii.TextWriteHandler)

Example 2 with IOService

use of com.hazelcast.nio.IOService 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 3 with IOService

use of com.hazelcast.nio.IOService in project hazelcast by hazelcast.

the class SocketWriterInitializerImpl method initOutputBuffer.

private void initOutputBuffer(TcpIpConnection connection, SocketWriter writer, String protocol) {
    IOService ioService = connection.getConnectionManager().getIoService();
    int sizeKb = CLUSTER.equals(protocol) ? ioService.getSocketSendBufferSize() : ioService.getSocketClientSendBufferSize();
    int size = KILO_BYTE * sizeKb;
    ByteBuffer outputBuffer = newByteBuffer(size, ioService.isSocketBufferDirect());
    if (CLUSTER.equals(protocol)) {
        outputBuffer.put(stringToBytes(CLUSTER));
    }
    writer.initOutputBuffer(outputBuffer);
    try {
        connection.setSendBufferSize(size);
    } catch (SocketException e) {
        logger.finest("Failed to adjust TCP send buffer of " + connection + " to " + size + " B.", e);
    }
}
Also used : SocketException(java.net.SocketException) IOService(com.hazelcast.nio.IOService) ByteBuffer(java.nio.ByteBuffer) IOUtil.newByteBuffer(com.hazelcast.nio.IOUtil.newByteBuffer)

Aggregations

IOService (com.hazelcast.nio.IOService)3 IOUtil.newByteBuffer (com.hazelcast.nio.IOUtil.newByteBuffer)2 ByteBuffer (java.nio.ByteBuffer)2 ReadHandler (com.hazelcast.internal.networking.ReadHandler)1 SocketChannelWrapper (com.hazelcast.internal.networking.SocketChannelWrapper)1 SocketWriter (com.hazelcast.internal.networking.SocketWriter)1 WriteHandler (com.hazelcast.internal.networking.WriteHandler)1 TextReadHandler (com.hazelcast.nio.ascii.TextReadHandler)1 TextWriteHandler (com.hazelcast.nio.ascii.TextWriteHandler)1 StringUtil.bytesToString (com.hazelcast.util.StringUtil.bytesToString)1 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 SocketException (java.net.SocketException)1