1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
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 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
23 public class SocketDescriptor extends CommonBase {
24 final bindings.LDKSocketDescriptor bindings_instance;
25 SocketDescriptor(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
26 private SocketDescriptor(bindings.LDKSocketDescriptor arg) {
27 super(bindings.LDKSocketDescriptor_new(arg));
28 this.ptrs_to.add(arg);
29 this.bindings_instance = arg;
31 @Override @SuppressWarnings("deprecation")
32 protected void finalize() throws Throwable {
33 if (ptr != 0) { bindings.SocketDescriptor_free(ptr); } super.finalize();
36 public static interface SocketDescriptorInterface {
38 * Attempts to send some data from the given slice to the peer.
40 * Returns the amount of data which was sent, possibly 0 if the socket has since disconnected.
41 * Note that in the disconnected case, [`PeerManager::socket_disconnected`] must still be
42 * called and further write attempts may occur until that time.
44 * If the returned size is smaller than `data.len()`, a
45 * [`PeerManager::write_buffer_space_avail`] call must be made the next time more data can be
46 * written. Additionally, until a `send_data` event completes fully, no further
47 * [`PeerManager::read_event`] calls should be made for the same peer! Because this is to
48 * prevent denial-of-service issues, you should not read or buffer any data from the socket
51 * If a [`PeerManager::read_event`] call on this descriptor had previously returned true
52 * (indicating that read events should be paused to prevent DoS in the send buffer),
53 * `resume_read` may be set indicating that read events on this descriptor should resume. A
54 * `resume_read` of false carries no meaning, and should not cause any action.
56 long send_data(byte[] data, boolean resume_read);
58 * Disconnect the socket pointed to by this SocketDescriptor.
60 * You do *not* need to call [`PeerManager::socket_disconnected`] with this socket after this
61 * call (doing so is a noop).
63 void disconnect_socket();
65 * Checks if two objects are equal given this object's this_arg pointer and another object.
67 boolean eq(SocketDescriptor other_arg);
69 * Calculate a succinct non-cryptographic hash for an object given its this_arg pointer.
70 * This is used, for example, for inclusion of this object in a hash map.
74 private static class LDKSocketDescriptorHolder { SocketDescriptor held; }
75 public static SocketDescriptor new_impl(SocketDescriptorInterface arg) {
76 final LDKSocketDescriptorHolder impl_holder = new LDKSocketDescriptorHolder();
77 impl_holder.held = new SocketDescriptor(new bindings.LDKSocketDescriptor() {
78 @Override public long send_data(byte[] data, boolean resume_read) {
79 long ret = arg.send_data(data, resume_read);
82 @Override public void disconnect_socket() {
83 arg.disconnect_socket();
85 @Override public boolean eq(long other_arg) {
86 SocketDescriptor ret_hu_conv = new SocketDescriptor(null, other_arg);
87 ret_hu_conv.ptrs_to.add(this);
88 boolean ret = arg.eq(ret_hu_conv);
91 @Override public long hash() {
92 long ret = arg.hash();
96 return impl_holder.held;
99 * Attempts to send some data from the given slice to the peer.
101 * Returns the amount of data which was sent, possibly 0 if the socket has since disconnected.
102 * Note that in the disconnected case, [`PeerManager::socket_disconnected`] must still be
103 * called and further write attempts may occur until that time.
105 * If the returned size is smaller than `data.len()`, a
106 * [`PeerManager::write_buffer_space_avail`] call must be made the next time more data can be
107 * written. Additionally, until a `send_data` event completes fully, no further
108 * [`PeerManager::read_event`] calls should be made for the same peer! Because this is to
109 * prevent denial-of-service issues, you should not read or buffer any data from the socket
112 * If a [`PeerManager::read_event`] call on this descriptor had previously returned true
113 * (indicating that read events should be paused to prevent DoS in the send buffer),
114 * `resume_read` may be set indicating that read events on this descriptor should resume. A
115 * `resume_read` of false carries no meaning, and should not cause any action.
117 public long send_data(byte[] data, boolean resume_read) {
118 long ret = bindings.SocketDescriptor_send_data(this.ptr, data, resume_read);
123 * Disconnect the socket pointed to by this SocketDescriptor.
125 * You do *not* need to call [`PeerManager::socket_disconnected`] with this socket after this
126 * call (doing so is a noop).
128 public void disconnect_socket() {
129 bindings.SocketDescriptor_disconnect_socket(this.ptr);
133 * Calculate a succinct non-cryptographic hash for an object given its this_arg pointer.
134 * This is used, for example, for inclusion of this object in a hash map.
137 long ret = bindings.SocketDescriptor_hash(this.ptr);
142 * Creates a copy of a SocketDescriptor
144 public SocketDescriptor clone() {
145 long ret = bindings.SocketDescriptor_clone(this.ptr);
146 SocketDescriptor ret_hu_conv = new SocketDescriptor(null, ret);
147 ret_hu_conv.ptrs_to.add(this);