summaryrefslogtreecommitdiff
path: root/implementation/endpoints/include/client_endpoint_impl.hpp
blob: f80e3856870a2d2702dc5ad777a7eee2a849d60a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (C) 2014-2016 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef VSOMEIP_CLIENT_ENDPOINT_IMPL_HPP
#define VSOMEIP_CLIENT_ENDPOINT_IMPL_HPP

#include <condition_variable>
#include <deque>
#include <mutex>
#include <vector>

#include <boost/array.hpp>
#include <boost/asio/io_service.hpp>
#include <boost/asio/ip/udp.hpp>
#include <boost/utility.hpp>

#include <vsomeip/constants.hpp>

#include "buffer.hpp"
#include "endpoint_impl.hpp"

namespace vsomeip {

class endpoint;
class endpoint_host;

template<typename Protocol>
class client_endpoint_impl: public endpoint_impl<Protocol>,
        public std::enable_shared_from_this<client_endpoint_impl<Protocol> > {
public:
	typedef typename Protocol::endpoint endpoint_type;
    typedef typename Protocol::socket socket_type;

    client_endpoint_impl(std::shared_ptr<endpoint_host> _host,
    		endpoint_type _local, endpoint_type _remote,
			boost::asio::io_service &_io,
            std::uint32_t _max_message_size);
    virtual ~client_endpoint_impl();

    bool send(const uint8_t *_data, uint32_t _size, bool _flush);
    bool send_to(const std::shared_ptr<endpoint_definition> _target,
                 const byte_t *_data, uint32_t _size, bool _flush = true);
    bool flush();

    void stop();
    void restart();

    bool is_client() const;

    bool is_connected() const;

public:
    void connect_cbk(boost::system::error_code const &_error);
    void wait_connect_cbk(boost::system::error_code const &_error);
    void send_cbk(boost::system::error_code const &_error, std::size_t _bytes);
    void flush_cbk(boost::system::error_code const &_error);

public:
    virtual void connect() = 0;
    virtual void receive() = 0;

protected:
    virtual void send_queued() = 0;

    socket_type socket_;
    endpoint_type remote_;

    uint16_t local_port_;

    boost::asio::system_timer flush_timer_;
    boost::asio::system_timer connect_timer_;
    uint32_t connect_timeout_;
    bool is_connected_;

    // send data
    message_buffer_ptr_t packetizer_;
    std::deque<message_buffer_ptr_t> queue_;

    std::mutex mutex_;

    bool was_not_connected_;
};

} // namespace vsomeip

#endif // VSOMEIP_CLIENT_ENDPOINT_IMPL_HPP