Ocean
Loading...
Searching...
No Matches
Socket.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_SOCKET_H
9#define FACEBOOK_NETWORK_SOCKET_H
10
14#include "ocean/network/Port.h"
15
16#include "ocean/base/Lock.h"
17
18#include <vector>
19
20namespace Ocean
21{
22
23namespace Network
24{
25
26/**
27 * This class is the base class for all sockets.
28 * @ingroup network
29 */
30class OCEAN_NETWORK_EXPORT Socket
31{
32 friend class SocketScheduler;
33 friend class Resolver;
34
35 public:
36
37#ifdef _WINDOWS
38
39 /**
40 * Definition of a socket id.
41 */
42 typedef SOCKET SocketId;
43
44#else
45 /**
46 * Definition of a socket id.
47 */
48 typedef int SocketId;
49
50#endif
51
52 /**
53 * Returns an invalid socket id.
54 * @return Invalid socket id
55 */
56 static constexpr SocketId invalidSocketId();
57
58 /**
59 * Definition of individual result values.
60 */
62 {
63 /// The function succeeded.
64 SR_SUCCEEDED = 0,
65 /// The function failed.
67 /// The function did not succeed as the resource was busy (not free, or would have blocked if configurated as blocking).
69 /// The function could not succeed as the resource was not connected.
70 SR_NOT_CONNECTED
71 };
72
73 /**
74 * Definition of a vector holding 8 bit values.
75 */
76 typedef std::vector<uint8_t> Buffer;
77
78 public:
79
80 /**
81 * Destructs a socket object.
82 */
83 virtual ~Socket();
84
85 /**
86 * Returns the socket id.
87 * @return Socket id
88 */
89 inline SocketId id() const;
90
91 /**
92 * Returns the own address of this socket.
93 * If the systems supports more than one network address use this function to determine which address is used for this socket.
94 * @return Own address
95 */
97
98 /**
99 * Returns the own port of this socket.
100 * @return Own port
101 */
102 Port port() const;
103
104 /**
105 * Sets the own address of this socket.
106 * If the systems supports more than one network address use this function to define which address to use for this socket.<br>
107 * However, usually is not necessary to define the local address.
108 * @param address The address to use for this socket
109 * @return True, if succeeded
110 */
111 virtual bool setAddress(const Address4& address);
112
113 /**
114 * Sets the own port of this socket.
115 * @param port Own port to set
116 * @return True, if succeeded
117 */
118 virtual bool setPort(const Port& port);
119
120 /**
121 * Returns whether this socket is valid.
122 * @return True, if so
123 */
124 explicit inline operator bool() const;
125
126 /**
127 * Sets the blocking mode of a socket.
128 * @param socketId The id of the socket for which the mode will be set, must be valid
129 * @param blocking True, if the socket will block
130 * @return True, if succeeded
131 */
132 static bool setBlockingMode(const SocketId socketId, const bool blocking);
133
134 protected:
135
136 /**
137 * Creates a new socket object.
138 */
140
141 /**
142 * Disabled copy constructor.
143 * @param object The object which would be copied
144 */
145 Socket(const Socket& object) = delete;
146
147 /**
148 * Sets the blocking mode of this socket.
149 * @param blocking True, if the socket will block
150 * @return True, if succeeded
151 */
152 bool setBlockingMode(const bool blocking);
153
154 /**
155 * Returns the maximal message size in bytes.
156 * Beware: Connection oriented socket have no message restrictions, in those cases the specified default value will be returned.
157 * @param defaultSize Default message size for connection oriented sockets
158 * @return Maximal message size
159 */
160 size_t maximalMessageSize(const size_t defaultSize = 65536);
161
162 /**
163 * Releases the socket.
164 * @return True, if succeeded
165 */
167
168 /**
169 * The scheduler event function.
170 * @return True, if the event function was busy; False, if the event function did nothing
171 */
172 virtual bool onScheduler();
173
174 /**
175 * Disabled copy operator.
176 * @param object The object which would be copied
177 * @return Reference to this object
178 */
179 Socket& operator=(const Socket& object) = delete;
180
181 protected:
182
183 /// Socket id.
184 SocketId socketId_ = invalidSocketId();
185
186 /// Socket lock.
187 mutable Lock lock_;
188
189 /// The network resource object.
191};
192
194{
195#ifdef _WINDOWS
196 return INVALID_SOCKET;
197#else
198 return SocketId(-1);
199#endif
200}
201
203{
204 return socketId_;
205}
206
207inline Socket::operator bool() const
208{
209 return socketId_ != invalidSocketId();
210}
211
212}
213
214}
215
216#endif // FACEBOOK_NETWORK_SOCKET_H
This class implements a recursive lock object.
Definition Lock.h:31
This class wraps an address number with 32 bits.
Definition Address4.h:26
This class implements a network resource manager object for platforms that rely on specific network/s...
Definition NetworkResource.h:27
This class wraps a port number with 16 bits.
Definition Port.h:26
This class implements an address resolver.
Definition Resolver.h:28
This class is the base class for all sockets.
Definition Socket.h:31
Address4 address() const
Returns the own address of this socket.
virtual bool setAddress(const Address4 &address)
Sets the own address of this socket.
static bool setBlockingMode(const SocketId socketId, const bool blocking)
Sets the blocking mode of a socket.
SocketId socketId_
Socket id.
Definition Socket.h:184
std::vector< uint8_t > Buffer
Definition of a vector holding 8 bit values.
Definition Socket.h:76
bool setBlockingMode(const bool blocking)
Sets the blocking mode of this socket.
bool releaseSocket()
Releases the socket.
virtual bool onScheduler()
The scheduler event function.
NetworkResource networkResource_
The network resource object.
Definition Socket.h:190
SOCKET SocketId
Definition of a socket id.
Definition Socket.h:42
virtual ~Socket()
Destructs a socket object.
SocketResult
Definition of individual result values.
Definition Socket.h:62
@ SR_BUSY
The function did not succeed as the resource was busy (not free, or would have blocked if configurate...
Definition Socket.h:68
@ SR_FAILED
The function failed.
Definition Socket.h:66
Socket()
Creates a new socket object.
Port port() const
Returns the own port of this socket.
Socket(const Socket &object)=delete
Disabled copy constructor.
static constexpr SocketId invalidSocketId()
Returns an invalid socket id.
Definition Socket.h:193
Socket & operator=(const Socket &object)=delete
Disabled copy operator.
SocketId id() const
Returns the socket id.
Definition Socket.h:202
size_t maximalMessageSize(const size_t defaultSize=65536)
Returns the maximal message size in bytes.
virtual bool setPort(const Port &port)
Sets the own port of this socket.
int SocketId
Definition of a socket id.
Definition Socket.h:48
Lock lock_
Socket lock.
Definition Socket.h:187
This class implements a high performance scheduler for socket events.
Definition SocketScheduler.h:32
The namespace covering the entire Ocean framework.
Definition Accessor.h:15