7a774c6859d770491278b0501b6bf36485cfc475
[ldk-java] / c_sharp / src / org / ldk / structs / CustomMessageHandler.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 BOLT1-compliant messages.
10  */
11 public class CustomMessageHandler : CommonBase {
12         internal readonly bindings.LDKCustomMessageHandler bindings_instance;
13         internal CustomMessageHandler(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }
14         private CustomMessageHandler(bindings.LDKCustomMessageHandler arg, bindings.LDKCustomMessageReader CustomMessageReader) : base(bindings.LDKCustomMessageHandler_new(arg, CustomMessageReader)) {
15                 this.ptrs_to.AddLast(arg);
16                 this.ptrs_to.AddLast(CustomMessageReader);
17                 this.bindings_instance = arg;
18         }
19         ~CustomMessageHandler() {
20                 if (ptr != 0) { bindings.CustomMessageHandler_free(ptr); }
21         }
22
23         public interface CustomMessageHandlerInterface {
24                 /**
25                  * Called with the message type that was received and the buffer to be read.
26                  * Can return a `MessageHandlingError` if the message could not be handled.
27                  */
28                 Result_NoneLightningErrorZ handle_custom_message(Type _msg, byte[] _sender_node_id);
29                 /**
30                  * Gets the list of pending messages which were generated by the custom message
31                  * handler, clearing the list in the process. The first tuple element must
32                  * correspond to the intended recipients node ids. If no connection to one of the
33                  * specified node does not exist, the message is simply not sent to it.
34                  */
35                 TwoTuple_PublicKeyTypeZ[] get_and_clear_pending_msg();
36         }
37         private class LDKCustomMessageHandlerHolder { internal CustomMessageHandler held; }
38         private class LDKCustomMessageHandlerImpl : bindings.LDKCustomMessageHandler {
39                 internal LDKCustomMessageHandlerImpl(CustomMessageHandlerInterface arg, LDKCustomMessageHandlerHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }
40                 private CustomMessageHandlerInterface arg;
41                 private LDKCustomMessageHandlerHolder impl_holder;
42                 public long handle_custom_message(long _msg, byte[] _sender_node_id) {
43                         Type ret_hu_conv = new Type(null, _msg);
44                         if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
45                         Result_NoneLightningErrorZ ret = arg.handle_custom_message(ret_hu_conv, _sender_node_id);
46                                 GC.KeepAlive(arg);
47                         long result = ret == null ? 0 : ret.clone_ptr();
48                         return result;
49                 }
50                 public long[] get_and_clear_pending_msg() {
51                         TwoTuple_PublicKeyTypeZ[] ret = arg.get_and_clear_pending_msg();
52                                 GC.KeepAlive(arg);
53                         long[] result = ret != null ? InternalUtils.mapArray(ret, ret_conv_25 => ret_conv_25 == null ? 0 : ret_conv_25.clone_ptr()) : null;
54                         return result;
55                 }
56         }
57         public static CustomMessageHandler new_impl(CustomMessageHandlerInterface arg, CustomMessageReader.CustomMessageReaderInterface CustomMessageReader_impl) {
58                 LDKCustomMessageHandlerHolder impl_holder = new LDKCustomMessageHandlerHolder();
59                 impl_holder.held = new CustomMessageHandler(new LDKCustomMessageHandlerImpl(arg, impl_holder), CustomMessageReader.new_impl(CustomMessageReader_impl).bindings_instance);
60                 return impl_holder.held;
61         }
62
63         /**
64          * Gets the underlying CustomMessageReader.
65          */
66         public CustomMessageReader get_custom_message_reader() {
67                 CustomMessageReader res = new CustomMessageReader(null, bindings.LDKCustomMessageHandler_get_CustomMessageReader(this.ptr));
68                 this.ptrs_to.AddLast(res);
69                 return res;
70         }
71
72         /**
73          * Called with the message type that was received and the buffer to be read.
74          * Can return a `MessageHandlingError` if the message could not be handled.
75          */
76         public Result_NoneLightningErrorZ handle_custom_message(org.ldk.structs.Type msg, byte[] sender_node_id) {
77                 long ret = bindings.CustomMessageHandler_handle_custom_message(this.ptr, msg == null ? 0 : msg.ptr, InternalUtils.check_arr_len(sender_node_id, 33));
78                 GC.KeepAlive(this);
79                 GC.KeepAlive(msg);
80                 GC.KeepAlive(sender_node_id);
81                 if (ret >= 0 && ret <= 4096) { return null; }
82                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
83                 if (this != null) { this.ptrs_to.AddLast(msg); };
84                 return ret_hu_conv;
85         }
86
87         /**
88          * Gets the list of pending messages which were generated by the custom message
89          * handler, clearing the list in the process. The first tuple element must
90          * correspond to the intended recipients node ids. If no connection to one of the
91          * specified node does not exist, the message is simply not sent to it.
92          */
93         public TwoTuple_PublicKeyTypeZ[] get_and_clear_pending_msg() {
94                 long[] ret = bindings.CustomMessageHandler_get_and_clear_pending_msg(this.ptr);
95                 GC.KeepAlive(this);
96                 int ret_conv_25_len = ret.Length;
97                 TwoTuple_PublicKeyTypeZ[] ret_conv_25_arr = new TwoTuple_PublicKeyTypeZ[ret_conv_25_len];
98                 for (int z = 0; z < ret_conv_25_len; z++) {
99                         long ret_conv_25 = ret[z];
100                         TwoTuple_PublicKeyTypeZ ret_conv_25_hu_conv = new TwoTuple_PublicKeyTypeZ(null, ret_conv_25);
101                         if (ret_conv_25_hu_conv != null) { ret_conv_25_hu_conv.ptrs_to.AddLast(this); };
102                         ret_conv_25_arr[z] = ret_conv_25_hu_conv;
103                 }
104                 return ret_conv_25_arr;
105         }
106
107 }
108 } } }