NCBI C++ ToolKit
ncbi_namedpipe_connector.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: ncbi_namedpipe_connector.cpp 97266 2022-06-30 14:11:07Z 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: Denis Vakatov, Anton Lavrentiev, Vladimir Ivanov
27  *
28  * File Description:
29  * Implement CONNECTOR for a named pipe interprocess communication
30  * (based on the NCBI CNamedPipe).
31  *
32  * See in "connectr.h" for the detailed specification of the underlying
33  * connector("CONNECTOR", "SConnectorTag") methods and structures.
34  *
35  */
36 
37 #include <ncbi_pch.hpp>
38 #include "ncbi_ansi_ext.h"
40 
41 
43 
44 
45 /***********************************************************************
46  * INTERNAL -- Auxiliary types and static functions
47  ***********************************************************************/
48 
49 // All internal data necessary to perform the (re)connect and i/o
50 typedef struct {
51  CNamedPipeClient* pipe; // pipe handle; non-NULL
52  string pipename; // pipe name
53  size_t pipesize; // pipe size
55 
56 
57 /***********************************************************************
58  * INTERNAL -- "s_VT_*" functions for the "virt. table" of connector methods
59  ***********************************************************************/
60 
61 
62 extern "C" {
63 
64 
65 static const char* s_VT_GetType
66 (CONNECTOR /*connector*/)
67 {
68  return "NAMEDPIPE";
69 }
70 
71 
72 static char* s_VT_Descr
73 (CONNECTOR connector)
74 {
75  SNamedPipeConnector* xxx = (SNamedPipeConnector*) connector->handle;
76  return strdup(xxx->pipename.c_str());
77 }
78 
79 
81 (CONNECTOR connector,
82  const STimeout* timeout)
83 {
84  _ASSERT(timeout != kDefaultTimeout);
85  SNamedPipeConnector* xxx = (SNamedPipeConnector*) connector->handle;
86  return xxx->pipe->Open(xxx->pipename, timeout, xxx->pipesize);
87 }
88 
89 
91 (CONNECTOR connector,
92  EIO_Event event,
93  const STimeout* timeout)
94 {
95  _ASSERT(timeout != kDefaultTimeout);
96  _ASSERT(event == eIO_Read || event == eIO_Write);
97  SNamedPipeConnector* xxx = (SNamedPipeConnector*) connector->handle;
98  return xxx->pipe->Wait(event, timeout);
99 }
100 
101 
103 (CONNECTOR connector,
104  const void* buf,
105  size_t size,
106  size_t* n_written,
107  const STimeout* timeout)
108 {
109  _ASSERT(timeout != kDefaultTimeout);
110  SNamedPipeConnector* xxx = (SNamedPipeConnector*) connector->handle;
111  _VERIFY(xxx->pipe->SetTimeout(eIO_Write, timeout) == eIO_Success);
112  return xxx->pipe->Write(buf, size, n_written);
113 }
114 
115 
117 (CONNECTOR connector,
118  void* buf,
119  size_t size,
120  size_t* n_read,
121  const STimeout* timeout)
122 {
123  _ASSERT(timeout != kDefaultTimeout);
124  SNamedPipeConnector* xxx = (SNamedPipeConnector*) connector->handle;
125  _VERIFY(xxx->pipe->SetTimeout(eIO_Read, timeout) == eIO_Success);
126  return xxx->pipe->Read(buf, size, n_read);
127 }
128 
129 
131 (CONNECTOR connector,
132  EIO_Event dir)
133 {
134  SNamedPipeConnector* xxx = (SNamedPipeConnector*) connector->handle;
135  return xxx->pipe->Status(dir);
136 }
137 
138 
140 (CONNECTOR connector,
141  const STimeout* /*timeout*/)
142 {
143  SNamedPipeConnector* xxx = (SNamedPipeConnector*) connector->handle;
144  return xxx->pipe->Close();
145 }
146 
147 
148 static void s_Setup
149 (CONNECTOR connector)
150 {
151  SMetaConnector* meta = connector->meta;
152 
153  // Initialize virtual table
154  CONN_SET_METHOD(meta, get_type, s_VT_GetType, connector);
155  CONN_SET_METHOD(meta, descr, s_VT_Descr, connector);
156  CONN_SET_METHOD(meta, open, s_VT_Open, connector);
157  CONN_SET_METHOD(meta, wait, s_VT_Wait, connector);
158  CONN_SET_METHOD(meta, write, s_VT_Write, connector);
159  CONN_SET_METHOD(meta, flush, 0, 0);
160  CONN_SET_METHOD(meta, read, s_VT_Read, connector);
161  CONN_SET_METHOD(meta, status, s_VT_Status, connector);
162  CONN_SET_METHOD(meta, close, s_VT_Close, connector);
164 }
165 
166 
167 static void s_Destroy
168 (CONNECTOR connector)
169 {
170  SNamedPipeConnector* xxx = (SNamedPipeConnector*) connector->handle;
171  connector->handle = 0;
172 
173  _ASSERT(xxx->pipe);
174  delete xxx->pipe;
175  xxx->pipe = 0;
176  delete xxx;
177  free(connector);
178 }
179 
180 
181 } /* extern "C" */
182 
183 
185 
186 
187 /***********************************************************************
188  * EXTERNAL -- the connector's "constructors"
189  ***********************************************************************/
190 
192 (const string& pipename,
193  size_t pipesize)
194 {
195  auto ccc = make_c_unique((SConnector*) malloc(sizeof(SConnector)));
196  if (!ccc)
197  return 0;
198 
199  // Initialize internal data structures
200  unique_ptr<SNamedPipeConnector> xxx(new SNamedPipeConnector);
201  xxx->pipe = new CNamedPipeClient;
202  xxx->pipename = pipename;
203  xxx->pipesize = pipesize;
204 
205  // Initialize connector data
206  ccc->handle = xxx.release();
207  ccc->next = 0;
208  ccc->meta = 0;
209  ccc->setup = s_Setup;
210  ccc->destroy = s_Destroy;
211 
212  return ccc.release();
213 }
214 
215 
CNamedPipeClient –.
int close(int fd)
Definition: connection.cpp:45
unique_ptr< T, TDeleter > make_c_unique(T *p, TDeleter d)
Eliminates the necessity for specifying types of both allocated resource and deallocating C function.
Definition: ncbimisc.hpp:1448
CONNECTOR NAMEDPIPE_CreateConnector(const string &pipename, size_t pipesize)
Create CNamedPipe-based CONNECTOR.
#define CONN_SET_METHOD(meta, method, function, connector)
const STimeout * default_timeout
default timeout pointer
void * handle
data handle of the connector
SMetaConnector * meta
back link to original meta
#define _VERIFY(expr)
Definition: ncbidbg.hpp:161
EIO_Status Wait(EIO_Event event, const STimeout *timeout)
Wait for I/O readiness in the pipe.
EIO_Status Status(EIO_Event direction) const
Return (for the specified "direction"): eIO_Closed – if the pipe is closed for I/O; eIO_Timeout – if ...
EIO_Status Close(void)
Close pipe connection.
EIO_Status Read(void *buf, size_t count, size_t *n_read=0)
Read data from the pipe.
EIO_Status SetTimeout(EIO_Event event, const STimeout *timeout)
Specify timeout for the pipe I/O (see Open|Read|Write functions).
EIO_Status Open(const string &pipename, const STimeout *timeout=kDefaultTimeout, size_t pipesize=0, TFlags flags=0)
Open a client-side pipe connection.
EIO_Status Write(const void *buf, size_t count, size_t *n_written=0)
Write data to the pipe.
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define kInfiniteTimeout
Definition: ncbi_types.h:82
EIO_Status
I/O status.
Definition: ncbi_core.h:132
EIO_Event
I/O event (or direction).
Definition: ncbi_core.h:118
#define kDefaultTimeout
Definition: ncbi_types.h:81
@ eIO_Success
everything is fine, no error occurred
Definition: ncbi_core.h:133
@ eIO_Write
write
Definition: ncbi_core.h:121
@ eIO_Read
read
Definition: ncbi_core.h:120
char * buf
const struct ncbi::grid::netcache::search::fields::SIZE size
#define strdup
Definition: ncbi_ansi_ext.h:70
static EIO_Status s_VT_Close(CONNECTOR connector, const STimeout *)
static EIO_Status s_VT_Write(CONNECTOR connector, const void *buf, size_t size, size_t *n_written, const STimeout *timeout)
static EIO_Status s_VT_Read(CONNECTOR connector, void *buf, size_t size, size_t *n_read, const STimeout *timeout)
static EIO_Status s_VT_Open(CONNECTOR connector, const STimeout *timeout)
static const char * s_VT_GetType(CONNECTOR)
static void s_Setup(CONNECTOR connector)
static EIO_Status s_VT_Status(CONNECTOR connector, EIO_Event dir)
static EIO_Status s_VT_Wait(CONNECTOR connector, EIO_Event event, const STimeout *timeout)
static void s_Destroy(CONNECTOR connector)
static char * s_VT_Descr(CONNECTOR connector)
Implement CONNECTOR for a named pipe interprocess communication (based on the NCBI CNamedPipe).
Connector specification.
Standard set of connector methods to handle a connection (corresponding connectors are also in here),...
Timeout structure.
Definition: ncbi_types.h:76
#define _ASSERT
void free(voidpf ptr)
voidp malloc(uInt size)
Modified on Sun May 05 05:16:20 2024 by modify_doxy.py rev. 669887