Package org.apache.mina.common

Examples of org.apache.mina.common.ByteBuffer


    }

    public void encode( ProtocolSession session, Object message, ProtocolEncoderOutput out ) throws ProtocolViolationException
    {
        AbstractMessage m = ( AbstractMessage ) message ;
        ByteBuffer buf = ByteBuffer.allocate( 16 );
        buf.setAutoExpand( true ); // Enable auto-expand for easier encoding
       
        // Encode a header
        buf.putShort( ( short ) type );
        buf.putInt( m.getSequence() );
       
        // Encode a body
        encodeBody( session, m, buf );
        buf.flip();
        out.write( buf );
    }
View Full Code Here


    {
        NextFilter nextFilter = ( NextFilter ) nextFilter0;
        IoSession session = ( IoSession ) session0;
        if( type == EventType.READ )
        {
            ByteBuffer buf = ( ByteBuffer ) data;
            nextFilter.dataRead( session, buf );
            buf.release();
        }
        else if( type == EventType.WRITTEN )
        {
            nextFilter.dataWritten( session, data );
        }
View Full Code Here

    }
   
    private void testConnector( IoConnector connector, boolean useLocalAddress ) throws Exception
    {
        EchoConnectorHandler handler = new EchoConnectorHandler();
        ByteBuffer readBuf = handler.readBuf;

        IoSession session = null;
        if( !useLocalAddress )
        {
            session = connector.connect(
                    new InetSocketAddress( InetAddress.getLocalHost(), port ),
                    handler );
        }
        else
        {
            int clientPort = port;
            for( int i = 0; i < 65536; i ++ )
            {
                clientPort = AvailablePortFinder.getNextAvailable( clientPort + 1 );
                try
                {
                    session = connector.connect(
                            new InetSocketAddress( InetAddress.getLocalHost(), port ),
                            new InetSocketAddress( clientPort ),
                            handler );
                    break;
                }
                catch( BindException e )
                {
                    // Try again until we succeed to bind.
                }
            }

            if( session == null )
            {
                Assert.fail( "Failed to find out an appropriate local address." );
            }
        }
       
        for( int i = 0; i < 10; i ++ )
        {
            ByteBuffer buf = ByteBuffer.allocate( 16 );
            buf.limit( 16 );
            fillWriteBuffer( buf, i );
            buf.flip();

            Object marker;
            if( ( i & 1 ) == 0 )
            {
                marker = new Integer( i );
            }
            else
            {
                marker = null;
            }

            session.write( buf, marker );

            // This will align message arrival order in UDP
            for( int j = 0; j < 100; j ++ )
            {
                if( readBuf.position() == ( i + 1 ) * 16 )
                {
                    break;
                }
                Thread.sleep( 10 );
            }
        }
       
        for( int i = 0; i < 100; i++ ) {
            if( readBuf.position() == 160 )
            {
                break;
            }
            else
            {
                Thread.sleep( 10 );
            }
        }

        session.close( true );
       
        Assert.assertEquals( 160, readBuf.position() );
        readBuf.flip();
       
        ByteBuffer expectedBuf = ByteBuffer.allocate( 160 );
        for( int i = 0; i < 10; i ++ ) {
            expectedBuf.limit( ( i + 1 ) * 16 );
            fillWriteBuffer( expectedBuf, i );
        }
        expectedBuf.position( 0 );
        assertEquals(expectedBuf, readBuf);
    }
