|
NCBI Home IEB Home C Toolkit docs C++ Toolkit source browser C Toolkit source browser (2) |
NCBI C Toolkit Cross ReferenceC/connect/ncbi_connection.h |
source navigation diff markup identifier search freetext search file search |
1 #ifndef CONNECT___NCBI_CONNECTION__H
2 #define CONNECT___NCBI_CONNECTION__H
3
4 /* $Id: ncbi_connection.h,v 6.21 2004/10/26 14:47:42 lavr Exp $
5 * ===========================================================================
6 *
7 * PUBLIC DOMAIN NOTICE
8 * National Center for Biotechnology Information
9 *
10 * This software/database is a "United States Government Work" under the
11 * terms of the United States Copyright Act. It was written as part of
12 * the author's official duties as a United States Government employee and
13 * thus cannot be copyrighted. This software/database is freely available
14 * to the public for use. The National Library of Medicine and the U.S.
15 * Government have not placed any restriction on its use or reproduction.
16 *
17 * Although all reasonable efforts have been taken to ensure the accuracy
18 * and reliability of the software and data, the NLM and the U.S.
19 * Government do not and cannot warrant the performance or results that
20 * may be obtained by using this software or data. The NLM and the U.S.
21 * Government disclaim all warranties, express or implied, including
22 * warranties of performance, merchantability or fitness for any particular
23 * purpose.
24 *
25 * Please cite the author in any work or product based on this material.
26 *
27 * ===========================================================================
28 *
29 * Author: Denis Vakatov
30 *
31 * File Description:
32 * Generic API to open and handle connection to an abstract I/O service.
33 * Several methods can be used to establish the connection, and each of them
34 * yields in a simple handle(of type "CONN") that contains a handle(of type
35 * "CONNECTOR") to a data and methods implementing the generic connection I/O
36 * operations. E.g. this API can be used to:
37 * 1) connect using HTTPD-based dispatcher (e.g. to NCBI services);
38 * 2) hit a CGI script;
39 * 3) connect to a bare socket at some "host:port";
40 * 4) whatever else can fit this paradigm -- see the SConnectorTag-related
41 * structures; e.g. it could be a plain file I/O or even a memory area.
42 *
43 * See in "ncbi_connector.h" for the detailed specification of the underlying
44 * connector("CONNECTOR", "SConnectorTag") methods and data structures.
45 *
46 */
47
48 #include <connect/ncbi_connector.h>
49
50
51 /** @addtogroup Connectors
52 *
53 * @{
54 */
55
56
57 #ifdef __cplusplus
58 extern "C" {
59 #endif
60
61
62 struct SConnectionTag;
63 typedef struct SConnectionTag* CONN; /* connection handle */
64
65
66 /* Compose all data necessary to establish a new connection
67 * (merely bind it to the specified connector). Unsuccessful completion
68 * sets conn to 0, and leaves connector intact (can be used again).
69 * NOTE1: The real connection will not be established right away. Instead,
70 * it will be established at the moment of the first call to one of
71 * "Flush", "Wait", "WaitAsync", "Write", or "Read" methods.
72 * NOTE2: "Connection establishment" at this level of abstraction may differ
73 * from actual link establishment at the underlying connector's level.
74 * NOTE3: Initial timeout values are set to CONN_DEFAULT_TIMEOUT, meaning
75 * that connector-specific timeouts are in force for the connection.
76 */
77 extern NCBI_XCONNECT_EXPORT EIO_Status CONN_Create
78 (CONNECTOR connector, /* [in] connector */
79 CONN* conn /* [out] handle of the created connection */
80 );
81
82
83 /* Reinit, using "connector".
84 * If "conn" is already opened then close the current connection at first,
85 * even if "connector" is just the same as the current connector.
86 * If "connector" is NULL then close and destroy current connector, and leave
87 * connection empty (effective way to destroy connector(s)).
88 * NOTE: Although it closes the previous connection immediately, however it
89 * does not open the new connection right away -- see notes to "Create".
90 */
91 extern NCBI_XCONNECT_EXPORT EIO_Status CONN_ReInit
92 (CONN conn, /* [in] connection handle */
93 CONNECTOR connector /* [in] new connector */
94 );
95
96
97 /* Get verbal representation of connection type as a character string.
98 * Note that the returned value is only valid until the next
99 * I/O operation in the connection. Return value NULL denotes
100 * unknown connection type.
101 */
102 extern NCBI_XCONNECT_EXPORT const char* CONN_GetType
103 (CONN conn /* [in] connection handle */
104 );
105
106
107 /* Return human-readable description of the connection as a character
108 * 0-terminated string. The string is not guaranteed to have any
109 * particular format and is intended solely for something like
110 * logging and debugging. Return NULL if the connection cannot
111 * provide any description information (or if it is in a bad state).
112 * Application program is responsible to deallocate the space occupied
113 * by the returned string calling free() when the description is no longer
114 * needed.
115 */
116 extern NCBI_XCONNECT_EXPORT char* CONN_Description
117 (CONN conn /* [in] connection handle */
118 );
119
120
121 /* Specify timeout for the connection I/O (including "Connect" (aka "Open")
122 * and "Close"). May be called at any time during the connection lifetime.
123 * NOTE1: if "new_timeout" is NULL then set the timeout to be infinite.
124 * NOTE2: if "new_timeout" is CONN_DEFAULT_TIMEOUT then underlying
125 * connector-specific value is used (this is the default).
126 */
127 extern NCBI_XCONNECT_EXPORT EIO_Status CONN_SetTimeout
128 (CONN conn, /* [in] connection handle */
129 EIO_Event event, /* [in] I/O direction */
130 const STimeout* new_timeout /* [in] new timeout */
131 );
132
133
134 /* Retrieve current timeout (return NULL if it is infinite).
135 * The returned pointer is guaranteed to point to the valid timeout structure
136 * (or be either NULL or CONN_DEFAULT_TIMEOUT) until the next "SetTimeout"
137 * or "Close" method's call.
138 */
139 extern NCBI_XCONNECT_EXPORT const STimeout* CONN_GetTimeout
140 (CONN conn, /* [in] connection handle */
141 EIO_Event event /* [in] I/O direction, not "eIO_ReadWrite" */
142 );
143
144
145 /* Block on the connection until it becomes available for either read or
146 * write (dep. on "event"), or until the timeout expires, or until any error.
147 * NOTE: "timeout" can also be one of two special values:
148 * NULL (means infinite), CONN_DEFAULT_TIMEOUT (connector-defined).
149 */
150 extern NCBI_XCONNECT_EXPORT EIO_Status CONN_Wait
151 (CONN conn, /* [in] connection handle */
152 EIO_Event event, /* [in] can be eIO_Read or eIO_Write only! */
153 const STimeout* timeout /* [in] the maximal wait time */
154 );
155
156
157 /* Write "size" bytes from the mem.buffer "buf" to the connection.
158 * In "*n_written", return the number of successfully written bytes.
159 * Parameter "how" modifies behavior of CONN_Write():
160 * eIO_WritePlain -- return eIO_Success if some data were written and
161 * yet write timeout had not occurred, error otherwise;
162 * eIO_WritePersist -- return eIO_Success only if all data were written and
163 * yet write timeout had not occurred, error otherwise.
164 * NOTE: See CONN_SetTimeout() hoe to set write timeout.
165 */
166 extern NCBI_XCONNECT_EXPORT EIO_Status CONN_Write
167 (CONN conn, /* [in] connection handle */
168 const void* buf, /* [in] pointer to the data buffer to write */
169 size_t size, /* [in] # of bytes to write */
170 size_t* n_written, /* [out, non-NULL] # of actually written bytes */
171 EIO_WriteMethod how /* [in] eIO_WritePlain or eIO_WritePersist */
172 );
173
174
175 /* Push back "size" bytes from the mem.buffer "buf" into connection.
176 * Return eIO_Success on success, other code on an error.
177 * NOTE1: Data pushed back are not necessarily those taken from the
178 * connection before.
179 * NOTE2: Upon successive read operation, the pushed back data are
180 * taken out first.
181 */
182 extern NCBI_XCONNECT_EXPORT EIO_Status CONN_PushBack
183 (CONN conn, /* [in] connection handle */
184 const void* buf, /* [in] pointer to the data being pushed back */
185 size_t size /* [in] # of bytes to push back */
186 );
187
188
189 /* Explicitly flush to the connection all data written by "CONN_Write()".
190 * NOTE1: CONN_Flush() effectively opens connection (if it wasn't open yet).
191 * NOTE2: Connection considered open if underlying connector's "Open" method
192 * has successfully executed; actual data link may not yet exist.
193 * NOTE3: CONN_Read() always calls CONN_Flush() before proceeding.
194 * So does CONN_Close() but only if connection is was open before.
195 */
196 extern NCBI_XCONNECT_EXPORT EIO_Status CONN_Flush
197 (CONN conn /* [in] connection handle */
198 );
199
200
201 /* Read up to "size" bytes from a connection to the buffer to pointed by "buf".
202 * In "*n_read", return the number of successfully read bytes.
203 * If there is absolutely no data available to read and the timeout (see
204 * CONN_SetTimeout()) is expired then return eIO_Timeout (and "*n_read" := 0).
205 * The arg "how" means:
206 * eIO_ReadPlain -- read presently available data only and return
207 * eIO_ReadPeek -- eIO_ReadPlain but dont discard read data from inp.queue
208 * eIO_ReadPersist -- try to read exactly "n" bytes; return eIO_Timeout if
209 * could not read the requested # of bytes, and read
210 * timeout has expired.
211 */
212 extern NCBI_XCONNECT_EXPORT EIO_Status CONN_Read
213 (CONN conn, /* [in] connection handle */
214 void* buf, /* [out] memory buffer to read to */
215 size_t size, /* [in] max. # of bytes to read */
216 size_t* n_read, /* [out, non-NULL] # of actually read bytes */
217 EIO_ReadMethod how /* [in] read/peek | persist */
218 );
219
220
221 /* Read up to "size" bytes from a connection into the string buffer pointed
222 * to by "line". Stop reading if either '\n' or an error is encountered.
223 * Replace '\n' with '\0'. Upon return "*n_read" contains the number
224 * of characters written to "line", not including the terminating '\0'.
225 * If not enough space provided in "line" to accomodate the '\0'-terminated
226 * line, then all "size" bytes are used and "*n_read" equals "size" on return.
227 * This is the only case when "line" will not be '\0'-terminated.
228 * Return code advises the caller whether another line read can be attempted:
229 * eIO_Success -- read completed successfully, keep reading;
230 * other code -- an error occurred, and further attempt may fail.
231 *
232 * This call utilizes eIO_Read timeout as set by CONN_SetTimeout().
233 */
234 extern NCBI_XCONNECT_EXPORT EIO_Status CONN_ReadLine
235 (CONN conn,
236 char* line,
237 size_t size,
238 size_t* n_read
239 );
240
241
242 /* Obtain status of the last IO operation. This is NOT a completion
243 * code of the last CONN-call, but rather a status from the lower level
244 * connector's layer.
245 */
246 extern NCBI_XCONNECT_EXPORT EIO_Status CONN_Status
247 (CONN conn, /* [in] connection handle */
248 EIO_Event dir /* [in] = {eIO_Read | eIO_Write} */
249 );
250
251
252 /* Close the connection, destroy relevant internal data.
253 * NOTE: whatever error code is returned, the connection handle "conn"
254 * will become invalid (so, you should not use it anymore).
255 */
256 extern NCBI_XCONNECT_EXPORT EIO_Status CONN_Close
257 (CONN conn /* [in] connection handle */
258 );
259
260
261 /* Set user callback function to be called upon an event specified by the
262 * callback type. Note that the callback function is always called prior to
263 * the event to happen, e.g. the eCONN_OnClose callback is called when
264 * the connection is about to close, but not closed yet.
265 * The callback function is supplied with 3 arguments: connection handle,
266 * type of event, and user data (specified when the callback was set).
267 * CONN_SetCallback stores previous callback in "old_cb" (if it is not NULL).
268 */
269 typedef enum {
270 eCONN_OnClose = 0
271 } ECONN_Callback;
272 #define CONN_N_CALLBACKS 1
273
274 typedef void (*FConnCallback)(CONN conn, ECONN_Callback type, void* data);
275
276 typedef struct {
277 FConnCallback func; /* Function to call on event */
278 void* data; /* Data to pass to the callback as last arg */
279 } SCONN_Callback;
280
281 extern NCBI_XCONNECT_EXPORT EIO_Status CONN_SetCallback
282 (CONN conn, /* [in] connection to set callback for */
283 ECONN_Callback type, /* [in] callback type */
284 const SCONN_Callback* new_cb, /* [in] callback to set (may be 0) */
285 SCONN_Callback* old_cb /* [out] to save old callback at (may be 0) */
286 );
287
288
289 #ifdef IMPLEMENTED__CONN_WaitAsync
290 /* Wait for an asynchronous I/O event, then call the specified handler.
291 * In the "handler" function:
292 * "event" -- is the I/O direction where the async. event happened
293 * "status" -- must be "eIO_Success" if it is ready for I/O
294 * "data" -- callback data (passed as "data" in CONN_WaitAsync())
295 * If "handler" is NULL then discard the current handler, if any.
296 * The "cleanup" function to be called right after the call to "handler" or
297 * by CONN_Close(), or if the handler is reset by calling CONN_WaitAsync()
298 * again -- whichever happens first.
299 */
300 typedef void (*FConnAsyncHandler)
301 (CONN conn,
302 EIO_Event event,
303 EIO_Status status,
304 void* data
305 );
306 typedef void (*FConnAsyncCleanup)(void* data);
307
308 extern EIO_Status CONN_WaitAsync
309 (CONN conn, /* [in] connection handle */
310 EIO_Event event, /* [in] I/O direction */
311 FConnAsyncHandler handler, /* [in] callback function */
312 void* data, /* [in] callback data */
313 FConnAsyncCleanup cleanup /* [in] cleanup procedure */
314 );
315 #endif /* IMPLEMENTED__CONN_WaitAsync */
316
317
318 #ifdef __cplusplus
319 } /* extern "C" */
320 #endif
321
322
323 /* @} */
324
325
326 /*
327 * ---------------------------------------------------------------------------
328 * $Log: ncbi_connection.h,v $
329 * Revision 6.21 2004/10/26 14:47:42 lavr
330 * Slight formatting
331 *
332 * Revision 6.20 2004/06/09 14:03:06 jcherry
333 * Moved #define out of enum body (SWIG was choking on this)
334 *
335 * Revision 6.19 2004/05/24 19:58:29 lavr
336 * +NCBI_XCONNECT_EXPORT for CONN_ReadLine()
337 *
338 * Revision 6.18 2004/05/24 19:53:30 lavr
339 * +CONN_ReadLine()
340 *
341 * Revision 6.17 2004/02/23 15:23:36 lavr
342 * New (last) parameter "how" added in CONN_Write() API call
343 *
344 * Revision 6.16 2003/05/14 03:47:12 lavr
345 * +CONN_Description()
346 *
347 * Revision 6.15 2003/04/09 17:58:43 siyan
348 * Added doxygen support
349 *
350 * Revision 6.14 2003/01/15 19:50:17 lavr
351 * +CONN_PushBack()
352 *
353 * Revision 6.13 2003/01/08 01:59:32 lavr
354 * DLL-ize CONNECT library for MSVC (add NCBI_XCONNECT_EXPORT)
355 *
356 * Revision 6.12 2002/09/19 18:00:04 lavr
357 * Header file guard macro changed
358 *
359 * Revision 6.11 2002/09/06 15:40:32 lavr
360 * More comments and notes to the API
361 *
362 * Revision 6.10 2002/08/07 16:27:33 lavr
363 * EIO_ReadMethod enums changed accordingly; log moved to the bottom
364 *
365 * Revision 6.9 2001/08/20 20:00:22 vakatov
366 * CONN_SetTimeout() to return "EIO_Status".
367 *
368 * Revision 6.8 2001/06/28 22:00:31 lavr
369 * Added function: CONN_SetCallback
370 * Added callback: eCONN_OnClose
371 *
372 * Revision 6.7 2001/04/24 21:19:29 lavr
373 * Introduced CONN_DEFAULT_TIMEOUT for use as a CONNECTOR-specific timeout
374 *
375 * Revision 6.6 2001/03/02 20:07:33 lavr
376 * Typo fixed
377 *
378 * Revision 6.5 2001/02/09 17:33:38 lavr
379 * CONN_GetType added
380 *
381 * Revision 6.4 2001/01/03 22:29:22 lavr
382 * Changed IOStatus -> Status
383 *
384 * Revision 6.3 2000/12/29 17:43:42 lavr
385 * Pretty printed;
386 * Reconnect renamed to ReInit with ability to close current connector
387 *
388 * Revision 6.2 2000/04/07 19:59:47 vakatov
389 * Moved forward-declaration of CONNECTOR from "ncbi_connection.h"
390 * to "ncbi_connector.h"
391 *
392 * Revision 6.1 2000/03/24 22:52:20 vakatov
393 * Initial revision
394 *
395 * ===========================================================================
396 */
397
398 #endif /* CONNECT___NCBI_CONNECTION__H */
399 |
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more information. |