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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* Copyright 2014 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
extern "C" WIN32
typedef DWORD lcbio_OSERR;
typedef int lcbio_OSERR;
/** @brief Information about a connected socket */
typedef struct lcbio_CONNINFO;
/** @brief Core socket structure */
typedef struct lcbio_SOCKET lcbio_SOCKET;
/**
* @name Connecting and Destroying a Socket
* @{
*/
/**
* Invoked when the connection result is ready
* @param s the socket to use. You should call lcbio_ref() on it. May be NULL
* in the case of an error
* @param arg user provided argument to the lcbio_connect() function
* @param err an error code (if connection is NULL)
* @param syserr the raw errno variable received.
*/
typedef void
;
/**
* Schedule a new connection to a remote endpoint.
*
* @param iot I/O table to use. The socket will increment the reference count of
* the table until the socket is destroyed.
* @param settings Settings structure. Used for logging
* @param dest the endpoint to connect to
* @param timeout number of time to wait for connection. The handler will be
* invoked with an error of `LCB_ETIMEDOUT` if a successful connection
* cannot be established in time.
* @param handler a handler to invoke with the result. The handler will always
* be invoked unless the request has been cancelled. You should inspect
* the socket and error code in the handler to see if the connection has
* been successful.
* @param arg the argument passed to the handler
* @return a request handle. The handle may be cancelled (to stop the pending
* connection attempt) before the handler is invoked.
* Once the handler is invoked, the returned handle is considered to
* be invalid; as such the following idiom should be employed:
*
*
* @code{.c}
* struct my_ctx {
* lcbio_SOCKET *sock;
* lcbio_pCONNSTART creq;
* }
*
* static void do_connect(void) {
* my_ctx *ctx;
* ctx->creq = lcbio_connect(iot, settings, dest, tmo, handler, ctx);
* // check errors..
* }
*
*
* static void handler(lcbio_SOCKET *s, void *arg, lcb_error_t err) {
* my_ctx *ctx = arg;
* ctx->creq = NULL;
* if (!(ctx->sock = s)) {
* ...
* }
* }
* @endcode
*/
lcbio_pCONNSTART
;
/**
* Wraps an existing socket descriptor into an lcbio_SOCKET structure
* @param iot
* @param settings
* @param fd The socket descriptor to wrap. This must refer to a _connected_
* socket (e.g. via `connect(2)` or `socketpair(2)`.
* @return A new socket object.
*/
lcbio_SOCKET *
;
/**
* Wraps `lcb_connect()` by traversing a list of hosts. This will cycle through
* each host in the list until a connection has been successful. Currently
* this will not intercept the handler but will catch any hostname lookup
* failures.
*
* @param iot
* @param settings
* @param hl The hostlist to traverse
* @param rollover If the hostlist position is at the end, this boolean parameter
* indicates whether the position should be reset
* @param timeout
* @param handler
* @param arg
* @see lcbio_connect()
*/
lcbio_pCONNSTART
;
/**
* Cancel a pending connection attempt. Once the attempt is cancelled the
* handler will not be invoked and the CONNSTART object will be invalid.
* @param cs the handle returned from lcbio_connect()
*/
void
;
/**
* Cancel any pending I/O on this socket. Outstanding callbacks for I/O (i.e.
* for completion-based reads and writes) will still be delivered with an error
* code. Outstanding callbacks for event-based I/O will not be invoked.
*
* This function does not modify the reference count of the socket directly
* but will clear any lcbio_PROTOCTX objects attached to it.
*/
void
;
/**
* Increment the reference count on the socket. When the socket is no longer
* needed, call lcbio_unref().
*/
/**
* Decrement the reference count on the socket. When the reference count hits
* zero, lcbio_shutdown() will be called.
*/
/** @} */
/**
* @name Protocol Contexts
* @{
*/
typedef enum lcbio_PROTOID;
/**
* @brief Protocol-specific data attached to lcbio_SOCKET.
*
* A protocol context is an object which is bound to the actual low level
* socket connection rather than the logical socket owner. This is used for
* resources which operate on the TCP state (such as encryption or authentication)
* or which employ socket reuse (for things such as pooling).
*/
typedef struct lcbio_PROTOCTX lcbio_PROTOCTX;
/**
* Attach an lcbio_PROTOCTX object to the socket. This object will remain
* part of the socket until lcbio_shutdown() is invoked, or the context itself
* is removed explicitly.
*
* @param socket the socket the context should be added to
* @param proto the object to be added. The protocol object should have its
* `id` and `dtor` fields initialized.
*/
void
;
/**
* Retrieve an existing protocol context by its ID
* @param socket The socket to query
* @param id The ID of the context
* @return the context, or NULL if not found
*/
lcbio_PROTOCTX *
;
/**
* Remove a protocol context by its ID
* @param socket socket from which to remove
* @param id The id of the context to remove
* @param call_dtor whether the destructor should be invoked
* @return the returned context, or NULL if not found
*/
lcbio_PROTOCTX *
;
/**
* Delete a protocol context by its pointer.
* @param socket The socket from which the context should be removed
* @param ctx The pointer to remove
* @param call_dtor Whether to invoke the destructor for the lcbio_PROTOCTX
*/
void
;
/** @private */
void
;
/** @} */
/**
* Get the lcb_host_t pointer indicating the endpoint the socket is connected to.
* @param sock The socket
* @return a pointer to the host.
*/
/**
* @private
* Internal destroy function for when the refcount hits 0
*/
void
;
/**
* @name IO Table Functions
* @details
* These functions provide the user-facing API for dealing with the lcbio_TABLE
* structure. These functions only control its handling as an opaque object.
* The definition of the structure may be found in <lcbio/iotable.h> and contains
* more routines for actually using it.
*
* @{
*/
/**
* Create a new table based on the input iops structure. The table itself retains
* ownership over the structure and will destroy it once the table itself has
* been destroyed.
* @param io An IOPS structure. See lcb_create_io_ops()
* @return A table with a reference count initialized to 1
*/
lcbio_pTABLE
;
/** Increment the reference count on the lcbio_TABLE */
void
;
/** Decrement the reference count on the lcbio_TABLE */
void
;
/** @}*/
/** @name IO Status Codes
*@{ */
typedef enum lcbio_IOSTATUS;
/** @} */
}
/**
* @}
*/