VSM C++ SDK
Vehicle Specific Modules SDK
io_stream.h
Go to the documentation of this file.
1 // Copyright (c) 2014, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
4 
11 #ifndef _IO_STREAM_H_
12 #define _IO_STREAM_H_
13 
14 #include <ugcs/vsm/io_buffer.h>
18 
19 #include <chrono>
20 
21 namespace ugcs {
22 namespace vsm {
23 
25 enum class Io_result {
27  OK,
29  TIMED_OUT,
31  CANCELED,
38  CLOSED,
44  LOCK_ERROR,
48 };
49 
54 class Io_stream: public std::enable_shared_from_this<Io_stream> {
56 public:
59 
62  typedef int64_t Offset;
66  static const Offset OFFSET_NONE;
70  static const Offset OFFSET_END;
71 
74 
77 
80 
82  enum class State {
84  CLOSED,
87  OPENING,
90  OPENING_PASSIVE,
92  OPENED
93  };
94 
96  enum class Type {
97  FILE,
98  SERIAL,
99  ANDROID_SERIAL,
100  TCP,
101  UDP,
102  UDP_MULTICAST,
103  CAN,
104  UNDEFINED
105  };
106 
107  virtual
108  ~Io_stream()
109  {}
110 
112  void
113  Add_ref();
114 
118  void
119  Release_ref();
120 
135  Offset offset,
136  Write_handler completion_handler = Make_dummy_callback<void, Io_result>(),
138  {
139  if (!!completion_handler != !!comp_ctx) {
140  VSM_EXCEPTION(Invalid_param_exception, "Completion handler can not "
141  "exist without completion context and vice versa.");
142  }
143  return Write_impl(buffer, offset, completion_handler, comp_ctx);
144  }
145 
157  Write_handler completion_handler = Make_dummy_callback<void, Io_result>(),
159  {
160  if (!!completion_handler != !!comp_ctx) {
161  VSM_EXCEPTION(Invalid_param_exception, "Completion handler can not "
162  "exist without completion context and vice versa.");
163  }
164  return Write_impl(buffer, OFFSET_NONE, completion_handler, comp_ctx);
165  }
166 
185  Read(size_t max_to_read, size_t min_to_read, Offset offset,
186  Read_handler completion_handler = Make_dummy_callback<void, Io_buffer::Ptr, Io_result>(),
188  {
189  if (!!completion_handler != !!comp_ctx) {
190  VSM_EXCEPTION(Invalid_param_exception, "Completion handler can not "
191  "exist without completion context and vice versa.");
192  }
193  if (max_to_read < min_to_read) {
194  VSM_EXCEPTION(Invalid_param_exception, "max_to_read cannot be less than min_to_read");
195  }
196  return Read_impl(max_to_read, min_to_read, offset, completion_handler,
197  comp_ctx);
198  }
199 
215  Read(size_t max_to_read, size_t min_to_read = 1,
216  Read_handler completion_handler = Make_dummy_callback<void, Io_buffer::Ptr, Io_result>(),
218  {
219  if (!!completion_handler != !!comp_ctx) {
220  VSM_EXCEPTION(Invalid_param_exception, "Completion handler can not "
221  "exist without completion context and vice versa.");
222  }
223  if (max_to_read < min_to_read) {
224  VSM_EXCEPTION(Invalid_param_exception, "max_to_read cannot be less than min_to_read");
225  }
226  return Read_impl(max_to_read, min_to_read, OFFSET_NONE, completion_handler, comp_ctx);
227  }
228 
237  Close(Close_handler completion_handler = Make_dummy_callback<void>(),
239  {
240  if (!!completion_handler != !!comp_ctx) {
241  VSM_EXCEPTION(Invalid_param_exception, "Completion handler can not "
242  "exist without completion context and vice versa.");
243  }
244  return Close_impl(completion_handler, comp_ctx);
245  }
246 
250  Io_stream(const Io_stream&) = delete;
251 
256  stream_type(type)
257  {}
258 
260  State
261  Get_state() const
262  {
263  return state;
264  }
265 
267  bool
268  Is_closed() const
269  {
270  return (state == State::CLOSED);
271  }
272 
274  std::string
275  Get_name() const;
276 
277  Type
278  Get_type() const
279  {
280  return stream_type;
281  }
282 
284  static const char*
285  Io_result_as_char(const Io_result res);
286 
287 protected:
288  Type stream_type;
289 
293  std::atomic_int ref_count = { 0 };
294 
305  virtual Operation_waiter
306  Write_impl(Io_buffer::Ptr buffer, Offset offset,
307  Write_handler completion_handler,
308  Request_completion_context::Ptr comp_ctx) = 0;
309 
320  virtual Operation_waiter
321  Read_impl(size_t max_to_read, size_t min_to_read, Offset offset,
322  Read_handler completion_handler,
323  Request_completion_context::Ptr comp_ctx) = 0;
324 
332  virtual Operation_waiter
333  Close_impl(Close_handler completion_handler,
334  Request_completion_context::Ptr comp_ctx) = 0;
335 
337  void
338  Set_name(const std::string&);
339 
340 private:
341 
345  static std::mutex name_mutex;
346 
348  std::string name = "[undefined]";
349 };
350 
353 
354 
355 DEFINE_CALLBACK_BUILDER(Make_read_callback, (Io_buffer::Ptr, Io_result),
356  (nullptr, Io_result::OTHER_FAILURE))
357 
358 } /* namespace vsm */
359 } /* namespace ugcs */
360 
361 #endif /* _IO_STREAM_H_ */
UGCS root namespace.
Definition: android-linux/ugcs/vsm/platform_sockets.h:27
Operation waiter class.
Exception class with one parameter.
Definition: exception.h:88
Io_stream(Type type)
Constructor.
Definition: io_stream.h:255
#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:62
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:215
static Ptr Create(Args &&...args)
Create an instance.
Definition: request_temp_completion_context.h:19
Io_result
Result of I/O operation.
Definition: io_stream.h:25
Reference guard class definition.
Abstract I/O stream interface.
Definition: io_stream.h:54
Operation completed successfully.
Bad address specific.
bool Is_closed() const
Checks if stream is closed or not.
Definition: io_stream.h:268
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:185
Some other system failure.
Remote side has explicitly refused the connection.
Helper class for proxying callback invocation.
Definition: callback.h:691
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:134
End of file encountered.
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:156
Generic I/O buffer.
Definition: io_buffer.h:33
Operation canceled.
Type
Stream types.
Definition: io_stream.h:96
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:237
#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:55
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.
State
Stream states.
Definition: io_stream.h:82
#define VSM_EXCEPTION(__exc_class, __msg,...)
Throw VSM exception instance.
Definition: exception.h:170
State Get_state() const
Get current state of the stream.
Definition: io_stream.h:261
Operation timed out.
Io_buffer class implementation.
Class for synchronizing with request execution.
Definition: operation_waiter.h:24