Diag-Client-Lib
tcp_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_TCP_TCP_SOCKET_H_
9 #define DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_SOCKET_TCP_TCP_SOCKET_H_
10 
11 #include <boost/asio.hpp>
12 
15 #include "core/include/result.h"
16 
17 namespace boost_support {
18 namespace socket {
19 namespace tcp {
20 
24 class TcpSocket final {
25  public:
29  enum class SocketError : std::uint8_t {
34  };
35 
40 
45 
50 
54  using Tcp = boost::asio::ip::tcp;
55 
59  using Socket = Tcp::socket;
60 
61  public:
71  TcpSocket(std::string_view local_ip_address, std::uint16_t local_port_num,
72  IoContext &io_context) noexcept;
73 
79  explicit TcpSocket(Socket socket) noexcept;
80 
84  TcpSocket(const TcpSocket &other) noexcept = delete;
85  TcpSocket &operator=(const TcpSocket &other) noexcept = delete;
86 
90  TcpSocket(TcpSocket &&other) noexcept = default;
91  TcpSocket &operator=(TcpSocket &&other) noexcept = default;
92 
96  ~TcpSocket() noexcept;
97 
102  core_type::Result<void, SocketError> Open() noexcept;
103 
112  core_type::Result<void, SocketError> Connect(std::string_view host_ip_address,
113  std::uint16_t host_port_num) noexcept;
114 
119  core_type::Result<void, SocketError> Disconnect() noexcept;
120 
127  core_type::Result<void, SocketError> Transmit(TcpMessageConstPtr tcp_message) noexcept;
128 
134 
139  core_type::Result<void, SocketError> Close() noexcept;
140 
141  private:
145  using TcpIpAddress = boost::asio::ip::address;
146 
150  using TcpErrorCodeType = boost::system::error_code;
151 
156 
160  Tcp::endpoint local_endpoint_;
161 };
162 } // namespace tcp
163 } // namespace socket
164 } // namespace boost_support
165 #endif // DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_SOCKET_TCP_TCP_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
Class used to create a tcp socket for handling transmission and reception of tcp message from driver.
Definition: tcp_socket.h:24
TcpSocket(std::string_view local_ip_address, std::uint16_t local_port_num, IoContext &io_context) noexcept
Constructs an instance of TcpSocket.
Definition: tcp_socket.cpp:19
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: tcp_socket.cpp:72
boost_support::message::tcp::TcpMessagePtr TcpMessagePtr
Type alias for Tcp message pointer.
Definition: tcp_socket.h:44
core_type::Result< void, SocketError > Close() noexcept
Function to destroy the socket.
Definition: tcp_socket.cpp:141
TcpSocket & operator=(TcpSocket &&other) noexcept=default
core_type::Result< TcpMessagePtr, SocketError > Read() noexcept
Function to read message from socket.
Definition: tcp_socket.cpp:150
TcpSocket(TcpSocket &&other) noexcept=default
Move assignment and Move constructor.
core_type::Result< void, SocketError > Disconnect() noexcept
Function to Disconnect from host.
Definition: tcp_socket.cpp:97
boost_support::message::tcp::TcpMessageConstPtr TcpMessageConstPtr
Type alias for Tcp message const pointer.
Definition: tcp_socket.h:49
Tcp::socket Socket
Type alias for tcp socket.
Definition: tcp_socket.h:59
boost::asio::ip::tcp Tcp
Type alias for tcp protocol.
Definition: tcp_socket.h:54
core_type::Result< void, SocketError > Transmit(TcpMessageConstPtr tcp_message) noexcept
Function to trigger transmission.
Definition: tcp_socket.cpp:115
TcpSocket(const TcpSocket &other) noexcept=delete
Deleted copy assignment and copy constructor.
~TcpSocket() noexcept
Destruct an instance of TcpSocket.
boost::asio::ip::address TcpIpAddress
Type alias for tcp ip address.
Definition: tcp_socket.h:145
TcpSocket & operator=(const TcpSocket &other) noexcept=delete
Socket tcp_socket_
Store the underlying tcp socket.
Definition: tcp_socket.h:155
Tcp::endpoint local_endpoint_
Store the local endpoints.
Definition: tcp_socket.h:160
boost::system::error_code TcpErrorCodeType
Type alias for tcp error codes.
Definition: tcp_socket.h:150
core_type::Result< void, SocketError > Open() noexcept
Function to open and bind the socket to provided ip address & port.
Definition: tcp_socket.cpp:30
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)