Diag-Client-Lib
tcp_server.cpp
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 
10 
13 
14 namespace boost_support {
15 namespace server {
16 namespace tcp {
17 
19  public:
23  using TcpConnection =
25 
31  explicit TcpServerImpl(std::string_view server_name, TcpSocket tcp_socket) noexcept
32  : tcp_connection_{server_name, std::move(tcp_socket)} {}
33 
37  TcpServerImpl(const TcpServerImpl &other) noexcept = delete;
38  TcpServerImpl &operator=(const TcpServerImpl &other) noexcept = delete;
39 
43  TcpServerImpl(TcpServerImpl &&other) noexcept = delete;
44  TcpServerImpl &operator=(TcpServerImpl &&other) noexcept = delete;
45 
49  ~TcpServerImpl() noexcept = default;
50 
54  void Initialize() noexcept { tcp_connection_.Initialize(); }
55 
59  void DeInitialize() noexcept { tcp_connection_.DeInitialize(); }
60 
67  void SetReadHandler(HandlerRead read_handler) noexcept {
68  tcp_connection_.SetReadHandler(std::move(read_handler));
69  }
70 
78  return tcp_connection_.Transmit(std::move(tcp_message));
79  }
80 
81  private:
86 };
87 
88 TcpServer::TcpServer(std::string_view server_name, TcpServer::TcpSocket tcp_socket) noexcept
89  : tcp_server_impl_{std::make_unique<TcpServerImpl>(server_name, std::move(tcp_socket))} {}
90 
91 TcpServer::TcpServer(TcpServer &&other) noexcept = default;
92 
93 TcpServer &TcpServer::operator=(TcpServer &&other) noexcept = default;
94 
95 TcpServer::~TcpServer() noexcept = default;
96 
97 void TcpServer::Initialize() noexcept { tcp_server_impl_->Initialize(); }
98 
99 void TcpServer::DeInitialize() noexcept { tcp_server_impl_->DeInitialize(); }
100 
102  tcp_server_impl_->SetReadHandler(std::move(read_handler));
103 }
104 
106  return tcp_server_impl_->Transmit(std::move(tcp_message));
107 }
108 } // namespace tcp
109 } // namespace server
110 } // namespace boost_support
TcpServerImpl & operator=(TcpServerImpl &&other) noexcept=delete
TcpConnection tcp_connection_
Store the tcp connection.
Definition: tcp_server.cpp:85
TcpServerImpl(const TcpServerImpl &other) noexcept=delete
Deleted copy assignment and copy constructor.
void SetReadHandler(HandlerRead read_handler) noexcept
Function to set the read handler that is invoked when message is received.
Definition: tcp_server.cpp:67
TcpServerImpl & operator=(const TcpServerImpl &other) noexcept=delete
~TcpServerImpl() noexcept=default
Destruct an instance of TcpServerImpl.
core_type::Result< void > Transmit(MessageConstPtr tcp_message)
Function to transmit the provided tcp message.
Definition: tcp_server.cpp:77
void DeInitialize() noexcept
De-initialize the TcpServerImpl.
Definition: tcp_server.cpp:59
TcpServerImpl(TcpServerImpl &&other) noexcept=delete
Deleted move assignment and move constructor.
void Initialize() noexcept
Initialize the TcpServerImpl.
Definition: tcp_server.cpp:54
TcpServerImpl(std::string_view server_name, TcpSocket tcp_socket) noexcept
Constructs an instance of TcpServerImpl.
Definition: tcp_server.cpp:31
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
void DeInitialize() noexcept
De-initialize the server.
Definition: tcp_server.cpp:99
TcpServer & operator=(const TcpServer &other) noexcept=delete
boost_support::message::tcp::TcpMessageConstPtr MessageConstPtr
Type alias for Tcp message const pointer.
Definition: tcp_server.h:50
Class used to create a tcp socket for handling transmission and reception of tcp message from driver.
Definition: tcp_socket.h:24
Class type to contains a value (of type ValueType), or an error (of type ErrorType)
Definition: result.h:29