View Full Code Here

    }

    private void readSession( DatagramSession session )
    {

        ByteBuffer readBuf = ByteBuffer.allocate( 2048 );
        try
        {
            int readBytes = session.getChannel().read( readBuf.buf() );
            if( readBytes > 0 )
            {
                readBuf.flip();
                ByteBuffer newBuf = ByteBuffer.allocate( readBuf.limit() );
                newBuf.put( readBuf );
                newBuf.flip();

                session.increaseReadBytes( readBytes );
                filters.dataRead( session, newBuf );
            }
        }
View Full Code Here

        DatagramChannel ch = session.getChannel();

        Queue writeBufferQueue = session.getWriteBufferQueue();
        Queue writeMarkerQueue = session.getWriteMarkerQueue();

        ByteBuffer buf;
        Object marker;
        for( ;; )
        {
            synchronized( writeBufferQueue )
            {
                buf = ( ByteBuffer ) writeBufferQueue.first();
                marker = writeMarkerQueue.first();
            }

            if( buf == null )
                break;

            if( buf.remaining() == 0 )
            {
                // pop and fire event
                synchronized( writeBufferQueue )
                {
                    writeBufferQueue.pop();
                    writeMarkerQueue.pop();
                }

                try
                {
                    buf.release();
                }
                catch( IllegalStateException e )
                {
                    session.getManagerFilterChain().exceptionCaught( session, e );
                }

                session.increaseWrittenWriteRequests();
                session.getManagerFilterChain().dataWritten( session, marker );
                continue;
            }

            SelectionKey key = session.getSelectionKey();
            if( key == null )
            {
                scheduleFlush( session );
                break;
            }
            if( !key.isValid() )
            {
                continue;
            }

            int writtenBytes = ch.write( buf.buf() );

            if( writtenBytes == 0 )
            {
                // Kernel buffer is full
                key.interestOps( key.interestOps() | SelectionKey.OP_WRITE );
View Full Code Here

        {
            sum += ( ( ByteBuffer ) bufferQueue.get( i ) ).remaining();
        }
       
        // Allocate a new BB that will contain all fragments
        ByteBuffer newBuf = ByteBuffer.allocate( sum );
       
        // and merge all.
        for( ;; )
        {
            ByteBuffer buf = ( ByteBuffer ) bufferQueue.pop();
            if( buf == null )
            {
                break;
            }
   
            newBuf.put( buf );
            buf.release();
        }
       
        // Push the new buffer finally.
        newBuf.flip();
        bufferQueue.push(newBuf);
View Full Code Here

    }

    private void readSession( DatagramSession session )
    {

        ByteBuffer readBuf = ByteBuffer.allocate( 2048 );
        try
        {
            SocketAddress remoteAddress = session.getChannel().receive(
                    readBuf.buf() );
            if( remoteAddress != null )
            {
                readBuf.flip();
                session.setRemoteAddress( remoteAddress );

                ByteBuffer newBuf = ByteBuffer.allocate( readBuf.limit() );
                newBuf.put( readBuf );
                newBuf.flip();

                session.increaseReadBytes( newBuf.remaining() );
                filters.dataRead( session, newBuf );
            }
        }
        catch( IOException e )
        {
View Full Code Here

        DatagramChannel ch = session.getChannel();

        Queue writeBufferQueue = session.getWriteBufferQueue();
        Queue writeMarkerQueue = session.getWriteMarkerQueue();

        ByteBuffer buf;
        Object marker;
        for( ;; )
        {
            synchronized( writeBufferQueue )
            {
                buf = ( ByteBuffer ) writeBufferQueue.first();
                marker = writeMarkerQueue.first();
            }

            if( buf == null )
                break;

            if( buf.remaining() == 0 )
            {
                // pop and fire event
                synchronized( writeBufferQueue )
                {
                    writeBufferQueue.pop();
                    writeMarkerQueue.pop();
                }

                try
                {
                    buf.release();
                }
                catch( IllegalStateException e )
                {
                    session.getManagerFilterChain().exceptionCaught( session, e );
                }

                session.increaseWrittenWriteRequests();
                session.getManagerFilterChain().dataWritten( session, marker );
                continue;
            }

            SelectionKey key = session.getSelectionKey();
            if( key == null )
            {
                scheduleFlush( session );
                break;
            }
            if( !key.isValid() )
            {
                continue;
            }

            int writtenBytes = ch
                    .send( buf.buf(), session.getRemoteAddress() );

            if( writtenBytes == 0 )
            {
                // Kernel buffer is full
                key.interestOps( key.interestOps() | SelectionKey.OP_WRITE );
View Full Code Here

        selectedKeys.clear();
    }

    private void read( SocketSession session )
    {
        ByteBuffer buf = ByteBuffer.allocate(
                (( SocketSessionConfig ) session.getConfig()).getSessionReceiveBufferSize() );
        SocketChannel ch = session.getChannel();

        try
        {
            int readBytes = 0;
            int ret;

            buf.clear();

            try
            {
                while( ( ret = ch.read( buf.buf() ) ) > 0 )
                {
                    readBytes += ret;
                }
            }
            finally
            {
                buf.flip();
            }

            session.increaseReadBytes( readBytes );
            session.resetIdleCount( IdleStatus.BOTH_IDLE );
            session.resetIdleCount( IdleStatus.READER_IDLE );

            if( readBytes > 0 )
            {
                ByteBuffer newBuf = ByteBuffer.allocate( readBytes );
                newBuf.put( buf );
                newBuf.flip();
                session.getManagerFilterChain().dataRead( session, newBuf );
            }
            if( ret < 0 )
            {
                scheduleRemove( session );
View Full Code Here

   
    private void releaseWriteBuffers( SocketSession session )
    {
        Queue writeBufferQueue = session.getWriteBufferQueue();
        session.getWriteMarkerQueue().clear();
        ByteBuffer buf;
       
        while( ( buf = (ByteBuffer) writeBufferQueue.pop() ) != null )
        {
            try
            {
                buf.release();
            }
            catch( IllegalStateException e )
            {
                session.getManagerFilterChain().exceptionCaught( session, e );
            }
View Full Code Here

TOP

Related Classes of org.apache.mina.common.ByteBuffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.