@Override public boolean isBlocking() { return socketChannel.isBlocking(); }
@Override public boolean isBlocking() { return socketChannel.isBlocking(); }
@Override public boolean isBlocking() { if( channel instanceof SocketChannel ) return ( (SocketChannel) channel ).isBlocking(); else if( channel instanceof WrappedByteChannel ) return ( (WrappedByteChannel) channel ).isBlocking(); return false; }
/** * Safely write buffer fully to blocking socket channel. * Will throw assert if non blocking channel passed. * * @param sockCh WritableByteChannel. * @param buf Buffer. * @throws IOException IOException. */ public static void writeFully(SocketChannel sockCh, ByteBuffer buf) throws IOException { int totalWritten = 0; assert sockCh.isBlocking() : "SocketChannel should be in blocking mode " + sockCh; while (buf.hasRemaining()) { int written = sockCh.write(buf); if (written < 0) throw new IOException("Error writing buffer to channel " + "[written = " + written + ", buf " + buf + ", totalWritten = " + totalWritten + "]"); totalWritten += written; } }
@Override public void write(int oneByte) throws IOException { if (!channel.isBlocking()) { throw new IllegalBlockingModeException(); } ByteBuffer buffer = ByteBuffer.allocate(1); buffer.put(0, (byte) (oneByte & 0xFF)); channel.write(buffer); } }
@Override public int read() throws IOException { if (!channel.isBlocking()) { throw new IllegalBlockingModeException(); } ByteBuffer buf = ByteBuffer.allocate(1); int result = channel.read(buf); return (result == -1) ? result : (buf.get(0) & 0xff); }
@Override public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException { Arrays.checkOffsetAndCount(buffer.length, byteOffset, byteCount); if (!channel.isBlocking()) { throw new IllegalBlockingModeException(); } ByteBuffer buf = ByteBuffer.wrap(buffer, byteOffset, byteCount); return channel.read(buf); } }
@Override public void write(byte[] buffer, int offset, int byteCount) throws IOException { Arrays.checkOffsetAndCount(buffer.length, offset, byteCount); ByteBuffer buf = ByteBuffer.wrap(buffer, offset, byteCount); if (!channel.isBlocking()) { throw new IllegalBlockingModeException(); } channel.write(buf); }
@Override public int read(final byte[] b, final int off, final int len) throws IOException { if (bufferedByte != null) { final byte retVal = bufferedByte; bufferedByte = null; b[off] = retVal; return 1; } final ByteBuffer buffer = ByteBuffer.wrap(b, off, len); final boolean blocking = channel.isBlocking(); try { channel.configureBlocking(true); final long maxTime = System.currentTimeMillis() + timeoutMillis; int bytesRead; do { bytesRead = channel.read(buffer); if (bytesRead == 0) { if (System.currentTimeMillis() > maxTime) { throw new SocketTimeoutException("Timed out reading from socket"); } } } while (bytesRead == 0); return bytesRead; } finally { if (!blocking) { channel.configureBlocking(false); } } }
final boolean blocking = channel.isBlocking();
public SocketChannel getTCPSocket() throws IOException { // Under lock, claim an existing open socket if possible synchronized(this) { // Limit myself to the number of open sockets from node-to-node while( _socksAvail == 0 ) try { wait(); } catch( InterruptedException ie ) { } // Claim an open socket SocketChannel sock = _socks[--_socksAvail]; if( sock != null ) { if( sock.isOpen() ) return sock; // Return existing socket! // Else its an already-closed socket, lower open TCP count assert TCPS.get() > 0; TCPS.decrementAndGet(); } } // Must make a fresh socket SocketChannel sock2 = SocketChannel.open(); sock2.socket().setReuseAddress(true); sock2.socket().setSendBufferSize(AutoBuffer.BBSIZE); boolean res = sock2.connect( _key ); assert res && !sock2.isConnectionPending() && sock2.isBlocking() && sock2.isConnected() && sock2.isOpen(); TCPS.incrementAndGet(); // Cluster-wide counting return sock2; } public synchronized void freeTCPSocket( SocketChannel sock ) {
/** * Initializes a new instance of this class. * * @param channelGroup group to associate new new channel to * @param tlsChannel existing TLS channel to be used asynchronously * @param socketChannel underlying socket * @throws ClosedChannelException if any of the underlying channels are closed. * @throws IllegalArgumentException is the socket is in blocking mode */ public AsynchronousTlsChannel( final AsynchronousTlsChannelGroup channelGroup, final TlsChannel tlsChannel, final SocketChannel socketChannel) throws ClosedChannelException, IllegalArgumentException { if (!socketChannel.isOpen()) { throw new ClosedChannelException(); } if (!tlsChannel.isOpen()) { throw new ClosedChannelException(); } if (socketChannel.isBlocking()) { throw new IllegalArgumentException("socket channel must be in non-blocking mode"); } this.group = channelGroup; this.tlsChannel = tlsChannel; this.registeredSocket = channelGroup.registerSocket(tlsChannel, socketChannel); }
boolean blocking = socketChannel.isBlocking(); if (blocking) { socketChannel.configureBlocking(false);
SocketAddress endpoint, int timeout) throws IOException { boolean blockingOn = channel.isBlocking(); if (blockingOn) { channel.configureBlocking(false);
@Override public void write(int oneByte) throws IOException { if (!channel.isBlocking()) { throw new IllegalBlockingModeException(); } ByteBuffer buffer = ByteBuffer.allocate(1); buffer.put(0, (byte) (oneByte & 0xFF)); channel.write(buffer); } }
@Override public int read() throws IOException { if (!channel.isBlocking()) { throw new IllegalBlockingModeException(); } ByteBuffer buf = ByteBuffer.allocate(1); int result = channel.read(buf); return (result == -1) ? result : (buf.get(0) & 0xff); }
@Override public int read() throws IOException { if (!channel.isBlocking()) { throw new IllegalBlockingModeException(); } ByteBuffer buf = ByteBuffer.allocate(1); int result = channel.read(buf); return (result == -1) ? result : (buf.get(0) & 0xff); }
@Override public void write(byte[] buffer, int offset, int byteCount) throws IOException { Arrays.checkOffsetAndCount(buffer.length, offset, byteCount); ByteBuffer buf = ByteBuffer.wrap(buffer, offset, byteCount); if (!channel.isBlocking()) { throw new IllegalBlockingModeException(); } channel.write(buf); }
@Override public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException { Arrays.checkOffsetAndCount(buffer.length, byteOffset, byteCount); if (!channel.isBlocking()) { throw new IllegalBlockingModeException(); } ByteBuffer buf = ByteBuffer.wrap(buffer, byteOffset, byteCount); return channel.read(buf); } }
/** * Add this channel. * The channel will either be unquarantined, or an error will be reported */ public void add(final SocketChannel channel, final QuarantineConversation conversation) { if (channel.isBlocking()) { throw new IllegalArgumentException("Channel is blocking"); } // add the decoder first, so it can quarantine the messages! decoder.add(channel, conversation); reader.add(channel); }