欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Mina服务器端启动 -- 1

程序员文章站 2024-01-06 17:27:46
...
  SocketAcceptor acceptor = new NioSocketAcceptor();


 public NioSocketAcceptor() {
        
    	
    	super(new DefaultSocketSessionConfig(), NioProcessor.class);
        
        //初始化前面设置在AbstractIoService 中的  sessionConfig
        ((DefaultSocketSessionConfig) getSessionConfig()).init(this);
 }


  protected AbstractPollingIoAcceptor(IoSessionConfig sessionConfig,
            Class<? extends IoProcessor<T>> processorClass) {
    	// new SimpleIoProcessorPool 新建 processor -- 处理线程
        this(sessionConfig, null, new SimpleIoProcessorPool<T>(processorClass),
                true);
}



//绑定端口  
acceptor.bind(new InetSocketAddress(PORT));


AbstractIoAcceptor 的 public final void bind(Iterable<? extends SocketAddress> localAddresses) 方法中

boundAddresses.addAll(bindInternal(localAddressesCopy));

    protected final Set<SocketAddress> bindInternal(
            List<? extends SocketAddress> localAddresses) throws Exception {
        // Create a bind request as a Future operation. When the selector
        // have handled the registration, it will signal this future.
        AcceptorOperationFuture request = new AcceptorOperationFuture(
                localAddresses);

        // adds the Registration request to the queue for the Workers
        // to handle
        //添加请求到注册列表
        registerQueue.add(request);

        // creates the Acceptor instance and has the local
        // executor kick it off.
        //开始用线程监听通道
        startupAcceptor();
        
        // As we just started the acceptor, we have to unblock the select()
        // in order to process the bind request we just have added to the 
        // registerQueue.
        wakeup();
        
        // Now, we wait until this request is completed.
        request.awaitUninterruptibly();

        if (request.getException() != null) {
            throw request.getException();
        }

        // Update the local addresses.
        // setLocalAddresses() shouldn't be called from the worker thread
        // because of deadlock.
        Set<SocketAddress> newLocalAddresses = new HashSet<SocketAddress>();
        
        for (H handle:boundHandles.values()) {
            newLocalAddresses.add(localAddress(handle));
        }

        return newLocalAddresses;
    }


startupAcceptor 中启动了一个 Acceptor 启动Socket服务




上一篇:

下一篇: