Skip to content

Commit 58cd82b

Browse files
MarioalexsanChrisThrasher
authored andcommitted
Update Network module code
1 parent 2adc2db commit 58cd82b

File tree

9 files changed

+98
-2
lines changed

9 files changed

+98
-2
lines changed

include/CSFML/Network/IpAddress.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ CSFML_NETWORK_API void sfIpAddress_toString(sfIpAddress address, char* string);
132132
/// address, and should be used for optimization purposes only
133133
/// (like sending the address through a socket).
134134
/// The integer produced by this function can then be converted
135-
/// back to a sfIpAddress with sfIpAddress_FromInteger.
135+
/// back to a sfIpAddress with sfIpAddress_fromInteger.
136136
///
137137
/// \param address Address object
138138
///

include/CSFML/Network/Packet.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ CSFML_NETWORK_API void sfPacket_append(sfPacket* packet, const void* data, size_
7575
///
7676
/// The next read operation will read data from this position
7777
///
78+
/// \param packet Packet object
79+
///
7880
/// \return The byte offset of the current read position
7981
///
8082
/// \see append
@@ -163,6 +165,8 @@ CSFML_NETWORK_API int16_t sfPacket_readInt16(sfPacket* packet);
163165
CSFML_NETWORK_API uint16_t sfPacket_readUint16(sfPacket* packet);
164166
CSFML_NETWORK_API int32_t sfPacket_readInt32(sfPacket* packet);
165167
CSFML_NETWORK_API uint32_t sfPacket_readUint32(sfPacket* packet);
168+
CSFML_NETWORK_API int64_t sfPacket_readInt64(sfPacket* packet);
169+
CSFML_NETWORK_API uint64_t sfPacket_readUint64(sfPacket* packet);
166170
CSFML_NETWORK_API float sfPacket_readFloat(sfPacket* packet);
167171
CSFML_NETWORK_API double sfPacket_readDouble(sfPacket* packet);
168172
CSFML_NETWORK_API void sfPacket_readString(sfPacket* packet, char* string);
@@ -181,6 +185,8 @@ CSFML_NETWORK_API void sfPacket_writeInt16(sfPacket* packet, int16_t);
181185
CSFML_NETWORK_API void sfPacket_writeUint16(sfPacket* packet, uint16_t);
182186
CSFML_NETWORK_API void sfPacket_writeInt32(sfPacket* packet, int32_t);
183187
CSFML_NETWORK_API void sfPacket_writeUint32(sfPacket* packet, uint32_t);
188+
CSFML_NETWORK_API void sfPacket_writeInt64(sfPacket* packet, int64_t);
189+
CSFML_NETWORK_API void sfPacket_writeUint64(sfPacket* packet, uint64_t);
184190
CSFML_NETWORK_API void sfPacket_writeFloat(sfPacket* packet, float);
185191
CSFML_NETWORK_API void sfPacket_writeDouble(sfPacket* packet, double);
186192
CSFML_NETWORK_API void sfPacket_writeString(sfPacket* packet, const char* string);

include/CSFML/Network/TcpListener.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ CSFML_NETWORK_API unsigned short sfTcpListener_getLocalPort(const sfTcpListener*
9999
/// If the socket was previously listening to another port,
100100
/// it will be stopped first and bound to the new port.
101101
///
102+
/// When providing `sfTcpListener_anyPort()` as port, the listener
103+
/// will request an available port from the system.
104+
/// The chosen port can be retrieved by calling `sfTcpListener_getLocalPort()`.
105+
///
102106
/// If there is no specific address to listen to, pass sfIpAddress_Any
103107
///
104108
/// \param listener TCP listener object
@@ -110,6 +114,17 @@ CSFML_NETWORK_API unsigned short sfTcpListener_getLocalPort(const sfTcpListener*
110114
////////////////////////////////////////////////////////////
111115
CSFML_NETWORK_API sfSocketStatus sfTcpListener_listen(sfTcpListener* listener, unsigned short port, sfIpAddress address);
112116

117+
////////////////////////////////////////////////////////////
118+
/// \brief Stop listening and close the socket
119+
///
120+
/// This function gracefully stops the listener. If the
121+
/// socket is not listening, this function has no effect.
122+
///
123+
/// \param listener TCP listener object
124+
///
125+
////////////////////////////////////////////////////////////
126+
CSFML_NETWORK_API void sfTcpListener_close(sfTcpListener* listener);
127+
113128
////////////////////////////////////////////////////////////
114129
/// \brief Accept a new connection
115130
///
@@ -127,3 +142,12 @@ CSFML_NETWORK_API sfSocketStatus sfTcpListener_listen(sfTcpListener* listener, u
127142
///
128143
////////////////////////////////////////////////////////////
129144
CSFML_NETWORK_API sfSocketStatus sfTcpListener_accept(sfTcpListener* listener, sfTcpSocket** connected);
145+
146+
////////////////////////////////////////////////////////////
147+
/// \brief Return the special value that tells the system
148+
/// to pick any available port
149+
///
150+
/// \return The value to use for any port
151+
///
152+
////////////////////////////////////////////////////////////
153+
CSFML_NETWORK_API unsigned short sfTcpListener_anyPort(void);

include/CSFML/Network/UdpSocket.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ CSFML_NETWORK_API unsigned short sfUdpSocket_getLocalPort(const sfUdpSocket* soc
102102
/// system to automatically pick an available port, and then
103103
/// call sfUdpSocket_getLocalPort to retrieve the chosen port.
104104
///
105+
/// When providing `sfUdpSocket_anyPort()` as port, the listener
106+
/// will request an available port from the system.
107+
/// The chosen port can be retrieved by calling `sfUdpSocket_getLocalPort()`.
108+
///
105109
/// If there is no specific address to listen to, pass sfIpAddress_Any
106110
///
107111
/// \param socket UDP socket object
@@ -215,3 +219,12 @@ CSFML_NETWORK_API sfSocketStatus
215219
///
216220
////////////////////////////////////////////////////////////
217221
CSFML_NETWORK_API unsigned int sfUdpSocket_maxDatagramSize(void);
222+
223+
////////////////////////////////////////////////////////////
224+
/// \brief Return the special value that tells the system
225+
/// to pick any available port
226+
///
227+
/// \return The value to use for any port
228+
///
229+
////////////////////////////////////////////////////////////
230+
CSFML_NETWORK_API unsigned short sfUdpSocket_anyPort(void);

src/CSFML/Network/IpAddress.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ namespace
4848

4949

5050
////////////////////////////////////////////////////////////
51-
const sfIpAddress sfIpAddress_None = sfIpAddress_fromBytes(0, 0, 0, 0);
51+
const sfIpAddress sfIpAddress_None = {{0}};
5252

5353

5454
////////////////////////////////////////////////////////////

src/CSFML/Network/Packet.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,20 @@ uint32_t sfPacket_readUint32(sfPacket* packet)
156156
*packet >> value;
157157
return value;
158158
}
159+
int64_t sfPacket_readInt64(sfPacket* packet)
160+
{
161+
assert(packet);
162+
int64_t value = 0;
163+
*packet >> value;
164+
return value;
165+
}
166+
uint64_t sfPacket_readUint64(sfPacket* packet)
167+
{
168+
assert(packet);
169+
uint64_t value = 0;
170+
*packet >> value;
171+
return value;
172+
}
159173
float sfPacket_readFloat(sfPacket* packet)
160174
{
161175
assert(packet);
@@ -219,6 +233,16 @@ void sfPacket_writeUint32(sfPacket* packet, uint32_t value)
219233
assert(packet);
220234
*packet << value;
221235
}
236+
void sfPacket_writeInt64(sfPacket* packet, int64_t value)
237+
{
238+
assert(packet);
239+
*packet << value;
240+
}
241+
void sfPacket_writeUint64(sfPacket* packet, uint64_t value)
242+
{
243+
assert(packet);
244+
*packet << value;
245+
}
222246
void sfPacket_writeFloat(sfPacket* packet, float value)
223247
{
224248
assert(packet);

src/CSFML/Network/TcpListener.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ sfSocketStatus sfTcpListener_listen(sfTcpListener* listener, unsigned short port
8686
}
8787

8888

89+
////////////////////////////////////////////////////////////
90+
void sfTcpListener_close(sfTcpListener* listener)
91+
{
92+
assert(listener);
93+
listener->close();
94+
}
95+
96+
8997
////////////////////////////////////////////////////////////
9098
sfSocketStatus sfTcpListener_accept(sfTcpListener* listener, sfTcpSocket** connected)
9199
{
@@ -102,3 +110,10 @@ sfSocketStatus sfTcpListener_accept(sfTcpListener* listener, sfTcpSocket** conne
102110

103111
return status;
104112
}
113+
114+
115+
////////////////////////////////////////////////////////////
116+
unsigned short sfTcpListener_anyPort()
117+
{
118+
return 0;
119+
}

src/CSFML/Network/UdpSocket.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,10 @@ unsigned int sfUdpSocket_maxDatagramSize()
203203
{
204204
return sf::UdpSocket::MaxDatagramSize;
205205
}
206+
207+
208+
////////////////////////////////////////////////////////////
209+
unsigned short sfUdpSocket_anyPort()
210+
{
211+
return 0;
212+
}

test/Network/IpAddress.test.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <catch2/catch_test_macros.hpp>
44

5+
#include <cstring>
6+
57
TEST_CASE("[Network] sfIpAddress")
68
{
79
SECTION("Constants")
@@ -10,6 +12,11 @@ TEST_CASE("[Network] sfIpAddress")
1012
CHECK(sfIpAddress_toInteger(sfIpAddress_Any) == 0);
1113
CHECK(sfIpAddress_toInteger(sfIpAddress_LocalHost) == 0x7F000001);
1214
CHECK(sfIpAddress_toInteger(sfIpAddress_Broadcast) == 0xFFFFFFFF);
15+
16+
CHECK(std::strcmp(sfIpAddress_None.address, "") == 0);
17+
CHECK(std::strcmp(sfIpAddress_Any.address, "0.0.0.0") == 0);
18+
CHECK(std::strcmp(sfIpAddress_LocalHost.address, "127.0.0.1") == 0);
19+
CHECK(std::strcmp(sfIpAddress_Broadcast.address, "255.255.255.255") == 0);
1320
}
1421

1522
SECTION("sfIpAddress_fromString")

0 commit comments

Comments
 (0)