Diag-Client-Lib
tcp_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_TCP_SERVER_H_
9 #define DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_SOCKET_TCP_TCP_SERVER_H_
10 
11 // includes
12 #include <boost/asio.hpp>
13 #include <string_view>
14 #include <vector>
15 
16 #include "tcp_message.h"
17 
18 namespace boost_support {
19 namespace socket {
20 namespace tcp {
21 // type alias for tcp socket
22 using Tcp = boost::asio::ip::tcp;
23 using TcpSocket = Tcp::socket;
24 
26  public:
27  // Tcp function template used for reception
28  using TcpHandlerRead = std::function<void(TcpMessagePtr)>;
29 
30  // Tcp Server connection class to create connection with client
32  public:
33  // ctor
34  TcpServerConnection(boost::asio::io_context &io_context, TcpHandlerRead &&tcp_handler_read);
35 
36  // dtor
37  ~TcpServerConnection() = default;
38 
39  // move ctor
41 
42  // move assignment
44 
45  // copy ctor & assignment deleted
48 
49  // Get reference to underlying socket
51 
52  // function to transmit tcp message
53  bool Transmit(TcpMessageConstPtr udp_tx_message);
54 
55  // function to handle read
56  bool ReceivedMessage();
57 
58  // function to close the socket
59  bool Shutdown();
60 
61  private:
62  // tcp socket
64 
65  // handler read
67  };
68 
69  public:
70  // type alias for tcp accepter
71  using TcpAccepter = boost::asio::ip::tcp::acceptor;
72 
73  // ctor
74  CreateTcpServerSocket(std::string_view local_ip_address, uint16_t local_port_num);
75 
76  // dtor
78 
79  // Blocking function get a tcp connection
81 
82  private:
83  // local Ip address
84  std::string local_ip_address_;
85  // local port number
86  uint16_t local_port_num_;
87  // tcp socket accepter
88  std::unique_ptr<TcpAccepter> tcp_accepter_;
89  // boost io context
90  boost::asio::io_context io_context_;
91 };
92 
93 } // namespace tcp
94 } // namespace socket
95 } // namespace boost_support
96 
97 #endif // DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_SOCKET_TCP_TCP_SERVER_H_
TcpServerConnection(boost::asio::io_context &io_context, TcpHandlerRead &&tcp_handler_read)
Definition: tcp_server.cpp:53
TcpServerConnection & operator=(TcpServerConnection &&)=default
TcpServerConnection & operator=(TcpServerConnection &)=delete
std::unique_ptr< TcpAccepter > tcp_accepter_
Definition: tcp_server.h:88
boost::asio::ip::tcp::acceptor TcpAccepter
Definition: tcp_server.h:71
CreateTcpServerSocket(std::string_view local_ip_address, uint16_t local_port_num)
Definition: tcp_server.cpp:19
std::function< void(TcpMessagePtr)> TcpHandlerRead
Definition: tcp_server.h:28
TcpServerConnection GetTcpServerConnection(TcpHandlerRead &&tcp_handler_read)
Definition: tcp_server.cpp:31
std::unique_ptr< const TcpMessage > TcpMessageConstPtr
The unique pointer to const TcpMessage.
Definition: tcp_message.h:172
boost::asio::ip::tcp Tcp
Definition: tcp_server.h:22
std::unique_ptr< TcpMessage > TcpMessagePtr
The unique pointer to TcpMessage.
Definition: tcp_message.h:177