View Javadoc
1   /*
2    * Copyright 2022 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.handler.codec.quic;
17  
18  /**
19   * A SSL related task that will be returned by {@link BoringSSL#SSL_getTask(long)}.
20   */
21  abstract class BoringSSLTask implements Runnable {
22      private final long ssl;
23      protected boolean didRun;
24  
25      // These fields are accessed via JNI.
26      private int returnValue;
27      private volatile boolean complete;
28  
29      protected BoringSSLTask(long ssl) {
30          // It is important that this constructor never throws. Be sure to not change this!
31          this.ssl = ssl;
32      }
33  
34      @Override
35      public final void run() {
36          if (!didRun) {
37              didRun = true;
38              runTask(ssl, (long ssl, int result) -> {
39                  returnValue = result;
40                  complete = true;
41              });
42          }
43      }
44  
45      /**
46       * Called once the task should be destroyed.
47       */
48      protected void destroy() {
49          // Noop
50      }
51  
52      /**
53       * Run the task and return the return value that should be passed back to OpenSSL.
54       */
55      protected abstract void runTask(long ssl, TaskCallback callback);
56  
57      interface TaskCallback {
58          void onResult(long ssl, int result);
59      }
60  }