package org.voovan.network.aio;

import org.voovan.network.EventTrigger;
import org.voovan.tools.log.Logger;

import java.io.IOException;
import java.nio.channels.AsynchronousCloseException;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;

/**
 * Aio Accept 事件
 * 
 * @author helyho
 *
 * Voovan Framework.
 * WebSite: https://github.com/helyho/Voovan
 * Licence: Apache v2 License
 */
public class AcceptCompletionHandler implements CompletionHandler<AsynchronousSocketChannel, AioServerSocket>{

	public AcceptCompletionHandler(){
	}
	
	@Override
	public void completed(AsynchronousSocketChannel socketChannel, AioServerSocket serverSocket) {
		try {
			//接续接收 accept 请求
			serverSocket.catchAccept();
			
			AioSocket socket = new AioSocket(serverSocket,socketChannel);
			
			//触发 Accept 事件
			EventTrigger.fireAccept(socket.getSession());
			
			
		} catch (IOException e) {
			EventTrigger.fireExceptionThread(null, e);
		}
	}

	@Override
	public void failed(Throwable exc, AioServerSocket attachment) {
		if(!(exc instanceof AsynchronousCloseException)) {
			Logger.error(new Exception(exc));
		}
	}

}

@@ -698,62 +698,8 @@
 y %7B%0A
-%09%09%09//%E6%8E%A5%E7%BB%AD%E6%8E%A5%E6%94%B6 accept %E8%AF%B7%E6%B1%82%0A%09%09%09serverSocket.catchAccept();%0A%09%09%09
 %0A%09%09%09
@@ -808,16 +808,22 @@
 reAccept
+Thread
 (socket.
@@ -837,23 +837,68 @@
 ion());%0A
+%0A
 %09%09%09
-%0A%09%09%09
+//%E6%8E%A5%E7%BB%AD%E6%8E%A5%E6%94%B6 accept %E8%AF%B7%E6%B1%82%0A%09%09%09serverSocket.catchAccept();%0A
 %0A%09%09%7D cat

优化 AcceptCompletionHandler