Ocean
Loading...
Searching...
No Matches
Port.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef FACEBOOK_NETWORK_PORT_H
9#define FACEBOOK_NETWORK_PORT_H
10
12#include "ocean/network/Data.h"
13
14namespace Ocean
15{
16
17namespace Network
18{
19
20/**
21 * This class wraps a port number with 16 bits.<br>
22 * Internally, the port number is stored in big-endian order.
23 * @ingroup network
24 */
25class Port : public Data
26{
27 public:
28
29 /**
30 * Creates a new port object with zero as default value.
31 */
32 Port() = default;
33
34 /**
35 * Creates a new port object.
36 * @param port The port number to be wrapped in big-endian order
37 */
38 explicit inline Port(const uint16_t port);
39
40 /**
41 * Creates a new port object by a given port number in big-endian, little-endian order or local platform order.
42 * @param port The port number to be wrapped
43 * @param orderType Type of the order
44 */
45 inline Port(const uint16_t port, const OrderType orderType);
46
47 /**
48 * Returns the port number in little-endian order.
49 * @return Port number in little-endian order
50 */
51 inline uint16_t littleEndian() const;
52
53 /**
54 * Returns the port number in a readable order.
55 * This function is platform specific.
56 */
57 inline uint16_t readable() const;
58
59 /**
60 * Returns whether this port hold a non-zero value.
61 * @return True, if so
62 */
63 inline bool isValid() const;
64
65 /**
66 * Returns whether this port holds a zero value.
67 * @return True, if so
68 */
69 inline bool isNull() const;
70
71 /**
72 * Returns whether two port number are identical.
73 * @param port Right port to compare
74 * @return True, if so
75 */
76 inline bool operator==(const Port& port) const;
77
78 /**
79 * Returns whether two port number are not identical.
80 * @param port Right port to compare
81 * @return True, if so
82 */
83 inline bool operator!=(const Port& port) const;
84
85 /**
86 * Casts the wrapped port number
87 * @return Wrapped port number in big-endian order
88 */
89 inline operator uint16_t() const;
90
91 protected:
92
93 /// Port number to wrap in big-endian order.
94 uint16_t port_ = 0u;
95};
96
97inline Port::Port(const uint16_t port) :
98 port_(port)
99{
100 // nothing to do here
101}
102
103inline Port::Port(const uint16_t port, const OrderType orderType)
104{
105 switch (orderType)
106 {
107 case TYPE_BIG_ENDIAN:
108 port_ = port;
109 break;
110
112 port_ = uint16_t((port >> 8u) | (port << 8u));
113 break;
114
115 case TYPE_READABLE:
116 port_ = toBigEndian(port);
117 break;
118 }
119}
120
121inline uint16_t Port::littleEndian() const
122{
123 return uint16_t((port_ >> 8u) | (port_ << 8u));
124}
125
126inline uint16_t Port::readable() const
127{
128 return fromBigEndian(port_);
129}
130
131inline bool Port::isValid() const
132{
133 return !isNull();
134}
135
136inline bool Port::isNull() const
137{
138 return port_ == 0u;
139}
140
141inline bool Port::operator==(const Port& port) const
142{
143 return port_ == port.port_;
144}
145
146inline bool Port::operator!=(const Port& port) const
147{
148 return port_ != port.port_;
149}
150
151inline Port::operator uint16_t() const
152{
153 return port_;
154}
155
156}
157
158}
159
160#endif
This class provides function handling different network data.
Definition Data.h:24
static int toBigEndian(const int32_t value)
Returns a value in big-endian order given in local system bit order.
Definition Data.h:107
OrderType
Definition of different order types.
Definition Data.h:31
@ TYPE_BIG_ENDIAN
Big endian type.
Definition Data.h:33
@ TYPE_LITTLE_ENDIAN
Little endian type.
Definition Data.h:35
@ TYPE_READABLE
Readable endian type which is platform specific.
Definition Data.h:37
static int32_t fromBigEndian(const int32_t value)
Returns a value in local system bit order given as big-endian bit order.
Definition Data.h:135
This class wraps a port number with 16 bits.
Definition Port.h:26
bool operator!=(const Port &port) const
Returns whether two port number are not identical.
Definition Port.h:146
uint16_t port_
Port number to wrap in big-endian order.
Definition Port.h:94
bool isNull() const
Returns whether this port holds a zero value.
Definition Port.h:136
uint16_t readable() const
Returns the port number in a readable order.
Definition Port.h:126
uint16_t littleEndian() const
Returns the port number in little-endian order.
Definition Port.h:121
bool isValid() const
Returns whether this port hold a non-zero value.
Definition Port.h:131
bool operator==(const Port &port) const
Returns whether two port number are identical.
Definition Port.h:141
Port()=default
Creates a new port object with zero as default value.
The namespace covering the entire Ocean framework.
Definition Accessor.h:15