26 std::uint16_t port_num) {
27 std::string connection_name{client_name};
28 connection_name.append(
"_");
29 connection_name.append(ip_address);
30 connection_name.append(
"_");
31 connection_name.append(std::to_string(port_num));
32 return connection_name;
39 template<
typename TlsVersion>
56 enum class State : std::uint8_t {
79 TlsClientImpl(std::string_view client_name, std::string_view local_ip_address,
80 std::uint16_t local_port_num, std::string_view ca_certification_path,
83 tls_context_{tls_version, ca_certification_path},
84 connection_state_{State::kDisconnected},
86 tcp_connection_{client_name,
87 TlsSocket{local_ip_address, local_port_num, tls_context_, io_context_}} {}
135 std::uint16_t host_port_num) {
138 if (connection_state_.load(std::memory_order_seq_cst) != State::kConnected) {
139 if (tcp_connection_.ConnectToHost(host_ip_address, host_port_num)) {
140 connection_state_.store(State::kConnected, std::memory_order_seq_cst);
141 result.EmplaceValue();
145 result.EmplaceValue();
148 [](std::stringstream &msg) { msg <<
"Tcp client is already connected"; });
160 if (connection_state_.load(std::memory_order_seq_cst) == State::kConnected) {
161 tcp_connection_.DisconnectFromHost();
162 connection_state_.store(State::kDisconnected, std::memory_order_seq_cst);
163 result.EmplaceValue();
168 [](std::stringstream &msg) { msg <<
"Tcp client is in disconnected state"; });
178 return (connection_state_.load(std::memory_order_seq_cst) == State::kConnected);
190 if (connection_state_.load(std::memory_order_seq_cst) == State::kConnected) {
191 if (tcp_connection_.Transmit(std::move(tcp_message))) { result.EmplaceValue(); }
195 FILE_NAME, __LINE__, __func__, [](std::stringstream &msg) {
196 msg <<
"Tcp client is Offline, please connect to server first";
229 template<
typename TlsVersion>
231 std::uint16_t local_port_num,
232 std::string_view ca_certification_path,
234 : tls_client_impl_{std::make_unique<TlsClientImpl>(client_name, local_ip_address,
235 local_port_num, ca_certification_path,
236 std::move(tls_version))} {}
238 template<
typename TlsVersion>
241 template<
typename TlsVersion>
244 template<
typename TlsVersion>
247 template<typename TlsVersion>
249 tls_client_impl_->Initialize();
252 template<
typename TlsVersion>
254 tls_client_impl_->DeInitialize();
257 template<
typename TlsVersion>
259 tls_client_impl_->SetReadHandler(std::move(read_handler));
262 template<
typename TlsVersion>
264 std::uint16_t host_port_num) {
265 return tls_client_impl_->ConnectToHost(host_ip_address, host_port_num);
268 template<
typename TlsVersion>
270 return tls_client_impl_->DisconnectFromHost();
273 template<
typename TlsVersion>
275 return tls_client_impl_->IsConnectedToHost();
278 template<
typename TlsVersion>
280 return tls_client_impl_->Transmit(std::move(tcp_message));
Class to provide implementation of tls client.
IoContext io_context_
Stores the io context.
void DeInitialize() noexcept
De-initialize the client.
TlsClientImpl & operator=(TlsClientImpl &&other) noexcept=delete
std::string client_name_
The client name.
core_type::Result< void > ConnectToHost(std::string_view host_ip_address, std::uint16_t host_port_num)
Function to connect to remote ip address and port number.
std::atomic< State > connection_state_
Store the state of tcp connection.
~TlsClientImpl() noexcept=default
Destruct an instance of TcpClientImpl.
core_type::Result< void > Transmit(MessageConstPtr tcp_message)
Function to transmit the provided tcp message.
core_type::Result< void > DisconnectFromHost()
Function to disconnect from remote host if already connected.
void SetReadHandler(HandlerRead read_handler) noexcept
Function to set the read handler that is invoked when message is received.
TlsContext tls_context_
Stores the tls context.
TlsClientImpl(TlsClientImpl &&other) noexcept=delete
Deleted move assignment and move constructor.
TlsClientImpl(std::string_view client_name, std::string_view local_ip_address, std::uint16_t local_port_num, std::string_view ca_certification_path, TlsVersion tls_version) noexcept
Constructs an instance of TcpClient.
TlsClientImpl & operator=(const TlsClientImpl &other) noexcept=delete
auto IsConnectedToHost() const noexcept -> bool
Function to get the connection status.
TcpConnectionSecured tcp_connection_
Store the tcp connection.
TlsClientImpl(const TlsClientImpl &other) noexcept=delete
Deleted copy assignment and copy constructor.
Client that manages secured tcp connection.
void SetReadHandler(HandlerRead read_handler) noexcept
Function to set the read handler that is invoked when message is received.
core_type::Result< void > ConnectToHost(std::string_view host_ip_address, std::uint16_t host_port_num)
Function to connect to remote ip address and port number.
auto IsConnectedToHost() const noexcept -> bool
Function to get the connection status.
core_type::Result< void > DisconnectFromHost()
Function to disconnect from remote host if already connected.
void DeInitialize() noexcept
De-initialize the client.
boost_support::message::tcp::TcpMessageConstPtr MessageConstPtr
Type alias for Tcp message const pointer.
core_type::Result< void > Transmit(MessageConstPtr tcp_message)
Function to transmit the provided tcp message.
~TlsClient() noexcept
Destruct an instance of TlsClient.
std::function< void(MessagePtr)> HandlerRead
Tcp function template used for reception.
TlsClient & operator=(const TlsClient &other) noexcept=delete
TlsClient(std::string_view client_name, std::string_view local_ip_address, std::uint16_t local_port_num, std::string_view ca_certification_path, TlsVersion tls_version) noexcept
Constructs an instance of TlsClient.
void Initialize() noexcept
Initialize the client.
static auto GetLibBoostLogger() noexcept -> LibBoostLogger &
Wrapper class to hold boost io context required for io object( sockets)
Tls context class responsible for setting cipher suite and loading certificates.
Class used to create a tcp socket for handling transmission and reception of tcp message from driver.
Class type to contains a value (of type ValueType), or an error (of type ErrorType)
std::string AppendIpAddressAndPort(std::string_view client_name, std::string_view ip_address, std::uint16_t port_num)
Function to append the ip address and port number to the connection name.
auto MakeErrorCode(BoostSupportErrorDomain::Errc code, BoostSupportErrorDomain::SupportDataType data) noexcept -> core_type::ErrorCode
Create a new ErrorCode within DoipErrorDomain.
State
Definitions of different connection state.