Diag-Client-Lib
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
tls_socket.h
Go to the documentation of this file.
1 /* Diagnostic Client library
2  * Copyright (C) 2024 Avijit Dey
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 #ifndef DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_SOCKET_TLS_TLS_SOCKET_H_
9 #define DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_SOCKET_TLS_TLS_SOCKET_H_
10 
11 #include <boost/asio.hpp>
12 
16 #include "core/include/result.h"
17 
18 namespace boost_support {
19 namespace socket {
20 namespace tls {
21 
25 class TlsSocket final {
26  public:
30  enum class SocketError : std::uint8_t {
36  };
37 
42 
47 
52 
56  using Tcp = boost::asio::ip::tcp;
57 
61  using TcpSocket = Tcp::socket;
62 
63  public:
73  TlsSocket(std::string_view local_ip_address, std::uint16_t local_port_num,
74  TlsContext &tls_context, IoContext &io_context) noexcept;
75 
81  TlsSocket(TcpSocket tcp_socket, TlsContext &tls_context) noexcept;
82 
86  TlsSocket(const TlsSocket &other) noexcept = delete;
87  TlsSocket &operator=(const TlsSocket &other) noexcept = delete;
88 
92  TlsSocket &operator=(TlsSocket &&other) noexcept;
93 
97  TlsSocket(TlsSocket &&other) noexcept;
98 
102  ~TlsSocket() noexcept;
103 
108  core_type::Result<void, SocketError> Open() noexcept;
109 
118  core_type::Result<void, SocketError> Connect(std::string_view host_ip_address,
119  std::uint16_t host_port_num) noexcept;
120 
125  core_type::Result<void, SocketError> Disconnect() noexcept;
126 
133  core_type::Result<void, SocketError> Transmit(TcpMessageConstPtr tcp_message) noexcept;
134 
140 
145  core_type::Result<void, SocketError> Close() noexcept;
146 
147  private:
151  using TcpIpAddress = boost::asio::ip::address;
152 
156  using TcpErrorCodeType = boost::system::error_code;
157 
161  using SslStream = boost::asio::ssl::stream<Tcp::socket>;
162 
167 
171  Tcp::endpoint local_endpoint_;
172 
173  private:
177  SslStream::lowest_layer_type &GetNativeTcpSocket();
178 };
179 } // namespace tls
180 } // namespace socket
181 } // namespace boost_support
182 #endif // DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_SOCKET_TLS_TLS_SOCKET_H_
Immutable class to store received tcp message.
Definition: tcp_message.h:26
Wrapper class to hold boost io context required for io object( sockets)
Definition: io_context.h:22
Tls context class responsible for setting cipher suite and loading certificates.
Definition: tls_context.h:24
Class used to create a tcp socket for handling transmission and reception of tcp message from driver.
Definition: tls_socket.h:25
TlsSocket(std::string_view local_ip_address, std::uint16_t local_port_num, TlsContext &tls_context, IoContext &io_context) noexcept
Constructs an instance of TcpSocket.
Definition: tls_socket.cpp:19
boost::system::error_code TcpErrorCodeType
Type alias for tcp error codes.
Definition: tls_socket.h:156
core_type::Result< TcpMessagePtr, SocketError > Read() noexcept
Function to read message from socket.
Definition: tls_socket.cpp:191
~TlsSocket() noexcept
Destruct an instance of TcpSocket.
boost::asio::ssl::stream< Tcp::socket > SslStream
Type alias for tcp socket.
Definition: tls_socket.h:161
boost_support::message::tcp::TcpMessagePtr TcpMessagePtr
Type alias for Tcp message pointer.
Definition: tls_socket.h:46
TlsSocket(const TlsSocket &other) noexcept=delete
Deleted copy assignment and copy constructor.
SslStream::lowest_layer_type & GetNativeTcpSocket()
Function to get the native tcp socket under tls socket.
Definition: tls_socket.cpp:256
core_type::Result< void, SocketError > Close() noexcept
Function to destroy the socket.
Definition: tls_socket.cpp:178
boost::asio::ip::tcp Tcp
Type alias for tcp protocol.
Definition: tls_socket.h:56
boost_support::message::tcp::TcpMessageConstPtr TcpMessageConstPtr
Type alias for Tcp message const pointer.
Definition: tls_socket.h:51
boost::asio::ip::address TcpIpAddress
Type alias for tcp ip address.
Definition: tls_socket.h:151
TlsSocket & operator=(const TlsSocket &other) noexcept=delete
SslStream ssl_stream_
Store the underlying tcp socket.
Definition: tls_socket.h:166
core_type::Result< void, SocketError > Open() noexcept
Function to open and bind the socket to provided ip address & port.
Definition: tls_socket.cpp:54
core_type::Result< void, SocketError > Connect(std::string_view host_ip_address, std::uint16_t host_port_num) noexcept
Function to connect to remote ip address and port number.
Definition: tls_socket.cpp:95
Tcp::endpoint local_endpoint_
Store the local endpoints.
Definition: tls_socket.h:171
Tcp::socket TcpSocket
Type alias for tcp socket.
Definition: tls_socket.h:61
core_type::Result< void, SocketError > Transmit(TcpMessageConstPtr tcp_message) noexcept
Function to trigger transmission.
Definition: tls_socket.cpp:152
core_type::Result< void, SocketError > Disconnect() noexcept
Function to Disconnect from host.
Definition: tls_socket.cpp:131
std::unique_ptr< TcpMessage > TcpMessagePtr
The unique pointer to TcpMessage.
Definition: tcp_message.h:151
std::unique_ptr< TcpMessage const > TcpMessageConstPtr
The unique pointer to const TcpMessage.
Definition: tcp_message.h:146
core_type::Result< T, E > Result
Class type to contains a value (of type ValueType), or an error (of type ErrorType)