I've just started using MINA to implement a TCP based server and it was all
going well until recently. I've started seeing an issue now where the
session.write() in the second call to messageReceived in my handler causes
the connection to close.

I've got a custom protocol codec, the encoder part of which looks like

    public void encode(IoSession session, Object request,
ProtocolEncoderOutput out) throws Exception
    {
        final Logger logger =
LoggerFactory.getLogger(MySqlPacketEncoder.class);
        out.flush();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

// code here converts the incoming request Object to bytes and writes to
ByteArrayOutputStream

        buffer.put(baos.toByteArray());
        buffer.flip();
        out.write(buffer);
        baos.close();
    }

In the messageReceived method of my IOHandler, I create a request object
then call session.write()

The main setup looks like this:


        final IoAcceptor acceptor = new NioSocketAcceptor();

        /**
         * We register a shutdown hook so we can make sure we unbind from
the
         * port and dispose the MINA stack properly
         */
        Runtime.getRuntime().addShutdownHook(new Thread()
        {
            @Override
            public void run()
            {

                acceptor.unbind();
                acceptor.dispose();

            }

        });

        acceptor.getFilterChain().addLast("logger", new LoggingFilter());
        acceptor.getFilterChain().addLast("protocol", new
ProtocolCodecFilter(new MyCodecFactory(false)));
        acceptor.setHandler(new MyHandler());

acceptor.getSessionConfig().setReadBufferSize(config.getReadBufferSize());
        acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE,
config.getIdleTimeout());
        ((SocketSessionConfig)
acceptor.getSessionConfig()).setTcpNoDelay(false);

        try
        {
            acceptor.bind(new InetSocketAddress(config.getPort()));
        }
        catch (IOException ex)
        {
            logger.error("Failed to bind to port " + config.getPort(), ex);
        }


With the logging filter enabled this is what I see:

15:37:03.102 [NioProcessor-1] INFO  o.a.m.filter.logging.LoggingFilter -
SENT: HeapBuffer[pos=0 lim=0 cap=0: empty]
15:37:03.102 [NioProcessor-1] INFO  o.a.m.filter.logging.LoggingFilter -
SENT: HeapBuffer[pos=0 lim=58 cap=64: 36 00 00 00 FF 6D 04 23 34 32 30 30
30 43 61 6E...]
15:37:03.103 [NioProcessor-1] INFO  o.a.m.filter.logging.LoggingFilter -
SENT: HeapBuffer[pos=0 lim=0 cap=0: empty]
15:37:03.104 [NioProcessor-1] INFO  o.a.m.filter.logging.LoggingFilter -
CLOSED

I've seen some other mentions of setting tcpNoDelay(false) but trying this
hasn't fixed the issue.

Can anyone shed some light on what's going wrong? Am I missing a step
somewhere?

Thanks,

Jennifer

Reply via email to