summaryrefslogtreecommitdiff
path: root/implementation/endpoints/include/tcp_server_endpoint_impl.hpp
blob: 72958683947ee4e0d1bf52d9694c14a4fab45ff4 (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
// Copyright (C) 2014-2015 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_TCP_SERVER_ENDPOINT_IMPL_HPP
#define VSOMEIP_TCP_SERVER_ENDPOINT_IMPL_HPP

#include <map>

#include <boost/asio/ip/tcp.hpp>
#include <boost/enable_shared_from_this.hpp>

#include <vsomeip/defines.hpp>
#include "server_endpoint_impl.hpp"

namespace vsomeip {

typedef server_endpoint_impl<boost::asio::ip::tcp,
        VSOMEIP_MAX_TCP_MESSAGE_SIZE> tcp_server_endpoint_base_impl;

class tcp_server_endpoint_impl: public tcp_server_endpoint_base_impl {

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

    void start();
    void stop();

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

    endpoint_type get_remote() const;
    bool get_remote_address(boost::asio::ip::address &_address) const;
    bool get_multicast(service_t, event_t, endpoint_type &) const;

    unsigned short get_local_port() const;
    bool is_reliable() const;
    bool is_local() const;

    client_t get_client(std::shared_ptr<endpoint_definition> _endpoint);

    // dummies to implement endpoint_impl interface
    // TODO: think about a better design!
    void receive();
    void restart();

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

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

        static ptr create(tcp_server_endpoint_impl *_server,
                          std::uint32_t _max_message_size);
        socket_type & get_socket();

        void start();
        void stop();
        void receive();

        client_t get_client(endpoint_type _endpoint_type);

        void send_queued(queue_iterator_type _queue_iterator);

    private:
        connection(tcp_server_endpoint_impl *_owner, std::uint32_t _max_message_size);
        void send_magic_cookie(message_buffer_ptr_t &_buffer);

        tcp_server_endpoint_impl::socket_type socket_;
        tcp_server_endpoint_impl *server_;

        uint32_t max_message_size_;

        receive_buffer_t recv_buffer_;
        size_t recv_buffer_size_;

    private:
        bool is_magic_cookie(size_t _offset) const;
        void receive_cbk(boost::system::error_code const &_error,
                         std::size_t _bytes);
        std::mutex stop_mutex_;
    };

    boost::asio::ip::tcp::acceptor acceptor_;
    std::map<endpoint_type, connection::ptr> connections_;
    connection *current_;

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

} // namespace vsomeip

#endif // VSOMEIP_TCP_SERVER_ENDPOINT_IMPL_HPP