Diag-Client-Lib
tls_server_.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_TCP_TLS_SERVER_H_
9 #define DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_SOCKET_TCP_TLS_SERVER_H_
10 
11 // includes
12 #include <boost/asio.hpp>
13 #include <boost/asio/ssl.hpp>
14 #include <optional>
15 #include <string_view>
16 #include <vector>
17 
19 #include "core/include/result.h"
20 
21 namespace boost_support {
22 namespace socket {
23 namespace tcp {
24 
28 class TcpServerConnection final {
29  public:
33  enum class TcpErrorCode : std::uint8_t { kOpenFailed, kBindingFailed, kGenericError };
34 
38  using TcpHandlerRead = std::function<void(message::tcp::TcpMessagePtr)>;
39 
43  using Tcp = boost::asio::ip::tcp;
44 
48  using TcpSocket = Tcp::socket;
49 
53  using TlsStream = boost::asio::ssl::stream<TcpSocket>;
54 
55  public:
63  TcpServerConnection(TlsStream tls_socket, TcpHandlerRead tcp_handler_read);
64 
68  ~TcpServerConnection() = default;
69 
73  TcpServerConnection(TcpServerConnection &&) noexcept = default;
74  TcpServerConnection &operator=(TcpServerConnection &&) noexcept = default;
75 
80  TcpServerConnection &operator=(TcpServerConnection const &) = delete;
81 
88  core_type::Result<void, TcpErrorCode> Transmit(message::tcp::TcpMessageConstPtr tcp_message);
89 
93  bool TryReceivingMessage();
94 
100 
101  private:
106 
111 
115  TlsStream::lowest_layer_type &GetNativeTcpSocket();
116 };
117 
121 class TlsServerSocket final {
122  public:
126  using TcpHandlerRead = std::function<void(message::tcp::TcpMessagePtr)>;
127 
131  using TcpAcceptor = boost::asio::ip::tcp::acceptor;
132 
133  public:
141  TlsServerSocket(std::string_view local_ip_address, std::uint16_t local_port_num);
142 
146  ~TlsServerSocket() = default;
147 
153  std::optional<TcpServerConnection> GetTcpServerConnection(TcpHandlerRead tcp_handler_read);
154 
155  private:
159  using Tcp = boost::asio::ip::tcp;
160 
164  std::string local_ip_address_;
165 
169  std::uint16_t local_port_num_;
170 
174  boost::asio::io_context io_context_;
175 
179  boost::asio::ssl::context io_ssl_context_;
180 
185 };
186 
187 } // namespace tcp
188 } // namespace socket
189 } // namespace boost_support
190 
191 #endif // DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_SOCKET_TCP_TLS_SERVER_H_
Tcp Server connection class to create connection with client.
Definition: tls_server_.h:28
~TcpServerConnection()=default
Destruct an instance of TcpServerConnection.
TcpServerConnection(TcpServerConnection &&) noexcept=default
Default move ctor and assignment operator.
boost::asio::ssl::stream< TcpSocket > TlsStream
Type alias for tls stream wrapping tcp socket.
Definition: tls_server_.h:53
boost::asio::ip::tcp Tcp
Type alias for tcp protocol.
Definition: tls_server_.h:43
core_type::Result< void, TcpErrorCode > Shutdown()
Function to shutdown the socket.
TcpServerConnection(TlsStream tls_socket, TcpHandlerRead tcp_handler_read)
Constructs an instance of TcpServerConnection.
Definition: tls_server_.cpp:65
std::function< void(message::tcp::TcpMessagePtr)> TcpHandlerRead
Tcp function template used for reception.
Definition: tls_server_.h:38
Tcp::socket TcpSocket
Type alias for tcp socket.
Definition: tls_server_.h:48
TlsStream::lowest_layer_type & GetNativeTcpSocket()
Function to get the native tcp socket under tls socket.
Definition: tls_server_.cpp:69
TcpHandlerRead tcp_handler_read_
Store the handler.
Definition: tls_server_.h:110
core_type::Result< void, TcpErrorCode > Transmit(message::tcp::TcpMessageConstPtr tcp_message)
Function to trigger transmission.
Definition: tls_server_.cpp:73
bool TryReceivingMessage()
Function to initiate reception of tcp message.
Class used to create a tls socket for server for handling transmission and reception of tcp message f...
Definition: tls_server_.h:121
boost::asio::ssl::context io_ssl_context_
boost io ssl context
Definition: tls_server_.h:179
TcpAcceptor tcp_acceptor_
Store tcp acceptor.
Definition: tls_server_.h:184
~TlsServerSocket()=default
Destruct an instance of TlsServerSocket.
boost::asio::ip::tcp::acceptor TcpAcceptor
Type alias for tcp acceptor.
Definition: tls_server_.h:131
boost::asio::ip::tcp Tcp
Type alias for tcp protocol.
Definition: tls_server_.h:159
std::uint16_t local_port_num_
Store local port number.
Definition: tls_server_.h:169
boost::asio::io_context io_context_
boost io context
Definition: tls_server_.h:174
std::function< void(message::tcp::TcpMessagePtr)> TcpHandlerRead
Tcp function template used for reception.
Definition: tls_server_.h:126
std::string local_ip_address_
Store local ip address.
Definition: tls_server_.h:164
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)