NCBI C++ ToolKit
asn_connection.c
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: asn_connection.c 61042 2013-12-22 17:58:39Z lavr $
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.1 streams on top of CONN (connection).
30  *
31  */
32 
33 #include <ctools/asn_connection.h>
34 #include "error_codes.h"
35 
36 
37 #define NCBI_USE_ERRCODE_X Ctools_ASN
38 
39 
40 #ifdef __cplusplus
41 extern "C" {
45 }
46 #endif
47 
48 
50 {
51  size_t n_read;
52  CONN_Read((CONN) conn, buf, len, &n_read, eIO_ReadPlain);
53  return (Int2) n_read;
54 }
55 
56 
58 {
59  size_t n_written;
60  CONN_Write((CONN) conn, buf, len, &n_written, eIO_WritePersist);
61  return (Int2) n_written;
62 }
63 
64 
67  AsnIoPtr aip;
68 };
69 
70 
72 {
73  EIO_Status status;
74  struct SAsnConn_Cbdata* cbdata = (struct SAsnConn_Cbdata*) data;
75 
76  assert(type == eCONN_OnClose && cbdata && cbdata->aip);
77  AsnIoFree(cbdata->aip, FALSE/*not a file - don't close*/);
78  status = cbdata->cb.func
79  ? cbdata->cb.func(conn, type, cbdata->cb.data)
80  : eIO_Success;
81  free(cbdata);
82  return status;
83 }
84 
85 
86 static int/*bool*/ s_AsnSetCloseCb(CONN conn, AsnIoPtr aip)
87 {
88  struct SAsnConn_Cbdata* cbdata =
89  (struct SAsnConn_Cbdata*) malloc(sizeof(*cbdata));
90 
91  if ( cbdata ) {
93  cbdata->aip = aip;
94  cb.func = s_AsnClose;
95  cb.data = cbdata;
96  CONN_SetCallback(conn, eCONN_OnClose, &cb, &cbdata->cb);
97  return 1/*success*/;
98  }
99 
101  "Cannot create close callback for ASN.1 CONN-based stream");
102  return 0/*failure*/;
103 }
104 
105 
107  EAsnConn_Direction direction,
108  EAsnConn_Format fmt)
109 {
110  /* NB: Do not use ASNIO_{TEXT|BIN}_{IN|OUT} because they subsume FILE */
111  AsnIoPtr aip;
112  int type;
113 
114  switch (fmt) {
115  case eAsnConn_Binary:
116  type = ASNIO_BIN;
117  break;
118  case eAsnConn_Text:
119  type = ASNIO_TEXT;
120  break;
121  default:
122  return 0;
123  }
124 
125  switch (direction) {
126  case eAsnConn_Input:
127  aip = AsnIoNew(type | ASNIO_IN, 0, (void*) conn, s_AsnRead, 0);
128  break;
129  case eAsnConn_Output:
130  aip = AsnIoNew(type | ASNIO_OUT, 0, (void*) conn, 0, s_AsnWrite);
131  break;
132  default:
133  return 0;
134  }
135 
136  if (aip && !s_AsnSetCloseCb(conn, aip)) {
137  AsnIoFree(aip, FALSE/*not a file*/);
138  aip = 0;
139  }
140  return aip;
141 }
142 
143 
144 CONN CreateAsnConn_ServiceEx(const char* service,
145  EAsnConn_Format input_fmt,
146  AsnIoPtr* input,
147  EAsnConn_Format output_fmt,
148  AsnIoPtr* output,
150  const SConnNetInfo* net_info,
151  const SSERVICE_Extra* params)
152 {
153  CONN conn;
154  CONNECTOR c = SERVICE_CreateConnectorEx(service, type, net_info, params);
155  if (!c || CONN_Create(c, &conn) != eIO_Success)
156  return 0/*failed*/;
157  assert(conn);
158 
159  if (input)
160  *input = CreateAsnConn(conn, eAsnConn_Input, input_fmt);
161 
162  if (output)
163  *output = CreateAsnConn(conn, eAsnConn_Output, output_fmt);
164 
165  return conn;
166 }
167 
168 
169 CONN CreateAsnConn_Service(const char* service,
170  EAsnConn_Format input_fmt,
171  AsnIoPtr* input,
172  EAsnConn_Format output_fmt,
173  AsnIoPtr* output)
174 {
175  return CreateAsnConn_ServiceEx(service, input_fmt, input,
176  output_fmt, output, fSERV_Any, 0, 0);
177 }
static Int2 LIBCALLBACK s_AsnWrite(Pointer conn, CharPtr buf, Uint2 len)
static EIO_Status s_AsnClose(CONN conn, TCONN_Callback type, void *data)
static Int2 LIBCALLBACK s_AsnRead(Pointer conn, CharPtr buf, Uint2 len)
static int s_AsnSetCloseCb(CONN conn, AsnIoPtr aip)
static CS_CONNECTION * conn
Definition: ct_dynamic.c:25
static int type
Definition: getdata.c:31
static SQLCHAR output[256]
Definition: print.c:5
char data[12]
Definition: iconv.c:80
AsnIoPtr CreateAsnConn(CONN conn, EAsnConn_Direction direction, EAsnConn_Format fmt)
EAsnConn_Format
CONN CreateAsnConn_ServiceEx(const char *service, EAsnConn_Format input_fmt, AsnIoPtr *input, EAsnConn_Format output_fmt, AsnIoPtr *output, TSERV_Type type, const SConnNetInfo *net_info, const SSERVICE_Extra *params)
EAsnConn_Direction
CONN CreateAsnConn_Service(const char *service, EAsnConn_Format input_fmt, AsnIoPtr *input, EAsnConn_Format output_fmt, AsnIoPtr *output)
@ eAsnConn_Text
@ eAsnConn_Binary
@ eAsnConn_Input
@ eAsnConn_Output
#define CharPtr
Definition: ncbistd.hpp:125
#define LIBCALLBACK
Definition: ncbistd.hpp:302
#define Pointer
Definition: ncbistd.hpp:114
ECONN_Callback
Set user callback function to be invoked upon an event specified by the callback type.
CONNECTOR SERVICE_CreateConnectorEx(const char *service, TSERV_Type types, const SConnNetInfo *net_info, const SSERVICE_Extra *extra)
EIO_Status CONN_Read(CONN conn, void *buf, size_t size, size_t *n_read, EIO_ReadMethod how)
EIO_Status CONN_SetCallback(CONN conn, ECONN_Callback type, const SCONN_Callback *new_cb, SCONN_Callback *old_cb)
unsigned int TCONN_Callback
EIO_Status CONN_Write(CONN conn, const void *buf, size_t size, size_t *n_written, EIO_WriteMethod how)
void * data
data to pass to the callback as its last arg
EIO_Status CONN_Create(CONNECTOR connector, CONN *conn)
Same as CONN_CreateEx() called with 0 in the "flags" parameter.
FCONN_Callback func
function address to call on the event
@ eCONN_OnClose
NB: CONN has been flushed prior to the call.
int16_t Int2
2-byte (16-bit) signed integer
Definition: ncbitype.h:100
uint16_t Uint2
2-byte (16-bit) unsigned integer
Definition: ncbitype.h:101
unsigned int TSERV_Type
Bitwise OR of ESERV_Type[Special].
Definition: ncbi_service.h:94
@ fSERV_Any
Definition: ncbi_service.h:79
EIO_Status
I/O status.
Definition: ncbi_core.h:132
@ eLOG_Error
Definition: ncbi_core.h:297
@ eIO_Success
everything is fine, no error occurred
Definition: ncbi_core.h:133
@ eIO_ReadPlain
read readily available data only, wait if none
Definition: ncbi_core.h:90
@ eIO_WritePersist
write exactly as much as specified, w/waits
Definition: ncbi_core.h:102
char * buf
static int input()
int len
#define CORE_LOG_X(subcode, level, message)
Definition: ncbi_priv.h:146
#define FALSE
bool replacment for C indicating false.
Definition: ncbi_std.h:101
#define assert(x)
Definition: srv_diag.hpp:58
SCONN_Callback cb
Connector specification.
Definition: type.c:6
void free(voidpf ptr)
voidp malloc(uInt size)
Modified on Fri Sep 20 14:57:53 2024 by modify_doxy.py rev. 669887