Coverage Report

Created: 2025-06-13 06:46

/src/Fast-DDS/include/fastdds/rtps/common/PortParameters.hpp
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
/**
16
 * @file PortParameters.hpp
17
 */
18
19
#ifndef FASTDDS_RTPS_COMMON__PORTPARAMETERS_HPP
20
#define FASTDDS_RTPS_COMMON__PORTPARAMETERS_HPP
21
22
#include <fastdds/rtps/common/Types.hpp>
23
#include <fastdds/dds/log/Log.hpp>
24
25
namespace eprosima {
26
namespace fastdds {
27
namespace rtps {
28
29
/**
30
 * Class PortParameters, to define the port parameters and gains related with the RTPS protocol.
31
 * @ingroup RTPS_ATTRIBUTES_MODULE
32
 */
33
class PortParameters
34
{
35
public:
36
37
    PortParameters()
38
105k
        : portBase(7400)
39
105k
        , domainIDGain(250)
40
105k
        , participantIDGain(2)
41
105k
        , offsetd0(0)
42
105k
        , offsetd1(10)
43
105k
        , offsetd2(1)
44
105k
        , offsetd3(11)
45
105k
        , offsetd4(2)
46
105k
    {
47
105k
    }
48
49
    virtual ~PortParameters()
50
104k
    {
51
104k
    }
52
53
    bool operator ==(
54
            const PortParameters& b) const
55
0
    {
56
0
        return (this->portBase == b.portBase) &&
57
0
               (this->domainIDGain == b.domainIDGain) &&
58
0
               (this->participantIDGain == b.participantIDGain) &&
59
0
               (this->offsetd0 == b.offsetd0) &&
60
0
               (this->offsetd1 == b.offsetd1) &&
61
0
               (this->offsetd2 == b.offsetd2) &&
62
0
               (this->offsetd3 == b.offsetd3) &&
63
0
               (this->offsetd4 == b.offsetd4);
64
0
    }
65
66
    /**
67
     * Get a multicast port based on the domain ID.
68
     *
69
     * @param domainId Domain ID.
70
     * @return Multicast port
71
     */
72
    inline uint32_t getMulticastPort(
73
            uint32_t domainId) const
74
0
    {
75
0
        uint32_t port = portBase + domainIDGain * domainId + offsetd0;
76
77
0
        if (port > 65535)
78
0
        {
79
0
            EPROSIMA_LOG_ERROR(RTPS, "Calculated port number is too high. Probably the domainId is over 232 "
80
0
                    << "or portBase is too high.");
81
0
            std::cout << "Calculated port number is too high. Probably the domainId is over 232 "
82
0
                      << "or portBase is too high." << std::endl;
83
0
            std::cout.flush();
84
0
            exit(EXIT_FAILURE);
85
0
        }
86
87
0
        return port;
88
0
    }
89
90
    /**
91
     * Get a unicast port based on the domain ID and the participant ID.
92
     *
93
     * @param domainId Domain ID.
94
     * @param RTPSParticipantID Participant ID.
95
     * @return Unicast port
96
     */
97
    inline uint32_t getUnicastPort(
98
            uint32_t domainId,
99
            uint32_t RTPSParticipantID) const
100
0
    {
101
0
        uint32_t port = portBase + domainIDGain * domainId + offsetd1   + participantIDGain * RTPSParticipantID;
102
103
0
        if (port > 65535)
104
0
        {
105
0
            EPROSIMA_LOG_ERROR(RTPS, "Calculated port number is too high. Probably the domainId is over 232, there are "
106
0
                    << "too much participants created or portBase is too high.");
107
0
            std::cout << "Calculated port number is too high. Probably the domainId is over 232, there are "
108
0
                      << "too much participants created or portBase is too high." << std::endl;
109
0
            std::cout.flush();
110
0
            exit(EXIT_FAILURE);
111
0
        }
112
113
0
        return port;
114
0
    }
115
116
    /**
117
     * Get a discovery server port based on the domain ID.
118
     *
119
     * @param domainId Domain ID.
120
     * @return Discovery server port
121
     */
122
    inline uint16_t get_discovery_server_port(
123
            uint32_t domainId) const
124
0
    {
125
0
        uint32_t port = portBase + domainIDGain * domainId + offsetd4;
126
127
0
        if (port > 65535)
128
0
        {
129
0
            EPROSIMA_LOG_ERROR(RTPS, "Calculated port number is too high. Probably the domainId is over 232 "
130
0
                    << "or portBase is too high.");
131
0
            std::cout << "Calculated port number is too high. Probably the domainId is over 232 "
132
0
                      << "or portBase is too high." << std::endl;
133
0
            std::cout.flush();
134
0
            exit(EXIT_FAILURE);
135
0
        }
136
137
0
        return static_cast<uint16_t>(port);
138
0
    }
139
140
public:
141
142
    //!PortBase, default value 7400.
143
    uint16_t portBase;
144
    //!DomainID gain, default value 250.
145
    uint16_t domainIDGain;
146
    //!ParticipantID gain, default value 2.
147
    uint16_t participantIDGain;
148
    //!Offset d0, default value 0.
149
    uint16_t offsetd0;
150
    //!Offset d1, default value 10.
151
    uint16_t offsetd1;
152
    //!Offset d2, default value 1.
153
    uint16_t offsetd2;
154
    //!Offset d3, default value 11.
155
    uint16_t offsetd3;
156
    //!Offset d4, default value 2.
157
    uint16_t offsetd4;
158
};
159
160
} // namespace rtps
161
} // namespace rtps
162
} // namespace eprosima
163
164
#endif // FASTDDS_RTPS_COMMON__PORTPARAMETERS_HPP