Seeing data corruption when using ChannelHandlerContext.set/getAttachement API

Greg Beeble beeblezap at gmail.com
Tue Jul 20 21:11:00 EDT 2010



Hrm.... Am I doing something wrong here.  I'm not seeing the new
ServerHandler() instance being created for each connection.  I've simplified
my code a bit and pasted it below:  Does anyone see something wrong here? 
What I'm seeing is that my instance var (_session) is created once and used
for *all* my connections.


    @ChannelPipelineCoverage("one")  //I'm running 3.1.5
    public class ServerHandler extends SimpleChannelUpstreamHandler {
        private volatile MySession _session = null;
        
        @Override
        public void channelDisconnected(ChannelHandlerContext ctx, 
                ChannelStateEvent e) throws Exception {
            if(_session != null){
                s_logger.info("Disconnected: " + _session.getAppName() + ":
" + 
                        _session.getNode());
		...
            }
        }
        
        @Override
        public void channelConnected(ChannelHandlerContext ctx,
                ChannelStateEvent e) throws Exception {
        }
        
        @Override
        public void messageReceived(ChannelHandlerContext ctx, MessageEvent
e) {
            Message msg = Message.wrap( (String)e.getMessage());
            if(_session != null){
                _session.getMessageQueue().offer(msg);
            }else if(_session == null && msg.getType() == Type.LOGIN) {
                _session = new MySession((LoginMessage)msg);
	        ...
            }else {
                s_logger.warn("MISSING SESSION: " + msg.toString());
                //close the connection
                ctx.getChannel().close();
            }
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx,
ExceptionEvent e) {
             if(_session != null){
                // Close the connection when an exception is raised.
                s_logger.error(
                        "Unexpected exception from downstream: " + session +
                        ctx.getChannel().getRemoteAddress().toString() ,
                        e.getCause());
                ...
             }
             e.getChannel().close();
        }
    }
...
public void start() {
       // Configure the server.
       ServerBootstrap bootstrap = new ServerBootstrap(
               new NioServerSocketChannelFactory(
                       Executors.newCachedThreadPool(),
                       Executors.newCachedThreadPool()));

       bootstrap.setOption("child.tcpNoDelay", true);
       bootstrap.setOption("child.keepAlive", true);

       ChannelPipeline pipeline = bootstrap.getPipeline();

       // Add the text line codec combination first,
       pipeline.addLast("framer", new DelimiterBasedFrameDecoder(
               1024, Delimiters.lineDelimiter()));
       pipeline.addLast("decoder", new StringDecoder());
       pipeline.addLast("encoder", new StringEncoder());

       // Set up the default event pipeline.
       bootstrap.getPipeline().addLast("handler", new ServerHandler());

       // Bind and start to accept incoming connections.
       bootstrap.bind(new InetSocketAddress(_port));
       }catch(Exception e){
           s_logger.error(e);
       }
   }


-- 
View this message in context: http://netty-forums-and-mailing-lists.685743.n2.nabble.com/Seeing-data-corruption-when-using-ChannelHandlerContext-set-getAttachement-API-tp5318952p5319218.html
Sent from the Netty User Group mailing list archive at Nabble.com.


More information about the netty-users mailing list