Line data Source code
1 : /********************************************************************************
2 : * Copyright (c) 2026 BMW AG
3 : *
4 : * This program and the accompanying materials are made available under the
5 : * terms of the Apache License Version 2.0 which is available at
6 : * https://www.apache.org/licenses/LICENSE-2.0
7 : *
8 : * SPDX-License-Identifier: Apache-2.0
9 : ********************************************************************************/
10 :
11 : #pragma once
12 :
13 : #include <io/IReader.h>
14 : #include <routing/util.h>
15 : #include <udp/socket/AbstractDatagramSocket.h>
16 :
17 : #include <platform/estdint.h>
18 :
19 : namespace io
20 : {
21 : namespace udp
22 : {
23 : class Sender
24 : {
25 : public:
26 103 : Sender(::io::IReader& input, ::udp::AbstractDatagramSocket& socket)
27 103 : : _input(input), _socket(socket), _socketErrorPdus()
28 103 : {}
29 :
30 123 : void run(size_t const maxNumFrames) { send(maxNumFrames); }
31 :
32 2 : ::routing::StatCounter::Type socketErrorPdus() const { return _socketErrorPdus; }
33 :
34 : private:
35 123 : void send(size_t const maxNumFrames)
36 : {
37 123 : size_t count = 0U;
38 123 : auto data = _input.peek();
39 222 : while ((count < maxNumFrames) && (!data.empty()))
40 : {
41 99 : if (_socket.send(data) != ::udp::AbstractDatagramSocket::ErrorCode::UDP_SOCKET_OK)
42 : {
43 1 : ++_socketErrorPdus;
44 : }
45 99 : _input.release();
46 99 : count++;
47 99 : data = _input.peek();
48 : }
49 123 : }
50 :
51 : ::io::IReader& _input;
52 : ::udp::AbstractDatagramSocket& _socket;
53 :
54 : mutable ::routing::StatCounter _socketErrorPdus;
55 : };
56 :
57 : } // namespace udp
58 : } // namespace io
|