NIO服務端主要建立過程

NIO服務端主要建立過程:
 
步驟一:打開ServerSocketChannel,用於監聽客戶端的鏈接,它是全部客戶端鏈接的副管道,示例代碼以下:     
ServerSocketChannel acceptorSvr = ServerSocketChannel.open();

步驟二:綁定監聽端口,設置鏈接爲非阻塞模式,示例代碼以下: java

acceptTorSvr.socket().bind(new InetScoketAddress(InetAddress.getByName(IP),port));
acceptorSvr.configureBlocking(false);

步驟三:建立Reactor線程,建立多路複用器並啓動線程,示例代碼以下: 網絡

Selector selector = Selector.open();
New Thread(new ReactorTask()).start();

步驟四:將ServerSocketChannel註冊到Reactor線程的多路複用器Selector上,監聽ACCEPT事件,示例代碼以下: 異步

SelectionKey key = acceptorSvr.register(selector,SelectionKey.OP_ACCEPT,ioHandler);

步驟五:多路複用器在線程run方法的無線循環體內輪詢準備就緒的key,示例代碼以下: socket

int num = selector.selector();
Set selectedKeys = selector.selectedKeys();
Iterator it = selectedKeys.iterator();
while(it.hasNext()){
     SelectionKey key = (SelectionKey)it.next();
     //....deal with i/o event ...
}

步驟六:多路複用器監聽到有新的客戶端接入,處理新的接入請求,完成TCP三次握手,創建物理鏈接,示例代碼以下: 線程

SocketChannel channel = svrChannel.accept();

步驟七:設置客戶端鏈路爲非阻塞模式,示例代碼以下: 指針

channel.configureBlocking(false);
channel.socket().setReuseAddress(true);
...

步驟八:將新接入的客戶端鏈接註冊到Reactor線程的多路複用器上,監聽讀操做,讀取客戶端發送的網絡消息,示例代碼以下: code

SelectionKey key = socketChannel.register(selector,SelectionKey.OP_READ,ioHandler);

步驟九:異步讀取客戶端請求消息到緩衝區,示例代碼以下: 對象

int readNumber = channel.read(receivedBuffer);

步驟十:對ByteBuffer進行編解碼,若是有半包消息指針reset,繼續讀取後續的報文,將解碼成功的消息封裝成Task,投遞到業務線程池中,進行業務邏輯編排,示例代碼以下: blog

Object message = null;
while(buffer.hasRemain()){
     byteBuffer.mark();
     Object message = decode(byteBuffer);
     if(message == null){
          byteBuffer.reset();
          break;
     }
     messageList.add(message);
}
if(!byteBuffer.hasRemain()){
     byteBuffer.clear();
}else{
     byteBuffer.compact();
}
if(messageList !=null & !messageList.isEmpty()){
     for(Object messageE : nessageList){
         handlerTask(messageE); 
     }
}
 
步驟十一:將POJO對象encode成ByteBuffer,調用SocketChannel的異步write接口,將消息異步發送給客戶端,示例代碼以下: 
socketChannel.write(buffer); 
相關文章
相關標籤/搜索