Skip to content

Commit 268e534

Browse files
committed
Merge RicoAntonioFelix:socket
1 parent 3ba47f8 commit 268e534

1 file changed

Lines changed: 19 additions & 53 deletions

File tree

api/net/socket.hpp

Lines changed: 19 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,27 @@
1616
// limitations under the License.
1717

1818
#pragma once
19-
#ifndef NET_SOCKET_HPP
20-
#define NET_SOCKET_HPP
19+
#ifndef NET_TCP_SOCKET_HPP
20+
#define NET_TCP_SOCKET_HPP
2121

22-
#include <net/ip4/addr.hpp>
22+
#include "common.hpp"
2323

2424
namespace net {
25+
namespace tcp {
2526

2627
/**
2728
* An IP address and port
2829
*/
2930
class Socket {
3031
public:
31-
32-
using Address = ip4::Addr;
33-
using port_t = uint16_t;
34-
35-
struct pair_hash {
36-
std::size_t operator () (const Socket& s) const {
37-
auto h1 = std::hash<Address>{}(s.address());
38-
auto h2 = std::hash<port_t>{}(s.port());
39-
return h1 ^ h2;
40-
}
41-
};
42-
4332
/**
4433
* Constructor
4534
*
4635
* Intialize an empty socket <0.0.0.0:0>
4736
*/
48-
Socket() noexcept
49-
: address_{}, port_{}
37+
constexpr Socket() noexcept
38+
: address_{}
39+
, port_{}
5040
{}
5141

5242
/**
@@ -60,24 +50,25 @@ class Socket {
6050
* @param port
6151
* The port associated with the process
6252
*/
63-
Socket(const Address address, const port_t port) noexcept
64-
: address_{address}, port_{port}
53+
constexpr Socket(const Address address, const port_t port) noexcept
54+
: address_{address}
55+
, port_{port}
6556
{}
6657

6758
/**
6859
* Get the socket's network address
6960
*
7061
* @return The socket's network address
7162
*/
72-
Address address() const noexcept
63+
constexpr Address address() const noexcept
7364
{ return address_; }
7465

7566
/**
7667
* Get the socket's port value
7768
*
7869
* @return The socket's port value
7970
*/
80-
port_t port() const noexcept
71+
constexpr port_t port() const noexcept
8172
{ return port_; }
8273

8374
/**
@@ -93,7 +84,7 @@ class Socket {
9384
*
9485
* @return true if this socket is empty, false otherwise
9586
*/
96-
bool is_empty() const noexcept
87+
constexpr bool is_empty() const noexcept
9788
{ return (address_ == 0) and (port_ == 0); }
9889

9990
/**
@@ -104,7 +95,7 @@ class Socket {
10495
*
10596
* @return true if the specified socket is equal, false otherwise
10697
*/
107-
bool operator==(const Socket& other) const noexcept
98+
constexpr bool operator==(const Socket& other) const noexcept
10899
{
109100
return (address() == other.address())
110101
and (port() == other.port());
@@ -118,7 +109,7 @@ class Socket {
118109
*
119110
* @return true if the specified socket is not equal, false otherwise
120111
*/
121-
bool operator!=(const Socket& other) const noexcept
112+
constexpr bool operator!=(const Socket& other) const noexcept
122113
{ return not (*this == other); }
123114

124115
/**
@@ -130,7 +121,7 @@ class Socket {
130121
* @return true if this socket is less-than the specified socket,
131122
* false otherwise
132123
*/
133-
bool operator<(const Socket& other) const noexcept
124+
constexpr bool operator<(const Socket& other) const noexcept
134125
{
135126
return (address() < other.address())
136127
or ((address() == other.address()) and (port() < other.port()));
@@ -145,39 +136,14 @@ class Socket {
145136
* @return true if this socket is greater-than the specified socket,
146137
* false otherwise
147138
*/
148-
bool operator>(const Socket& other) const noexcept
139+
constexpr bool operator>(const Socket& other) const noexcept
149140
{ return not (*this < other); }
150-
151141
private:
152142
Address address_;
153143
port_t port_;
154-
155144
}; //< class Socket
156145

157-
class Quadruple {
158-
159-
public:
160-
Quadruple() noexcept
161-
: source_{}, destination_{}
162-
{}
163-
164-
Quadruple(const Socket::Address src_address, const Socket::port_t src_port,
165-
const Socket::Address dst_address, const Socket::port_t dst_port) noexcept
166-
: source_{src_address, src_port}, destination_{dst_address, dst_port}
167-
{}
168-
169-
const Socket& source() const noexcept
170-
{ return source_; }
171-
172-
const Socket& destination() const noexcept
173-
{ return destination_; }
174-
175-
private:
176-
Socket source_;
177-
Socket destination_;
178-
179-
}; //< class Quadruple
180-
146+
} //< namespace tcp
181147
} //< namespace net
182148

183-
#endif //< NET_SOCKET_HPP
149+
#endif //< NET_TCP_SOCKET_HPP

0 commit comments

Comments
 (0)