|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
令人可喜的是java现在已经开源了,所以我想我上述的想法也许有一天会实现,因为java一直都是不断创新的语言,每次创新都会给我们惊喜,这也是我喜欢java的一个原因。version:netty-3.2.1.final
location:org.jboss.netty.channel.socket.nio.NioWorker.read(SelectionKey)
代码片断以下:- 01ByteBufferbb=recvBufferPool.acquire(predictedRecvBufSize);0203...0405if(readBytes>0){06bb.flip();0708finalChannelBufferFactorybufferFactory=09channel.getConfig().getBufferFactory();10finalChannelBufferbuffer=bufferFactory.getBuffer(11bb.order(bufferFactory.getDefaultOrder()));1213recvBufferPool.release(bb);1415//Updatethepredictor.16predictor.previousReceiveBufferSize(readBytes);1718//Firetheevent.19fireMessageReceived(channel,buffer);20}else{21recvBufferPool.release(bb);22}
复制代码 fireMessageReceived会终极触发handler的messageReceived办法的挪用,但吸收数据的buffer已在此前就偿还给了缓存池(recvBufferPool).实在,bb的接纳放在fireMessageReceived之前或以后,在一样平常情形下对实行的了局不发生影响,究竟在fireMessageReceived实行终了时,bb也就利用完了.可是,如有三个条件前提同时满意时,就会呈现bb被覆写成绩.
- 利用ExecutionHandler异步化messageReceived的实行,那末bb就有大概在(异步线程)读取之前,被(以后线程)用厥后的数据覆写,除非buffer是bb的正本;
- 利用DirectChannelBufferFactory完成zero-copy,那末buffer一定是对bb的wrapped,但bb并非间接服用的,而是由recvBufferPool办理,除非bb恰好满意了厥后数据的必要;
- 利用FixedReceiveBufferSizePredictor令每次对缓存巨细需求都一样,那末bb就会被覆写.
重现这一圈套的代码以下:- 01packagecn.cafusic.netty.execution.bug;0203importjava.net.InetSocketAddress;04importjava.util.concurrent.Executors;05importjava.util.concurrent.TimeUnit;0607importorg.jboss.netty.bootstrap.ClientBootstrap;08importorg.jboss.netty.bootstrap.ServerBootstrap;09importorg.jboss.netty.buffer.ChannelBuffer;10importorg.jboss.netty.buffer.ChannelBuffers;11importorg.jboss.netty.buffer.DirectChannelBufferFactory;12importorg.jboss.netty.channel.Channel;13importorg.jboss.netty.channel.ChannelFactory;14importorg.jboss.netty.channel.ChannelFuture;15importorg.jboss.netty.channel.ChannelHandlerContext;16importorg.jboss.netty.channel.ChannelPipeline;17importorg.jboss.netty.channel.ChannelStateEvent;18importorg.jboss.netty.channel.FixedReceiveBufferSizePredictor;19importorg.jboss.netty.channel.MessageEvent;20importorg.jboss.netty.channel.SimpleChannelHandler;21importorg.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;22importorg.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;23importorg.jboss.netty.channel.socket.nio.NioSocketChannelConfig;24importorg.jboss.netty.handler.execution.ExecutionHandler;2526/**27*Bug28*29*@author<ahref=mailto:jushi@taobao.com>jushi</a>30*@created2010-8-1731*32*/33publicclassBug{3435privatestaticclassClientHandlerextendsSimpleChannelHandler{36@Override37publicvoidmessageReceived(finalChannelHandlerContextctx,38finalMessageEvente)throwsException{39ChannelBufferbuf=(ChannelBuffer)e.getMessage();40while(buf.readable()){41System.out.print((char)buf.readByte());42}43System.out.println();44e.getChannel().close();45}46}4748privatestaticclassServerHandlerextendsSimpleChannelHandler{49@Override50publicvoidmessageReceived(finalChannelHandlerContextctx,51finalMessageEvente)throwsException{52TimeUnit.SECONDS.sleep(1);//delayforbufferrewrite.53e.getChannel().write(e.getMessage());54}5556@Override57publicvoidchannelConnected(ChannelHandlerContextctx,58ChannelStateEvente)throwsException{59System.out.println("connected:"+e.getChannel());60NioSocketChannelConfigconfig=61(NioSocketChannelConfig)e.getChannel().getConfig();62config.setBufferFactory(newDirectChannelBufferFactory());//zero-copy63config.setReceiveBufferSizePredictor(newFixedReceiveBufferSizePredictor(10));//fixbuffersizerequirement64}6566@Override67publicvoidchannelClosed(ChannelHandlerContextctx,ChannelStateEvente)throwsException{68System.out.println("closed:"+e.getChannel());69}70}7172staticvoidserve(){73finalChannelFactoryfactory=74newNioServerSocketChannelFactory(Executors.newSingleThreadExecutor(),75Executors.newSingleThreadExecutor(),761);77finalServerBootstrapbootstrap=newServerBootstrap(factory);7879ChannelPipelinepipeline=bootstrap.getPipeline();80pipeline.addLast("execution",81newExecutionHandler(Executors.newCachedThreadPool()));//asyncmessagereceived82pipeline.addLast("handler",newServerHandler());8384bootstrap.setOption("child.tcpNoDelay",true);85bootstrap.setOption("child.keepAlive",true);86finalChannelbind=bootstrap.bind(newInetSocketAddress(8080));8788Runtime.getRuntime().addShutdownHook(newThread(){89@Override90publicvoidrun(){91System.out.println("shutdown");92bind.close().awaitUninterruptibly();93bootstrap.releaseExternalResources();94}95});96}9798staticvoidconnect(){99finalChannelFactoryfactory=100newNioClientSocketChannelFactory(Executors.newSingleThreadExecutor(),101Executors.newSingleThreadExecutor(),1021);103104finalClientBootstrapbootstrap=newClientBootstrap(factory);105106ChannelPipelinepipeline=bootstrap.getPipeline();107pipeline.addLast("handler",newClientHandler());108109bootstrap.setOption("child.tcpNoDelay",true);110bootstrap.setOption("child.keepAlive",true);111ChannelFuturefuture=112bootstrap.connect(newInetSocketAddress("localhost",8080));113Channelchannel=future.awaitUninterruptibly().getChannel();114channel.write(ChannelBuffers.wrappedBuffer("++++++++++".getBytes()))115.awaitUninterruptibly();116channel.write(ChannelBuffers.wrappedBuffer("----------".getBytes()))117.awaitUninterruptibly();118channel.write(ChannelBuffers.wrappedBuffer("==========".getBytes()))119.awaitUninterruptibly();120channel.getCloseFuture().awaitUninterruptibly();121bootstrap.releaseExternalResources();122}123124publicstaticvoidmain(String[]args){125serve();126connect();127System.exit(0);128}129}
复制代码 C++编译的是本地码,优点是启动快,而且可以精确控制资源因此可以开发很高效的程序.缺点是编程麻烦,而且容易留下安全隐患.跨平台靠源代码在各个平台间分别编译(一处编写到处编译) |
|