summaryrefslogtreecommitdiff
path: root/implementation/endpoints/include/local_server_endpoint_impl.hpp
blob: b0ac2babcca0308ed4df77ab664ac1df86efc130 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// 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_LOCAL_SERVER_ENDPOINT_IMPL_HPP
#define VSOMEIP_LOCAL_SERVER_ENDPOINT_IMPL_HPP

#include <map>

#include <boost/asio/io_service.hpp>
#include <boost/asio/local/stream_protocol.hpp>
#include <boost/enable_shared_from_this.hpp>

#ifdef WIN32
#include <boost/asio/ip/tcp.hpp>
#endif

#include <vsomeip/defines.hpp>

#include "buffer.hpp"
#include "server_endpoint_impl.hpp"

namespace vsomeip {

#ifdef WIN32
typedef server_endpoint_impl<
            boost::asio::ip::tcp
        > local_server_endpoint_base_impl;
#else
typedef server_endpoint_impl<
            boost::asio::local::stream_protocol
        > local_server_endpoint_base_impl;
#endif

class local_server_endpoint_impl: public local_server_endpoint_base_impl {

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

    void start();
    void stop();

    void restart();
    void receive();

    bool send_to(const std::shared_ptr<endpoint_definition>,
                 const byte_t *_data, uint32_t _size, bool _flush);
    void send_queued(queue_iterator_type _queue_iterator);

    endpoint_type get_remote() const;
    bool get_default_target(service_t, endpoint_type &) const;

    bool is_local() const;

private:
    class connection: public boost::enable_shared_from_this<connection> {

    public:
        typedef boost::shared_ptr<connection> ptr;

        static ptr create(std::weak_ptr<local_server_endpoint_impl> _server,
                std::uint32_t _max_message_size);
        socket_type & get_socket();

        void start();
        void stop();

        void send_queued(queue_iterator_type _queue_iterator);

    private:
        connection(std::weak_ptr<local_server_endpoint_impl> _server,
                   std::uint32_t _max_message_size);

        void send_magic_cookie();

        local_server_endpoint_impl::socket_type socket_;
        std::weak_ptr<local_server_endpoint_impl> server_;

        uint32_t max_message_size_;

        receive_buffer_t recv_buffer_;
        size_t recv_buffer_size_;

    private:
        void receive_cbk(boost::system::error_code const &_error,
                         std::size_t _bytes);
    };

#ifdef WIN32
    boost::asio::ip::tcp::acceptor acceptor_;
#else
    boost::asio::local::stream_protocol::acceptor acceptor_;
#endif

    std::mutex connections_mutex_;
    std::map<endpoint_type, connection::ptr> connections_;
    connection::ptr current_;

private:
    void remove_connection(connection *_connection);
    void accept_cbk(connection::ptr _connection,
                    boost::system::error_code const &_error);
};

} // namespace vsomeip

#endif // VSOMEIP_LOCAL_SERVER_ENDPOINT_IMPL_HPP