1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
11 * Provides an object which can be used to send data to and which uniquely identifies a connection
12 * to a remote host. You will need to be able to generate multiple of these which meet Eq and
13 * implement Hash to meet the PeerManager API.
15 * For efficiency, Clone should be relatively cheap for this type.
17 * Two descriptors may compare equal (by [`cmp::Eq`] and [`hash::Hash`]) as long as the original
18 * has been disconnected, the [`PeerManager`] has been informed of the disconnection (either by it
19 * having triggered the disconnection or a call to [`PeerManager::socket_disconnected`]), and no
20 * further calls to the [`PeerManager`] related to the original socket occur. This allows you to
21 * use a file descriptor for your SocketDescriptor directly, however for simplicity you may wish
22 * to simply use another value which is guaranteed to be globally unique instead.
24 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
25 public class SocketDescriptor extends CommonBase {
26 final bindings.LDKSocketDescriptor bindings_instance;
27 SocketDescriptor(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
28 private SocketDescriptor(bindings.LDKSocketDescriptor arg) {
29 super(bindings.LDKSocketDescriptor_new(arg));
30 this.ptrs_to.add(arg);
31 this.bindings_instance = arg;
33 @Override @SuppressWarnings("deprecation")
34 protected void finalize() throws Throwable {
35 if (ptr != 0) { bindings.SocketDescriptor_free(ptr); } super.finalize();
38 public static interface SocketDescriptorInterface {
40 * Attempts to send some data from the given slice to the peer.
42 * Returns the amount of data which was sent, possibly 0 if the socket has since disconnected.
43 * Note that in the disconnected case, [`PeerManager::socket_disconnected`] must still be
44 * called and further write attempts may occur until that time.
46 * If the returned size is smaller than `data.len()`, a
47 * [`PeerManager::write_buffer_space_avail`] call must be made the next time more data can be
48 * written. Additionally, until a `send_data` event completes fully, no further
49 * [`PeerManager::read_event`] calls should be made for the same peer! Because this is to
50 * prevent denial-of-service issues, you should not read or buffer any data from the socket
53 * If a [`PeerManager::read_event`] call on this descriptor had previously returned true
54 * (indicating that read events should be paused to prevent DoS in the send buffer),
55 * `resume_read` may be set indicating that read events on this descriptor should resume. A
56 * `resume_read` of false carries no meaning, and should not cause any action.
58 long send_data(byte[] data, boolean resume_read);
60 * Disconnect the socket pointed to by this SocketDescriptor.
62 * You do *not* need to call [`PeerManager::socket_disconnected`] with this socket after this
63 * call (doing so is a noop).
65 void disconnect_socket();
67 * Checks if two objects are equal given this object's this_arg pointer and another object.
69 boolean eq(SocketDescriptor other_arg);
71 * Calculate a succinct non-cryptographic hash for an object given its this_arg pointer.
72 * This is used, for example, for inclusion of this object in a hash map.
76 private static class LDKSocketDescriptorHolder { SocketDescriptor held; }
77 public static SocketDescriptor new_impl(SocketDescriptorInterface arg) {
78 final LDKSocketDescriptorHolder impl_holder = new LDKSocketDescriptorHolder();
79 impl_holder.held = new SocketDescriptor(new bindings.LDKSocketDescriptor() {
80 @Override public long send_data(byte[] data, boolean resume_read) {
81 long ret = arg.send_data(data, resume_read);
82 Reference.reachabilityFence(arg);
85 @Override public void disconnect_socket() {
86 arg.disconnect_socket();
87 Reference.reachabilityFence(arg);
89 @Override public boolean eq(long other_arg) {
90 SocketDescriptor ret_hu_conv = new SocketDescriptor(null, other_arg);
91 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
92 boolean ret = arg.eq(ret_hu_conv);
93 Reference.reachabilityFence(arg);
96 @Override public long hash() {
97 long ret = arg.hash();
98 Reference.reachabilityFence(arg);
102 return impl_holder.held;
105 * Attempts to send some data from the given slice to the peer.
107 * Returns the amount of data which was sent, possibly 0 if the socket has since disconnected.
108 * Note that in the disconnected case, [`PeerManager::socket_disconnected`] must still be
109 * called and further write attempts may occur until that time.
111 * If the returned size is smaller than `data.len()`, a
112 * [`PeerManager::write_buffer_space_avail`] call must be made the next time more data can be
113 * written. Additionally, until a `send_data` event completes fully, no further
114 * [`PeerManager::read_event`] calls should be made for the same peer! Because this is to
115 * prevent denial-of-service issues, you should not read or buffer any data from the socket
118 * If a [`PeerManager::read_event`] call on this descriptor had previously returned true
119 * (indicating that read events should be paused to prevent DoS in the send buffer),
120 * `resume_read` may be set indicating that read events on this descriptor should resume. A
121 * `resume_read` of false carries no meaning, and should not cause any action.
123 public long send_data(byte[] data, boolean resume_read) {
124 long ret = bindings.SocketDescriptor_send_data(this.ptr, data, resume_read);
125 Reference.reachabilityFence(this);
126 Reference.reachabilityFence(data);
127 Reference.reachabilityFence(resume_read);
132 * Disconnect the socket pointed to by this SocketDescriptor.
134 * You do *not* need to call [`PeerManager::socket_disconnected`] with this socket after this
135 * call (doing so is a noop).
137 public void disconnect_socket() {
138 bindings.SocketDescriptor_disconnect_socket(this.ptr);
139 Reference.reachabilityFence(this);
143 * Calculate a succinct non-cryptographic hash for an object given its this_arg pointer.
144 * This is used, for example, for inclusion of this object in a hash map.
147 long ret = bindings.SocketDescriptor_hash(this.ptr);
148 Reference.reachabilityFence(this);
152 @Override public int hashCode() {
153 return (int)this.hash();
156 long ret = bindings.SocketDescriptor_clone_ptr(this.ptr);
157 Reference.reachabilityFence(this);
162 * Creates a copy of a SocketDescriptor
164 public SocketDescriptor clone() {
165 long ret = bindings.SocketDescriptor_clone(this.ptr);
166 Reference.reachabilityFence(this);
167 if (ret >= 0 && ret <= 4096) { return null; }
168 SocketDescriptor ret_hu_conv = new SocketDescriptor(null, ret);
169 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };