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.uring;
17
18 import io.netty.channel.IoEvent;
19 import io.netty.channel.IoRegistration;
20
21 import java.nio.ByteBuffer;
22
23 /**
24 * {@link IoEvent} that will be produced as an result of a {@link IoUringIoOps}.
25 */
26 public final class IoUringIoEvent implements IoEvent {
27
28 private byte opcode;
29 private int res;
30 private int flags;
31 private short data;
32 private ByteBuffer extraCqeData;
33
34 /**
35 * Create a new instance
36 *
37 * @param res the result.
38 * @param flags the flags
39 * @param opcode the op code
40 * @param data the user data that was given as part of the submission.
41 */
42 public IoUringIoEvent(int res, int flags, byte opcode, short data) {
43 this.res = res;
44 this.flags = flags;
45 this.opcode = opcode;
46 this.data = data;
47 }
48
49 // Used internally to reduce object creation
50 void update(int res, int flags, byte opcode, short data, ByteBuffer extraCqeData) {
51 this.res = res;
52 this.flags = flags;
53 this.opcode = opcode;
54 this.data = data;
55 this.extraCqeData = extraCqeData;
56 }
57
58 /**
59 * Returns the result.
60 *
61 * @return the result
62 */
63 public int res() {
64 return res;
65 }
66
67 /**
68 * Returns the flags.
69 *
70 * @return flags
71 */
72 public int flags() {
73 return flags;
74 }
75
76 /**
77 * Returns the op code of the {@link IoUringIoOps}.
78 *
79 * @return opcode
80 */
81 public byte opcode() {
82 return opcode;
83 }
84
85 /**
86 * Returns the data that is passed as part of {@link IoUringIoOps}.
87 *
88 * @return data.
89 */
90 public short data() {
91 return data;
92 }
93
94 /**
95 * Returns the extra data for the CQE. This will only be non-null of the ring was setup with
96 * {@code IORING_SETUP_CQE32}. As this {@link ByteBuffer} maps into the shared completion queue its important
97 * to not hold any reference to it outside of the {@link IoUringIoHandle#handle(IoRegistration, IoEvent)} method.
98 *
99 * @return extra data for the CQE or {@code null}.
100 */
101 public ByteBuffer extraCqeData() {
102 return extraCqeData;
103 }
104
105 @Override
106 public String toString() {
107 return "IOUringIoEvent{" +
108 "opcode=" + opcode +
109 ", res=" + res +
110 ", flags=" + flags +
111 ", data=" + data +
112 '}';
113 }
114 }