summaryrefslogtreecommitdiff
path: root/implementation/endpoints/src/udp_client_endpoint_impl.cpp
blob: 602525292d6b004f92e0e3a25ae5c9c5bcba15cc (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// 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/.

#include <iomanip>
#include <sstream>

#include <boost/asio/ip/multicast.hpp>

#include "../include/endpoint_host.hpp"
#include "../include/udp_client_endpoint_impl.hpp"
#include "../../logging/include/logger.hpp"
#include "../../utility/include/utility.hpp"

namespace vsomeip {

udp_client_endpoint_impl::udp_client_endpoint_impl(
        std::shared_ptr< endpoint_host > _host,
        endpoint_type _local,
        endpoint_type _remote,
        boost::asio::io_service &_io)
    : udp_client_endpoint_base_impl(_host, _local, _remote, _io,
            VSOMEIP_MAX_UDP_MESSAGE_SIZE),
      recv_buffer_(VSOMEIP_MAX_UDP_MESSAGE_SIZE, 0) {
}

udp_client_endpoint_impl::~udp_client_endpoint_impl() {
    std::shared_ptr<endpoint_host> its_host = host_.lock();
    if (its_host) {
        its_host->release_port(local_.port(), false);
    }
}

bool udp_client_endpoint_impl::is_local() const {
    return false;
}

void udp_client_endpoint_impl::connect() {
    // In case a client endpoint port was configured,
    // bind to it before connecting
    if (local_.port() != ILLEGAL_PORT) {
        boost::system::error_code its_bind_error;
        socket_.bind(local_, its_bind_error);
        if(its_bind_error) {
            VSOMEIP_WARNING << "tcp_client_endpoint::connect: "
                    "Error binding socket: " << its_bind_error.message();
        }
    }

    socket_.async_connect(
        remote_,
        std::bind(
            &udp_client_endpoint_base_impl::connect_cbk,
            shared_from_this(),
            std::placeholders::_1
        )
    );
}

void udp_client_endpoint_impl::start() {
    boost::system::error_code its_error;
    socket_.open(remote_.protocol(), its_error);
    if (!its_error || its_error == boost::asio::error::already_open) {
        connect();
    } else {
        VSOMEIP_WARNING << "udp_client_endpoint::connect: Error opening socket: "
                << its_error.message();
    }
}

void udp_client_endpoint_impl::send_queued() {
    message_buffer_ptr_t its_buffer;
    if(queue_.size()) {
        its_buffer = queue_.front();
    } else {
        return;
    }
#if 0
    std::stringstream msg;
    msg << "ucei<" << remote_.address() << ":"
        << std::dec << remote_.port()  << ">::sq: ";
    for (std::size_t i = 0; i < its_buffer->size(); i++)
        msg << std::hex << std::setw(2) << std::setfill('0')
            << (int)(*its_buffer)[i] << " ";
    VSOMEIP_INFO << msg.str();
#endif
    socket_.async_send(
        boost::asio::buffer(*its_buffer),
        std::bind(
            &udp_client_endpoint_base_impl::send_cbk,
            shared_from_this(),
            std::placeholders::_1,
            std::placeholders::_2
        )
    );
}

void udp_client_endpoint_impl::receive() {
    socket_.async_receive_from(
        boost::asio::buffer(&recv_buffer_[0], max_message_size_),
        remote_,
        std::bind(
            &udp_client_endpoint_impl::receive_cbk,
            std::dynamic_pointer_cast<
                udp_client_endpoint_impl
            >(shared_from_this()),
            std::placeholders::_1,
            std::placeholders::_2
        )
    );
}

bool udp_client_endpoint_impl::get_remote_address(
        boost::asio::ip::address &_address) const {
    const boost::asio::ip::address its_address = remote_.address();
    if (its_address.is_unspecified()) {
        return false;
    }
    _address = its_address;
    return true;
}

unsigned short udp_client_endpoint_impl::get_local_port() const {
    boost::system::error_code its_error;
    if (socket_.is_open()) {
        return socket_.local_endpoint(its_error).port();
    }
    return 0;
}

unsigned short udp_client_endpoint_impl::get_remote_port() const {
    boost::system::error_code its_error;
    if (socket_.is_open()) {
        return socket_.remote_endpoint(its_error).port();
    }
    return 0;
}

void udp_client_endpoint_impl::receive_cbk(
        boost::system::error_code const &_error, std::size_t _bytes) {
    if (_error == boost::asio::error::operation_aborted) {
        // endpoint was stopped
        shutdown_and_close_socket();
        return;
    }
    std::shared_ptr<endpoint_host> its_host = host_.lock();
    if (!_error && 0 < _bytes && its_host) {
#if 0
        std::stringstream msg;
        msg << "ucei::rcb(" << _error.message() << "): ";
        for (std::size_t i = 0; i < _bytes + recv_buffer_size_; ++i)
            msg << std::hex << std::setw(2) << std::setfill('0')
                << (int) recv_buffer_[i] << " ";
        VSOMEIP_INFO << msg.str();
#endif
        std::size_t remaining_bytes = _bytes;
        std::size_t i = 0;
        boost::system::error_code its_error;
        endpoint_type its_endpoint(socket_.remote_endpoint(its_error));
        const boost::asio::ip::address its_remote_address(its_endpoint.address());
        const std::uint16_t its_remote_port(its_endpoint.port());

        do {
            uint32_t current_message_size
                = utility::get_message_size(&this->recv_buffer_[i],
                        (uint32_t) remaining_bytes);
            if (current_message_size > VSOMEIP_SOMEIP_HEADER_SIZE &&
                    current_message_size <= remaining_bytes) {
                remaining_bytes -= current_message_size;

                its_host->on_message(&recv_buffer_[i], current_message_size,
                        this, boost::asio::ip::address(),
                        VSOMEIP_ROUTING_CLIENT, its_remote_address,
                        its_remote_port);
            } else {
                VSOMEIP_ERROR << "Received a unreliable vSomeIP message with bad "
                        "length field. Message size: " << current_message_size
                        << " Bytes. From: " << remote_.address() << ":"
                        << remote_.port() << ". Dropping message.";
                remaining_bytes = 0;
            }
            i += current_message_size;
        } while (remaining_bytes > 0);
    }
    if (!_error) {
        receive();
    } else {
        if (_error == boost::asio::error::connection_refused) {
            shutdown_and_close_socket();
        } else if (socket_.is_open()) {
            receive();
        }
    }
}

} // namespace vsomeip