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);
84 @Override public void disconnect_socket() {
85 arg.disconnect_socket();
87 @Override public boolean eq(long other_arg) {
88 SocketDescriptor ret_hu_conv = new SocketDescriptor(null, other_arg);
89 ret_hu_conv.ptrs_to.add(this);
90 boolean ret = arg.eq(ret_hu_conv);
93 @Override public long hash() {
94 long ret = arg.hash();
98 return impl_holder.held;
101 * Attempts to send some data from the given slice to the peer.
103 * Returns the amount of data which was sent, possibly 0 if the socket has since disconnected.
104 * Note that in the disconnected case, [`PeerManager::socket_disconnected`] must still be
105 * called and further write attempts may occur until that time.
107 * If the returned size is smaller than `data.len()`, a
108 * [`PeerManager::write_buffer_space_avail`] call must be made the next time more data can be
109 * written. Additionally, until a `send_data` event completes fully, no further
110 * [`PeerManager::read_event`] calls should be made for the same peer! Because this is to
111 * prevent denial-of-service issues, you should not read or buffer any data from the socket
114 * If a [`PeerManager::read_event`] call on this descriptor had previously returned true
115 * (indicating that read events should be paused to prevent DoS in the send buffer),
116 * `resume_read` may be set indicating that read events on this descriptor should resume. A
117 * `resume_read` of false carries no meaning, and should not cause any action.
119 public long send_data(byte[] data, boolean resume_read) {
120 long ret = bindings.SocketDescriptor_send_data(this.ptr, data, resume_read);
121 Reference.reachabilityFence(this);
122 Reference.reachabilityFence(data);
123 Reference.reachabilityFence(resume_read);
128 * Disconnect the socket pointed to by this SocketDescriptor.
130 * You do *not* need to call [`PeerManager::socket_disconnected`] with this socket after this
131 * call (doing so is a noop).
133 public void disconnect_socket() {
134 bindings.SocketDescriptor_disconnect_socket(this.ptr);
135 Reference.reachabilityFence(this);
139 * Calculate a succinct non-cryptographic hash for an object given its this_arg pointer.
140 * This is used, for example, for inclusion of this object in a hash map.
143 long ret = bindings.SocketDescriptor_hash(this.ptr);
144 Reference.reachabilityFence(this);
148 @Override public int hashCode() {
149 return (int)this.hash();
152 long ret = bindings.SocketDescriptor_clone_ptr(this.ptr);
153 Reference.reachabilityFence(this);
158 * Creates a copy of a SocketDescriptor
160 public SocketDescriptor clone() {
161 long ret = bindings.SocketDescriptor_clone(this.ptr);
162 Reference.reachabilityFence(this);
163 if (ret >= 0 && ret <= 4096) { return null; }
164 SocketDescriptor ret_hu_conv = new SocketDescriptor(null, ret);
165 ret_hu_conv.ptrs_to.add(this);