Diag-Client-Lib
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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_INCLUDE_BOOST_SUPPORT_SERVER_TCP_TCP_SERVER_H_
9 #define DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_INCLUDE_BOOST_SUPPORT_SERVER_TCP_TCP_SERVER_H_
10 
11 #include <functional>
12 #include <memory>
13 
15 #include "core/include/result.h"
16 
17 namespace boost_support {
18 namespace socket {
19 namespace tcp {
20 class TcpSocket;
21 } // namespace tcp
22 } // namespace socket
23 
24 namespace server {
25 namespace tcp {
26 
30 class TcpServer final {
31  public:
36 
41 
46 
51 
55  using HandlerRead = std::function<void(MessagePtr)>;
56 
57  public:
65  explicit TcpServer(std::string_view server_name, TcpSocket tcp_socket) noexcept;
66 
70  TcpServer(const TcpServer &other) noexcept = delete;
71  TcpServer &operator=(const TcpServer &other) noexcept = delete;
72 
76  TcpServer(TcpServer &&other) noexcept;
77  TcpServer &operator=(TcpServer &&other) noexcept;
78 
82  ~TcpServer() noexcept;
83 
87  void Initialize() noexcept;
88 
92  void DeInitialize() noexcept;
93 
100  void SetReadHandler(HandlerRead read_handler) noexcept;
101 
108  core_type::Result<void> Transmit(MessageConstPtr tcp_message);
109 
110  private:
114  class TcpServerImpl;
115 
119  std::unique_ptr<TcpServerImpl> tcp_server_impl_;
120 };
121 
122 } // namespace tcp
123 } // namespace server
124 } // namespace boost_support
125 #endif // DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_INCLUDE_BOOST_SUPPORT_SERVER_TCP_TCP_SERVER_H_
Immutable class to store received tcp message.
Definition: tcp_message.h:26
Server that manages unsecured/ secured tcp connection.
Definition: tcp_server.h:30
std::function< void(MessagePtr)> HandlerRead
Tcp function template used for reception.
Definition: tcp_server.h:55
core_type::Result< void > Transmit(MessageConstPtr tcp_message)
Function to transmit the provided tcp message.
Definition: tcp_server.cpp:105
~TcpServer() noexcept
Destruct an instance of TcpServer.
void Initialize() noexcept
Initialize the server.
Definition: tcp_server.cpp:97
void SetReadHandler(HandlerRead read_handler) noexcept
Function to set the read handler that is invoked when message is received.
Definition: tcp_server.cpp:101
std::unique_ptr< TcpServerImpl > tcp_server_impl_
Unique pointer to tcp server implementation.
Definition: tcp_server.h:114
TcpServer(std::string_view server_name, TcpSocket tcp_socket) noexcept
Constructs an instance of TcpServer.
Definition: tcp_server.cpp:88
TcpServer(TcpServer &&other) noexcept
Move assignment and move constructor.
TcpServer & operator=(TcpServer &&other) noexcept
void DeInitialize() noexcept
De-initialize the server.
Definition: tcp_server.cpp:99
TcpServer & operator=(const TcpServer &other) noexcept=delete
boost_support::message::tcp::TcpMessagePtr MessagePtr
Type alias for Tcp message pointer.
Definition: tcp_server.h:45
boost_support::message::tcp::TcpMessageConstPtr MessageConstPtr
Type alias for Tcp message const pointer.
Definition: tcp_server.h:50
TcpServer(const TcpServer &other) noexcept=delete
Deleted copy assignment and copy constructor.
Class used to create a tcp socket for handling transmission and reception of tcp message from driver.
Definition: tcp_socket.h:24
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)