FreeCMS商业版

功能强大、开源、代码通俗易懂、注释详细、面向二次开发友好!

承接网站建设、软件开发、微信开发项目,价格从优,欢迎咨询合作!

FreeCMS商业版V2.8已发布!购买请联系客服QQ 124812878 查看更新日志



在线演示 立即购买 下载免费版 下载商业版用户手册
客服QQ: 124812878 联系电话: 18339991503 Email: freeteam@foxmail.com

手写 reactor( netty reactor 模型) - Ctrl+C&Ctrl+V - ITeye博客

加入收藏夹】     【打印】     【关闭】 来源: 日期:2018-03-07 22:00:04 点击量: 收藏

 

public class Dispacther implements Runnable{

	private String host = "127.0.0.1";
	private int port = 8080;
	
	public final Selector selector;  
    public final ServerSocketChannel serverSocketChannel;
	
    public Dispacther() throws IOException {
    	selector=Selector.open(); 
    	serverSocketChannel=ServerSocketChannel.open();  
    	InetSocketAddress inetSocketAddress=new InetSocketAddress(this.host,this.port);  
        serverSocketChannel.socket().bind(inetSocketAddress);  
        serverSocketChannel.configureBlocking(false);
        
        SelectionKey selectionKey=serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        
        selectionKey.attach(new Acceptor(this));
	}
    
	
	@Override
	public void run() { 
		try {  
            while(!Thread.interrupted()){  
                selector.select();  
                Set<SelectionKey> selectionKeys= selector.selectedKeys();  
                Iterator<SelectionKey> it=selectionKeys.iterator();   
                while(it.hasNext()){   
                    SelectionKey selectionKey=it.next();  
                    dispatch(selectionKey);  
                    selectionKeys.clear();  
                }  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        } 
	} 

	 void dispatch(SelectionKey key) {  
	        Runnable r = (Runnable)(key.attachment());    
	        if (r != null){    
	            r.run();  
	        }    
	    }   
}

 

 

 

public class Acceptor implements Runnable{

	private Dispacther dispacther;
	
	
	public Acceptor(Dispacther dispacther) {
		this.dispacther = dispacther;
	}


	@Override
	public void run() { 
		 try {  
	            SocketChannel socketChannel=dispacther.serverSocketChannel.accept();  
	            if(socketChannel!=null) 
	                new SocketHandler(dispacther.selector, socketChannel);  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	}

	 

}

 

 

public class SocketHandler implements Runnable {

   private SocketChannel socketChannel;  
   private Charset charset = Charset.forName("UTF-8");
   private Selector selector;
   
   public SocketHandler(Selector selector,SocketChannel socketChannel) throws IOException{  
        this.socketChannel=socketChannel;  
        this.selector = selector;
        socketChannel.configureBlocking(false);   
        SelectionKey selectionKey=socketChannel.register(selector, 0);  
  
        selectionKey.attach(this);     
        selectionKey.interestOps(SelectionKey.OP_READ);    
        selector.wakeup();  
    }  

	@Override
	public void run() { 
		  try { 	
			    ByteBuffer buff = ByteBuffer.allocate(1024);
				String content = "";
				while (socketChannel.read(buff) > 0) {
					socketChannel.read(buff);
					buff.flip();
					content += charset.decode(buff);
				}
				if(!"".equals(content)){
					System.out.println(  " content : " + content);
					for (SelectionKey key : this.selector.keys()) {
						Channel targetChannel = key.channel();
						if (targetChannel instanceof SocketChannel) {
							SocketChannel dest = (SocketChannel) targetChannel;
							dest.write(charset.encode(content));
							dest.close();
						}
					}
				}
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	}

	 

	 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者 

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(支持支付宝和微信 以及扣扣群),没钱捧个人场,谢谢各位。

 

个人主页http:http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/



 
 
 谢谢您的赞助,我会做的更好!


上一条


下一条

相关新闻
自定义表单

本例固定链接: 分享到:
更多