d3b989ff326e827a169a6f8c73a837e621056324
[ldk-java] / c_sharp / src / org / ldk / structs / CustomOnionMessageHandler.cs
1 using org.ldk.impl;
2 using org.ldk.enums;
3 using org.ldk.util;
4 using System;
5
6 namespace org { namespace ldk { namespace structs {
7
8 /**
9  * Handler for custom onion messages. If you are using [`SimpleArcOnionMessenger`],
10  * [`SimpleRefOnionMessenger`], or prefer to ignore inbound custom onion messages,
11  * [`IgnoringMessageHandler`] must be provided to [`OnionMessenger::new`]. Otherwise, a custom
12  * implementation of this trait must be provided, with [`CustomMessage`] specifying the supported
13  * message types.
14  * 
15  * See [`OnionMessenger`] for example usage.
16  * 
17  * [`IgnoringMessageHandler`]: crate::ln::peer_handler::IgnoringMessageHandler
18  * [`CustomMessage`]: Self::CustomMessage
19  */
20 public class CustomOnionMessageHandler : CommonBase {
21         internal readonly bindings.LDKCustomOnionMessageHandler bindings_instance;
22         internal CustomOnionMessageHandler(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }
23         private CustomOnionMessageHandler(bindings.LDKCustomOnionMessageHandler arg) : base(bindings.LDKCustomOnionMessageHandler_new(arg)) {
24                 this.ptrs_to.AddLast(arg);
25                 this.bindings_instance = arg;
26         }
27         ~CustomOnionMessageHandler() {
28                 if (ptr != 0) { bindings.CustomOnionMessageHandler_free(ptr); }
29         }
30
31         public interface CustomOnionMessageHandlerInterface {
32                 /**
33                  * Called with the custom message that was received.
34                  */
35                 void handle_custom_message(CustomOnionMessageContents _msg);
36                 /**
37                  * Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the
38                  * message type is unknown.
39                  */
40                 Result_COption_CustomOnionMessageContentsZDecodeErrorZ read_custom_message(long _message_type, byte[] _buffer);
41         }
42         private class LDKCustomOnionMessageHandlerHolder { internal CustomOnionMessageHandler held; }
43         private class LDKCustomOnionMessageHandlerImpl : bindings.LDKCustomOnionMessageHandler {
44                 internal LDKCustomOnionMessageHandlerImpl(CustomOnionMessageHandlerInterface arg, LDKCustomOnionMessageHandlerHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }
45                 private CustomOnionMessageHandlerInterface arg;
46                 private LDKCustomOnionMessageHandlerHolder impl_holder;
47                 public void handle_custom_message(long _msg) {
48                         CustomOnionMessageContents ret_hu_conv = new CustomOnionMessageContents(null, _msg);
49                         if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
50                         arg.handle_custom_message(ret_hu_conv);
51                                 GC.KeepAlive(arg);
52                 }
53                 public long read_custom_message(long _message_type, byte[] _buffer) {
54                         Result_COption_CustomOnionMessageContentsZDecodeErrorZ ret = arg.read_custom_message(_message_type, _buffer);
55                                 GC.KeepAlive(arg);
56                         long result = ret == null ? 0 : ret.clone_ptr();
57                         return result;
58                 }
59         }
60         public static CustomOnionMessageHandler new_impl(CustomOnionMessageHandlerInterface arg) {
61                 LDKCustomOnionMessageHandlerHolder impl_holder = new LDKCustomOnionMessageHandlerHolder();
62                 impl_holder.held = new CustomOnionMessageHandler(new LDKCustomOnionMessageHandlerImpl(arg, impl_holder));
63                 return impl_holder.held;
64         }
65         /**
66          * Called with the custom message that was received.
67          */
68         public void handle_custom_message(org.ldk.structs.CustomOnionMessageContents msg) {
69                 bindings.CustomOnionMessageHandler_handle_custom_message(this.ptr, msg == null ? 0 : msg.ptr);
70                 GC.KeepAlive(this);
71                 GC.KeepAlive(msg);
72                 if (this != null) { this.ptrs_to.AddLast(msg); };
73         }
74
75         /**
76          * Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the
77          * message type is unknown.
78          */
79         public Result_COption_CustomOnionMessageContentsZDecodeErrorZ read_custom_message(long message_type, byte[] buffer) {
80                 long ret = bindings.CustomOnionMessageHandler_read_custom_message(this.ptr, message_type, buffer);
81                 GC.KeepAlive(this);
82                 GC.KeepAlive(message_type);
83                 GC.KeepAlive(buffer);
84                 if (ret >= 0 && ret <= 4096) { return null; }
85                 Result_COption_CustomOnionMessageContentsZDecodeErrorZ ret_hu_conv = Result_COption_CustomOnionMessageContentsZDecodeErrorZ.constr_from_ptr(ret);
86                 return ret_hu_conv;
87         }
88
89 }
90 } } }