Esempio n. 1
0
  /** {@inheritDoc} */
  @Override
  public long write(final SelectionKey key) throws IOException {
    final DatagramChannel channel = (DatagramChannel) key.channel();
    final MessageBufferConsumer<ByteBuffer> chnIn = getChannelInput();
    long n = 0;
    long k = chnIn.remaining();
    for (; k >= 0; k--) {
      final SocketAddress address;
      try {
        final long sequence = chnIn.acquire();
        try {
          final ByteBuffer msg = chnIn.get(sequence);
          sendBuffer.clear();
          codec.put(msg, sendBuffer);
          msg.clear();
          sendBuffer.flip();
          address = (SocketAddress) chnIn.attachment(sequence);
        } finally {
          chnIn.release(sequence);
        }
      } catch (final InterruptedException e) {
        throw new IOException(e);
      }

      limiter.send(sendBuffer.remaining());

      do {
        n += channel.send(sendBuffer, address);
      } while (sendBuffer.remaining() > 0);
    }
    if (chnIn.remaining() == 0) {
      disableWriter();
    }
    return n;
  }