directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Trustin Lee <trus...@gmail.com>
Subject Re: [mina] Suggestions for a proxy server
Date Mon, 25 Apr 2005 01:55:12 GMT
Hi Luca,

2005/4/25, Luca Zago <luca.zago@gmail.com>:
> Thanks for the Fix.
> I am conitnuing my testing, yes for this small char server. But I am
> facing some problems with the solution of sending the message to all
> opened sessions..
> my dataRead method looks like:
> 
> public void dataRead( IoSession session, ByteBuffer rb ) {
>        System.out.println( Thread.currentThread().getName() + ' '
>                + session.getRemoteAddress() + ": READ: " + rb.toString());
>        ByteBuffer wb = ByteBuffer.allocate( rb.remaining() );
>        wb.put( rb );
>        wb.flip();
>        for (int i = 0 ; i < sessions.size(); i ++) {
>            IoSession sessionTemp = (IoSession)sessions.get(i);
>            sessionTemp.write(wb,null);
>        }
>    }
> but after the second sending I get an exception:
> java.lang.IllegalStateException: Already released buffer.  You
> released the buffer too many times.
>    at org.apache.mina.common.ByteBuffer$DefaultByteBuffer.release(ByteBuffer.java:557)
>    at org.apache.mina.io.socket.SocketIoProcessor.flush(SocketIoProcessor.java:431)
>    at org.apache.mina.io.socket.SocketIoProcessor.flushSessions(SocketIoProcessor.java:392)
> 
> I read the code of ByteBuffer.. it seems that the buffer has been
> never released so the counter refCount is never decreased. Even if I
> don't understand the meaning of that counter.

Please read JavaDoc for ByteBuffer class.  It is automatically
released when you write the buffer you allocated.  So If you're
writing your buffer multiple times, you have to acquire it to prevent
release.  Here is the fixed code:

public void dataRead( IoSession session, ByteBuffer rb ) {
       System.out.println( Thread.currentThread().getName() + ' '
               + session.getRemoteAddress() + ": READ: " + rb.toString());
       ByteBuffer wb = ByteBuffer.allocate( rb.remaining() );
       wb.put( rb );
       wb.flip();
       for (int i = 0 ; i < sessions.size(); i ++) {
           IoSession sessionTemp = (IoSession)sessions.get(i);
           wb.acquire()
           sessionTemp.write(wb,null);
       }
       wb.release();
   }

> P.S: I am reading all the code, if I can get some ideas I will let you know

Thank you, please let us know if you found any improvement!

<snip/>

Trustin
-- 
what we call human nature is actually human habit
--
http://gleamynode.net/

Mime
View raw message