Diag-Client-Lib
tcp_message.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_TCP_MESSAGE_H_
9 #define DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_SOCKET_TCP_TCP_MESSAGE_H_
10 
11 #include <memory>
12 #include <string>
13 #include <string_view>
14 #include <vector>
15 
16 #include "core/include/span.h"
17 
18 namespace boost_support {
19 namespace socket {
20 namespace tcp {
21 
25 class TcpMessage final {
26  public:
30  enum class SocketState : std::uint8_t {
31  kIdle = 0x00,
38  };
39 
43  enum class SocketError : std::uint8_t { kNone = 0x00 };
44 
48  using BufferType = std::vector<uint8_t>;
49 
53  using IpAddressType = std::string_view;
54 
55  public:
60  : socket_state_{SocketState::kIdle},
61  socket_error_{SocketError::kNone},
62  rx_buffer_{},
63  tx_buffer_{},
66 
76  TcpMessage(IpAddressType host_ip_address, std::uint16_t host_port_number, BufferType &&payload)
77  : socket_state_{SocketState::kIdle},
78  socket_error_{SocketError::kNone},
79  rx_buffer_{std::move(payload)},
80  tx_buffer_{},
81  host_ip_address_{host_ip_address},
82  host_port_number_{host_port_number} {}
83 
84  TcpMessage(TcpMessage &&other) noexcept = default;
85  TcpMessage &operator=(TcpMessage &&other) noexcept = default;
86 
87  TcpMessage(const TcpMessage &other) = delete;
88  TcpMessage &operator=(const TcpMessage &other) = delete;
89 
93  ~TcpMessage() = default;
94 
100 
105  std::uint16_t GetHostPortNumber() const { return host_port_number_; }
106 
112 
118 
123  BufferType const &GetTxBuffer() const { return tx_buffer_; }
124 
130 
136 
137  private:
142 
147 
152 
157 
161  std::string host_ip_address_;
162 
166  std::uint16_t host_port_number_;
167 };
168 
172 using TcpMessageConstPtr = std::unique_ptr<const TcpMessage>;
173 
177 using TcpMessagePtr = std::unique_ptr<TcpMessage>;
178 
182 constexpr std::uint8_t kDoipheadrSize = 8U;
183 
184 } // namespace tcp
185 } // namespace socket
186 } // namespace boost_support
187 #endif // DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_SOCKET_TCP_TCP_MESSAGE_H_
Immutable class to store received tcp message.
Definition: tcp_message.h:25
SocketState
Definition of different socket state.
Definition: tcp_message.h:30
std::string host_ip_address_
Store remote ip address.
Definition: tcp_message.h:161
TcpMessage()
Default constructor.
Definition: tcp_message.h:59
BufferType rx_buffer_
The reception buffer.
Definition: tcp_message.h:151
std::uint16_t host_port_number_
Store remote port number.
Definition: tcp_message.h:166
SocketError
Definition of different socket error that could occur.
Definition: tcp_message.h:43
IpAddressType GetHostIpAddress() const
Get the host ip address.
Definition: tcp_message.h:99
core_type::Span< std::uint8_t > GetRxBuffer()
Get the view to the rx buffer.
Definition: tcp_message.h:111
SocketError socket_error_
Store the socket error.
Definition: tcp_message.h:146
SocketError GetSocketError() const
Get the error of underlying socket.
Definition: tcp_message.h:135
TcpMessage & operator=(const TcpMessage &other)=delete
~TcpMessage()=default
Destructs an instance of TcpMessage.
TcpMessage(IpAddressType host_ip_address, std::uint16_t host_port_number, BufferType &&payload)
Constructs an instance of TcpMessage.
Definition: tcp_message.h:76
std::string_view IpAddressType
Type alias of IP address type.
Definition: tcp_message.h:53
std::vector< uint8_t > BufferType
Type alias for underlying buffer.
Definition: tcp_message.h:48
SocketState socket_state_
Store the socket state.
Definition: tcp_message.h:141
std::uint16_t GetHostPortNumber() const
Get the host port number.
Definition: tcp_message.h:105
BufferType tx_buffer_
The transmission buffer.
Definition: tcp_message.h:156
TcpMessage & operator=(TcpMessage &&other) noexcept=default
BufferType & GetTxBuffer()
Get the reference to tx buffer.
Definition: tcp_message.h:117
TcpMessage(TcpMessage &&other) noexcept=default
TcpMessage(const TcpMessage &other)=delete
SocketState GetSocketState() const
Get the state of underlying socket.
Definition: tcp_message.h:129
BufferType const & GetTxBuffer() const
Get the reference to tx buffer.
Definition: tcp_message.h:123
std::unique_ptr< const TcpMessage > TcpMessageConstPtr
The unique pointer to const TcpMessage.
Definition: tcp_message.h:172
constexpr std::uint8_t kDoipheadrSize
Doip HeaderSize.
Definition: tcp_message.h:182
std::unique_ptr< TcpMessage > TcpMessagePtr
The unique pointer to TcpMessage.
Definition: tcp_message.h:177