BDE 4.14.0 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.
99struct PipeUtil {
100
101 /// Load into the specified `pipeName` the system-dependent canonical pipe
102 /// name corresponding to the specified `baseName`. Return 0 on success,
103 /// and a nonzero value if `baseName` cannot be part of a pipe name on this
104 /// system.
105 ///
106 /// On Unix systems, the canonical name is defined by prefixing `baseName`
107 /// with the directory specified by the `SOCKDIR` environment variable if
108 /// it is set, otherwise with the directory specified by the `TMPDIR`
109 /// environment variable if it is set, and otherwise by the current
110 /// directory.
111 ///
112 /// On Windows systems, the canonical name is defined by prefixing
113 /// `baseName` with "\\.\pipe\".
114 ///
115 static int makeCanonicalName(bsl::string *pipeName,
116 const bsl::string_view& baseName);
117 /// Finally, any uppercase characters in `baseName` are converted to lower
118 /// case in the canonical name.
119 static int makeCanonicalName(std::string *pipeName,
120 const bsl::string_view& baseName);
121#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR_STRING
122 static int makeCanonicalName(std::pmr::string *pipeName,
123 const bsl::string_view& baseName);
124#endif
125
126 /// Send the specified `message` to the pipe with the specified UTF-8
127 /// `pipeName`. Return 0 on success, and a nonzero value otherwise.
128 /// `message is output in a single `write' operation; consequently,
129 /// messages that do not exceed the `PIPE_BUF` value (see
130 /// @ref bdls_pipeutil-pipe-atomicity will not be interleaved even when multiple
131 /// concurrent processes are writing to `pipeName`. The behavior is
132 /// undefined unless `pipeName` is a valid UTF-8 string.
133 static int send(const bsl::string_view& pipeName,
134 const bsl::string_view& message);
135
136 /// Return `true` if the pipe with the specified UTF-8 `pipeName` exists,
137 /// the calling process has permission to write to it, and some process is
138 /// able to read the bytes written to it, and `false` otherwise. On
139 /// Windows, this function may block and may write an "empty message",
140 /// consisting of a single newline. The behavior is undefined unless
141 /// `pipeName` is a valid UTF-8 string. Note that even though a process
142 /// might have the pipe open for reading, this function might still return
143 /// `false` because there are not sufficient resources available.
144 static bool isOpenForReading(const bsl::string_view& pipeName);
145};
146
147} // close package namespace
148
149
150#endif
151
152// ----------------------------------------------------------------------------
153// Copyright 2015 Bloomberg Finance L.P.
154//
155// Licensed under the Apache License, Version 2.0 (the "License");
156// you may not use this file except in compliance with the License.
157// You may obtain a copy of the License at
158//
159// http://www.apache.org/licenses/LICENSE-2.0
160//
161// Unless required by applicable law or agreed to in writing, software
162// distributed under the License is distributed on an "AS IS" BASIS,
163// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
164// See the License for the specific language governing permissions and
165// limitations under the License.
166// ----------------------------- END-OF-FILE ----------------------------------
167
168/** @} */
169/** @} */
170/** @} */
Definition bslstl_stringview.h:441
Definition bslstl_string.h:1281
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdls_fdstreambuf.h:412
Definition bdls_pipeutil.h:99
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)