1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import javax.annotation.Nullable;
10 * Provides an object which can be used to send data to and which uniquely identifies a connection
11 * to a remote host. You will need to be able to generate multiple of these which meet Eq and
12 * implement Hash to meet the PeerManager API.
14 * For efficiency, Clone should be relatively cheap for this type.
16 * Two descriptors may compare equal (by [`cmp::Eq`] and [`hash::Hash`]) as long as the original
17 * has been disconnected, the [`PeerManager`] has been informed of the disconnection (either by it
18 * having triggered the disconnection or a call to [`PeerManager::socket_disconnected`]), and no
19 * further calls to the [`PeerManager`] related to the original socket occur. This allows you to
20 * use a file descriptor for your SocketDescriptor directly, however for simplicity you may wish
21 * to simply use another value which is guaranteed to be globally unique instead.
23 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
24 public class SocketDescriptor extends CommonBase {
25 final bindings.LDKSocketDescriptor bindings_instance;
26 SocketDescriptor(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
27 private SocketDescriptor(bindings.LDKSocketDescriptor arg) {
28 super(bindings.LDKSocketDescriptor_new(arg));
29 this.ptrs_to.add(arg);
30 this.bindings_instance = arg;
32 @Override @SuppressWarnings("deprecation")
33 protected void finalize() throws Throwable {
34 if (ptr != 0) { bindings.SocketDescriptor_free(ptr); } super.finalize();
37 public static interface SocketDescriptorInterface {
39 * Attempts to send some data from the given slice to the peer.
41 * Returns the amount of data which was sent, possibly 0 if the socket has since disconnected.
42 * Note that in the disconnected case, [`PeerManager::socket_disconnected`] must still be
43 * called and further write attempts may occur until that time.
45 * If the returned size is smaller than `data.len()`, a
46 * [`PeerManager::write_buffer_space_avail`] call must be made the next time more data can be
47 * written. Additionally, until a `send_data` event completes fully, no further
48 * [`PeerManager::read_event`] calls should be made for the same peer! Because this is to
49 * prevent denial-of-service issues, you should not read or buffer any data from the socket
52 * If a [`PeerManager::read_event`] call on this descriptor had previously returned true
53 * (indicating that read events should be paused to prevent DoS in the send buffer),
54 * `resume_read` may be set indicating that read events on this descriptor should resume. A
55 * `resume_read` of false carries no meaning, and should not cause any action.
57 long send_data(byte[] data, boolean resume_read);
59 * Disconnect the socket pointed to by this SocketDescriptor.
61 * You do *not* need to call [`PeerManager::socket_disconnected`] with this socket after this
62 * call (doing so is a noop).
64 void disconnect_socket();
66 * Checks if two objects are equal given this object's this_arg pointer and another object.
68 boolean eq(SocketDescriptor other_arg);
70 * Calculate a succinct non-cryptographic hash for an object given its this_arg pointer.
71 * This is used, for example, for inclusion of this object in a hash map.
75 private static class LDKSocketDescriptorHolder { SocketDescriptor held; }
76 public static SocketDescriptor new_impl(SocketDescriptorInterface arg) {
77 final LDKSocketDescriptorHolder impl_holder = new LDKSocketDescriptorHolder();
78 impl_holder.held = new SocketDescriptor(new bindings.LDKSocketDescriptor() {
79 @Override public long send_data(byte[] data, boolean resume_read) {
80 long ret = arg.send_data(data, resume_read);
83 @Override public void disconnect_socket() {
84 arg.disconnect_socket();
86 @Override public boolean eq(long other_arg) {
87 SocketDescriptor ret_hu_conv = new SocketDescriptor(null, other_arg);
88 ret_hu_conv.ptrs_to.add(this);
89 boolean ret = arg.eq(ret_hu_conv);
92 @Override public long hash() {
93 long ret = arg.hash();
97 return impl_holder.held;
100 * Attempts to send some data from the given slice to the peer.
102 * Returns the amount of data which was sent, possibly 0 if the socket has since disconnected.
103 * Note that in the disconnected case, [`PeerManager::socket_disconnected`] must still be
104 * called and further write attempts may occur until that time.
106 * If the returned size is smaller than `data.len()`, a
107 * [`PeerManager::write_buffer_space_avail`] call must be made the next time more data can be
108 * written. Additionally, until a `send_data` event completes fully, no further
109 * [`PeerManager::read_event`] calls should be made for the same peer! Because this is to
110 * prevent denial-of-service issues, you should not read or buffer any data from the socket
113 * If a [`PeerManager::read_event`] call on this descriptor had previously returned true
114 * (indicating that read events should be paused to prevent DoS in the send buffer),
115 * `resume_read` may be set indicating that read events on this descriptor should resume. A
116 * `resume_read` of false carries no meaning, and should not cause any action.
118 public long send_data(byte[] data, boolean resume_read) {
119 long ret = bindings.SocketDescriptor_send_data(this.ptr, data, resume_read);
124 * Disconnect the socket pointed to by this SocketDescriptor.
126 * You do *not* need to call [`PeerManager::socket_disconnected`] with this socket after this
127 * call (doing so is a noop).
129 public void disconnect_socket() {
130 bindings.SocketDescriptor_disconnect_socket(this.ptr);
134 * Calculate a succinct non-cryptographic hash for an object given its this_arg pointer.
135 * This is used, for example, for inclusion of this object in a hash map.
138 long ret = bindings.SocketDescriptor_hash(this.ptr);
143 * Creates a copy of a SocketDescriptor
145 public SocketDescriptor clone() {
146 long ret = bindings.SocketDescriptor_clone(this.ptr);
147 if (ret < 1024) { return null; }
148 SocketDescriptor ret_hu_conv = new SocketDescriptor(null, ret);
149 ret_hu_conv.ptrs_to.add(this);