6 namespace org { namespace ldk { namespace structs {
9 * Provides an object which can be used to send data to and which uniquely identifies a connection
10 * to a remote host. You will need to be able to generate multiple of these which meet Eq and
11 * implement Hash to meet the PeerManager API.
13 * For efficiency, Clone should be relatively cheap for this type.
15 * Two descriptors may compare equal (by [`cmp::Eq`] and [`hash::Hash`]) as long as the original
16 * has been disconnected, the [`PeerManager`] has been informed of the disconnection (either by it
17 * having triggered the disconnection or a call to [`PeerManager::socket_disconnected`]), and no
18 * further calls to the [`PeerManager`] related to the original socket occur. This allows you to
19 * use a file descriptor for your SocketDescriptor directly, however for simplicity you may wish
20 * to simply use another value which is guaranteed to be globally unique instead.
22 public class SocketDescriptor : CommonBase {
23 internal readonly bindings.LDKSocketDescriptor bindings_instance;
24 internal SocketDescriptor(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }
25 private SocketDescriptor(bindings.LDKSocketDescriptor arg) : base(bindings.LDKSocketDescriptor_new(arg)) {
26 this.ptrs_to.AddLast(arg);
27 this.bindings_instance = arg;
30 if (ptr != 0) { bindings.SocketDescriptor_free(ptr); }
33 public interface SocketDescriptorInterface {
35 * Attempts to send some data from the given slice to the peer.
37 * Returns the amount of data which was sent, possibly 0 if the socket has since disconnected.
38 * Note that in the disconnected case, [`PeerManager::socket_disconnected`] must still be
39 * called and further write attempts may occur until that time.
41 * If the returned size is smaller than `data.len()`, a
42 * [`PeerManager::write_buffer_space_avail`] call must be made the next time more data can be
43 * written. Additionally, until a `send_data` event completes fully, no further
44 * [`PeerManager::read_event`] calls should be made for the same peer! Because this is to
45 * prevent denial-of-service issues, you should not read or buffer any data from the socket
48 * If a [`PeerManager::read_event`] call on this descriptor had previously returned true
49 * (indicating that read events should be paused to prevent DoS in the send buffer),
50 * `resume_read` may be set indicating that read events on this descriptor should resume. A
51 * `resume_read` of false carries no meaning, and should not cause any action.
53 long send_data(byte[] _data, bool _resume_read);
55 * Disconnect the socket pointed to by this SocketDescriptor.
57 * You do *not* need to call [`PeerManager::socket_disconnected`] with this socket after this
58 * call (doing so is a noop).
60 void disconnect_socket();
62 * Checks if two objects are equal given this object's this_arg pointer and another object.
64 bool eq(SocketDescriptor _other_arg);
66 * Calculate a succinct non-cryptographic hash for an object given its this_arg pointer.
67 * This is used, for example, for inclusion of this object in a hash map.
71 private class LDKSocketDescriptorHolder { internal SocketDescriptor held; }
72 private class LDKSocketDescriptorImpl : bindings.LDKSocketDescriptor {
73 internal LDKSocketDescriptorImpl(SocketDescriptorInterface arg, LDKSocketDescriptorHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }
74 private SocketDescriptorInterface arg;
75 private LDKSocketDescriptorHolder impl_holder;
76 public long send_data(byte[] _data, bool _resume_read) {
77 long ret = arg.send_data(_data, _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();
98 public static SocketDescriptor new_impl(SocketDescriptorInterface arg) {
99 LDKSocketDescriptorHolder impl_holder = new LDKSocketDescriptorHolder();
100 impl_holder.held = new SocketDescriptor(new LDKSocketDescriptorImpl(arg, impl_holder));
101 return impl_holder.held;
104 * Attempts to send some data from the given slice to the peer.
106 * Returns the amount of data which was sent, possibly 0 if the socket has since disconnected.
107 * Note that in the disconnected case, [`PeerManager::socket_disconnected`] must still be
108 * called and further write attempts may occur until that time.
110 * If the returned size is smaller than `data.len()`, a
111 * [`PeerManager::write_buffer_space_avail`] call must be made the next time more data can be
112 * written. Additionally, until a `send_data` event completes fully, no further
113 * [`PeerManager::read_event`] calls should be made for the same peer! Because this is to
114 * prevent denial-of-service issues, you should not read or buffer any data from the socket
117 * If a [`PeerManager::read_event`] call on this descriptor had previously returned true
118 * (indicating that read events should be paused to prevent DoS in the send buffer),
119 * `resume_read` may be set indicating that read events on this descriptor should resume. A
120 * `resume_read` of false carries no meaning, and should not cause any action.
122 public long send_data(byte[] data, bool resume_read) {
123 long ret = bindings.SocketDescriptor_send_data(this.ptr, data, resume_read);
126 GC.KeepAlive(resume_read);
131 * Disconnect the socket pointed to by this SocketDescriptor.
133 * You do *not* need to call [`PeerManager::socket_disconnected`] with this socket after this
134 * call (doing so is a noop).
136 public void disconnect_socket() {
137 bindings.SocketDescriptor_disconnect_socket(this.ptr);
142 * Calculate a succinct non-cryptographic hash for an object given its this_arg pointer.
143 * This is used, for example, for inclusion of this object in a hash map.
146 long ret = bindings.SocketDescriptor_hash(this.ptr);
151 public override int GetHashCode() {
152 return (int)this.hash();
154 internal long clone_ptr() {
155 long ret = bindings.SocketDescriptor_clone_ptr(this.ptr);
161 * Creates a copy of a SocketDescriptor
163 public SocketDescriptor clone() {
164 long ret = bindings.SocketDescriptor_clone(this.ptr);
166 if (ret >= 0 && ret <= 4096) { return null; }
167 SocketDescriptor ret_hu_conv = new SocketDescriptor(null, ret);
168 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };