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 * You probably want to just extend an int and put a file descriptor in a struct and implement
16 * send_data. Note that if you are using a higher-level net library that may call close() itself,
17 * be careful to ensure you don't have races whereby you might register a new connection with an
18 * fd which is the same as a previous one which has yet to be removed via
19 * PeerManager::socket_disconnected().
21 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
22 public class SocketDescriptor extends CommonBase {
23 final bindings.LDKSocketDescriptor bindings_instance;
24 SocketDescriptor(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
25 private SocketDescriptor(bindings.LDKSocketDescriptor arg) {
26 super(bindings.LDKSocketDescriptor_new(arg));
27 this.ptrs_to.add(arg);
28 this.bindings_instance = arg;
30 @Override @SuppressWarnings("deprecation")
31 protected void finalize() throws Throwable {
32 if (ptr != 0) { bindings.SocketDescriptor_free(ptr); } super.finalize();
35 public static interface SocketDescriptorInterface {
37 * Attempts to send some data from the given slice to the peer.
39 * Returns the amount of data which was sent, possibly 0 if the socket has since disconnected.
40 * Note that in the disconnected case, socket_disconnected must still fire and further write
41 * attempts may occur until that time.
43 * If the returned size is smaller than data.len(), a write_available event must
44 * trigger the next time more data can be written. Additionally, until the a send_data event
45 * completes fully, no further read_events should trigger on the same peer!
47 * If a read_event on this descriptor had previously returned true (indicating that read
48 * events should be paused to prevent DoS in the send buffer), resume_read may be set
49 * indicating that read events on this descriptor should resume. A resume_read of false does
50 * not* imply that further read events should be paused.
52 long send_data(byte[] data, boolean resume_read);
54 * Disconnect the socket pointed to by this SocketDescriptor. Once this function returns, no
55 * more calls to write_buffer_space_avail, read_event or socket_disconnected may be made with
56 * this descriptor. No socket_disconnected call should be generated as a result of this call,
57 * though races may occur whereby disconnect_socket is called after a call to
58 * socket_disconnected but prior to socket_disconnected returning.
60 void disconnect_socket();
62 * Checks if two objects are equal given this object's this_arg pointer and another object.
64 boolean 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 static class LDKSocketDescriptorHolder { SocketDescriptor held; }
72 public static SocketDescriptor new_impl(SocketDescriptorInterface arg) {
73 final LDKSocketDescriptorHolder impl_holder = new LDKSocketDescriptorHolder();
74 impl_holder.held = new SocketDescriptor(new bindings.LDKSocketDescriptor() {
75 @Override public long send_data(byte[] data, boolean resume_read) {
76 long ret = arg.send_data(data, resume_read);
79 @Override public void disconnect_socket() {
80 arg.disconnect_socket();
82 @Override public boolean eq(long other_arg) {
83 SocketDescriptor ret_hu_conv = new SocketDescriptor(null, other_arg);
84 ret_hu_conv.ptrs_to.add(this);
85 boolean ret = arg.eq(ret_hu_conv);
88 @Override public long hash() {
89 long ret = arg.hash();
93 return impl_holder.held;
96 * Attempts to send some data from the given slice to the peer.
98 * Returns the amount of data which was sent, possibly 0 if the socket has since disconnected.
99 * Note that in the disconnected case, socket_disconnected must still fire and further write
100 * attempts may occur until that time.
102 * If the returned size is smaller than data.len(), a write_available event must
103 * trigger the next time more data can be written. Additionally, until the a send_data event
104 * completes fully, no further read_events should trigger on the same peer!
106 * If a read_event on this descriptor had previously returned true (indicating that read
107 * events should be paused to prevent DoS in the send buffer), resume_read may be set
108 * indicating that read events on this descriptor should resume. A resume_read of false does
109 * not* imply that further read events should be paused.
111 public long send_data(byte[] data, boolean resume_read) {
112 long ret = bindings.SocketDescriptor_send_data(this.ptr, data, resume_read);
117 * Disconnect the socket pointed to by this SocketDescriptor. Once this function returns, no
118 * more calls to write_buffer_space_avail, read_event or socket_disconnected may be made with
119 * this descriptor. No socket_disconnected call should be generated as a result of this call,
120 * though races may occur whereby disconnect_socket is called after a call to
121 * socket_disconnected but prior to socket_disconnected returning.
123 public void disconnect_socket() {
124 bindings.SocketDescriptor_disconnect_socket(this.ptr);
128 * Calculate a succinct non-cryptographic hash for an object given its this_arg pointer.
129 * This is used, for example, for inclusion of this object in a hash map.
132 long ret = bindings.SocketDescriptor_hash(this.ptr);
137 * Creates a copy of a SocketDescriptor
139 public SocketDescriptor clone() {
140 long ret = bindings.SocketDescriptor_clone(this.ptr);
141 SocketDescriptor ret_hu_conv = new SocketDescriptor(null, ret);
142 ret_hu_conv.ptrs_to.add(this);