Diag-Client-Lib
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Public Types | Public Member Functions | Private Attributes | List of all members
boost_support::socket::IoContext Class Referencefinal

Wrapper class to hold boost io context required for io object( sockets) More...

#include <io_context.h>

Collaboration diagram for boost_support::socket::IoContext:
Collaboration graph
[legend]

Public Types

using Context = boost::asio::io_context
 Type alias for boost context. More...
 

Public Member Functions

 IoContext () noexcept
 Default constructs an instance of IoContext. More...
 
 IoContext (std::string_view context_name) noexcept
 Constructs an instance of IoContext. More...
 
 IoContext (const IoContext &other) noexcept=delete
 Deleted copy assignment and copy constructor. More...
 
IoContextoperator= (const IoContext &other) noexcept=delete
 
 IoContext (IoContext &&other) noexcept=delete
 Deleted move assignment and move constructor. More...
 
IoContextoperator= (IoContext &&other) noexcept=delete
 
 ~IoContext () noexcept
 Destruct an instance of IoContext. More...
 
void Initialize () noexcept
 Initialize the context. More...
 
void DeInitialize () noexcept
 De-initialize the context. More...
 
ContextGetContext () noexcept
 Function to get the io context reference. More...
 

Private Attributes

Context io_context_
 boost io context More...
 
std::atomic_bool exit_request_
 Flag to terminate the thread. More...
 
std::atomic_bool running_
 Flag to start the thread. More...
 
std::condition_variable cond_var_
 Conditional variable to block the thread. More...
 
std::string context_name_
 The name of context. More...
 
utility::thread::Thread thread_
 The thread itself. More...
 
std::mutex mutex_
 mutex to lock critical section More...
 

Detailed Description

Wrapper class to hold boost io context required for io object( sockets)

Definition at line 22 of file io_context.h.

Member Typedef Documentation

◆ Context

using boost_support::socket::IoContext::Context = boost::asio::io_context

Type alias for boost context.

Definition at line 27 of file io_context.h.

Constructor & Destructor Documentation

◆ IoContext() [1/4]

boost_support::socket::IoContext::IoContext ( )
noexcept

Default constructs an instance of IoContext.

Definition at line 14 of file io_context.cpp.

15  : io_context_{},
16  exit_request_{false},
17  running_{false},
18  cond_var_{},
19  mutex_{} {}
std::atomic_bool exit_request_
Flag to terminate the thread.
Definition: io_context.h:82
std::mutex mutex_
mutex to lock critical section
Definition: io_context.h:107
Context io_context_
boost io context
Definition: io_context.h:77
std::condition_variable cond_var_
Conditional variable to block the thread.
Definition: io_context.h:92
std::atomic_bool running_
Flag to start the thread.
Definition: io_context.h:87

◆ IoContext() [2/4]

boost_support::socket::IoContext::IoContext ( std::string_view  context_name)
noexcept

Constructs an instance of IoContext.

Definition at line 21 of file io_context.cpp.

22  : io_context_{},
23  exit_request_{false},
24  running_{false},
25  cond_var_{},
26  context_name_{context_name},
27  thread_{},
28  mutex_{} {
29  // start thread to execute async tasks
31  std::unique_lock<std::mutex> lck(mutex_);
32  while (!exit_request_) {
33  if (!running_) {
34  cond_var_.wait(lck, [this]() { return exit_request_ || running_; });
35  }
36  if (!exit_request_) {
37  if (running_) {
38  lck.unlock();
39  io_context_.restart();
40  io_context_.run();
41  lck.lock();
42  }
43  }
44  }
45  });
46 }
std::string context_name_
The name of context.
Definition: io_context.h:97
utility::thread::Thread thread_
The thread itself.
Definition: io_context.h:102

◆ IoContext() [3/4]

boost_support::socket::IoContext::IoContext ( const IoContext other)
deletenoexcept

Deleted copy assignment and copy constructor.

◆ IoContext() [4/4]

boost_support::socket::IoContext::IoContext ( IoContext &&  other)
deletenoexcept

Deleted move assignment and move constructor.

◆ ~IoContext()

boost_support::socket::IoContext::~IoContext ( )
noexcept

Destruct an instance of IoContext.

Definition at line 48 of file io_context.cpp.

48  {
49  exit_request_ = true;
50  running_ = false;
51  cond_var_.notify_all();
52  thread_.Join();
53 }
void Join() noexcept
Function to join the running thread.
Definition: thread.h:62

References cond_var_, exit_request_, utility::thread::Thread::Join(), running_, and thread_.

Here is the call graph for this function:

Member Function Documentation

◆ DeInitialize()

void boost_support::socket::IoContext::DeInitialize ( )
noexcept

De-initialize the context.

Definition at line 61 of file io_context.cpp.

61  {
62  std::lock_guard<std::mutex> lock{mutex_};
63  running_ = false;
64  io_context_.stop();
65  cond_var_.notify_all();
66 }

References cond_var_, io_context_, mutex_, and running_.

Referenced by boost_support::client::udp::UdpClient::UdpClientImpl::DeInitialize().

Here is the caller graph for this function:

◆ GetContext()

IoContext::Context & boost_support::socket::IoContext::GetContext ( )
noexcept

Function to get the io context reference.

Returns
The reference to io context

Definition at line 68 of file io_context.cpp.

68 { return io_context_; }

References io_context_.

◆ Initialize()

void boost_support::socket::IoContext::Initialize ( )
noexcept

Initialize the context.

Definition at line 55 of file io_context.cpp.

55  {
56  std::lock_guard<std::mutex> lock{mutex_};
57  running_ = true;
58  cond_var_.notify_all();
59 }

References cond_var_, mutex_, and running_.

Referenced by boost_support::client::udp::UdpClient::UdpClientImpl::Initialize().

Here is the caller graph for this function:

◆ operator=() [1/2]

IoContext& boost_support::socket::IoContext::operator= ( const IoContext other)
deletenoexcept

◆ operator=() [2/2]

IoContext& boost_support::socket::IoContext::operator= ( IoContext &&  other)
deletenoexcept

Member Data Documentation

◆ cond_var_

std::condition_variable boost_support::socket::IoContext::cond_var_
private

Conditional variable to block the thread.

Definition at line 92 of file io_context.h.

Referenced by DeInitialize(), Initialize(), and ~IoContext().

◆ context_name_

std::string boost_support::socket::IoContext::context_name_
private

The name of context.

Definition at line 97 of file io_context.h.

◆ exit_request_

std::atomic_bool boost_support::socket::IoContext::exit_request_
private

Flag to terminate the thread.

Definition at line 82 of file io_context.h.

Referenced by ~IoContext().

◆ io_context_

Context boost_support::socket::IoContext::io_context_
private

boost io context

Definition at line 77 of file io_context.h.

Referenced by DeInitialize(), and GetContext().

◆ mutex_

std::mutex boost_support::socket::IoContext::mutex_
private

mutex to lock critical section

Definition at line 107 of file io_context.h.

Referenced by DeInitialize(), and Initialize().

◆ running_

std::atomic_bool boost_support::socket::IoContext::running_
private

Flag to start the thread.

Definition at line 87 of file io_context.h.

Referenced by DeInitialize(), Initialize(), and ~IoContext().

◆ thread_

utility::thread::Thread boost_support::socket::IoContext::thread_
private

The thread itself.

Definition at line 102 of file io_context.h.

Referenced by ~IoContext().


The documentation for this class was generated from the following files: