21 : ssl_stream_{io_context.GetContext(), tls_context.GetContext()},
22 local_endpoint_{boost::asio::ip::make_address(local_ip_address), local_port_num} {}
25 : ssl_stream_{std::move(tcp_socket), tls_context.GetContext()},
30 ssl_stream_.handshake(boost::asio::ssl::stream_base::server, ec);
32 if (ec.value() == boost::system::errc::success) {
33 printf(
"Connected with %s encryption\n", SSL_get_cipher(ssl_stream_.native_handle()));
36 FILE_NAME, __LINE__, __func__, [ec](std::stringstream &msg) {
37 msg <<
"Tls client handshake with host failed with error: " << ec.message();
43 : ssl_stream_{std::move(other.ssl_stream_)},
44 local_endpoint_{std::move(other.local_endpoint_)} {}
47 ssl_stream_ = std::move(std::move(other.ssl_stream_));
48 local_endpoint_ = std::move(other.local_endpoint_);
59 GetNativeTcpSocket().open(local_endpoint_.protocol(), ec);
60 if (ec.value() == boost::system::errc::success) {
62 GetNativeTcpSocket().set_option(boost::asio::socket_base::reuse_address{
true});
64 GetNativeTcpSocket().non_blocking(
false);
66 GetNativeTcpSocket().bind(local_endpoint_, ec);
68 if (ec.value() == boost::system::errc::success) {
71 FILE_NAME, __LINE__, __func__, [
this](std::stringstream &msg) {
72 Tcp::endpoint
const endpoint_{GetNativeTcpSocket().local_endpoint()};
73 msg <<
"Tls Socket opened and bound to "
74 <<
"<" << endpoint_.address().to_string() <<
"," << endpoint_.port() <<
">";
76 result.EmplaceValue();
80 FILE_NAME, __LINE__, __func__, [ec](std::stringstream &msg) {
81 msg <<
"Tls Socket binding failed with message: " << ec.message();
83 result.EmplaceError(SocketError::kBindingFailed);
87 FILE_NAME, __LINE__, __func__, [ec](std::stringstream &msg) {
88 msg <<
"Tls Socket opening failed with error: " << ec.message();
90 result.EmplaceError(SocketError::kOpenFailed);
96 std::string_view host_ip_address, std::uint16_t host_port_num) noexcept {
101 GetNativeTcpSocket().connect(
102 Tcp::endpoint{boost::asio::ip::make_address(host_ip_address), host_port_num}, ec);
103 if (ec.value() == boost::system::errc::success) {
105 FILE_NAME, __LINE__, __func__, [
this](std::stringstream &msg) {
106 Tcp::endpoint
const endpoint_{GetNativeTcpSocket().remote_endpoint()};
107 msg <<
"Tls socket connected to host "
108 <<
"<" << endpoint_.address().to_string() <<
"," << endpoint_.port() <<
">";
111 ssl_stream_.handshake(boost::asio::ssl::stream_base::client, ec);
112 if (ec.value() == boost::system::errc::success) {
113 printf(
"Connected with %s encryption\n", SSL_get_cipher(ssl_stream_.native_handle()));
114 result.EmplaceValue();
117 FILE_NAME, __LINE__, __func__, [ec](std::stringstream &msg) {
118 msg <<
"Tls client handshake with host failed with error: " << ec.message();
120 result.EmplaceError(SocketError::kTlsHandshakeFailed);
124 FILE_NAME, __LINE__, __func__, [ec](std::stringstream &msg) {
125 msg <<
"Tls client socket connect to host failed with error: " << ec.message();
140 if (ec.value() == boost::system::errc::success) {
142 result.EmplaceValue();
145 FILE_NAME, __LINE__, __func__, [ec](std::stringstream &msg) {
146 msg <<
"Tls client socket disconnection from host failed with error: " << ec.message();
159 boost::asio::buffer(tcp_message->GetPayload().data(), tcp_message->GetPayload().size()), ec);
161 if (ec.value() == boost::system::errc::success) {
163 FILE_NAME, __LINE__, __func__, [
this](std::stringstream &msg) {
164 Tcp::endpoint
const endpoint_{GetNativeTcpSocket().remote_endpoint()};
165 msg <<
"Tcp message sent to "
166 <<
"<" << endpoint_.address().to_string() <<
"," << endpoint_.port() <<
">";
168 result.EmplaceValue();
171 FILE_NAME, __LINE__, __func__, [ec](std::stringstream &msg) {
172 msg <<
"Tcp message sending failed with error: " << ec.message();
187 result.EmplaceValue();
201 if (ec.value() == boost::system::errc::success) {
203 std::uint32_t read_next_bytes = [&rx_buffer]() noexcept -> std::uint32_t {
204 return static_cast<std::uint32_t
>(
205 (
static_cast<std::uint32_t
>(rx_buffer[4u] << 24u) & 0xFF000000) |
206 (
static_cast<std::uint32_t
>(rx_buffer[5u] << 16u) & 0x00FF0000) |
207 (
static_cast<std::uint32_t
>(rx_buffer[6u] << 8u) & 0x0000FF00) |
208 (
static_cast<std::uint32_t
>(rx_buffer[7u] & 0x000000FF)));
211 if (read_next_bytes != 0u) {
215 [&remote_endpoint, read_next_bytes](std::stringstream &msg) {
216 msg <<
"Tcp Message with length= " << read_next_bytes <<
" received from "
217 <<
"<" << remote_endpoint.address().to_string() <<
"," << remote_endpoint.port()
229 remote_endpoint.address().to_string(), remote_endpoint.port(), std::move(rx_buffer))};
232 [&remote_endpoint, read_next_bytes](std::stringstream &msg) {
233 msg <<
"Tcp Message with length= " << read_next_bytes <<
" received from "
234 <<
"<" << remote_endpoint.address().to_string() <<
"," << remote_endpoint.port()
237 result.EmplaceValue(std::move(tcp_rx_message));
241 [](std::stringstream &msg) { msg <<
"Tcp Message read ignored as header size is zero"; });
243 }
else if (ec.value() == boost::asio::error::eof) {
246 [ec](std::stringstream &msg) { msg <<
"Remote Disconnected with: " << ec.message(); });
249 FILE_NAME, __LINE__, __func__, [ec](std::stringstream &msg) {
250 msg <<
"Remote Disconnected with undefined error: " << ec.message();
static auto GetLibBoostLogger() noexcept -> LibBoostLogger &
std::vector< std::uint8_t > BufferType
Type alias for underlying buffer.
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.
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.
boost::system::error_code TcpErrorCodeType
Type alias for tcp error codes.
core_type::Result< TcpMessagePtr, SocketError > Read() noexcept
Function to read message from socket.
~TlsSocket() noexcept
Destruct an instance of TcpSocket.
boost_support::message::tcp::TcpMessagePtr TcpMessagePtr
Type alias for Tcp message pointer.
SslStream::lowest_layer_type & GetNativeTcpSocket()
Function to get the native tcp socket under tls socket.
SocketError
Socket error code.
core_type::Result< void, SocketError > Close() noexcept
Function to destroy the socket.
boost_support::message::tcp::TcpMessageConstPtr TcpMessageConstPtr
Type alias for Tcp message const pointer.
TlsSocket & operator=(const TlsSocket &other) noexcept=delete
SslStream ssl_stream_
Store the underlying tcp socket.
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.
Tcp::socket TcpSocket
Type alias for tcp socket.
core_type::Result< void, SocketError > Transmit(TcpMessageConstPtr tcp_message) noexcept
Function to trigger transmission.
core_type::Result< void, SocketError > Disconnect() noexcept
Function to Disconnect from host.
Class type to contains a value (of type ValueType), or an error (of type ErrorType)
constexpr std::uint8_t kDoipheadrSize
Doip HeaderSize.
boost::system::error_code TcpErrorCodeType
core_type::Result< T, E > Result
Class type to contains a value (of type ValueType), or an error (of type ErrorType)