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      boolean contains(int value) {
87          return (this.value & value) != 0;
88      }
89  
90      /**
91       * Return a {@link EpollIoOps} which is a combination of the current and the given {@link EpollIoOps}.
92       *
93       * @param ops   the {@link EpollIoOps} that should be added to this one.
94       * @return      a {@link EpollIoOps}.
95       */
96      public EpollIoOps with(EpollIoOps ops) {
97          if (contains(ops)) {
98              return this;
99          }
100         return valueOf(value | ops.value());
101     }
102 
103     /**
104      * Return a {@link EpollIoOps} which is not a combination of the current and the given {@link EpollIoOps}.
105      *
106      * @param ops   the {@link EpollIoOps} that should be remove from this one.
107      * @return      a {@link EpollIoOps}.
108      */
109     public EpollIoOps without(EpollIoOps ops) {
110         if (!contains(ops)) {
111             return this;
112         }
113         return valueOf(value & ~ops.value());
114     }
115 
116     /**
117      * Returns the underlying value of the {@link EpollIoOps}.
118      *
119      * @return value.
120      */
121     public int value() {
122         return value;
123     }
124 
125     @Override
126     public boolean equals(Object o) {
127         if (this == o) {
128             return true;
129         }
130         if (o == null || getClass() != o.getClass()) {
131             return false;
132         }
133         EpollIoOps nioOps = (EpollIoOps) o;
134         return value == nioOps.value;
135     }
136 
137     @Override
138     public int hashCode() {
139         return value;
140     }
141 
142     /**
143      * Returns a {@link EpollIoOps} for the given value.
144      *
145      * @param   value the value
146      * @return  the {@link EpollIoOps}.
147      */
148     public static EpollIoOps valueOf(int value) {
149         return eventOf(value).ops();
150     }
151 
152     @Override
153     public String toString() {
154         return "EpollIoOps{" +
155                 "value=" + value +
156                 '}';
157     }
158 
159     static EpollIoEvent eventOf(int value) {
160         if (value > 0 && value < EVENTS.length) {
161             EpollIoEvent event = EVENTS[value];
162             if (event != null) {
163                 return event;
164             }
165         }
166         return new DefaultEpollIoEvent(new EpollIoOps(value));
167     }
168 
169     private static final class DefaultEpollIoEvent implements EpollIoEvent {
170         private final EpollIoOps ops;
171 
172         DefaultEpollIoEvent(EpollIoOps ops) {
173             this.ops = ops;
174         }
175 
176         @Override
177         public EpollIoOps ops() {
178             return ops;
179         }
180 
181         @Override
182         public boolean equals(Object o) {
183             if (this == o) {
184                 return true;
185             }
186             if (o == null || getClass() != o.getClass()) {
187                 return false;
188             }
189             EpollIoEvent event = (EpollIoEvent) o;
190             return event.ops().equals(ops());
191         }
192 
193         @Override
194         public int hashCode() {
195             return ops().hashCode();
196         }
197 
198         @Override
199         public String toString() {
200             return "DefaultEpollIoEvent{" +
201                     "ops=" + ops +
202                     '}';
203         }
204     }
205 }