|
NCBI Home IEB Home C Toolkit docs C++ Toolkit source browser C Toolkit source browser (2) |
NCBI C Toolkit Cross ReferenceC/ctools/asn_connection.c |
source navigation diff markup identifier search freetext search file search |
1 /* $Id: asn_connection.c,v 1.9 2007/10/17 15:25:44 kazimird Exp $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Author: Anton Lavrentiev
27 *
28 * File Description:
29 * Build C Toolkit ASN streams on top of CONN (connection).
30 *
31 */
32
33 #include <ctools/asn_connection.h>
34 #include "../connect/ncbi_priv.h"
35 #include "error_codes.h"
36
37
38 #define NCBI_USE_ERRCODE_X Ctools_ASN
39
40
41 #ifdef __cplusplus
42 extern "C" {
43 static Int2 LIBCALLBACK s_AsnRead(Pointer p, CharPtr buff, Uint2 len);
44 static Int2 LIBCALLBACK s_AsnWrite(Pointer p, CharPtr buff, Uint2 len);
45 static void s_CloseAsnConn(CONN conn, ECONN_Callback type, void* data);
46 }
47 #endif
48
49
50 static Int2 LIBCALLBACK s_AsnRead(Pointer p, CharPtr buff, Uint2 len)
51 {
52 size_t n_read;
53 CONN_Read((CONN) p, buff, len, &n_read, eIO_ReadPlain);
54 return (Int2) n_read;
55 }
56
57
58 static Int2 LIBCALLBACK s_AsnWrite(Pointer p, CharPtr buff, Uint2 len)
59 {
60 size_t n_written;
61 CONN_Write((CONN) p, buff, len, &n_written, eIO_WritePersist);
62 return (Int2) n_written;
63 }
64
65
66 struct SAsnConn_Cbdata {
67 SCONN_Callback cb;
68 AsnIoPtr ptr;
69 };
70
71
72 static void s_CloseAsnConn(CONN conn, ECONN_Callback type, void* data)
73 {
74 struct SAsnConn_Cbdata* cbdata = (struct SAsnConn_Cbdata*) data;
75
76 assert(type == eCONN_OnClose && cbdata && cbdata->ptr);
77 CONN_SetCallback(conn, type, &cbdata->cb, 0);
78 AsnIoFree(cbdata->ptr, 0/*not a file - don't close*/);
79 if ( cbdata->cb.func )
80 (*cbdata->cb.func)(conn, type, cbdata->cb.data);
81 free(cbdata);
82 }
83
84
85 static void s_SetAsnConn_CloseCb(CONN conn, AsnIoPtr ptr)
86 {
87 struct SAsnConn_Cbdata* cbdata = (struct SAsnConn_Cbdata*)
88 malloc(sizeof(*cbdata));
89
90 assert( ptr );
91 if ( cbdata ) {
92 SCONN_Callback cb;
93 cbdata->ptr = ptr;
94 cb.func = s_CloseAsnConn;
95 cb.data = cbdata;
96 CONN_SetCallback(conn, eCONN_OnClose, &cb, &cbdata->cb);
97 } else
98 CORE_LOG_X(1, eLOG_Error,
99 "Cannot create cleanup callback for ASN conn-based stream");
100 }
101
102
103 AsnIoPtr CreateAsnConn(CONN conn,
104 EAsnConn_Direction direction,
105 EAsnConn_Format fmt)
106 {
107 AsnIoPtr ptr;
108 int flags;
109
110 switch (fmt) {
111 case eAsnConn_Binary:
112 flags = ASNIO_BIN;
113 break;
114 case eAsnConn_Text:
115 flags = ASNIO_TEXT;
116 break;
117 default:
118 return 0;
119 }
120
121 switch (direction) {
122 case eAsnConn_Input:
123 ptr = AsnIoNew(flags | ASNIO_IN, 0, (void*) conn, s_AsnRead, 0);
124 break;
125 case eAsnConn_Output:
126 ptr = AsnIoNew(flags | ASNIO_OUT, 0, (void*) conn, 0, s_AsnWrite);
127 break;
128 default:
129 return 0;
130 }
131
132 if (ptr)
133 s_SetAsnConn_CloseCb(conn, ptr);
134 return ptr;
135 }
136
137
138 CONN CreateAsnConn_ServiceEx(const char* service,
139 EAsnConn_Format input_fmt,
140 AsnIoPtr* input,
141 EAsnConn_Format output_fmt,
142 AsnIoPtr* output,
143 TSERV_Type type,
144 const SConnNetInfo* net_info,
145 const SSERVICE_Extra* params)
146 {
147 CONN conn;
148 CONNECTOR c = SERVICE_CreateConnectorEx(service, type, net_info, params);
149 if (!c || CONN_Create(c, &conn) != eIO_Success)
150 return 0/*failed*/;
151 assert(conn);
152
153 if (input)
154 *input = CreateAsnConn(conn, eAsnConn_Input, input_fmt);
155
156 if (output)
157 *output = CreateAsnConn(conn, eAsnConn_Output, output_fmt);
158
159 return conn;
160 }
161
162
163 CONN CreateAsnConn_Service(const char* service,
164 EAsnConn_Format input_fmt,
165 AsnIoPtr* input,
166 EAsnConn_Format output_fmt,
167 AsnIoPtr* output)
168 {
169 return CreateAsnConn_ServiceEx(service, input_fmt, input,
170 output_fmt, output, fSERV_Any, 0, 0);
171 }
172 |
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more information. |