7 namespace org { namespace ldk { namespace structs {
11 /** An implementation of SocketDescriptor */
12 public interface SocketDescriptorInterface {
13 /**Attempts to send some data from the given slice to the peer.
15 * Returns the amount of data which was sent, possibly 0 if the socket has since disconnected.
16 * Note that in the disconnected case, [`PeerManager::socket_disconnected`] must still be
17 * called and further write attempts may occur until that time.
19 * If the returned size is smaller than `data.len()`, a
20 * [`PeerManager::write_buffer_space_avail`] call must be made the next time more data can be
21 * written. Additionally, until a `send_data` event completes fully, no further
22 * [`PeerManager::read_event`] calls should be made for the same peer! Because this is to
23 * prevent denial-of-service issues, you should not read or buffer any data from the socket
26 * If a [`PeerManager::read_event`] call on this descriptor had previously returned true
27 * (indicating that read events should be paused to prevent DoS in the send buffer),
28 * `resume_read` may be set indicating that read events on this descriptor should resume. A
29 * `resume_read` of false carries no meaning, and should not cause any action.
31 long send_data(byte[] data, bool resume_read);
32 /**Disconnect the socket pointed to by this SocketDescriptor.
34 * You do *not* need to call [`PeerManager::socket_disconnected`] with this socket after this
35 * call (doing so is a noop).
37 void disconnect_socket();
38 /**Checks if two objects are equal given this object's this_arg pointer and another object.
40 bool eq(SocketDescriptor other_arg);
41 /**Calculate a succinct non-cryptographic hash for an object given its this_arg pointer.
42 * This is used, for example, for inclusion of this object in a hash map.
48 * Provides an object which can be used to send data to and which uniquely identifies a connection
49 * to a remote host. You will need to be able to generate multiple of these which meet Eq and
50 * implement Hash to meet the PeerManager API.
52 * For efficiency, [`Clone`] should be relatively cheap for this type.
54 * Two descriptors may compare equal (by [`cmp::Eq`] and [`hash::Hash`]) as long as the original
55 * has been disconnected, the [`PeerManager`] has been informed of the disconnection (either by it
56 * having triggered the disconnection or a call to [`PeerManager::socket_disconnected`]), and no
57 * further calls to the [`PeerManager`] related to the original socket occur. This allows you to
58 * use a file descriptor for your SocketDescriptor directly, however for simplicity you may wish
59 * to simply use another value which is guaranteed to be globally unique instead.
61 public class SocketDescriptor : CommonBase {
62 internal bindings.LDKSocketDescriptor bindings_instance;
63 internal long instance_idx;
65 internal SocketDescriptor(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }
67 if (ptr != 0) { bindings.SocketDescriptor_free(ptr); }
70 private class LDKSocketDescriptorHolder { internal SocketDescriptor held; }
71 private class LDKSocketDescriptorImpl : bindings.LDKSocketDescriptor {
72 internal LDKSocketDescriptorImpl(SocketDescriptorInterface arg, LDKSocketDescriptorHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }
73 private SocketDescriptorInterface arg;
74 private LDKSocketDescriptorHolder impl_holder;
75 public long send_data(long _data, bool _resume_read) {
76 byte[] _data_conv = InternalUtils.decodeUint8Array(_data);
77 long ret = arg.send_data(_data_conv, _resume_read);
81 public void disconnect_socket() {
82 arg.disconnect_socket();
85 public bool eq(long _other_arg) {
86 SocketDescriptor ret_hu_conv = new SocketDescriptor(null, _other_arg);
87 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
88 bool ret = arg.eq(ret_hu_conv);
93 long ret = arg.hash();
99 /** Creates a new instance of SocketDescriptor from a given implementation */
100 public static SocketDescriptor new_impl(SocketDescriptorInterface arg) {
101 LDKSocketDescriptorHolder impl_holder = new LDKSocketDescriptorHolder();
102 LDKSocketDescriptorImpl impl = new LDKSocketDescriptorImpl(arg, impl_holder);
103 long[] ptr_idx = bindings.LDKSocketDescriptor_new(impl);
105 impl_holder.held = new SocketDescriptor(null, ptr_idx[0]);
106 impl_holder.held.instance_idx = ptr_idx[1];
107 impl_holder.held.bindings_instance = impl;
108 return impl_holder.held;
112 * Attempts to send some data from the given slice to the peer.
114 * Returns the amount of data which was sent, possibly 0 if the socket has since disconnected.
115 * Note that in the disconnected case, [`PeerManager::socket_disconnected`] must still be
116 * called and further write attempts may occur until that time.
118 * If the returned size is smaller than `data.len()`, a
119 * [`PeerManager::write_buffer_space_avail`] call must be made the next time more data can be
120 * written. Additionally, until a `send_data` event completes fully, no further
121 * [`PeerManager::read_event`] calls should be made for the same peer! Because this is to
122 * prevent denial-of-service issues, you should not read or buffer any data from the socket
125 * If a [`PeerManager::read_event`] call on this descriptor had previously returned true
126 * (indicating that read events should be paused to prevent DoS in the send buffer),
127 * `resume_read` may be set indicating that read events on this descriptor should resume. A
128 * `resume_read` of false carries no meaning, and should not cause any action.
130 public long send_data(byte[] data, bool resume_read) {
131 long ret = bindings.SocketDescriptor_send_data(this.ptr, InternalUtils.encodeUint8Array(data), resume_read);
134 GC.KeepAlive(resume_read);
139 * Disconnect the socket pointed to by this SocketDescriptor.
141 * You do *not* need to call [`PeerManager::socket_disconnected`] with this socket after this
142 * call (doing so is a noop).
144 public void disconnect_socket() {
145 bindings.SocketDescriptor_disconnect_socket(this.ptr);
150 * Calculate a succinct non-cryptographic hash for an object given its this_arg pointer.
151 * This is used, for example, for inclusion of this object in a hash map.
154 long ret = bindings.SocketDescriptor_hash(this.ptr);
159 public override int GetHashCode() {
160 return (int)this.hash();
162 internal long clone_ptr() {
163 long ret = bindings.SocketDescriptor_clone_ptr(this.ptr);
169 * Creates a copy of a SocketDescriptor
171 public SocketDescriptor clone() {
172 long ret = bindings.SocketDescriptor_clone(this.ptr);
174 if (ret >= 0 && ret <= 4096) { return null; }
175 SocketDescriptor ret_hu_conv = new SocketDescriptor(null, ret);
176 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };