hello,
I am very glad to write to you to discuss some of my doubts. Recently, I
read the write processs of mina(source code). There is something really puzzled
me.
the flushNow function in mina is not so easy to understand. the following
are the origin source code and I add some comment at key point.
finalboolean hasFragmentation =
session.getTransportMetadata().hasFragmentation();
finalWriteRequestQueue 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.
finalint 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();// get the current write request
if(req ==null){
req = writeRequestQueue.poll(session); // each time will poll the first request
from write request queue, and assign it
// to current
write request if current write request is null
if(req ==null){
break;
}
session.setCurrentWriteRequest(req);
}
int localWrittenBytes =0;
Object message = req.getMessage();// get message from req
if(message instanceofIoBuffer){
// writeBuffer will write message out, there are two condition:
// 1) hasFragmentation == true, then every time just write
min((maxWrittenBytes - writtenBytes), buf.remaining())
// 2) hasFragmentation == false, then write the whole
message out
localWrittenBytes = writeBuffer(session, req, hasFragmentation, maxWrittenBytes
- writtenBytes,
currentTime); // in writeBuffer, if message were totally written out, will
setCurrentRequest(null)
// why here after writeBuffer, localWrittenBytes >0 and message has
Remaining() then jump out this flush and let Processor to process this session
after next select ??? bad network state???
// localWrittenBytes > 0 and message.hasRemaining()==
true may happen when the message is huge, bigger then
// maxWrittenBytes and network state is good, why not
put it to then end of the flushingSessions queue but reinterest
// it in writing?
//
if((localWrittenBytes >0)&&((IoBuffer) message).hasRemaining()){
// the buffer isn't empty, we re-interest it in writing
writtenBytes += localWrittenBytes;
setInterestedInWrite(session,true);
returnfalse;
}
}elseif{...}
if(localWrittenBytes ==0){
// Kernel buffer is full.
setInterestedInWrite(session,true);
returnfalse;
}
writtenBytes += localWrittenBytes;
// if after serveral rounds writtenBytes >= maxWrittenBytes , put current
session to queue end for fairness.
if(writtenBytes >= maxWrittenBytes){
// Wrote too much
scheduleFlush(session);
returnfalse;
}
if(message instanceofIoBuffer){
((IoBuffer) message).free();
}
}while(writtenBytes < maxWrittenBytes); // this round the cummulated
writtenBytes still < maxWrittenBytes
}catch(..){
....}
it's hard to describe what i want to understand just though mail, but hope
you can understand what i am saying, looking forward to your respond.
Best Wishes!