BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdls_pipeutil.h
Go to the documentation of this file.
1/// @file bdls_pipeutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdls_pipeutil.h -*-C++-*-
8#ifndef INCLUDED_BDLS_PIPEUTIL
9#define INCLUDED_BDLS_PIPEUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdls_pipeutil bdls_pipeutil
15/// @brief Provide basic portable named-pipe utilities.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdls
19/// @{
20/// @addtogroup bdls_pipeutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdls_pipeutil-purpose"> Purpose</a>
25/// * <a href="#bdls_pipeutil-classes"> Classes </a>
26/// * <a href="#bdls_pipeutil-description"> Description </a>
27/// * <a href="#bdls_pipeutil-pipe-atomicity"> Pipe Atomicity </a>
28///
29/// # Purpose {#bdls_pipeutil-purpose}
30/// Provide basic portable named-pipe utilities.
31///
32/// # Classes {#bdls_pipeutil-classes}
33///
34/// - bdls::PipeUtil: Portable utility methods for naming and accessing pipes
35///
36/// @see balb_pipecontrolchannel
37///
38/// # Description {#bdls_pipeutil-description}
39/// This component, `bdls::PipeUtil`, provides portable utility
40/// methods for named pipes.
41///
42/// ## Pipe Atomicity {#bdls_pipeutil-pipe-atomicity}
43///
44///
45/// Applications that expect multiple writers to a single pipe must should be
46/// aware that message content might be corrupted (interleaved) unless:
47///
48/// 1. Each message is written to the pipe in a single `write` system call.
49/// 2. The length of each message is less than `PIPE_BUF` (the limit for
50/// guaranteed atomicity).
51///
52/// The value `PIPE_BUF` depends on the platform:
53/// @code
54/// +------------------------------+------------------+
55/// | Platform | PIPE_BUF (bytes) |
56/// +------------------------------+------------------+
57/// | POSIX (minimum requirement)) | 512 |
58/// | IBM | 32,768 |
59/// | SUN | 32,768 |
60/// | Linux | 65,536 |
61/// | Windows | 65,536 |
62/// +------------------------------+------------------+
63/// @endcode
64///
65/// Also note that Linux allows the `PIPE_BUF` size to be changed via the
66/// `fcntl` system call.
67/// @}
68/** @} */
69/** @} */
70
71/** @addtogroup bdl
72 * @{
73 */
74/** @addtogroup bdls
75 * @{
76 */
77/** @addtogroup bdls_pipeutil
78 * @{
79 */
80
81#include <bdlscm_version.h>
82
83#include <bsl_string.h>
84#include <bsl_string_view.h>
85
87
88#include <string> // 'std::string', 'std::pmr::string'
89
90
91namespace bdls {
92
93 // ===============
94 // struct PipeUtil
95 // ===============
96
97/// This struct contains utility methods for platform-independent named pipe
98/// operations.
99///
100/// See @ref bdls_pipeutil
101struct PipeUtil {
102
103 /// Load into the specified `pipeName` the system-dependent canonical pipe
104 /// name corresponding to the specified `baseName`. Return 0 on success,
105 /// and a nonzero value if `baseName` cannot be part of a pipe name on this
106 /// system.
107 ///
108 /// On Unix systems, the canonical name is defined by prefixing `baseName`
109 /// with the directory specified by the `SOCKDIR` environment variable if
110 /// it is set, otherwise with the directory specified by the `TMPDIR`
111 /// environment variable if it is set, and otherwise by the current
112 /// directory.
113 ///
114 /// On Windows systems, the canonical name is defined by prefixing
115 /// `baseName` with "\\.\pipe\".
116 ///
117 /// Finally, any uppercase characters in `baseName` are converted to lower
118 /// case in the canonical name.
119 static int makeCanonicalName(bsl::string *pipeName,
120 const bsl::string_view& baseName);
121 static int makeCanonicalName(std::string *pipeName,
122 const bsl::string_view& baseName);
123#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR_STRING
124 static int makeCanonicalName(std::pmr::string *pipeName,
125 const bsl::string_view& baseName);
126#endif
127
128 /// Send the specified `message` to the pipe with the specified UTF-8
129 /// `pipeName`. Return 0 on success, and a nonzero value otherwise.
130 /// `message is output in a single `write' operation; consequently,
131 /// messages that do not exceed the `PIPE_BUF` value (see
132 /// @ref bdls_pipeutil-pipe-atomicity will not be interleaved even when multiple
133 /// concurrent processes are writing to `pipeName`.
134 ///
135 /// \pre The behavior is undefined unless `pipeName` is a valid UTF-8 string.
136 static int send(const bsl::string_view& pipeName,
137 const bsl::string_view& message);
138
139 /// Return `true` if the pipe with the specified UTF-8 `pipeName` exists,
140 /// the calling process has permission to write to it, and some process is
141 /// able to read the bytes written to it, and `false` otherwise. On
142 /// Windows, this function may block and may write an "empty message", consisting of a single newline.
143 ///
144 /// \pre The behavior is undefined unless `pipeName` is a valid UTF-8 string.
145 ///
146 /// \note Note that even though a process
147 /// might have the pipe open for reading, this function might still return
148 /// `false` because there are not sufficient resources available.
149 static bool isOpenForReading(const bsl::string_view& pipeName);
150};
151
152} // close package namespace
153
154
155#endif
156
157// ----------------------------------------------------------------------------
158// Copyright 2015 Bloomberg Finance L.P.
159//
160// Licensed under the Apache License, Version 2.0 (the "License");
161// you may not use this file except in compliance with the License.
162// You may obtain a copy of the License at
163//
164// http://www.apache.org/licenses/LICENSE-2.0
165//
166// Unless required by applicable law or agreed to in writing, software
167// distributed under the License is distributed on an "AS IS" BASIS,
168// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
169// See the License for the specific language governing permissions and
170// limitations under the License.
171// ----------------------------- END-OF-FILE ----------------------------------
172
173/** @} */
174/** @} */
175/** @} */
Definition bslstl_stringview.h:471
Definition bslstl_string.h:1252
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdls_fdstreambuf.h:412
Definition bdls_pipeutil.h:101
static int makeCanonicalName(bsl::string *pipeName, const bsl::string_view &baseName)
static int send(const bsl::string_view &pipeName, const bsl::string_view &message)
static int makeCanonicalName(std::string *pipeName, const bsl::string_view &baseName)
static bool isOpenForReading(const bsl::string_view &pipeName)