1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// ---------------- [ File: bitcoin-sock/src/interface.rs ]
crate::ix!();
pub trait SockInterface:
SockGet
+ SockRelease
+ Reset
+ SockSend
+ SockRecv
+ SockConnect
+ SockGetSockOpt
+ SockWait
/* Higher level, convenience, methods. These may throw. */
+ SockSendComplete
+ SockRecvUntilTerminator
+ SockIsConnected {}
pub trait SockGet {
/**
| Get the value of the contained socket.
|
|
| -----------
| @return
|
| socket or INVALID_SOCKET if empty
|
*/
fn get(&self) -> CSocket;
}
pub trait SockRelease {
/**
| Get the value of the contained socket
| and drop ownership. It will not be closed
| by the destructor after this call.
|
|
| -----------
| @return
|
| socket or INVALID_SOCKET if empty
|
*/
fn release(&mut self) -> CSocket;
}
pub trait Reset {
/**
| Close if non-empty.
|
*/
fn reset(&mut self);
}
pub trait SockSend {
/**
| send(2) wrapper. Equivalent to `send(this->Get(),
| data, len, flags);`. Code that uses
| this wrapper can be unit tested if this
| method is overridden by a mock Sock implementation.
|
*/
fn send(&self,
data: *const c_void,
len: usize,
flags: i32) -> isize;
}
pub trait SockRecv {
/**
| recv(2) wrapper. Equivalent to `recv(this->Get(),
| buf, len, flags);`. Code that uses this
| wrapper can be unit tested if this method
| is overridden by a mock Sock implementation.
|
*/
fn recv(&self,
buf: *mut c_void,
len: usize,
flags: i32) -> isize;
}
pub trait SockConnect {
/**
| connect(2) wrapper. Equivalent to
| `connect(this->Get(), addr, addrlen)`.
| Code that uses this wrapper can be unit
| tested if this method is overridden
| by a mock Sock implementation.
|
*/
fn connect(&self,
addr: *const SocketAddr,
addr_len: libc::socklen_t) -> i32;
}
pub trait SockGetSockOpt {
/**
| getsockopt(2) wrapper. Equivalent
| to `getsockopt(this->Get(), level,
| opt_name, opt_val, opt_len)`. Code
| that uses this wrapper can be unit tested
| if this method is overridden by a mock
| Sock implementation.
|
*/
fn get_sock_opt(&self,
level: i32,
opt_name: i32,
opt_val: *mut c_void,
opt_len: *mut libc::socklen_t) -> i32;
}
pub trait SockWait {
/**
| Wait for readiness for input (recv)
| or output (send).
|
| -----------
| @param[in] timeout
|
| Wait this much for at least one of the
| requested events to occur.
| ----------
| @param[in] requested
|
| Wait for those events, bitwise-or of
| `RECV` and `SEND`.
| ----------
| @param[out] occurred
|
| If not nullptr and `true` is returned,
| then upon return this indicates which
| of the requested events occurred. A
| timeout is indicated by return value
| of `true` and `occurred` being set to
| 0.
|
| -----------
| @return
|
| true on success and false otherwise
|
*/
fn wait(&self,
timeout: Instant /* millis */,
requested: SockEvent,
occurred: *mut SockEvent) -> bool;
}
pub trait SockSendComplete {
/**
| Send the given data, retrying on transient
| errors.
|
| -----------
| @param[in] data
|
| Data to send.
| ----------
| @param[in] timeout
|
| Timeout for the entire operation.
| ----------
| @param[in] interrupt
|
| If this is signaled then the operation
| is canceled. @throws std::runtime_error
| if the operation cannot be completed.
| In this case only some of the data will
| be written to the socket.
|
*/
fn send_complete(&self,
data: &String,
timeout: Instant /* millis */,
interrupt: &mut ThreadInterrupt);
}
pub trait SockRecvUntilTerminator {
/**
| Read from socket until a terminator
| character is encountered. Will never
| consume bytes past the terminator from
| the socket.
|
| -----------
| @param[in] terminator
|
| Character up to which to read from the
| socket.
| ----------
| @param[in] timeout
|
| Timeout for the entire operation.
| ----------
| @param[in] interrupt
|
| If this is signaled then the operation
| is canceled.
| ----------
| @param[in] max_data
|
| The maximum amount of data (in bytes)
| to receive. If this many bytes are received
| and there is still no terminator, then
| this method will throw an exception.
|
| -----------
| @return
|
| The data that has been read, without
| the terminating character. @throws
| std::runtime_error if the operation
| cannot be completed. In this case some
| bytes may have been consumed from the
| socket.
|
*/
fn recv_until_terminator(&self,
terminator: u8,
timeout: Instant /* millis */,
interrupt: &mut ThreadInterrupt,
max_data: usize) -> String;
}
pub trait SockIsConnected {
/**
| Check if still connected.
|
| -----------
| @param[out] errmsg
|
| The error string, if the socket has been
| disconnected.
|
| -----------
| @return
|
| true if connected
|
*/
fn is_connected(&self, errmsg: &mut String) -> bool;
}