查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.channel.socket.nio;
17  
18  import org.jboss.netty.channel.Channel;
19  import org.jboss.netty.channel.ChannelEvent;
20  import org.jboss.netty.channel.ChannelFuture;
21  import org.jboss.netty.channel.ChannelPipeline;
22  import org.jboss.netty.channel.ChannelState;
23  import org.jboss.netty.channel.ChannelStateEvent;
24  import org.jboss.netty.channel.MessageEvent;
25  
26  import java.net.SocketAddress;
27  
28  class NioServerSocketPipelineSink extends AbstractNioChannelSink {
29  
30      public void eventSunk(
31              ChannelPipeline pipeline, ChannelEvent e) throws Exception {
32          Channel channel = e.getChannel();
33          if (channel instanceof NioServerSocketChannel) {
34              handleServerSocket(e);
35          } else if (channel instanceof NioSocketChannel) {
36              handleAcceptedSocket(e);
37          }
38      }
39  
40      private static void handleServerSocket(ChannelEvent e) {
41          if (!(e instanceof ChannelStateEvent)) {
42              return;
43          }
44  
45          ChannelStateEvent event = (ChannelStateEvent) e;
46          NioServerSocketChannel channel =
47              (NioServerSocketChannel) event.getChannel();
48          ChannelFuture future = event.getFuture();
49          ChannelState state = event.getState();
50          Object value = event.getValue();
51  
52          switch (state) {
53          case OPEN:
54              if (Boolean.FALSE.equals(value)) {
55                  ((NioServerBoss) channel.boss).close(channel, future);
56              }
57              break;
58          case BOUND:
59              if (value != null) {
60                  ((NioServerBoss) channel.boss).bind(channel, future, (SocketAddress) value);
61              } else {
62                  ((NioServerBoss) channel.boss).close(channel, future);
63              }
64              break;
65          default:
66              break;
67          }
68      }
69  
70      private static void handleAcceptedSocket(ChannelEvent e) {
71          if (e instanceof ChannelStateEvent) {
72              ChannelStateEvent event = (ChannelStateEvent) e;
73              NioSocketChannel channel = (NioSocketChannel) event.getChannel();
74              ChannelFuture future = event.getFuture();
75              ChannelState state = event.getState();
76              Object value = event.getValue();
77  
78              switch (state) {
79              case OPEN:
80                  if (Boolean.FALSE.equals(value)) {
81                      channel.worker.close(channel, future);
82                  }
83                  break;
84              case BOUND:
85              case CONNECTED:
86                  if (value == null) {
87                      channel.worker.close(channel, future);
88                  }
89                  break;
90              case INTEREST_OPS:
91                  channel.worker.setInterestOps(channel, future, ((Integer) value).intValue());
92                  break;
93              }
94          } else if (e instanceof MessageEvent) {
95              MessageEvent event = (MessageEvent) e;
96              NioSocketChannel channel = (NioSocketChannel) event.getChannel();
97              boolean offered = channel.writeBufferQueue.offer(event);
98              assert offered;
99              channel.worker.writeFromUserCode(channel);
100         }
101     }
102 }