Merge pull request #141 from TheBlueMatt/main
[ldk-java] / c_sharp / src / org / ldk / structs / CustomMessageReader.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  * Trait to be implemented by custom message (unrelated to the channel/gossip LN layers)
10  * decoders.
11  */
12 public class CustomMessageReader : CommonBase {
13         internal readonly bindings.LDKCustomMessageReader bindings_instance;
14         internal CustomMessageReader(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }
15         private CustomMessageReader(bindings.LDKCustomMessageReader arg) : base(bindings.LDKCustomMessageReader_new(arg)) {
16                 this.ptrs_to.AddLast(arg);
17                 this.bindings_instance = arg;
18         }
19         ~CustomMessageReader() {
20                 if (ptr != 0) { bindings.CustomMessageReader_free(ptr); }
21         }
22
23         public interface CustomMessageReaderInterface {
24                 /**
25                  * Decodes a custom message to `CustomMessageType`. If the given message type is known to the
26                  * implementation and the message could be decoded, must return `Ok(Some(message))`. If the
27                  * message type is unknown to the implementation, must return `Ok(None)`. If a decoding error
28                  * occur, must return `Err(DecodeError::X)` where `X` details the encountered error.
29                  */
30                 Result_COption_TypeZDecodeErrorZ read(short _message_type, byte[] _buffer);
31         }
32         private class LDKCustomMessageReaderHolder { internal CustomMessageReader held; }
33         private class LDKCustomMessageReaderImpl : bindings.LDKCustomMessageReader {
34                 internal LDKCustomMessageReaderImpl(CustomMessageReaderInterface arg, LDKCustomMessageReaderHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }
35                 private CustomMessageReaderInterface arg;
36                 private LDKCustomMessageReaderHolder impl_holder;
37                 public long read(short _message_type, byte[] _buffer) {
38                         Result_COption_TypeZDecodeErrorZ ret = arg.read(_message_type, _buffer);
39                                 GC.KeepAlive(arg);
40                         long result = ret == null ? 0 : ret.clone_ptr();
41                         return result;
42                 }
43         }
44         public static CustomMessageReader new_impl(CustomMessageReaderInterface arg) {
45                 LDKCustomMessageReaderHolder impl_holder = new LDKCustomMessageReaderHolder();
46                 impl_holder.held = new CustomMessageReader(new LDKCustomMessageReaderImpl(arg, impl_holder));
47                 return impl_holder.held;
48         }
49         /**
50          * Decodes a custom message to `CustomMessageType`. If the given message type is known to the
51          * implementation and the message could be decoded, must return `Ok(Some(message))`. If the
52          * message type is unknown to the implementation, must return `Ok(None)`. If a decoding error
53          * occur, must return `Err(DecodeError::X)` where `X` details the encountered error.
54          */
55         public Result_COption_TypeZDecodeErrorZ read(short message_type, byte[] buffer) {
56                 long ret = bindings.CustomMessageReader_read(this.ptr, message_type, buffer);
57                 GC.KeepAlive(this);
58                 GC.KeepAlive(message_type);
59                 GC.KeepAlive(buffer);
60                 if (ret >= 0 && ret <= 4096) { return null; }
61                 Result_COption_TypeZDecodeErrorZ ret_hu_conv = Result_COption_TypeZDecodeErrorZ.constr_from_ptr(ret);
62                 return ret_hu_conv;
63         }
64
65 }
66 } } }