1 // Copyright (C) 2014-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
2 // This Source Code Form is subject to the terms of the Mozilla Public
3 // License, v. 2.0. If a copy of the MPL was not distributed with this
4 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 
6 #ifndef VSOMEIP_V3_TCP_CLIENT_ENDPOINT_IMPL_HPP_
7 #define VSOMEIP_V3_TCP_CLIENT_ENDPOINT_IMPL_HPP_
8 
9 #include <chrono>
10 
11 #include <boost/asio/ip/tcp.hpp>
12 #include <boost/asio/steady_timer.hpp>
13 
14 #include <vsomeip/defines.hpp>
15 #include "client_endpoint_impl.hpp"
16 
17 namespace vsomeip_v3 {
18 
19 typedef client_endpoint_impl<
20             boost::asio::ip::tcp
21         > tcp_client_endpoint_base_impl;
22 
23 class tcp_client_endpoint_impl: public tcp_client_endpoint_base_impl {
24 public:
25     tcp_client_endpoint_impl(const std::shared_ptr<endpoint_host>& _endpoint_host,
26                              const std::shared_ptr<routing_host>& _routing_host,
27                              const endpoint_type& _local,
28                              const endpoint_type& _remote,
29                              boost::asio::io_service &_io,
30                              const std::shared_ptr<configuration>& _configuration);
31     virtual ~tcp_client_endpoint_impl();
32 
33     void start();
34     void restart(bool _force);
35 
36     bool get_remote_address(boost::asio::ip::address &_address) const;
37     std::uint16_t get_remote_port() const;
38     bool is_reliable() const;
39     bool is_local() const;
40     void print_status();
41 
42     void send_cbk(boost::system::error_code const &_error, std::size_t _bytes,
43                   const message_buffer_ptr_t& _sent_msg);
44 private:
45     void send_queued(message_buffer_ptr_t _buffer);
46     void get_configured_times_from_endpoint(
47             service_t _service, method_t _method,
48             std::chrono::nanoseconds *_debouncing,
49             std::chrono::nanoseconds *_maximum_retention) const;
50     bool is_magic_cookie(const message_buffer_ptr_t& _recv_buffer,
51                          size_t _offset) const;
52     void send_magic_cookie(message_buffer_ptr_t &_buffer);
53 
54     void receive_cbk(boost::system::error_code const &_error,
55                      std::size_t _bytes,
56                      const message_buffer_ptr_t&  _recv_buffer,
57                      std::size_t _recv_buffer_size);
58 
59     void connect();
60     void receive();
61     void receive(message_buffer_ptr_t  _recv_buffer,
62                  std::size_t _recv_buffer_size,
63                  std::size_t _missing_capacity);
64     void calculate_shrink_count(const message_buffer_ptr_t& _recv_buffer,
65                                 std::size_t _recv_buffer_size);
66     const std::string get_address_port_remote() const;
67     const std::string get_address_port_local() const;
68     void handle_recv_buffer_exception(const std::exception &_e,
69                                       const message_buffer_ptr_t& _recv_buffer,
70                                       std::size_t _recv_buffer_size);
71     void set_local_port();
72     std::size_t write_completion_condition(
73             const boost::system::error_code& _error,
74             std::size_t _bytes_transferred, std::size_t _bytes_to_send,
75             service_t _service, method_t _method, client_t _client, session_t _session,
76             const std::chrono::steady_clock::time_point _start);
77     std::string get_remote_information() const;
78     std::shared_ptr<struct timing> get_timing(
79             const service_t& _service, const instance_t& _instance) const;
80     bool tp_segmentation_enabled(service_t _service, method_t _method) const;
81     std::uint32_t get_max_allowed_reconnects() const;
82     void max_allowed_reconnects_reached();
83 
84     void wait_until_sent(const boost::system::error_code &_error);
85 
86     const std::uint32_t recv_buffer_size_initial_;
87     message_buffer_ptr_t recv_buffer_;
88     std::uint32_t shrink_count_;
89     const std::uint32_t buffer_shrink_threshold_;
90 
91     const boost::asio::ip::address remote_address_;
92     const std::uint16_t remote_port_;
93     std::chrono::steady_clock::time_point last_cookie_sent_;
94     const std::chrono::milliseconds send_timeout_;
95     const std::chrono::milliseconds send_timeout_warning_;
96 
97     std::uint32_t tcp_restart_aborts_max_;
98     std::uint32_t tcp_connect_time_max_;
99     std::atomic<uint32_t> aborted_restart_count_;
100     std::chrono::steady_clock::time_point connect_timepoint_;
101 
102     std::mutex sent_mutex_;
103     bool is_sending_;
104     boost::asio::steady_timer sent_timer_;
105 
106 };
107 
108 } // namespace vsomeip_v3
109 
110 #endif // VSOMEIP_V3_TCP_CLIENT_ENDPOINT_IMPL_HPP_
111