Mstdlib-1.24.0
m_io_block.h
1/* The MIT License (MIT)
2 *
3 * Copyright (c) 2017 Monetra Technologies, LLC.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24#ifndef __M_IO_BLOCK_H__
25#define __M_IO_BLOCK_H__
26
27/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
28
29#include <mstdlib/base/m_defs.h>
30#include <mstdlib/base/m_types.h>
31#include <mstdlib/io/m_io.h>
32#include <mstdlib/io/m_event.h>
33
34/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
35
36__BEGIN_DECLS
37
38/*! \addtogroup m_io_block Common Blocking (synchronous) IO functions
39 * \ingroup m_eventio
40 *
41 * The io system can be used with blocking operations. An explicit event loop
42 * is not required. This allows the stacked layers to be utilized with a more
43 * traditional blocking design.
44 *
45 * Here is an example of the system using the loopback io back end to simulate
46 * a network connection to a remote server.
47 *
48 * \code{.c}
49 * #include <mstdlib/mstdlib.h>
50 * #include <mstdlib/mstdlib_io.h>
51 *
52 * int main(int argc, char *argv)
53 * {
54 * M_io_t *io = NULL;
55 * M_buf_t *buf;
56 * M_parser_t *parser;
57 * char *out;
58 *
59 * buf = M_buf_create();
60 * parser = M_parser_create(M_PARSER_FLAG_NONE);
61 * M_io_loopback_create(&io);
62 *
63 * M_io_block_connect(io);
64 *
65 * M_buf_add_str(buf, "TEST 123");
66 * M_io_block_write_from_buf(io, buf, M_TIMEOUT_INF);
67 *
68 * M_io_block_read_into_parser(io, parser, M_TIMEOUT_INF);
69 * out = M_parser_read_strdup(parser, M_parser_len(parser));
70 * M_printf("%s\n", out);
71 * M_free(out);
72 *
73 * M_buf_add_str(buf, "abc 456");
74 * M_io_block_write_from_buf(io, buf, M_TIMEOUT_INF);
75 *
76 * M_io_block_read_into_parser(io, parser, M_TIMEOUT_INF);
77 * out = M_parser_read_strdup(parser, M_parser_len(parser));
78 * M_printf("%s\n", out);
79 * M_free(out);
80 *
81 * M_parser_destroy(parser);
82 * M_buf_cancel(buf);
83 * M_io_block_disconnect(io);
84 * return 0;
85 * }
86 * \endcode
87 *
88 * @{
89 */
90
91
92/*! Connect the io object to the remote end point.
93 *
94 * \param[in] io io object.
95 *
96 * \return Result.
97 */
99
100
101/*! Accept an io connection.
102 *
103 * \param[out] io_out io object created from the accept.
104 * \param[in] server_io io object which was listening.
105 * \param[in] timeout_ms Amount of time in milliseconds to wait for data.
106 *
107 * \return Result.
108 */
109M_API M_io_error_t M_io_block_accept(M_io_t **io_out, M_io_t *server_io, M_uint64 timeout_ms);
110
111
112/*! Read from an io object.
113 *
114 * \param[in] io io object.
115 * \param[out] buf Buffer to store data read from io object.
116 * \param[in] buf_len Lenght of provided buffer.
117 * \param[out] len_read Number of bytes fread from the io object.
118 * \param[in] timeout_ms Amount of time in milliseconds to wait for data.
119 *
120 * \return Result.
121 */
122M_API M_io_error_t M_io_block_read(M_io_t *io, unsigned char *buf, size_t buf_len, size_t *len_read, M_uint64 timeout_ms);
123
124
125/*! Read from an io object into an M_buf_t.
126 *
127 * This will read all available data into the buffer.
128 *
129 * \param[in] io io object.
130 * \param[out] buf Buffer to store data read from io object.
131 * \param[in] timeout_ms Amount of time in milliseconds to wait for data.
132 *
133 * \return Result.
134 */
135M_API M_io_error_t M_io_block_read_into_buf(M_io_t *io, M_buf_t *buf, M_uint64 timeout_ms);
136
137
138/*! Read from an io object into an M_parser_t.
139 *
140 * This will read all available data into the buffer.
141 *
142 * \param[in] io io object.
143 * \param[out] parser Parser to store data read from io object.
144 * \param[in] timeout_ms Amount of time in milliseconds to wait for data.
145 *
146 * \return Result.
147 */
148M_API M_io_error_t M_io_block_read_into_parser(M_io_t *io, M_parser_t *parser, M_uint64 timeout_ms);
149
150
151/*! Write data to an io object.
152 *
153 * This function will attempt to write as much data as possible. If not all data
154 * is written the application should try again.
155 *
156 * \param[in] io io object.
157 * \param[in] buf Buffer to write from.
158 * \param[in] buf_len Number of bytes in buffer to write.
159 * \param[out] len_written Number of bytes from the buffer written.
160 * \param[in] timeout_ms Amount of time in milliseconds to wait for data.
161 *
162 * \return Result.
163 */
164M_API M_io_error_t M_io_block_write(M_io_t *io, const unsigned char *buf, size_t buf_len, size_t *len_written, M_uint64 timeout_ms);
165
166
167/*! Write data to an io object from an M_buf_t.
168 *
169 * This function will attempt to write as much data as possible. If not all data
170 * is written the application should try again.
171 *
172 * \param[in] io io object.
173 * \param[in] buf Buffer to write from.
174 * \param[in] timeout_ms Amount of time in milliseconds to wait for data.
175 *
176 * \return Result.
177 */
178M_API M_io_error_t M_io_block_write_from_buf(M_io_t *io, M_buf_t *buf, M_uint64 timeout_ms);
179
180/*! Gracefully issue a disconnect to the communications object.
181 *
182 * \param[in] io io object.
183 *
184 * \return Result.
185 */
187
188/*! @} */
189
190__END_DECLS
191
192#endif /* __M_IO_BLOCK_H__ */
struct M_buf M_buf_t
Definition: m_buf.h:77
M_io_error_t M_io_block_read_into_buf(M_io_t *io, M_buf_t *buf, M_uint64 timeout_ms)
M_io_error_t M_io_block_write_from_buf(M_io_t *io, M_buf_t *buf, M_uint64 timeout_ms)
M_io_error_t M_io_block_read(M_io_t *io, unsigned char *buf, size_t buf_len, size_t *len_read, M_uint64 timeout_ms)
M_io_error_t M_io_block_accept(M_io_t **io_out, M_io_t *server_io, M_uint64 timeout_ms)
M_io_error_t M_io_block_disconnect(M_io_t *io)
M_io_error_t M_io_block_write(M_io_t *io, const unsigned char *buf, size_t buf_len, size_t *len_written, M_uint64 timeout_ms)
M_io_error_t M_io_block_read_into_parser(M_io_t *io, M_parser_t *parser, M_uint64 timeout_ms)
M_io_error_t M_io_block_connect(M_io_t *io)
enum M_io_error M_io_error_t
Definition: m_io.h:93
struct M_io M_io_t
Definition: m_io.h:59
struct M_parser M_parser_t
Definition: m_parser.h:52