Line data Source code
1 : /********************************************************************************
2 : * Copyright (c) 2026 Accenture
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 "someip/NetworkResource.h"
14 : #include "someip/SomeIpConstants.h"
15 :
16 : #include <ip/IPEndpoint.h>
17 : #include <tcp/IDataListener.h>
18 : #include <tcp/socket/AbstractSocket.h>
19 :
20 : #include <etl/span.h>
21 : #include <etl/tuple.h>
22 :
23 : #include <cstdint>
24 :
25 : // These classes and functions are in the global scope for backwards compatiblity
26 : etl::tuple<uint16_t, ::someip::PortRangeReturnCode> computeNextLocalPort(uint16_t, uint16_t);
27 :
28 : namespace someip
29 : {
30 :
31 : class TcpServer;
32 :
33 : /**
34 : * A TCP proxy for a protocol specific network-resource.
35 : */
36 : class TcpProxy
37 : : public NetworkResource
38 : , public ::tcp::IDataListener
39 : {
40 : public:
41 : class IConnectionListener
42 : {
43 : public:
44 76 : virtual ~IConnectionListener() = default;
45 :
46 : virtual void connectionChanged(TcpProxy& proxy) = 0;
47 : };
48 :
49 : explicit TcpProxy(::tcp::AbstractSocket& socket);
50 :
51 : bool isInitialized() const override;
52 :
53 : ::tcp::AbstractSocket& getSocket() const;
54 :
55 97 : void setInternalBuffer(::etl::span<uint8_t> const buffer) { _pBuffer = buffer; }
56 :
57 3 : ::etl::span<uint8_t> getInternalBuffer() const { return _pBuffer; }
58 :
59 : void setConnectionListener(IConnectionListener* listener);
60 :
61 : bool isOpen() const override;
62 : bool isConnected() const override;
63 :
64 : bool isIdle() const;
65 :
66 : ::etl::expected<uint16_t, PortError> getLocalPort() const override;
67 : uint8_t getProto() const override;
68 :
69 : ::ip::IPEndpoint getRemoteEndpoint() const;
70 :
71 : bool open(::ip::IPEndpoint const& localEndpoint, ::ip::IPEndpoint const& remoteEndpoint);
72 :
73 : void close() override;
74 :
75 : bool send(::ip::IPEndpoint const& remoteEndpoint, uint32_t length) override;
76 : bool send(uint32_t length) override;
77 :
78 : /** \see IDataListener */
79 : void dataReceived(uint16_t length) override;
80 :
81 : /** \see IDataListener */
82 : void connectionClosed(::tcp::IDataListener::ErrorCode status) override;
83 :
84 : void abort();
85 :
86 : private:
87 : friend class TcpServer;
88 :
89 : void connected(::tcp::AbstractSocket::ErrorCode status);
90 :
91 : void openConnection(uint16_t localPort);
92 : void closeConnection();
93 :
94 : /**
95 : * Return length of message starting at given offset in the internal buffer,
96 : * or 0 if not enough header data available.
97 : */
98 : uint32_t parseMessageLength(uint32_t offset) const;
99 :
100 : ::tcp::AbstractSocket& _socket;
101 : IConnectionListener* _pConnectionListener;
102 : IConnectionListener* _pParentServerListener;
103 :
104 : ::etl::span<uint8_t> _pBuffer;
105 : uint32_t _bufferOffset;
106 : uint16_t _dynamicLocalPort;
107 : };
108 :
109 : } // namespace someip
|