1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
11 * Trait to be implemented by custom message (unrelated to the channel/gossip LN layers)
14 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
15 public class CustomMessageReader extends CommonBase {
16 final bindings.LDKCustomMessageReader bindings_instance;
17 CustomMessageReader(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
18 private CustomMessageReader(bindings.LDKCustomMessageReader arg) {
19 super(bindings.LDKCustomMessageReader_new(arg));
20 this.ptrs_to.add(arg);
21 this.bindings_instance = arg;
23 @Override @SuppressWarnings("deprecation")
24 protected void finalize() throws Throwable {
25 if (ptr != 0) { bindings.CustomMessageReader_free(ptr); } super.finalize();
28 * Destroys the object, freeing associated resources. After this call, any access
29 * to this object may result in a SEGFAULT or worse.
31 * You should generally NEVER call this method. You should let the garbage collector
32 * do this for you when it finalizes objects. However, it may be useful for types
33 * which represent locks and should be closed immediately to avoid holding locks
36 public void destroy() {
37 if (ptr != 0) { bindings.CustomMessageReader_free(ptr); }
40 public static interface CustomMessageReaderInterface {
42 * Decodes a custom message to `CustomMessageType`. If the given message type is known to the
43 * implementation and the message could be decoded, must return `Ok(Some(message))`. If the
44 * message type is unknown to the implementation, must return `Ok(None)`. If a decoding error
45 * occur, must return `Err(DecodeError::X)` where `X` details the encountered error.
47 Result_COption_TypeZDecodeErrorZ read(short message_type, byte[] buffer);
49 private static class LDKCustomMessageReaderHolder { CustomMessageReader held; }
50 public static CustomMessageReader new_impl(CustomMessageReaderInterface arg) {
51 final LDKCustomMessageReaderHolder impl_holder = new LDKCustomMessageReaderHolder();
52 impl_holder.held = new CustomMessageReader(new bindings.LDKCustomMessageReader() {
53 @Override public long read(short message_type, byte[] buffer) {
54 Result_COption_TypeZDecodeErrorZ ret = arg.read(message_type, buffer);
55 Reference.reachabilityFence(arg);
56 long result = ret == null ? 0 : ret.clone_ptr();
60 return impl_holder.held;
63 * Decodes a custom message to `CustomMessageType`. If the given message type is known to the
64 * implementation and the message could be decoded, must return `Ok(Some(message))`. If the
65 * message type is unknown to the implementation, must return `Ok(None)`. If a decoding error
66 * occur, must return `Err(DecodeError::X)` where `X` details the encountered error.
68 public Result_COption_TypeZDecodeErrorZ read(short message_type, byte[] buffer) {
69 long ret = bindings.CustomMessageReader_read(this.ptr, message_type, buffer);
70 Reference.reachabilityFence(this);
71 Reference.reachabilityFence(message_type);
72 Reference.reachabilityFence(buffer);
73 if (ret >= 0 && ret <= 4096) { return null; }
74 Result_COption_TypeZDecodeErrorZ ret_hu_conv = Result_COption_TypeZDecodeErrorZ.constr_from_ptr(ret);