7c9933051cbaeb2809803f6c792504ebaa596eb8
[ldk-java] / src / main / java / org / ldk / structs / SocketDescriptor.java
1 package org.ldk.structs;
2
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
5 import org.ldk.util.*;
6 import java.util.Arrays;
7
8 /**
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.
12  * 
13  * For efficiency, Clone should be relatively cheap for this type.
14  * 
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().
20  */
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;
29         }
30         @Override @SuppressWarnings("deprecation")
31         protected void finalize() throws Throwable {
32                 if (ptr != 0) { bindings.SocketDescriptor_free(ptr); } super.finalize();
33         }
34
35         public static interface SocketDescriptorInterface {
36                 /**
37                  * Attempts to send some data from the given slice to the peer.
38                  * 
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.
42                  * 
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!
46                  * 
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.
51                  */
52                 long send_data(byte[] data, boolean resume_read);
53                 /**
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.
59                  */
60                 void disconnect_socket();
61                 /**
62                  * Checks if two objects are equal given this object's this_arg pointer and another object.
63                  */
64                 boolean eq(SocketDescriptor other_arg);
65                 /**
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.
68                  */
69                 long hash();
70         }
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);
77                                 return ret;
78                         }
79                         @Override public void disconnect_socket() {
80                                 arg.disconnect_socket();
81                         }
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);
86                                 return ret;
87                         }
88                         @Override public long hash() {
89                                 long ret = arg.hash();
90                                 return ret;
91                         }
92                 });
93                 return impl_holder.held;
94         }
95         /**
96          * Attempts to send some data from the given slice to the peer.
97          * 
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.
101          * 
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!
105          * 
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.
110          */
111         public long send_data(byte[] data, boolean resume_read) {
112                 long ret = bindings.SocketDescriptor_send_data(this.ptr, data, resume_read);
113                 return ret;
114         }
115
116         /**
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.
122          */
123         public void disconnect_socket() {
124                 bindings.SocketDescriptor_disconnect_socket(this.ptr);
125         }
126
127         /**
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.
130          */
131         public long hash() {
132                 long ret = bindings.SocketDescriptor_hash(this.ptr);
133                 return ret;
134         }
135
136         /**
137          * Creates a copy of a SocketDescriptor
138          */
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);
143                 return ret_hu_conv;
144         }
145
146 }