11 #ifndef _UGCS_VSM_IO_STREAM_H_
12 #define _UGCS_VSM_IO_STREAM_H_
29 static constexpr
int MIN_UDP_PAYLOAD_SIZE_TO_READ = 1500 - 60 - 8;
34 static constexpr
int MAX_TCP_PAYLOAD_SIZE_TO_READ = 1500 - 60 - 40;
66 class Io_stream:
public std::enable_shared_from_this<Io_stream> {
149 Write_handler completion_handler = Make_dummy_callback<void, Io_result>(),
152 if (!!completion_handler != !!comp_ctx) {
153 VSM_EXCEPTION(Invalid_param_exception,
"Completion handler can not "
154 "exist without completion context and vice versa.");
156 return Write_impl(buffer, offset, completion_handler, comp_ctx);
170 Write_handler completion_handler = Make_dummy_callback<void, Io_result>(),
173 if (!!completion_handler != !!comp_ctx) {
174 VSM_EXCEPTION(Invalid_param_exception,
"Completion handler can not "
175 "exist without completion context and vice versa.");
199 Read_handler completion_handler = Make_dummy_callback<void, Io_buffer::Ptr, Io_result>(),
202 if (!!completion_handler != !!comp_ctx) {
203 VSM_EXCEPTION(Invalid_param_exception,
"Completion handler can not "
204 "exist without completion context and vice versa.");
206 if (max_to_read < min_to_read) {
207 VSM_EXCEPTION(Invalid_param_exception,
"max_to_read cannot be less than min_to_read");
209 return Read_impl(max_to_read, min_to_read, offset, completion_handler,
232 Read(
size_t max_to_read,
size_t min_to_read = 1,
233 Read_handler completion_handler = Make_dummy_callback<void, Io_buffer::Ptr, Io_result>(),
236 if (!!completion_handler != !!comp_ctx) {
237 VSM_EXCEPTION(Invalid_param_exception,
"Completion handler can not "
238 "exist without completion context and vice versa.");
240 if (min_to_read && max_to_read == 0) {
243 switch (stream_type) {
245 case Type::UDP_MULTICAST:
246 max_to_read = MIN_UDP_PAYLOAD_SIZE_TO_READ;
249 max_to_read = MAX_TCP_PAYLOAD_SIZE_TO_READ;
252 max_to_read = min_to_read;
255 }
else if (max_to_read < min_to_read) {
256 VSM_EXCEPTION(Invalid_param_exception,
"max_to_read cannot be less than min_to_read");
272 if (!!completion_handler != !!comp_ctx) {
273 VSM_EXCEPTION(Invalid_param_exception,
"Completion handler can not "
274 "exist without completion context and vice versa.");
276 return Close_impl(completion_handler, comp_ctx);
376 static std::mutex name_mutex;
379 std::string name =
"[undefined]";
387 (
nullptr, Io_result::OTHER_FAILURE))
State state
Current state of the stream.
Definition: io_stream.h:323
static const Offset OFFSET_NONE
Offset special value which indicates that the offset value is not specified.
Definition: io_stream.h:79
Io_stream(Type type)
Constructor.
Definition: io_stream.h:287
#define DEFINE_CALLBACK_BUILDER(__name, __types, __values)
Define callback builder function.
Definition: callback.h:42
int64_t Offset
Offset for read/write operations.
Definition: io_stream.h:75
Operation_waiter Read(size_t max_to_read, size_t min_to_read=1, Read_handler completion_handler=Make_dummy_callback< void, Io_buffer::Ptr, Io_result >(), Request_completion_context::Ptr comp_ctx=Request_temp_completion_context::Create())
Initiate read operation.
Definition: io_stream.h:232
static Ptr Create(Args &&...args)
Create an instance.
Definition: request_temp_completion_context.h:19
Callback_proxy< void, Io_result > Write_handler
Default prototype for write operation completion handler.
Definition: io_stream.h:86
Io_result
Result of I/O operation.
Definition: io_stream.h:37
Reference guard class definition.
Abstract I/O stream interface.
Definition: io_stream.h:66
Operation completed successfully.
virtual Operation_waiter Close_impl(Close_handler completion_handler, Request_completion_context::Ptr comp_ctx)=0
Close call implementation.
bool Is_closed() const
Checks if stream is closed or not.
Definition: io_stream.h:300
Operation_waiter Read(size_t max_to_read, size_t min_to_read, Offset offset, Read_handler completion_handler=Make_dummy_callback< void, Io_buffer::Ptr, Io_result >(), Request_completion_context::Ptr comp_ctx=Request_temp_completion_context::Create())
Initiate read operation.
Definition: io_stream.h:198
Some other system failure.
Callback_proxy< void, Io_buffer::Ptr, Io_result > Read_handler
Default prototype for read operation completion handler.
Definition: io_stream.h:89
Remote side has explicitly refused the connection.
Helper class for proxying callback invocation.
Definition: callback.h:699
Operation_waiter Write(Io_buffer::Ptr buffer, Offset offset, Write_handler completion_handler=Make_dummy_callback< void, Io_result >(), Request_completion_context::Ptr comp_ctx=Request_temp_completion_context::Create())
Initiate write operation.
Definition: io_stream.h:147
void Set_name(const std::string &)
Set the stream name.
virtual Operation_waiter Read_impl(size_t max_to_read, size_t min_to_read, Offset offset, Read_handler completion_handler, Request_completion_context::Ptr comp_ctx)=0
Read call implementation.
std::atomic_int ref_count
Reference counter.
Definition: io_stream.h:325
Operation_waiter Write(Io_buffer::Ptr buffer, Write_handler completion_handler=Make_dummy_callback< void, Io_result >(), Request_completion_context::Ptr comp_ctx=Request_temp_completion_context::Create())
Initiate write operation.
Definition: io_stream.h:169
Generic I/O buffer.
Definition: io_buffer.h:33
virtual Operation_waiter Write_impl(Io_buffer::Ptr buffer, Offset offset, Write_handler completion_handler, Request_completion_context::Ptr comp_ctx)=0
Write call implementation.
void Release_ref()
Release reference for the stream.
Type
Stream types.
Definition: io_stream.h:109
void Add_ref()
Add reference to the stream.
std::shared_ptr< Request_context > Ptr
Pointer type.
Definition: request_context.h:25
std::shared_ptr< Io_buffer > Ptr
Pointer type.
Definition: io_buffer.h:34
Operation_waiter Close(Close_handler completion_handler=Make_dummy_callback< void >(), Request_completion_context::Ptr comp_ctx=Request_temp_completion_context::Create())
Initiate stream close operation.
Definition: io_stream.h:269
static const Offset OFFSET_END
Offset special value which indicates that the offset value corresponds to the stream end (e...
Definition: io_stream.h:83
std::string Get_name() const
Get human readable stream name.
Io_stream(const Io_stream &)=delete
There is no sense in copying the stream.
#define DEFINE_COMMON_CLASS(__class_name,...)
Use this macro to define some common attributes for a class.
Definition: utils.h:25
std::shared_ptr< Io_stream > Ptr
Pointer type.
Definition: io_stream.h:67
Callback_proxy< void > Close_handler
Default prototype for close operation completion handler.
Definition: io_stream.h:92
Insufficient permissions for the requested operation.
Reference guard objects keep references for managed objects.
Definition: reference_guard.h:31
Stream has been or is closed.
static const char * Io_result_as_char(const Io_result res)
Convert Io_result value to character string.
State
Stream states.
Definition: io_stream.h:95
#define VSM_EXCEPTION(__exc_class, __msg,...)
Throw VSM exception instance.
Definition: exception.h:168
State Get_state() const
Get current state of the stream.
Definition: io_stream.h:293
Io_buffer class implementation.
Class for synchronizing with request execution.
Definition: operation_waiter.h:24