Refine search
/** * Push a buffer into the queue. There is no mechanism to limit the number of pushed buffers; if such a mechanism * is desired, it must be implemented externally, for example maybe using a {@link Semaphore}. * * @param pooledBuffer the buffer from which more data should be read */ public void push(final Pooled<ByteBuffer> pooledBuffer) { synchronized (this) { if (pooledBuffer.getResource().hasRemaining() && !eof && failure == null) { queue.add(pooledBuffer); notifyAll(); } else { pooledBuffer.free(); } } }
private boolean writeBuffer() throws IOException { final ByteBuffer buffer = transmitBuffer.getResource(); if (buffer.position() > 0) buffer.flip(); try { while (buffer.hasRemaining()) { final int res = next.write(buffer); if (res == 0) { return false; } } return true; } finally { buffer.compact(); } }
private void asyncCloseMessage(final int id) { Pooled<ByteBuffer> pooled = connection.allocate(); boolean ok = false; try { ByteBuffer byteBuffer = pooled.getResource(); byteBuffer.put(Protocol.MESSAGE_CLOSE); byteBuffer.putInt(channelId); byteBuffer.putShort((short) id); byteBuffer.flip(); ok = true; connection.send(pooled); } finally { if (! ok) pooled.free(); } }
static void sendCloseRequestBody(RemoteConnection remoteConnection) { final Pooled<ByteBuffer> pooled = remoteConnection.allocate(); boolean ok = false; try { final ByteBuffer buffer = pooled.getResource(); buffer.put(Protocol.CONNECTION_CLOSE); buffer.flip(); remoteConnection.send(pooled, true); ok = true; } finally { if (! ok) { pooled.free(); } } }
private void send(boolean eof) throws IOException { assert Thread.holdsLock(this); assert !closed; final Pooled<ByteBuffer> pooledBuffer = buffer; final ByteBuffer buffer = pooledBuffer == null ? null : pooledBuffer.getResource(); this.buffer = null; if (buffer != null && buffer.position() > 0) { buffer.flip(); send(pooledBuffer, eof); } else if (eof) { Pooled<ByteBuffer> pooledBuffer1 = getBuffer(); final ByteBuffer buffer1 = pooledBuffer1.getResource(); buffer1.flip(); send(pooledBuffer1, eof); } }
public void free() { if (freedUpdater.compareAndSet(this, 0, 1)) { zero(allocated.getResource()); allocated.free(); } }
private boolean flushLocal() throws IOException { try { final ByteBuffer buffer = pooledBuffer.getResource(); if (buffer.position() > 0) { buffer.flip(); try { for (;;) { super.write(buffer); if (! buffer.hasRemaining()) { if (terminate) { pooledBuffer.free(); } return true; } } } finally { buffer.compact(); } } else { return true; } } catch (IllegalStateException ignored) { return true; } }
public int read(final ByteBuffer dst) throws IOException { try { final ByteBuffer buffer = pooledBuffer.getResource(); final int lim = buffer.limit(); final int pos = buffer.position(); final int rem = lim - pos; if (rem > 0) { return Buffers.copy(dst, buffer); } else { final int dstRem = dst.remaining(); buffer.clear(); try { final long rres = next.read(new ByteBuffer[] { dst, buffer }, 0, 2); if (rres == -1L) { return -1; } } finally { buffer.flip(); } return dst.remaining() - dstRem; } } catch (IllegalStateException ignored) { return -1; } }
public XnioByteBufferPool(Pool<ByteBuffer> pool) { this.pool = pool; Pooled<ByteBuffer> buf = pool.allocate(); bufferSize = buf.getResource().remaining(); direct = !buf.getResource().hasArray(); buf.free(); if(direct) { arrayBackedPool = new DefaultByteBufferPool(false, bufferSize); } else { arrayBackedPool = this; } }
public void sendAuthReject(final int id) throws IOException { log.tracef("Sending authentication reject for ID %08x", id); // todo: allocate small buffer final Pooled<ByteBuffer> pooled = remoteConnection.allocate(); boolean ok = false; try { final ByteBuffer buffer = pooled.getResource(); buffer.put(Protocol.APP_AUTH_REJECT); buffer.putInt(id); buffer.flip(); remoteConnection.send(pooled); ok = true; } finally { if (! ok) pooled.free(); } }
private boolean doFlushBuffer() throws IOException { assert Thread.holdsLock(writeLock); final ByteBuffer buffer = transmitBuffer.getResource(); buffer.flip(); try { while (buffer.hasRemaining()) { final int res = channel.write(buffer); if (res == 0) { log.tracef("Did not fully flush %s", this); return false; } } log.tracef("Fully flushed %s", this); return true; } finally { buffer.compact(); } }
/** * Push a buffer back to the head of the stream. Once the buffer data is consumed, it will * be released back to its original pool (if any). * * @param pooledBuffer the buffer to push back */ public void pushBack(Pooled<ByteBuffer> pooledBuffer) { if (pooledBuffer == null) { return; } if (shutdown || ! pooledBuffer.getResource().hasRemaining()) { pooledBuffer.free(); } else { current = new BufferConduit(current, pooledBuffer); } }
public void sendAuthDelete(final int id) throws IOException { log.tracef("Sending authentication delete for ID %08x", id); // todo: allocate small buffer final Pooled<ByteBuffer> pooled = remoteConnection.allocate(); boolean ok = false; try { final ByteBuffer buffer = pooled.getResource(); buffer.put(Protocol.APP_AUTH_DELETE); buffer.putInt(id); buffer.flip(); remoteConnection.send(pooled); ok = true; } finally { if (! ok) pooled.free(); } }
public int write(final ByteBuffer src) throws IOException { try { final ByteBuffer buffer = pooledBuffer.getResource(); final int pos = buffer.position(); final int lim = buffer.limit(); buffer.flip(); try { super.write(new ByteBuffer[] { buffer, src }, 0, 2);
@Override protected void onFullBinaryMessage(WebSocketChannel channel, BufferedBinaryMessage message) { this.session.handleMessage(Type.BINARY, toMessage(Type.BINARY, message.getData().getResource())); message.getData().free(); }
void rejectAuthentication(String mechName) { // reject log.rejectedInvalidMechanism(mechName); final Pooled<ByteBuffer> pooled = connection.allocate(); boolean ok = false; try { final ByteBuffer sendBuffer = pooled.getResource(); sendBuffer.put(Protocol.AUTH_REJECTED); sendBuffer.flip(); connection.send(pooled); ok = true; } finally { if (! ok) pooled.free(); } }
/** * Flushes all data in the wrapped bytes buffer. * * @return {@code true} if all data available has been flushed * * @throws IOException if an unexpected IO exception occurs */ private boolean doFlush() throws IOException { assert Thread.holdsLock(getWrapLock()); assert ! Thread.holdsLock(getUnwrapLock()); final ByteBuffer buffer; buffer = sendBuffer.getResource(); buffer.flip(); try { while (buffer.hasRemaining()) { final int res = sinkConduit.write(buffer); if (res == 0) { return false; } } } finally { buffer.compact(); } return sinkConduit.flush(); }
@Override protected void onFullPongMessage(WebSocketChannel channel, BufferedBinaryMessage message) { this.session.handleMessage(Type.PONG, toMessage(Type.PONG, message.getData().getResource())); message.getData().free(); }
public void sendAuthDeleteAck(final int id) throws IOException { log.tracef("Sending authentication delete ack for ID %08x", id); // todo: allocate small buffer final Pooled<ByteBuffer> pooled = remoteConnection.allocate(); boolean ok = false; try { final ByteBuffer buffer = pooled.getResource(); buffer.put(Protocol.APP_AUTH_DELETE_ACK); buffer.putInt(id); buffer.flip(); remoteConnection.send(pooled); ok = true; } finally { if (! ok) pooled.free(); } }
public int receive(final ByteBuffer dst) throws IOException { final ByteBuffer receiveBuffer = this.receiveBuffer.getResource(); int res; do { return res; receiveBuffer.flip(); try { final int length = receiveBuffer.getInt();