View Javadoc
1   /*
2    * Copyright 2024 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    *   https://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 io.netty.channel.epoll;
17  
18  import io.netty.channel.IoOps;
19  
20  /**
21   * Implementation of {@link IoOps} that is used by {@link EpollIoHandler} and so for epoll based transports.
22   */
23  public final class EpollIoOps implements IoOps {
24  
25      static {
26          // Need to ensure we load the native lib before trying to use the values in Native to construct the different
27          // instances.
28          Epoll.ensureAvailability();
29      }
30  
31      /**
32       * Interested in IO events which tell that the underlying channel is writable again or a connection
33       * attempt can be continued.
34       */
35      public static final EpollIoOps EPOLLOUT = new EpollIoOps(Native.EPOLLOUT);
36  
37      /**
38       * Interested in IO events which should be handled by finish pending connect operations
39       */
40      public static final EpollIoOps EPOLLIN = new EpollIoOps(Native.EPOLLIN);
41  
42      /**
43       * Error condition happened on the associated file descriptor.
44       */
45      public static final EpollIoOps EPOLLERR = new EpollIoOps(Native.EPOLLERR);
46  
47      /**
48       * Interested in IO events which should be handled by reading data.
49       */
50      public static final EpollIoOps EPOLLRDHUP = new EpollIoOps(Native.EPOLLRDHUP);
51  
52      public static final EpollIoOps EPOLLET = new EpollIoOps(Native.EPOLLET);
53  
54      // Just use an array to store often used values.
55      private static final EpollIoEvent[] EVENTS;
56  
57      static {
58          EpollIoOps all = new EpollIoOps(EPOLLOUT.value | EPOLLIN.value | EPOLLERR.value | EPOLLRDHUP.value);
59          EVENTS = new EpollIoEvent[all.value + 1];
60          addToArray(EVENTS, EPOLLOUT);
61          addToArray(EVENTS, EPOLLIN);
62          addToArray(EVENTS, EPOLLERR);
63          addToArray(EVENTS, EPOLLRDHUP);
64          addToArray(EVENTS, all);
65      }
66  
67      private static void addToArray(EpollIoEvent[] array, EpollIoOps ops) {
68          array[ops.value] = new DefaultEpollIoEvent(ops);
69      }
70  
71      final int value;
72  
73      private EpollIoOps(int value) {
74          this.value = value;
75      }
76  
77      /**
78       * Returns {@code true} if this {@link EpollIoOps} is a combination of the given {@link EpollIoOps}.
79       * @param ops   the ops.
80       * @return      {@code true} if a combination of the given.
81       */
82      public boolean contains(EpollIoOps ops) {
83          return (value & ops.value) != 0;
84      }
85  
86      /**
87       * Return a {@link EpollIoOps} which is a combination of the current and the given {@link EpollIoOps}.
88       *
89       * @param ops   the {@link EpollIoOps} that should be added to this one.
90       * @return      a {@link EpollIoOps}.
91       */
92      public EpollIoOps with(EpollIoOps ops) {
93          if (contains(ops)) {
94              return this;
95          }
96          return valueOf(value | ops.value());
97      }
98  
99      /**
100      * Return a {@link EpollIoOps} which is not a combination of the current and the given {@link EpollIoOps}.
101      *
102      * @param ops   the {@link EpollIoOps} that should be remove from this one.
103      * @return      a {@link EpollIoOps}.
104      */
105     public EpollIoOps without(EpollIoOps ops) {
106         if (!contains(ops)) {
107             return this;
108         }
109         return valueOf(value & ~ops.value());
110     }
111 
112     /**
113      * Returns the underlying value of the {@link EpollIoOps}.
114      *
115      * @return value.
116      */
117     public int value() {
118         return value;
119     }
120 
121     @Override
122     public boolean equals(Object o) {
123         if (this == o) {
124             return true;
125         }
126         if (o == null || getClass() != o.getClass()) {
127             return false;
128         }
129         EpollIoOps nioOps = (EpollIoOps) o;
130         return value == nioOps.value;
131     }
132 
133     @Override
134     public int hashCode() {
135         return value;
136     }
137 
138     /**
139      * Returns a {@link EpollIoOps} for the given value.
140      *
141      * @param   value the value
142      * @return  the {@link EpollIoOps}.
143      */
144     public static EpollIoOps valueOf(int value) {
145         return eventOf(value).ops();
146     }
147 
148     @Override
149     public String toString() {
150         return "EpollIoOps{" +
151                 "value=" + value +
152                 '}';
153     }
154 
155     static EpollIoEvent eventOf(int value) {
156         if (value > 0 && value < EVENTS.length) {
157             EpollIoEvent event = EVENTS[value];
158             if (event != null) {
159                 return event;
160             }
161         }
162         return new DefaultEpollIoEvent(new EpollIoOps(value));
163     }
164 
165     private static final class DefaultEpollIoEvent implements EpollIoEvent {
166         private final EpollIoOps ops;
167 
168         DefaultEpollIoEvent(EpollIoOps ops) {
169             this.ops = ops;
170         }
171 
172         @Override
173         public EpollIoOps ops() {
174             return ops;
175         }
176 
177         @Override
178         public boolean equals(Object o) {
179             if (this == o) {
180                 return true;
181             }
182             if (o == null || getClass() != o.getClass()) {
183                 return false;
184             }
185             EpollIoEvent event = (EpollIoEvent) o;
186             return event.ops().equals(ops());
187         }
188 
189         @Override
190         public int hashCode() {
191             return ops().hashCode();
192         }
193 
194         @Override
195         public String toString() {
196             return "DefaultEpollIoEvent{" +
197                     "ops=" + ops +
198                     '}';
199         }
200     }
201 }