这里可以发现Acceptor主要就是接受socket,然后把它注册到poller中,我们继续看看是如何注册的。
- /**6.类NioEndpoint
- * Registers a newly created socket with the poller.
- *
- * @param socket The newly created socket
- * @param socketWrapper The socket wrapper
- */
- public void register(final NioChannel socket, final NioSocketWrapper socketWrapper) {
- socketWrapper.interestOps(SelectionKey.OP_READ);//this is what OP_REGISTER turns into.
- PollerEvent r = null;
- if (eventCache != null) {
- r = eventCache.pop();
- }
- if (r == null) {
- r = new PollerEvent(socket, OP_REGISTER);
- } else {
- r.reset(socket, OP_REGISTER);
- }
- addEvent(r);
- }
- /** 7.类:PollerEvent implements Runnable
- public void run() {
- //省略部分代码
- socket.getIOChannel().register(socket.getSocketWrapper().getPoller().getSelector(), SelectionKey.OP_READ, socket.getSocketWrapper());
- }
这里发现最终就是采用NIO模型把其注册到通道中。(这里涉及NIO网络编程知识,不了解的同学可以传送这里)。那么注册完毕后,我们看看Poller做了什么事情。
- */
- /**8.类:NioEndPoint内部类 Poller implements Runnable
- **/
- @Override
- public void run() {
- // Loop until destroy() is called
- while (true) {
- //省略部分代码
- Iterator<SelectionKey> iterator =
- keyCount > 0 ? selector.selectedKeys().iterator() : null;
- // Walk through the collection of ready keys and dispatch
- // any active event.
- while (iterator != null && iterator.hasNext()) {
- SelectionKey sk = iterator.next();
- NioSocketWrapper socketWrapper = (NioSocketWrapper) sk.attachment();
- // Attachment may be null if another thread has called
- // cancelledKey()
- if (socketWrapper == null) {
- iterator.remove();
- } else {
- iterator.remove();
- //sock处理
- processKey(sk, socketWrapper);
- }
- }
- //省略部分代码
- }
这个就是通过selector把之前注册的事件取出来,从而完成了调用。
- //9.类: NioEndPoint内部类 Poller implements Runnable
- protected void processKey(SelectionKey sk, NioSocketWrapper socketWrapper) {
- //省略大部分代码
- processSocket(socketWrapper, SocketEvent.OPEN_WRITE, true)
-
- }
-
- //10.类:AbstractEndPoint
- public boolean processSocket(SocketWrapperBase<S> socketWrapper,
- SocketEvent event, boolean dispatch) {
- //省略部分代码
- Executor executor = getExecutor();
- if (dispatch && executor != null) {
- executor.execute(sc);
- } else {
- sc.run();
- }
-
- return true;
- }
- //11.类:SocketProcessorBase implements Runnable
- public final void run() {
- synchronized (socketWrapper) {
- // It is possible that processing may be triggered for read and
- // write at the same time. The sync above makes sure that processing
- // does not occur in parallel. The test below ensures that if the
- // first event to be processed results in the socket being closed,
- // the subsequent events are not processed.
- if (socketWrapper.isClosed()) {
- return;
- }
- doRun();
- }
- }
-
- //类:12.NioEndPoint extends AbstractJsseEndpoint<NioChannel,SocketChannel>
- protected void doRun() {
- //省略部分代码
- if (handshake == 0) {
- SocketState state = SocketState.OPEN;
- // Process the request from this socket
- if (event == null) {
- state = getHandler().process(socketWrapper, SocketEvent.OPEN_READ);
- } else {
- state = getHandler().process(socketWrapper, event);
- }
- if (state == SocketState.CLOSED) {
- poller.cancelledKey(key, socketWrapper);
- }
- }
- }
-
(编辑:好传媒网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|