/**
   * @param bytebuffer ByteBuffer
   * @param flip boolean
   * @return int
   * @throws IOException TODO Fix non blocking write properly
   */
  private synchronized int writeToSocket(ByteBuffer bytebuffer, boolean block, boolean flip)
      throws IOException {
    if (flip) bytebuffer.flip();

    int written = 0;
    NioEndpoint.KeyAttachment att = (NioEndpoint.KeyAttachment) socket.getAttachment(false);
    if (att == null) throw new IOException("Key must be cancelled");
    long writeTimeout = att.getTimeout();
    Selector selector = null;
    try {
      selector = pool.get();
    } catch (IOException x) {
      // ignore
    }
    try {
      written = pool.write(bytebuffer, socket, selector, writeTimeout, block, lastWrite);
      // make sure we are flushed
      do {
        if (socket.flush(true, selector, writeTimeout, lastWrite)) break;
      } while (true);
    } finally {
      if (selector != null) pool.put(selector);
    }
    if (block) bytebuffer.clear(); // only clear
    return written;
  }
  /** Callback to write data from the buffer. */
  private void flushBuffer() throws IOException {

    // prevent timeout for async,
    SelectionKey key = socket.getIOChannel().keyFor(socket.getPoller().getSelector());
    if (key != null) {
      NioEndpoint.KeyAttachment attach = (NioEndpoint.KeyAttachment) key.attachment();
      attach.access();
    }

    // write to the socket, if there is anything to write
    if (socket.getBufHandler().getWriteBuffer().position() > 0) {
      socket.getBufHandler().getWriteBuffer().flip();
      writeToSocket(socket.getBufHandler().getWriteBuffer(), true, false);
    }
  }
 private synchronized void addToBB(byte[] buf, int offset, int length) throws IOException {
   while (length > 0) {
     int thisTime = length;
     if (socket.getBufHandler().getWriteBuffer().position()
             == socket.getBufHandler().getWriteBuffer().capacity()
         || socket.getBufHandler().getWriteBuffer().remaining() == 0) {
       flushBuffer();
     }
     if (thisTime > socket.getBufHandler().getWriteBuffer().remaining()) {
       thisTime = socket.getBufHandler().getWriteBuffer().remaining();
     }
     socket.getBufHandler().getWriteBuffer().put(buf, offset, thisTime);
     length = length - thisTime;
     offset = offset + thisTime;
   }
   NioEndpoint.KeyAttachment ka = (NioEndpoint.KeyAttachment) socket.getAttachment(false);
   if (ka != null) ka.access(); // prevent timeouts for just doing client writes
 }
Exemplo n.º 4
0
 private int readSocket(byte[] buf, int pos, int n, boolean block) throws IOException {
   int nRead = 0;
   ByteBuffer readBuffer = socketWrapper.getSocket().getBufHandler().getReadBuffer();
   readBuffer.clear();
   readBuffer.limit(n);
   if (block) {
     Selector selector = null;
     try {
       selector = pool.get();
     } catch (IOException x) {
       // Ignore
     }
     try {
       NioEndpoint.KeyAttachment att =
           (NioEndpoint.KeyAttachment) socketWrapper.getSocket().getAttachment(false);
       if (att == null) throw new IOException("Key must be cancelled.");
       nRead = pool.read(readBuffer, socketWrapper.getSocket(), selector, att.getTimeout());
     } catch (EOFException eof) {
       nRead = -1;
     } finally {
       if (selector != null) pool.put(selector);
     }
   } else {
     nRead = socketWrapper.getSocket().read(readBuffer);
   }
   if (nRead > 0) {
     readBuffer.flip();
     readBuffer.limit(nRead);
     readBuffer.get(buf, pos, nRead);
     return nRead;
   } else if (nRead == -1) {
     // return false;
     throw new EOFException(sm.getString("iib.eof.error"));
   } else {
     return 0;
   }
 }