Java Code Examples for org.apache.mina.core.write.WriteRequestQueue#poll()

The following examples show how to use org.apache.mina.core.write.WriteRequestQueue#poll() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractPollingConnectionlessIoAcceptor.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
private boolean flush( S session, long currentTime ) throws Exception
{
    final WriteRequestQueue writeRequestQueue = session.getWriteRequestQueue();
    final int maxWrittenBytes = session.getConfig().getMaxReadBufferSize()
        + ( session.getConfig().getMaxReadBufferSize() >>> 1 );

    int writtenBytes = 0;

    try
    {
        for ( ;; )
        {
            WriteRequest req = session.getCurrentWriteRequest();

            if ( req == null )
            {
                req = writeRequestQueue.poll( session );

                if ( req == null )
                {
                    setInterestedInWrite( session, false );
                    break;
                }

                session.setCurrentWriteRequest( req );
            }

            IoBuffer buf = ( IoBuffer ) req.getMessage();

            if ( buf.remaining() == 0 )
            {
                // Clear and fire event
                session.setCurrentWriteRequest( null );
                buf.reset();
                session.getFilterChain().fireMessageSent( req );
                continue;
            }

            SocketAddress destination = req.getDestination();

            if ( destination == null )
            {
                destination = session.getRemoteAddress();
            }

            int localWrittenBytes = send( session, buf, destination );

            if ( ( localWrittenBytes == 0 ) || ( writtenBytes >= maxWrittenBytes ) )
            {
                // Kernel buffer is full or wrote too much
                setInterestedInWrite( session, true );

                return false;
            }
            else
            {
                setInterestedInWrite( session, false );

                // Clear and fire event
                session.setCurrentWriteRequest( null );
                writtenBytes += localWrittenBytes;
                buf.reset();
                session.getFilterChain().fireMessageSent( req );
            }
        }
    }
    finally
    {
        session.increaseWrittenBytes( writtenBytes, currentTime );
    }

    return true;
}
 
Example 2
Source File: AbstractPollingIoProcessor.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
private boolean flushNow(S session, long currentTime) {
    if (!session.isConnected()) {
        scheduleRemove(session);
        return false;
    }

    final boolean hasFragmentation = session.getTransportMetadata().hasFragmentation();

    final WriteRequestQueue writeRequestQueue = session.getWriteRequestQueue();

    // Set limitation for the number of written bytes for read-write
    // fairness. I used maxReadBufferSize * 3 / 2, which yields best
    // performance in my experience while not breaking fairness much.
    final int maxWrittenBytes = session.getConfig().getMaxReadBufferSize()
            + (session.getConfig().getMaxReadBufferSize() >>> 1);
    int writtenBytes = 0;
    WriteRequest req = null;

    try {
        // Clear OP_WRITE
        setInterestedInWrite(session, false);

        do {
            // Check for pending writes.
            req = session.getCurrentWriteRequest();

            if (req == null) {
                req = writeRequestQueue.poll(session);

                if (req == null) {
                    break;
                }

                session.setCurrentWriteRequest(req);
            }

            int localWrittenBytes = 0;
            Object message = req.getMessage();

            if (message instanceof IoBuffer) {
                localWrittenBytes = writeBuffer(session, req, hasFragmentation, maxWrittenBytes - writtenBytes,
                        currentTime);

                if ((localWrittenBytes > 0) && ((IoBuffer) message).hasRemaining()) {
                    // the buffer isn't empty, we re-interest it in writing
                    writtenBytes += localWrittenBytes;
                    setInterestedInWrite(session, true);
                    return false;
                }
            } else if (message instanceof FileRegion) {
                localWrittenBytes = writeFile(session, req, hasFragmentation, maxWrittenBytes - writtenBytes,
                        currentTime);

                // Fix for Java bug on Linux
                // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5103988
                // If there's still data to be written in the FileRegion,
                // return 0 indicating that we need
                // to pause until writing may resume.
                if ((localWrittenBytes > 0) && (((FileRegion) message).getRemainingBytes() > 0)) {
                    writtenBytes += localWrittenBytes;
                    setInterestedInWrite(session, true);
                    return false;
                }
            } else {
                throw new IllegalStateException("Don't know how to handle message of type '"
                        + message.getClass().getName() + "'.  Are you missing a protocol encoder?");
            }

            if (localWrittenBytes == 0) {
                // Kernel buffer is full.
                setInterestedInWrite(session, true);
                return false;
            }

            writtenBytes += localWrittenBytes;

            if (writtenBytes >= maxWrittenBytes) {
                // Wrote too much
                scheduleFlush(session);
                return false;
            }
        } while (writtenBytes < maxWrittenBytes);
    } catch (Exception e) {
        if (req != null) {
            req.getFuture().setException(e);
        }

        IoFilterChain filterChain = session.getFilterChain();
        filterChain.fireExceptionCaught(e);
        return false;
    }

    return true;
}
 
Example 3
Source File: NioProcessor.java    From jane with GNU Lesser General Public License v3.0 4 votes vote down vote up
void flushNow(NioSession session) {
	if (session.isClosing())
		return;
	try {
		WriteRequestQueue writeQueue = session.getWriteRequestQueue();
		for (WriteRequest req; (req = writeQueue.peek()) != null; writeQueue.poll()) {
			Object message = req.writeRequestMessage();
			if (message instanceof IoBuffer) {
				IoBuffer buf = (IoBuffer)message;
				if (buf.hasRemaining()) {
					session.getChannel().write(buf.buf());
					if (buf.hasRemaining()) {
						session.setInterestedInWrite(true);
						return;
					}
				}
				req.writeRequestFuture().setWritten();
				buf.free();
			} else if (message instanceof FileRegion) {
				FileRegion region = (FileRegion)message;
				long len = region.getRemainingBytes();
				if (len > 0) {
					region.update(region.getFileChannel().transferTo(region.getPosition(), len, session.getChannel()));
  						if (region.getRemainingBytes() > 0) {
  							session.setInterestedInWrite(true);
  							return;
  						}
				}
				req.writeRequestFuture().setWritten();
			} else if (req == NioSession.CLOSE_REQUEST) {
				session.closeNow();
				break;
			} else if (req == NioSession.SHUTDOWN_REQUEST) {
				session.getChannel().shutdownOutput();
				break;
			} else
				throw new IllegalStateException("unknown message type for writting: " + message.getClass().getName() + ": " + message);
		}
		session.setInterestedInWrite(false);
	} catch (Exception e) {
		session.closeNow();
		session.removeNow(e);
	}
}