@Override default boolean isOpen() { return getSocketChannel().isOpen(); } }
public static void readBytes(ISocketChannel socketChannel, ByteBuffer byteBuffer, int length) throws IOException { byteBuffer.clear(); byteBuffer.limit(length); while (byteBuffer.remaining() > 0 && socketChannel.read(byteBuffer) > 0); if (byteBuffer.remaining() > 0) { throw new EOFException(); } byteBuffer.flip(); }
public static void transferBufferToChannel(ISocketChannel socketChannel, ByteBuffer requestBuffer) throws IOException { while (requestBuffer.hasRemaining()) { socketChannel.write(requestBuffer); } }
private synchronized void closeLogReplicationChannel() { try { if (logRepChannel != null && logRepChannel.getSocketChannel().isOpen()) { ReplicationProtocol.sendGoodbye(logRepChannel); logRepChannel.close(); logRepChannel = null; } } catch (IOException e) { LOGGER.warn("Exception while closing socket", e); } }
@Override public void run() { Thread.currentThread().setName("Replication Worker"); try { if (socketChannel.requiresHandshake() && !socketChannel.handshake()) { return; } socketChannel.getSocketChannel().configureBlocking(true); ReplicationRequestType requestType = ReplicationProtocol.getRequestType(socketChannel, inBuffer); while (requestType != ReplicationRequestType.GOODBYE) { handle(requestType); requestType = ReplicationProtocol.getRequestType(socketChannel, inBuffer); } } catch (Exception e) { LOGGER.warn("Unexpected error during replication.", e); } finally { NetworkUtil.closeQuietly(socketChannel); } }
boolean performPendingWrite(ISocketChannel sc) throws IOException { if (pendingWriteCompletion && !sc.completeWrite()) { return false; int written = sc.write(cmdWriteBuffer); muxDemux.getPerformanceCounters().addSignalingBytesWritten(written); if (written < len) { try { pendingBuffer.limit(pendingWriteSize + pendingBuffer.position()); int written = sc.write(pendingBuffer); muxDemux.getPerformanceCounters().addPayloadBytesWritten(written); pendingWriteSize -= written; if (sc.isPendingWrite()) { pendingWriteCompletion = true; return false;
private void socketConnected(InetSocketAddress remoteAddress, SocketChannel channel) { final ISocketChannel socketChannel = socketChannelFactory.createClientChannel(channel); final PendingHandshakeConnection conn = new PendingHandshakeConnection(socketChannel, remoteAddress, OUTGOING); if (socketChannel.requiresHandshake()) { asyncHandshake(conn); } else { conn.handshakeSuccess = true; handshakeCompletedConnections.add(conn); } }
private void write(SelectionKey writableKey) { SocketChannel channel = (SocketChannel) writableKey.channel(); IPCHandle handle = (IPCHandle) writableKey.attachment(); final ISocketChannel socketChannel = handle.getSocketChannel(); ByteBuffer writeBuffer = handle.getOutBuffer(); try { int len = socketChannel.write(writeBuffer); if (len < 0) { close(writableKey, channel); return; } system.getPerformanceCounters().addMessageBytesSent(len); if (!writeBuffer.hasRemaining() && !socketChannel.isPendingWrite()) { writableKey.interestOps(writableKey.interestOps() & ~SelectionKey.OP_WRITE); } if (handle.full()) { handle.clearFull(); selector.wakeup(); } } catch (IOException e) { LOGGER.error("TCP write error to {}", handle.getRemoteAddress(), e); close(writableKey, channel); } }
void driveReaderStateMachine() throws IOException, NetException { ISocketChannel sc = tcpConnection.getSocketChannel(); int chunksRead = 0; while (chunksRead < MAX_CHUNKS_READ_PER_CYCLE || sc.isPendingRead()) { if (readerState.readBuffer.remaining() > 0) { int read = sc.read(readerState.readBuffer); if (read < 0) { throw new NetException("Socket Closed");
public static ISocketChannel establishReplicaConnection(INcApplicationContext appCtx, InetSocketAddress location) throws IOException { final SocketChannel socketChannel = SocketChannel.open(); NetworkUtil.configure(socketChannel); socketChannel.connect(location); // perform handshake in a non-blocking mode socketChannel.configureBlocking(false); final ISocketChannelFactory socketChannelFactory = appCtx.getServiceContext().getControllerService().getNetworkSecurityManager().getSocketChannelFactory(); final ISocketChannel clientChannel = socketChannelFactory.createClientChannel(socketChannel); if (clientChannel.requiresHandshake() && !clientChannel.handshake()) { throw new IllegalStateException("handshake failure"); } // switch to blocking mode after handshake success socketChannel.configureBlocking(true); return clientChannel; }
public synchronized void close() { key.cancel(); try { channel.close(); } catch (IOException e) { LOGGER.error(() -> "Error closing channel at: " + remoteAddress, e); } }
private void connectionReceived(SocketChannel channel) { final ISocketChannel socketChannel = socketChannelFactory.createServerChannel(channel); final PendingHandshakeConnection conn = new PendingHandshakeConnection(socketChannel, null, INCOMING); if (socketChannel.requiresHandshake()) { asyncHandshake(conn); } else { conn.handshakeSuccess = true; handshakeCompletedConnections.add(conn); } }
public TCPConnection(TCPEndpoint endpoint, ISocketChannel channel, SelectionKey key, Selector selector, ConnectionType type) { this.endpoint = endpoint; this.channel = channel; this.key = key; this.selector = selector; this.type = type; remoteAddress = (InetSocketAddress) channel.getSocketChannel().socket().getRemoteSocketAddress(); }
currentReadBuffer.limit(currentReadBuffer.position() + rSize); int len; len = sc.read(currentReadBuffer); if (len < 0) { throw new NetException("Socket Closed");
private void finishConnect(SelectionKey connectableKey) { SocketChannel channel = (SocketChannel) connectableKey.channel(); IPCHandle handle = (IPCHandle) connectableKey.attachment(); boolean connected = false; try { connected = channel.finishConnect(); if (connected) { SelectionKey channelKey = channel.register(selector, SelectionKey.OP_READ); final ISocketChannel clientChannel = socketChannelFactory.createClientChannel(channel); if (clientChannel.requiresHandshake()) { asyncHandshake(clientChannel, handle, channelKey); } else { connectionEstablished(handle, channelKey, clientChannel); } } } catch (IOException e) { LOGGER.warn("Exception finishing connect", e); } finally { if (!connected) { LOGGER.warn("Failed to finish connect to {}", handle.getRemoteAddress()); close(connectableKey, channel); } } }
public InetSocketAddress getLocalAddress() { return (InetSocketAddress) channel.getSocketChannel().socket().getLocalSocketAddress(); }
int len; try { len = sc.read(currentReadBuffer); if (len < 0) { throw new NetException("Socket Closed");
private void accept() { SocketChannel channel = null; SelectionKey channelKey = null; try { channel = serverSocketChannel.accept(); register(channel); final ISocketChannel serverChannel = socketChannelFactory.createServerChannel(channel); channelKey = channel.register(selector, SelectionKey.OP_READ); if (serverChannel.requiresHandshake()) { asyncHandshake(serverChannel, null, channelKey); } else { connectionReceived(serverChannel, channelKey); } } catch (IOException e) { LOGGER.error("Failed to accept channel ", e); close(channelKey, channel); } }
public static boolean isHealthy(ISocketChannel sc) { return sc != null && sc.getSocketChannel().isOpen() && sc.getSocketChannel().isConnected(); } }
private void read(SelectionKey readableKey) { SocketChannel channel = (SocketChannel) readableKey.channel(); IPCHandle handle = (IPCHandle) readableKey.attachment(); ByteBuffer readBuffer = handle.getInBuffer(); try { int len = handle.getSocketChannel().read(readBuffer); if (len < 0) { close(readableKey, channel); return; } system.getPerformanceCounters().addMessageBytesReceived(len); handle.processIncomingMessages(); if (!readBuffer.hasRemaining()) { handle.resizeInBuffer(); } } catch (IOException e) { LOGGER.error("TCP read error from {}", handle.getRemoteAddress(), e); close(readableKey, channel); } }