Skip to content

Commit

Permalink
Update Network module code
Browse files Browse the repository at this point in the history
  • Loading branch information
Marioalexsan authored and ChrisThrasher committed Jan 13, 2025
1 parent 2adc2db commit 58cd82b
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 2 deletions.
2 changes: 1 addition & 1 deletion include/CSFML/Network/IpAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ CSFML_NETWORK_API void sfIpAddress_toString(sfIpAddress address, char* string);
/// address, and should be used for optimization purposes only
/// (like sending the address through a socket).
/// The integer produced by this function can then be converted
/// back to a sfIpAddress with sfIpAddress_FromInteger.
/// back to a sfIpAddress with sfIpAddress_fromInteger.
///
/// \param address Address object
///
Expand Down
6 changes: 6 additions & 0 deletions include/CSFML/Network/Packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ CSFML_NETWORK_API void sfPacket_append(sfPacket* packet, const void* data, size_
///
/// The next read operation will read data from this position
///
/// \param packet Packet object
///
/// \return The byte offset of the current read position
///
/// \see append
Expand Down Expand Up @@ -163,6 +165,8 @@ CSFML_NETWORK_API int16_t sfPacket_readInt16(sfPacket* packet);
CSFML_NETWORK_API uint16_t sfPacket_readUint16(sfPacket* packet);
CSFML_NETWORK_API int32_t sfPacket_readInt32(sfPacket* packet);
CSFML_NETWORK_API uint32_t sfPacket_readUint32(sfPacket* packet);
CSFML_NETWORK_API int64_t sfPacket_readInt64(sfPacket* packet);
CSFML_NETWORK_API uint64_t sfPacket_readUint64(sfPacket* packet);
CSFML_NETWORK_API float sfPacket_readFloat(sfPacket* packet);
CSFML_NETWORK_API double sfPacket_readDouble(sfPacket* packet);
CSFML_NETWORK_API void sfPacket_readString(sfPacket* packet, char* string);
Expand All @@ -181,6 +185,8 @@ CSFML_NETWORK_API void sfPacket_writeInt16(sfPacket* packet, int16_t);
CSFML_NETWORK_API void sfPacket_writeUint16(sfPacket* packet, uint16_t);
CSFML_NETWORK_API void sfPacket_writeInt32(sfPacket* packet, int32_t);
CSFML_NETWORK_API void sfPacket_writeUint32(sfPacket* packet, uint32_t);
CSFML_NETWORK_API void sfPacket_writeInt64(sfPacket* packet, int64_t);
CSFML_NETWORK_API void sfPacket_writeUint64(sfPacket* packet, uint64_t);
CSFML_NETWORK_API void sfPacket_writeFloat(sfPacket* packet, float);
CSFML_NETWORK_API void sfPacket_writeDouble(sfPacket* packet, double);
CSFML_NETWORK_API void sfPacket_writeString(sfPacket* packet, const char* string);
Expand Down
24 changes: 24 additions & 0 deletions include/CSFML/Network/TcpListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ CSFML_NETWORK_API unsigned short sfTcpListener_getLocalPort(const sfTcpListener*
/// If the socket was previously listening to another port,
/// it will be stopped first and bound to the new port.
///
/// When providing `sfTcpListener_anyPort()` as port, the listener
/// will request an available port from the system.
/// The chosen port can be retrieved by calling `sfTcpListener_getLocalPort()`.
///
/// If there is no specific address to listen to, pass sfIpAddress_Any
///
/// \param listener TCP listener object
Expand All @@ -110,6 +114,17 @@ CSFML_NETWORK_API unsigned short sfTcpListener_getLocalPort(const sfTcpListener*
////////////////////////////////////////////////////////////
CSFML_NETWORK_API sfSocketStatus sfTcpListener_listen(sfTcpListener* listener, unsigned short port, sfIpAddress address);

////////////////////////////////////////////////////////////
/// \brief Stop listening and close the socket
///
/// This function gracefully stops the listener. If the
/// socket is not listening, this function has no effect.
///
/// \param listener TCP listener object
///
////////////////////////////////////////////////////////////
CSFML_NETWORK_API void sfTcpListener_close(sfTcpListener* listener);

////////////////////////////////////////////////////////////
/// \brief Accept a new connection
///
Expand All @@ -127,3 +142,12 @@ CSFML_NETWORK_API sfSocketStatus sfTcpListener_listen(sfTcpListener* listener, u
///
////////////////////////////////////////////////////////////
CSFML_NETWORK_API sfSocketStatus sfTcpListener_accept(sfTcpListener* listener, sfTcpSocket** connected);

////////////////////////////////////////////////////////////
/// \brief Return the special value that tells the system
/// to pick any available port
///
/// \return The value to use for any port
///
////////////////////////////////////////////////////////////
CSFML_NETWORK_API unsigned short sfTcpListener_anyPort(void);
13 changes: 13 additions & 0 deletions include/CSFML/Network/UdpSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ CSFML_NETWORK_API unsigned short sfUdpSocket_getLocalPort(const sfUdpSocket* soc
/// system to automatically pick an available port, and then
/// call sfUdpSocket_getLocalPort to retrieve the chosen port.
///
/// When providing `sfUdpSocket_anyPort()` as port, the listener
/// will request an available port from the system.
/// The chosen port can be retrieved by calling `sfUdpSocket_getLocalPort()`.
///
/// If there is no specific address to listen to, pass sfIpAddress_Any
///
/// \param socket UDP socket object
Expand Down Expand Up @@ -215,3 +219,12 @@ CSFML_NETWORK_API sfSocketStatus
///
////////////////////////////////////////////////////////////
CSFML_NETWORK_API unsigned int sfUdpSocket_maxDatagramSize(void);

////////////////////////////////////////////////////////////
/// \brief Return the special value that tells the system
/// to pick any available port
///
/// \return The value to use for any port
///
////////////////////////////////////////////////////////////
CSFML_NETWORK_API unsigned short sfUdpSocket_anyPort(void);
2 changes: 1 addition & 1 deletion src/CSFML/Network/IpAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace


////////////////////////////////////////////////////////////
const sfIpAddress sfIpAddress_None = sfIpAddress_fromBytes(0, 0, 0, 0);
const sfIpAddress sfIpAddress_None = {{0}};


////////////////////////////////////////////////////////////
Expand Down
24 changes: 24 additions & 0 deletions src/CSFML/Network/Packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,20 @@ uint32_t sfPacket_readUint32(sfPacket* packet)
*packet >> value;
return value;
}
int64_t sfPacket_readInt64(sfPacket* packet)
{
assert(packet);
int64_t value = 0;
*packet >> value;
return value;
}
uint64_t sfPacket_readUint64(sfPacket* packet)
{
assert(packet);
uint64_t value = 0;
*packet >> value;
return value;
}
float sfPacket_readFloat(sfPacket* packet)
{
assert(packet);
Expand Down Expand Up @@ -219,6 +233,16 @@ void sfPacket_writeUint32(sfPacket* packet, uint32_t value)
assert(packet);
*packet << value;
}
void sfPacket_writeInt64(sfPacket* packet, int64_t value)
{
assert(packet);
*packet << value;
}
void sfPacket_writeUint64(sfPacket* packet, uint64_t value)
{
assert(packet);
*packet << value;
}
void sfPacket_writeFloat(sfPacket* packet, float value)
{
assert(packet);
Expand Down
15 changes: 15 additions & 0 deletions src/CSFML/Network/TcpListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ sfSocketStatus sfTcpListener_listen(sfTcpListener* listener, unsigned short port
}


////////////////////////////////////////////////////////////
void sfTcpListener_close(sfTcpListener* listener)
{
assert(listener);
listener->close();
}


////////////////////////////////////////////////////////////
sfSocketStatus sfTcpListener_accept(sfTcpListener* listener, sfTcpSocket** connected)
{
Expand All @@ -102,3 +110,10 @@ sfSocketStatus sfTcpListener_accept(sfTcpListener* listener, sfTcpSocket** conne

return status;
}


////////////////////////////////////////////////////////////
unsigned short sfTcpListener_anyPort()
{
return 0;
}
7 changes: 7 additions & 0 deletions src/CSFML/Network/UdpSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,10 @@ unsigned int sfUdpSocket_maxDatagramSize()
{
return sf::UdpSocket::MaxDatagramSize;
}


////////////////////////////////////////////////////////////
unsigned short sfUdpSocket_anyPort()
{
return 0;
}
7 changes: 7 additions & 0 deletions test/Network/IpAddress.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <catch2/catch_test_macros.hpp>

#include <cstring>

TEST_CASE("[Network] sfIpAddress")
{
SECTION("Constants")
Expand All @@ -10,6 +12,11 @@ TEST_CASE("[Network] sfIpAddress")
CHECK(sfIpAddress_toInteger(sfIpAddress_Any) == 0);
CHECK(sfIpAddress_toInteger(sfIpAddress_LocalHost) == 0x7F000001);
CHECK(sfIpAddress_toInteger(sfIpAddress_Broadcast) == 0xFFFFFFFF);

CHECK(std::strcmp(sfIpAddress_None.address, "") == 0);
CHECK(std::strcmp(sfIpAddress_Any.address, "0.0.0.0") == 0);
CHECK(std::strcmp(sfIpAddress_LocalHost.address, "127.0.0.1") == 0);
CHECK(std::strcmp(sfIpAddress_Broadcast.address, "255.255.255.255") == 0);
}

SECTION("sfIpAddress_fromString")
Expand Down

0 comments on commit 58cd82b

Please sign in to comment.